Refactor KeyManager startup so managers are created earlier.

This commit is contained in:
akwizgran
2021-05-21 13:18:59 +01:00
parent 9cc8d44778
commit 6e6cadd3ad
3 changed files with 38 additions and 26 deletions

View File

@@ -23,7 +23,7 @@ public interface KeyManager {
/** /**
* Derives and stores a set of rotation mode transport keys for * Derives and stores a set of rotation mode transport keys for
* communicating with the given contact over the given transport and * communicating with the given contact over the given transport and
* returns the key set ID. * returns the key set ID, or null if the transport is not supported.
* <p/> * <p/>
* {@link StreamContext StreamContexts} for the contact can be created * {@link StreamContext StreamContexts} for the contact can be created
* after this method has returned. * after this method has returned.
@@ -31,6 +31,7 @@ public interface KeyManager {
* @param alice True if the local party is Alice * @param alice True if the local party is Alice
* @param active Whether the derived keys can be used for outgoing streams * @param active Whether the derived keys can be used for outgoing streams
*/ */
@Nullable
KeySetId addRotationKeys(Transaction txn, ContactId c, TransportId t, KeySetId addRotationKeys(Transaction txn, ContactId c, TransportId t,
SecretKey rootKey, long timestamp, boolean alice, SecretKey rootKey, long timestamp, boolean alice,
boolean active) throws DbException; boolean active) throws DbException;

View File

@@ -51,7 +51,6 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
private final DatabaseComponent db; private final DatabaseComponent db;
private final Executor dbExecutor; private final Executor dbExecutor;
private final PluginConfig pluginConfig; private final PluginConfig pluginConfig;
private final TransportKeyManagerFactory transportKeyManagerFactory;
private final TransportCrypto transportCrypto; private final TransportCrypto transportCrypto;
private final ConcurrentHashMap<TransportId, TransportKeyManager> managers; private final ConcurrentHashMap<TransportId, TransportKeyManager> managers;
@@ -61,34 +60,39 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
KeyManagerImpl(DatabaseComponent db, KeyManagerImpl(DatabaseComponent db,
@DatabaseExecutor Executor dbExecutor, @DatabaseExecutor Executor dbExecutor,
PluginConfig pluginConfig, PluginConfig pluginConfig,
TransportKeyManagerFactory transportKeyManagerFactory, TransportCrypto transportCrypto,
TransportCrypto transportCrypto) { TransportKeyManagerFactory transportKeyManagerFactory) {
this.db = db; this.db = db;
this.dbExecutor = dbExecutor; this.dbExecutor = dbExecutor;
this.pluginConfig = pluginConfig; this.pluginConfig = pluginConfig;
this.transportKeyManagerFactory = transportKeyManagerFactory;
this.transportCrypto = transportCrypto; this.transportCrypto = transportCrypto;
managers = new ConcurrentHashMap<>(); managers = new ConcurrentHashMap<>();
for (SimplexPluginFactory f : pluginConfig.getSimplexFactories()) {
TransportKeyManager m = transportKeyManagerFactory.
createTransportKeyManager(f.getId(), f.getMaxLatency());
managers.put(f.getId(), m);
}
for (DuplexPluginFactory f : pluginConfig.getDuplexFactories()) {
TransportKeyManager m = transportKeyManagerFactory.
createTransportKeyManager(f.getId(), f.getMaxLatency());
managers.put(f.getId(), m);
}
} }
@Override @Override
public void startService() throws ServiceException { public void startService() throws ServiceException {
if (used.getAndSet(true)) throw new IllegalStateException(); if (used.getAndSet(true)) throw new IllegalStateException();
Map<TransportId, Integer> transports = new HashMap<>();
for (SimplexPluginFactory f : pluginConfig.getSimplexFactories())
transports.put(f.getId(), f.getMaxLatency());
for (DuplexPluginFactory f : pluginConfig.getDuplexFactories())
transports.put(f.getId(), f.getMaxLatency());
try { try {
db.transaction(false, txn -> { db.transaction(false, txn -> {
for (Entry<TransportId, Integer> e : transports.entrySet()) for (SimplexPluginFactory f :
db.addTransport(txn, e.getKey(), e.getValue()); pluginConfig.getSimplexFactories()) {
for (Entry<TransportId, Integer> e : transports.entrySet()) { db.addTransport(txn, f.getId(), f.getMaxLatency());
TransportKeyManager m = transportKeyManagerFactory managers.get(f.getId()).start(txn);
.createTransportKeyManager(e.getKey(), }
e.getValue()); for (DuplexPluginFactory f :
managers.put(e.getKey(), m); pluginConfig.getDuplexFactories()) {
m.start(txn); db.addTransport(txn, f.getId(), f.getMaxLatency());
managers.get(f.getId()).start(txn);
} }
}); });
} catch (DbException e) { } catch (DbException e) {

View File

@@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
@@ -71,8 +72,7 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
private final SecretKey rootKey = getSecretKey(); private final SecretKey rootKey = getSecretKey();
private final Random random = new Random(); private final Random random = new Random();
private final KeyManagerImpl keyManager = new KeyManagerImpl(db, executor, private KeyManagerImpl keyManager;
pluginConfig, transportKeyManagerFactory, transportCrypto);
@Before @Before
public void testStartService() throws Exception { public void testStartService() throws Exception {
@@ -83,18 +83,25 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
singletonList(pluginFactory); singletonList(pluginFactory);
int maxLatency = 1337; int maxLatency = 1337;
context.checking(new DbExpectations() {{ context.checking(new Expectations() {{
oneOf(pluginConfig).getSimplexFactories(); allowing(pluginConfig).getSimplexFactories();
will(returnValue(factories)); will(returnValue(factories));
oneOf(pluginFactory).getId(); allowing(pluginFactory).getId();
will(returnValue(transportId)); will(returnValue(transportId));
oneOf(pluginFactory).getMaxLatency(); allowing(pluginFactory).getMaxLatency();
will(returnValue(maxLatency)); will(returnValue(maxLatency));
oneOf(db).addTransport(txn, transportId, maxLatency); allowing(pluginConfig).getDuplexFactories();
will(returnValue(emptyList()));
oneOf(transportKeyManagerFactory) oneOf(transportKeyManagerFactory)
.createTransportKeyManager(transportId, maxLatency); .createTransportKeyManager(transportId, maxLatency);
will(returnValue(transportKeyManager)); will(returnValue(transportKeyManager));
oneOf(pluginConfig).getDuplexFactories(); }});
keyManager = new KeyManagerImpl(db, executor,
pluginConfig, transportCrypto, transportKeyManagerFactory);
context.checking(new DbExpectations() {{
oneOf(db).addTransport(txn, transportId, maxLatency);
oneOf(db).transaction(with(false), withDbRunnable(txn)); oneOf(db).transaction(with(false), withDbRunnable(txn));
oneOf(transportKeyManager).start(txn); oneOf(transportKeyManager).start(txn);
}}); }});