mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Move local author creation into IdentityManager.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package org.briarproject.bramble.identity;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author.Status;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
@@ -21,6 +24,8 @@ import static org.briarproject.bramble.api.identity.Author.Status.OURSELVES;
|
||||
import static org.briarproject.bramble.api.identity.Author.Status.UNKNOWN;
|
||||
import static org.briarproject.bramble.api.identity.Author.Status.UNVERIFIED;
|
||||
import static org.briarproject.bramble.api.identity.Author.Status.VERIFIED;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -30,25 +35,51 @@ class IdentityManagerImpl implements IdentityManager {
|
||||
Logger.getLogger(IdentityManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
private final AuthorFactory authorFactory;
|
||||
|
||||
// The local author is immutable so we can cache it
|
||||
@Nullable
|
||||
private volatile LocalAuthor cachedAuthor;
|
||||
|
||||
@Inject
|
||||
IdentityManagerImpl(DatabaseComponent db) {
|
||||
IdentityManagerImpl(DatabaseComponent db, CryptoComponent crypto,
|
||||
AuthorFactory authorFactory) {
|
||||
this.db = db;
|
||||
this.crypto = crypto;
|
||||
this.authorFactory = authorFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerLocalAuthor(LocalAuthor localAuthor)
|
||||
throws DbException {
|
||||
public LocalAuthor createLocalAuthor(String name) {
|
||||
long start = now();
|
||||
KeyPair keyPair = crypto.generateSignatureKeyPair();
|
||||
byte[] publicKey = keyPair.getPublic().getEncoded();
|
||||
byte[] privateKey = keyPair.getPrivate().getEncoded();
|
||||
LocalAuthor localAuthor = authorFactory.createLocalAuthor(name,
|
||||
publicKey, privateKey);
|
||||
logDuration(LOG, "Creating local author", start);
|
||||
return localAuthor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerLocalAuthor(LocalAuthor a) {
|
||||
cachedAuthor = a;
|
||||
LOG.info("Local author registered");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeLocalAuthor() throws DbException {
|
||||
LocalAuthor cached = cachedAuthor;
|
||||
if (cached == null) {
|
||||
LOG.info("No local author to store");
|
||||
return;
|
||||
}
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
db.addLocalAuthor(txn, cached);
|
||||
db.commitTransaction(txn);
|
||||
cachedAuthor = localAuthor;
|
||||
LOG.info("Local author registered");
|
||||
LOG.info("Local author stored");
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.briarproject.bramble.lifecycle;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.db.DataTooNewException;
|
||||
import org.briarproject.bramble.api.db.DataTooOldException;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
@@ -9,9 +7,7 @@ import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.MigrationListener;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.lifecycle.Service;
|
||||
import org.briarproject.bramble.api.lifecycle.ServiceException;
|
||||
@@ -26,7 +22,6 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
|
||||
@@ -60,8 +55,6 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
|
||||
private final List<Service> services;
|
||||
private final List<Client> clients;
|
||||
private final List<ExecutorService> executors;
|
||||
private final CryptoComponent crypto;
|
||||
private final AuthorFactory authorFactory;
|
||||
private final IdentityManager identityManager;
|
||||
private final Semaphore startStopSemaphore = new Semaphore(1);
|
||||
private final CountDownLatch dbLatch = new CountDownLatch(1);
|
||||
@@ -72,12 +65,9 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
|
||||
|
||||
@Inject
|
||||
LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus,
|
||||
CryptoComponent crypto, AuthorFactory authorFactory,
|
||||
IdentityManager identityManager) {
|
||||
this.db = db;
|
||||
this.eventBus = eventBus;
|
||||
this.crypto = crypto;
|
||||
this.authorFactory = authorFactory;
|
||||
this.identityManager = identityManager;
|
||||
services = new CopyOnWriteArrayList<>();
|
||||
clients = new CopyOnWriteArrayList<>();
|
||||
@@ -104,25 +94,8 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
|
||||
executors.add(e);
|
||||
}
|
||||
|
||||
private LocalAuthor createLocalAuthor(String nickname) {
|
||||
long start = now();
|
||||
KeyPair keyPair = crypto.generateSignatureKeyPair();
|
||||
byte[] publicKey = keyPair.getPublic().getEncoded();
|
||||
byte[] privateKey = keyPair.getPrivate().getEncoded();
|
||||
LocalAuthor localAuthor = authorFactory
|
||||
.createLocalAuthor(nickname, publicKey, privateKey);
|
||||
logDuration(LOG, "Creating local author", start);
|
||||
return localAuthor;
|
||||
}
|
||||
|
||||
private void registerLocalAuthor(LocalAuthor author) throws DbException {
|
||||
long start = now();
|
||||
identityManager.registerLocalAuthor(author);
|
||||
logDuration(LOG, "Registering local author", start);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StartResult startServices(@Nullable String nickname) {
|
||||
public StartResult startServices() {
|
||||
if (!startStopSemaphore.tryAcquire()) {
|
||||
LOG.info("Already starting or stopping");
|
||||
return ALREADY_RUNNING;
|
||||
@@ -134,10 +107,7 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
|
||||
boolean reopened = db.open(this);
|
||||
if (reopened) logDuration(LOG, "Reopening database", start);
|
||||
else logDuration(LOG, "Creating database", start);
|
||||
|
||||
if (nickname != null) {
|
||||
registerLocalAuthor(createLocalAuthor(nickname));
|
||||
}
|
||||
identityManager.storeLocalAuthor();
|
||||
|
||||
state = STARTING_SERVICES;
|
||||
dbLatch.countDown();
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
package org.briarproject.bramble.lifecycle;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||
@@ -54,11 +49,9 @@ public class LifecycleModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
LifecycleManager provideLifecycleManager(DatabaseComponent db,
|
||||
EventBus eventBus, CryptoComponent crypto,
|
||||
AuthorFactory authorFactory, IdentityManager identityManager) {
|
||||
return new LifecycleManagerImpl(db, eventBus, crypto, authorFactory,
|
||||
identityManager);
|
||||
LifecycleManager provideLifecycleManager(
|
||||
LifecycleManagerImpl lifecycleManager) {
|
||||
return lifecycleManager;
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
||||
@@ -2,15 +2,21 @@ package org.briarproject.bramble.identity;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -27,24 +33,48 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final IdentityManager identityManager;
|
||||
private final DatabaseComponent db = context.mock(DatabaseComponent.class);
|
||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||
private final AuthorFactory authorFactory =
|
||||
context.mock(AuthorFactory.class);
|
||||
private final PublicKey publicKey = context.mock(PublicKey.class);
|
||||
private final PrivateKey privateKey = context.mock(PrivateKey.class);
|
||||
|
||||
private final Transaction txn = new Transaction(null, false);
|
||||
private final LocalAuthor localAuthor = getLocalAuthor();
|
||||
private final Collection<LocalAuthor> localAuthors =
|
||||
Collections.singletonList(localAuthor);
|
||||
private final String authorName = localAuthor.getName();
|
||||
private final KeyPair keyPair = new KeyPair(publicKey, privateKey);
|
||||
private final byte[] publicKeyBytes = localAuthor.getPublicKey();
|
||||
private final byte[] privateKeyBytes = localAuthor.getPrivateKey();
|
||||
private IdentityManager identityManager;
|
||||
|
||||
public IdentityManagerImplTest() {
|
||||
identityManager = new IdentityManagerImpl(db);
|
||||
@Before
|
||||
public void setUp() {
|
||||
identityManager = new IdentityManagerImpl(db, crypto, authorFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterLocalAuthor() throws DbException {
|
||||
expectRegisterLocalAuthor();
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
public void testCreateLocalAuthor() {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(crypto).generateSignatureKeyPair();
|
||||
will(returnValue(keyPair));
|
||||
oneOf(publicKey).getEncoded();
|
||||
will(returnValue(publicKeyBytes));
|
||||
oneOf(privateKey).getEncoded();
|
||||
will(returnValue(privateKeyBytes));
|
||||
oneOf(authorFactory).createLocalAuthor(authorName,
|
||||
publicKeyBytes, privateKeyBytes);
|
||||
will(returnValue(localAuthor));
|
||||
}});
|
||||
|
||||
assertEquals(localAuthor,
|
||||
identityManager.createLocalAuthor(authorName));
|
||||
}
|
||||
|
||||
private void expectRegisterLocalAuthor() throws DbException {
|
||||
@Test
|
||||
public void testRegisterAndStoreLocalAuthor() throws DbException {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).startTransaction(false);
|
||||
will(returnValue(txn));
|
||||
@@ -52,6 +82,10 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
oneOf(db).commitTransaction(txn);
|
||||
oneOf(db).endTransaction(txn);
|
||||
}});
|
||||
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||
identityManager.storeLocalAuthor();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,7 +103,6 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetCachedLocalAuthor() throws DbException {
|
||||
expectRegisterLocalAuthor();
|
||||
identityManager.registerLocalAuthor(localAuthor);
|
||||
assertEquals(localAuthor, identityManager.getLocalAuthor());
|
||||
}
|
||||
|
||||
@@ -46,16 +46,6 @@ public class TestDatabaseConfig implements DatabaseConfig {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocalAuthorName(String nickname) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalAuthorName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return maxSize;
|
||||
|
||||
@@ -11,7 +11,6 @@ import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
@@ -40,7 +39,7 @@ public class TestLifecycleModule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public StartResult startServices(@Nullable String nickname) {
|
||||
public StartResult startServices() {
|
||||
return StartResult.SUCCESS;
|
||||
}
|
||||
|
||||
@@ -49,15 +48,15 @@ public class TestLifecycleModule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitForDatabase() throws InterruptedException {
|
||||
public void waitForDatabase() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitForStartup() throws InterruptedException {
|
||||
public void waitForStartup() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitForShutdown() throws InterruptedException {
|
||||
public void waitForShutdown() {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user