Pass database key into LifecycleManager.

This commit is contained in:
akwizgran
2018-07-27 15:38:48 +01:00
parent 58d09d0742
commit 287f3760cd
14 changed files with 31 additions and 51 deletions

View File

@@ -16,7 +16,7 @@ public interface ContactManager {
/** /**
* Registers a hook to be called whenever a contact is added or removed. * Registers a hook to be called whenever a contact is added or removed.
* This method should be called before * This method should be called before
* {@link LifecycleManager#startServices()}. * {@link LifecycleManager#startServices(SecretKey)}.
*/ */
void registerContactHook(ContactHook hook); void registerContactHook(ContactHook hook);

View File

@@ -1,5 +1,6 @@
package org.briarproject.bramble.api.lifecycle; 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.db.DatabaseComponent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Client; import org.briarproject.bramble.api.sync.Client;
@@ -16,7 +17,7 @@ import java.util.concurrent.ExecutorService;
public interface LifecycleManager { public interface LifecycleManager {
/** /**
* The result of calling {@link #startServices()}. * The result of calling {@link #startServices(SecretKey)}.
*/ */
enum StartResult { enum StartResult {
ALREADY_RUNNING, ALREADY_RUNNING,
@@ -42,27 +43,27 @@ public interface LifecycleManager {
/** /**
* Registers a {@link Service} to be started and stopped. This method * 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); void registerService(Service s);
/** /**
* Registers a {@link Client} to be started. This method should be called * Registers a {@link Client} to be started. This method should be called
* before {@link #startServices()}. * before {@link #startServices(SecretKey)}.
*/ */
void registerClient(Client c); void registerClient(Client c);
/** /**
* Registers an {@link ExecutorService} to be shut down. This method * 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); void registerForShutdown(ExecutorService e);
/** /**
* Opens the {@link DatabaseComponent} and starts any registered * Opens the {@link DatabaseComponent} using the given key and starts any
* {@link Client Clients} and {@link Service Services}. * registered {@link Client Clients} and {@link Service Services}.
*/ */
StartResult startServices(); StartResult startServices(SecretKey dbKey);
/** /**
* Stops any registered {@link Service Services}, shuts down any * Stops any registered {@link Service Services}, shuts down any

View File

@@ -1,5 +1,6 @@
package org.briarproject.bramble.api.sync; 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.DbException;
import org.briarproject.bramble.api.db.Metadata; import org.briarproject.bramble.api.db.Metadata;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
@@ -35,7 +36,8 @@ public interface ValidationManager {
/** /**
* Registers the message validator for the given client. This method * 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, void registerMessageValidator(ClientId c, int majorVersion,
MessageValidator v); MessageValidator v);
@@ -43,7 +45,8 @@ public interface ValidationManager {
/** /**
* Registers the incoming message hook for the given client. The hook will * Registers the incoming message hook for the given client. The hook will
* be called once for each incoming message that passes validation. This * 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, void registerIncomingMessageHook(ClientId c, int majorVersion,
IncomingMessageHook hook); IncomingMessageHook hook);

View File

@@ -2,6 +2,7 @@ package org.briarproject.bramble.api.versioning;
import org.briarproject.bramble.api.contact.Contact; import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.bramble.api.contact.ContactId; 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.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; 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 * 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 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, void registerClient(ClientId clientId, int majorVersion, int minorVersion,
ClientVersioningHook hook); ClientVersioningHook hook);

View File

@@ -1,6 +1,5 @@
package org.briarproject.bramble.lifecycle; package org.briarproject.bramble.lifecycle;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DataTooNewException; import org.briarproject.bramble.api.db.DataTooNewException;
import org.briarproject.bramble.api.db.DataTooOldException; import org.briarproject.bramble.api.db.DataTooOldException;
@@ -57,7 +56,6 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
private final List<Service> services; private final List<Service> services;
private final List<Client> clients; private final List<Client> clients;
private final List<ExecutorService> executors; private final List<ExecutorService> executors;
private final AccountManager accountManager;
private final IdentityManager identityManager; private final IdentityManager identityManager;
private final Semaphore startStopSemaphore = new Semaphore(1); private final Semaphore startStopSemaphore = new Semaphore(1);
private final CountDownLatch dbLatch = new CountDownLatch(1); private final CountDownLatch dbLatch = new CountDownLatch(1);
@@ -68,10 +66,9 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
@Inject @Inject
LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus, LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus,
AccountManager accountManager, IdentityManager identityManager) { IdentityManager identityManager) {
this.db = db; this.db = db;
this.eventBus = eventBus; this.eventBus = eventBus;
this.accountManager = accountManager;
this.identityManager = identityManager; this.identityManager = identityManager;
services = new CopyOnWriteArrayList<>(); services = new CopyOnWriteArrayList<>();
clients = new CopyOnWriteArrayList<>(); clients = new CopyOnWriteArrayList<>();
@@ -99,7 +96,7 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
} }
@Override @Override
public StartResult startServices() { public StartResult startServices(SecretKey dbKey) {
if (!startStopSemaphore.tryAcquire()) { if (!startStopSemaphore.tryAcquire()) {
LOG.info("Already starting or stopping"); LOG.info("Already starting or stopping");
return ALREADY_RUNNING; return ALREADY_RUNNING;
@@ -108,9 +105,7 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
LOG.info("Starting services"); LOG.info("Starting services");
long start = now(); long start = now();
SecretKey key = accountManager.getDatabaseKey(); boolean reopened = db.open(dbKey, this);
if (key == null) throw new IllegalStateException();
boolean reopened = db.open(key, this);
if (reopened) logDuration(LOG, "Reopening database", start); if (reopened) logDuration(LOG, "Reopening database", start);
else logDuration(LOG, "Creating database", start); else logDuration(LOG, "Creating database", start);
identityManager.storeLocalAuthor(); identityManager.storeLocalAuthor();

View File

@@ -1,5 +1,6 @@
package org.briarproject.bramble.test; 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.IoExecutor;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.lifecycle.Service; import org.briarproject.bramble.api.lifecycle.Service;
@@ -39,7 +40,7 @@ public class TestLifecycleModule {
} }
@Override @Override
public StartResult startServices() { public StartResult startServices(SecretKey dbKey) {
return StartResult.SUCCESS; return StartResult.SUCCESS;
} }

View File

@@ -18,6 +18,7 @@ import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import org.briarproject.bramble.api.account.AccountManager; 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;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult; import org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult;
import org.briarproject.bramble.api.system.AndroidExecutor; import org.briarproject.bramble.api.system.AndroidExecutor;
@@ -97,7 +98,8 @@ public class BriarService extends Service {
stopSelf(); stopSelf();
return; return;
} }
if (!accountManager.hasDatabaseKey()) { SecretKey dbKey = accountManager.getDatabaseKey();
if (dbKey == null) {
LOG.info("No database key"); LOG.info("No database key");
stopSelf(); stopSelf();
return; return;
@@ -142,7 +144,7 @@ public class BriarService extends Service {
nm.cancel(REMINDER_NOTIFICATION_ID); nm.cancel(REMINDER_NOTIFICATION_ID);
// Start the services in a background thread // Start the services in a background thread
new Thread(() -> { new Thread(() -> {
StartResult result = lifecycleManager.startServices(); StartResult result = lifecycleManager.startServices(dbKey);
if (result == SUCCESS) { if (result == SUCCESS) {
started = true; started = true;
} else if (result == ALREADY_RUNNING) { } else if (result == ALREADY_RUNNING) {

View File

@@ -27,6 +27,7 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@@ -53,10 +54,8 @@ public class FeedManagerIntegrationTest extends BriarTestCase {
LocalAuthor localAuthor = identityManager.createLocalAuthor("feedTest"); LocalAuthor localAuthor = identityManager.createLocalAuthor("feedTest");
identityManager.registerLocalAuthor(localAuthor); identityManager.registerLocalAuthor(localAuthor);
component.getAccountManager().createAccount("password");
lifecycleManager = component.getLifecycleManager(); lifecycleManager = component.getLifecycleManager();
lifecycleManager.startServices(); lifecycleManager.startServices(getSecretKey());
lifecycleManager.waitForStartup(); lifecycleManager.waitForStartup();
feedManager = component.getFeedManager(); feedManager = component.getFeedManager();

View File

@@ -1,7 +1,5 @@
package org.briarproject.briar.feed; 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.identity.IdentityManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.client.ClientModule; import org.briarproject.bramble.client.ClientModule;
@@ -38,7 +36,6 @@ import dagger.Component;
TestSecureRandomModule.class, TestSecureRandomModule.class,
TestSocksModule.class, TestSocksModule.class,
TestDnsModule.class, TestDnsModule.class,
AccountModule.class,
BriarClientModule.class, BriarClientModule.class,
ClientModule.class, ClientModule.class,
ContactModule.class, ContactModule.class,
@@ -82,8 +79,6 @@ interface FeedManagerIntegrationTestComponent {
IdentityManager getIdentityManager(); IdentityManager getIdentityManager();
AccountManager getAccountManager();
LifecycleManager getLifecycleManager(); LifecycleManager getLifecycleManager();
FeedManager getFeedManager(); FeedManager getFeedManager();

View File

@@ -1,6 +1,5 @@
package org.briarproject.briar.introduction; package org.briarproject.briar.introduction;
import org.briarproject.bramble.account.AccountModule;
import org.briarproject.bramble.client.ClientModule; import org.briarproject.bramble.client.ClientModule;
import org.briarproject.bramble.contact.ContactModule; import org.briarproject.bramble.contact.ContactModule;
import org.briarproject.bramble.crypto.CryptoExecutorModule; import org.briarproject.bramble.crypto.CryptoExecutorModule;
@@ -37,7 +36,6 @@ import dagger.Component;
TestDatabaseModule.class, TestDatabaseModule.class,
TestPluginConfigModule.class, TestPluginConfigModule.class,
TestSecureRandomModule.class, TestSecureRandomModule.class,
AccountModule.class,
BlogModule.class, BlogModule.class,
BriarClientModule.class, BriarClientModule.class,
ClientModule.class, ClientModule.class,

View File

@@ -103,11 +103,9 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Add an identity for the user // Add an identity for the user
IdentityManager identityManager = device.getIdentityManager(); IdentityManager identityManager = device.getIdentityManager();
identityManager.registerLocalAuthor(local); identityManager.registerLocalAuthor(local);
// Create an account
device.getAccountManager().createAccount("password");
// Start the lifecycle manager // Start the lifecycle manager
LifecycleManager lifecycleManager = device.getLifecycleManager(); LifecycleManager lifecycleManager = device.getLifecycleManager();
lifecycleManager.startServices(); lifecycleManager.startServices(getSecretKey());
lifecycleManager.waitForStartup(); lifecycleManager.waitForStartup();
// Add the other user as a contact // Add the other user as a contact
ContactManager contactManager = device.getContactManager(); ContactManager contactManager = device.getContactManager();

View File

@@ -1,7 +1,5 @@
package org.briarproject.briar.messaging; 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.contact.ContactManager;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.identity.IdentityManager; import org.briarproject.bramble.api.identity.IdentityManager;
@@ -41,7 +39,6 @@ import dagger.Component;
TestDatabaseModule.class, TestDatabaseModule.class,
TestPluginConfigModule.class, TestPluginConfigModule.class,
TestSecureRandomModule.class, TestSecureRandomModule.class,
AccountModule.class,
BriarClientModule.class, BriarClientModule.class,
ClientModule.class, ClientModule.class,
ContactModule.class, ContactModule.class,
@@ -80,8 +77,6 @@ interface SimplexMessagingIntegrationTestComponent {
IdentityManager getIdentityManager(); IdentityManager getIdentityManager();
AccountManager getAccountManager();
ContactManager getContactManager(); ContactManager getContactManager();
MessagingManager getMessagingManager(); MessagingManager getMessagingManager();

View File

@@ -140,9 +140,6 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
assertTrue(testDir.mkdirs()); assertTrue(testDir.mkdirs());
createComponents(); createComponents();
c0.getAccountManager().createAccount("password");
c1.getAccountManager().createAccount("password");
c2.getAccountManager().createAccount("password");
identityManager0 = c0.getIdentityManager(); identityManager0 = c0.getIdentityManager();
identityManager1 = c1.getIdentityManager(); identityManager1 = c1.getIdentityManager();
identityManager2 = c2.getIdentityManager(); identityManager2 = c2.getIdentityManager();
@@ -196,9 +193,9 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
lifecycleManager0 = c0.getLifecycleManager(); lifecycleManager0 = c0.getLifecycleManager();
lifecycleManager1 = c1.getLifecycleManager(); lifecycleManager1 = c1.getLifecycleManager();
lifecycleManager2 = c2.getLifecycleManager(); lifecycleManager2 = c2.getLifecycleManager();
lifecycleManager0.startServices(); lifecycleManager0.startServices(getSecretKey());
lifecycleManager1.startServices(); lifecycleManager1.startServices(getSecretKey());
lifecycleManager2.startServices(); lifecycleManager2.startServices(getSecretKey());
lifecycleManager0.waitForStartup(); lifecycleManager0.waitForStartup();
lifecycleManager1.waitForStartup(); lifecycleManager1.waitForStartup();
lifecycleManager2.waitForStartup(); lifecycleManager2.waitForStartup();

View File

@@ -1,7 +1,5 @@
package org.briarproject.briar.test; package org.briarproject.briar.test;
import org.briarproject.bramble.account.AccountModule;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.client.ClientHelper; import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.contact.ContactManager; import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DatabaseComponent;
@@ -56,7 +54,6 @@ import dagger.Component;
TestDatabaseModule.class, TestDatabaseModule.class,
TestPluginConfigModule.class, TestPluginConfigModule.class,
TestSecureRandomModule.class, TestSecureRandomModule.class,
AccountModule.class,
BlogModule.class, BlogModule.class,
BriarClientModule.class, BriarClientModule.class,
ClientModule.class, ClientModule.class,
@@ -123,8 +120,6 @@ public interface BriarIntegrationTestComponent {
IdentityManager getIdentityManager(); IdentityManager getIdentityManager();
AccountManager getAccountManager();
ClientHelper getClientHelper(); ClientHelper getClientHelper();
ContactManager getContactManager(); ContactManager getContactManager();