From 287f3760cdf094f4cc2ae24fdb99004a90f09dfa Mon Sep 17 00:00:00 2001 From: akwizgran Date: Fri, 27 Jul 2018 15:38:48 +0100 Subject: [PATCH] Pass database key into LifecycleManager. --- .../bramble/api/contact/ContactManager.java | 2 +- .../bramble/api/lifecycle/LifecycleManager.java | 15 ++++++++------- .../bramble/api/sync/ValidationManager.java | 7 +++++-- .../api/versioning/ClientVersioningManager.java | 3 ++- .../bramble/lifecycle/LifecycleManagerImpl.java | 11 +++-------- .../bramble/test/TestLifecycleModule.java | 3 ++- .../briarproject/briar/android/BriarService.java | 6 ++++-- .../briar/feed/FeedManagerIntegrationTest.java | 5 ++--- .../feed/FeedManagerIntegrationTestComponent.java | 5 ----- .../IntroductionIntegrationTestComponent.java | 2 -- .../SimplexMessagingIntegrationTest.java | 4 +--- .../SimplexMessagingIntegrationTestComponent.java | 5 ----- .../briar/test/BriarIntegrationTest.java | 9 +++------ .../briar/test/BriarIntegrationTestComponent.java | 5 ----- 14 files changed, 31 insertions(+), 51 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java index 589fa53c2..517dcb89f 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java @@ -16,7 +16,7 @@ public interface ContactManager { /** * Registers a hook to be called whenever a contact is added or removed. * This method should be called before - * {@link LifecycleManager#startServices()}. + * {@link LifecycleManager#startServices(SecretKey)}. */ void registerContactHook(ContactHook hook); diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java index c54910b62..c44cf8879 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java @@ -1,5 +1,6 @@ package org.briarproject.bramble.api.lifecycle; +import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.sync.Client; @@ -16,7 +17,7 @@ import java.util.concurrent.ExecutorService; public interface LifecycleManager { /** - * The result of calling {@link #startServices()}. + * The result of calling {@link #startServices(SecretKey)}. */ enum StartResult { ALREADY_RUNNING, @@ -42,27 +43,27 @@ public interface LifecycleManager { /** * Registers a {@link Service} to be started and stopped. This method - * should be called before {@link #startServices()}. + * should be called before {@link #startServices(SecretKey)}. */ void registerService(Service s); /** * Registers a {@link Client} to be started. This method should be called - * before {@link #startServices()}. + * before {@link #startServices(SecretKey)}. */ void registerClient(Client c); /** * Registers an {@link ExecutorService} to be shut down. This method - * should be called before {@link #startServices()}. + * should be called before {@link #startServices(SecretKey)}. */ void registerForShutdown(ExecutorService e); /** - * Opens the {@link DatabaseComponent} and starts any registered - * {@link Client Clients} and {@link Service Services}. + * Opens the {@link DatabaseComponent} using the given key and starts any + * registered {@link Client Clients} and {@link Service Services}. */ - StartResult startServices(); + StartResult startServices(SecretKey dbKey); /** * Stops any registered {@link Service Services}, shuts down any diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/sync/ValidationManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/sync/ValidationManager.java index 5fe103ce8..3b5c90342 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/sync/ValidationManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/sync/ValidationManager.java @@ -1,5 +1,6 @@ package org.briarproject.bramble.api.sync; +import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.Metadata; import org.briarproject.bramble.api.db.Transaction; @@ -35,7 +36,8 @@ public interface ValidationManager { /** * Registers the message validator for the given client. This method - * should be called before {@link LifecycleManager#startServices()}. + * should be called before + * {@link LifecycleManager#startServices(SecretKey)}. */ void registerMessageValidator(ClientId c, int majorVersion, MessageValidator v); @@ -43,7 +45,8 @@ public interface ValidationManager { /** * Registers the incoming message hook for the given client. The hook will * be called once for each incoming message that passes validation. This - * method should be called before {@link LifecycleManager#startServices()}. + * method should be called before + * {@link LifecycleManager#startServices(SecretKey)}. */ void registerIncomingMessageHook(ClientId c, int majorVersion, IncomingMessageHook hook); diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java index f2e98ea2c..00b3d672c 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java @@ -2,6 +2,7 @@ package org.briarproject.bramble.api.versioning; import org.briarproject.bramble.api.contact.Contact; import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.lifecycle.LifecycleManager; @@ -25,7 +26,7 @@ public interface ClientVersioningManager { /** * Registers a client that will be advertised to contacts. The hook will * be called when the visibility of the client changes. This method should - * be called before {@link LifecycleManager#startServices()}. + * be called before {@link LifecycleManager#startServices(SecretKey)}. */ void registerClient(ClientId clientId, int majorVersion, int minorVersion, ClientVersioningHook hook); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/lifecycle/LifecycleManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/lifecycle/LifecycleManagerImpl.java index a2c4d7e3f..130595ab6 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/lifecycle/LifecycleManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/lifecycle/LifecycleManagerImpl.java @@ -1,6 +1,5 @@ package org.briarproject.bramble.lifecycle; -import org.briarproject.bramble.api.account.AccountManager; import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.db.DataTooNewException; import org.briarproject.bramble.api.db.DataTooOldException; @@ -57,7 +56,6 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener { private final List services; private final List clients; private final List executors; - private final AccountManager accountManager; private final IdentityManager identityManager; private final Semaphore startStopSemaphore = new Semaphore(1); private final CountDownLatch dbLatch = new CountDownLatch(1); @@ -68,10 +66,9 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener { @Inject LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus, - AccountManager accountManager, IdentityManager identityManager) { + IdentityManager identityManager) { this.db = db; this.eventBus = eventBus; - this.accountManager = accountManager; this.identityManager = identityManager; services = new CopyOnWriteArrayList<>(); clients = new CopyOnWriteArrayList<>(); @@ -99,7 +96,7 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener { } @Override - public StartResult startServices() { + public StartResult startServices(SecretKey dbKey) { if (!startStopSemaphore.tryAcquire()) { LOG.info("Already starting or stopping"); return ALREADY_RUNNING; @@ -108,9 +105,7 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener { LOG.info("Starting services"); long start = now(); - SecretKey key = accountManager.getDatabaseKey(); - if (key == null) throw new IllegalStateException(); - boolean reopened = db.open(key, this); + boolean reopened = db.open(dbKey, this); if (reopened) logDuration(LOG, "Reopening database", start); else logDuration(LOG, "Creating database", start); identityManager.storeLocalAuthor(); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/test/TestLifecycleModule.java b/bramble-core/src/test/java/org/briarproject/bramble/test/TestLifecycleModule.java index 01d97c52b..21f6ccfe9 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/test/TestLifecycleModule.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/test/TestLifecycleModule.java @@ -1,5 +1,6 @@ package org.briarproject.bramble.test; +import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.lifecycle.IoExecutor; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.Service; @@ -39,7 +40,7 @@ public class TestLifecycleModule { } @Override - public StartResult startServices() { + public StartResult startServices(SecretKey dbKey) { return StartResult.SUCCESS; } diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java index 980e6cdfc..9e43ca2b2 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java @@ -18,6 +18,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.content.ContextCompat; import org.briarproject.bramble.api.account.AccountManager; +import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult; import org.briarproject.bramble.api.system.AndroidExecutor; @@ -97,7 +98,8 @@ public class BriarService extends Service { stopSelf(); return; } - if (!accountManager.hasDatabaseKey()) { + SecretKey dbKey = accountManager.getDatabaseKey(); + if (dbKey == null) { LOG.info("No database key"); stopSelf(); return; @@ -142,7 +144,7 @@ public class BriarService extends Service { nm.cancel(REMINDER_NOTIFICATION_ID); // Start the services in a background thread new Thread(() -> { - StartResult result = lifecycleManager.startServices(); + StartResult result = lifecycleManager.startServices(dbKey); if (result == SUCCESS) { started = true; } else if (result == ALREADY_RUNNING) { diff --git a/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTest.java index 649add3f2..ced26a4a1 100644 --- a/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTest.java @@ -27,6 +27,7 @@ import org.junit.Test; import java.io.File; import java.util.Collection; +import static org.briarproject.bramble.test.TestUtils.getSecretKey; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -53,10 +54,8 @@ public class FeedManagerIntegrationTest extends BriarTestCase { LocalAuthor localAuthor = identityManager.createLocalAuthor("feedTest"); identityManager.registerLocalAuthor(localAuthor); - component.getAccountManager().createAccount("password"); - lifecycleManager = component.getLifecycleManager(); - lifecycleManager.startServices(); + lifecycleManager.startServices(getSecretKey()); lifecycleManager.waitForStartup(); feedManager = component.getFeedManager(); diff --git a/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTestComponent.java b/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTestComponent.java index 3fc4a9ea6..685eedacb 100644 --- a/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTestComponent.java +++ b/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerIntegrationTestComponent.java @@ -1,7 +1,5 @@ package org.briarproject.briar.feed; -import org.briarproject.bramble.account.AccountModule; -import org.briarproject.bramble.api.account.AccountManager; import org.briarproject.bramble.api.identity.IdentityManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.client.ClientModule; @@ -38,7 +36,6 @@ import dagger.Component; TestSecureRandomModule.class, TestSocksModule.class, TestDnsModule.class, - AccountModule.class, BriarClientModule.class, ClientModule.class, ContactModule.class, @@ -82,8 +79,6 @@ interface FeedManagerIntegrationTestComponent { IdentityManager getIdentityManager(); - AccountManager getAccountManager(); - LifecycleManager getLifecycleManager(); FeedManager getFeedManager(); diff --git a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTestComponent.java b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTestComponent.java index 2da286b05..108f48849 100644 --- a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTestComponent.java +++ b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTestComponent.java @@ -1,6 +1,5 @@ package org.briarproject.briar.introduction; -import org.briarproject.bramble.account.AccountModule; import org.briarproject.bramble.client.ClientModule; import org.briarproject.bramble.contact.ContactModule; import org.briarproject.bramble.crypto.CryptoExecutorModule; @@ -37,7 +36,6 @@ import dagger.Component; TestDatabaseModule.class, TestPluginConfigModule.class, TestSecureRandomModule.class, - AccountModule.class, BlogModule.class, BriarClientModule.class, ClientModule.class, diff --git a/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTest.java index afef68c28..c17f3a35d 100644 --- a/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTest.java @@ -103,11 +103,9 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { // Add an identity for the user IdentityManager identityManager = device.getIdentityManager(); identityManager.registerLocalAuthor(local); - // Create an account - device.getAccountManager().createAccount("password"); // Start the lifecycle manager LifecycleManager lifecycleManager = device.getLifecycleManager(); - lifecycleManager.startServices(); + lifecycleManager.startServices(getSecretKey()); lifecycleManager.waitForStartup(); // Add the other user as a contact ContactManager contactManager = device.getContactManager(); diff --git a/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTestComponent.java b/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTestComponent.java index a5f9d9bc6..7dade1ed1 100644 --- a/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTestComponent.java +++ b/briar-core/src/test/java/org/briarproject/briar/messaging/SimplexMessagingIntegrationTestComponent.java @@ -1,7 +1,5 @@ package org.briarproject.briar.messaging; -import org.briarproject.bramble.account.AccountModule; -import org.briarproject.bramble.api.account.AccountManager; import org.briarproject.bramble.api.contact.ContactManager; import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.identity.IdentityManager; @@ -41,7 +39,6 @@ import dagger.Component; TestDatabaseModule.class, TestPluginConfigModule.class, TestSecureRandomModule.class, - AccountModule.class, BriarClientModule.class, ClientModule.class, ContactModule.class, @@ -80,8 +77,6 @@ interface SimplexMessagingIntegrationTestComponent { IdentityManager getIdentityManager(); - AccountManager getAccountManager(); - ContactManager getContactManager(); MessagingManager getMessagingManager(); diff --git a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java index cfe531782..f10d94183 100644 --- a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java @@ -140,9 +140,6 @@ public abstract class BriarIntegrationTest