diff --git a/nexus-app/src/main/java/com/github/nexus/app/Launcher.java b/nexus-app/src/main/java/com/github/nexus/app/Launcher.java index 86c760779f..c511a982d6 100644 --- a/nexus-app/src/main/java/com/github/nexus/app/Launcher.java +++ b/nexus-app/src/main/java/com/github/nexus/app/Launcher.java @@ -1,10 +1,15 @@ package com.github.nexus.app; +import com.github.nexus.config.ConfigHolder; +import com.github.nexus.config.Configuration; import com.github.nexus.server.RestServer; import com.github.nexus.server.RestServerFactory; import com.github.nexus.service.locator.ServiceLocator; -import java.net.URI; + import javax.ws.rs.core.UriBuilder; +import java.net.URI; + +import static java.util.Collections.singletonList; /** * The main entry point for the application. @@ -14,11 +19,16 @@ public class Launcher { public static final URI SERVER_URI = UriBuilder.fromUri("http://0.0.0.0/").port(8080).build(); - public static void main(String... args) throws Exception { + public static void main(final String... args) throws Exception { + + final Configuration configuration = new Configuration(); + ConfigHolder.INSTANCE.setConfiguration(configuration); + configuration.setPublicKeys(singletonList("key.pub")); + configuration.setPrivateKeys(singletonList("key.key")); - Nexus nexus = new Nexus(ServiceLocator.create()); + final Nexus nexus = new Nexus(ServiceLocator.create()); - RestServer restServer = RestServerFactory.create().createServer(SERVER_URI, nexus); + final RestServer restServer = RestServerFactory.create().createServer(SERVER_URI, nexus); restServer.start(); diff --git a/nexus-app/src/main/java/com/github/nexus/config/ConfigHolder.java b/nexus-app/src/main/java/com/github/nexus/config/ConfigHolder.java new file mode 100644 index 0000000000..bd4695482a --- /dev/null +++ b/nexus-app/src/main/java/com/github/nexus/config/ConfigHolder.java @@ -0,0 +1,34 @@ +package com.github.nexus.config; + +import java.util.Objects; + +public enum ConfigHolder { + + INSTANCE; + + private Configuration config; + + private boolean initialised; + + ConfigHolder() { + this.initialised = false; + } + + public synchronized void setConfiguration(final Configuration config) { + if(initialised) { + throw new RuntimeException("Configuration already set"); + } + + this.initialised = true; + this.config = Objects.requireNonNull(config); + } + + public static ConfigHolder getInstance() { + return INSTANCE; + } + + public Configuration getConfig() { + return config; + } + +} diff --git a/nexus-app/src/main/java/com/github/nexus/config/Configuration.java b/nexus-app/src/main/java/com/github/nexus/config/Configuration.java new file mode 100644 index 0000000000..c673caf5cf --- /dev/null +++ b/nexus-app/src/main/java/com/github/nexus/config/Configuration.java @@ -0,0 +1,33 @@ +package com.github.nexus.config; + +import java.util.ArrayList; +import java.util.List; + +public class Configuration { + + private List publicKeys; + + private List privateKeys; + + public Configuration() { + this.publicKeys = new ArrayList<>(); + this.privateKeys = new ArrayList<>(); + } + + public List getPublicKeys() { + return publicKeys; + } + + public void setPublicKeys(final List publicKeys) { + this.publicKeys = publicKeys; + } + + public List getPrivateKeys() { + return privateKeys; + } + + public void setPrivateKeys(final List privateKeys) { + this.privateKeys = privateKeys; + } + +} diff --git a/nexus-app/src/main/java/com/github/nexus/enclave/keys/KeyManagerImpl.java b/nexus-app/src/main/java/com/github/nexus/enclave/keys/KeyManagerImpl.java index 985d0d41df..0c3170ab39 100644 --- a/nexus-app/src/main/java/com/github/nexus/enclave/keys/KeyManagerImpl.java +++ b/nexus-app/src/main/java/com/github/nexus/enclave/keys/KeyManagerImpl.java @@ -1,5 +1,6 @@ package com.github.nexus.enclave.keys; +import com.github.nexus.config.Configuration; import com.github.nexus.enclave.keys.model.Key; import com.github.nexus.enclave.keys.model.KeyException; import com.github.nexus.enclave.keys.model.KeyPair; @@ -20,6 +21,7 @@ import java.util.stream.IntStream; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.stream.Collectors.toList; public class KeyManagerImpl implements KeyManager { @@ -35,7 +37,11 @@ public class KeyManagerImpl implements KeyManager { private final String baseKeygenPath; public KeyManagerImpl(final String baseKeygenPath, final NaclFacade nacl, final List publicKeyPaths, final List privateKeyPaths) { - this(baseKeygenPath, nacl, null); + + this.nacl = Objects.requireNonNull(nacl, "nacl is required"); + this.baseKeygenPath = Objects.requireNonNull(baseKeygenPath, "basepath is required"); + + this.ourKeys = new HashSet<>(); if (publicKeyPaths.size() != privateKeyPaths.size()) { LOGGER.error( @@ -52,24 +58,17 @@ public KeyManagerImpl(final String baseKeygenPath, final NaclFacade nacl, final ourKeys.addAll(keys); - } - public KeyManagerImpl(final String baseKeygenPath, final NaclFacade nacl, final Collection initialKeyset) { - - this.nacl = Objects.requireNonNull(nacl); - this.baseKeygenPath = Objects.requireNonNull(baseKeygenPath); + public KeyManagerImpl(final String baseKeygenPath, final NaclFacade nacl, final Configuration configuration) { - this.ourKeys = new HashSet<>(); - - if (initialKeyset != null) { - this.ourKeys.addAll(initialKeyset); - } - - } + this( + baseKeygenPath, + nacl, + Objects.requireNonNull(configuration, "config must not be null").getPublicKeys().stream().map(Paths::get).collect(toList()), + configuration.getPrivateKeys().stream().map(Paths::get).collect(toList()) + ); - public KeyManagerImpl(final String baseKeygenPath, final NaclFacade nacl) { - this(baseKeygenPath, nacl, null); } @Override diff --git a/nexus-app/src/main/resources/nexus-spring.xml b/nexus-app/src/main/resources/nexus-spring.xml index 83e5762af4..3cf9657316 100644 --- a/nexus-app/src/main/resources/nexus-spring.xml +++ b/nexus-app/src/main/resources/nexus-spring.xml @@ -36,9 +36,14 @@ + + - + + + + diff --git a/nexus-app/src/test/java/com/github/nexus/NexusIT.java b/nexus-app/src/test/java/com/github/nexus/NexusIT.java index 1d93296085..a9e28affe0 100644 --- a/nexus-app/src/test/java/com/github/nexus/NexusIT.java +++ b/nexus-app/src/test/java/com/github/nexus/NexusIT.java @@ -1,26 +1,37 @@ package com.github.nexus; import com.github.nexus.app.Nexus; +import com.github.nexus.config.ConfigHolder; +import com.github.nexus.config.Configuration; import com.github.nexus.service.locator.ServiceLocator; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.Base64; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Ignore; +import org.junit.Test; + import javax.json.Json; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Base64; + +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; -import org.glassfish.jersey.test.JerseyTest; -import org.junit.Ignore; -import org.junit.Test; public class NexusIT extends JerseyTest { - public NexusIT() { - } - @Override protected Application configure() { + + if (ConfigHolder.INSTANCE.getConfig() == null) { + ConfigHolder.INSTANCE.setConfiguration(new Configuration()); + } + + final Configuration config = ConfigHolder.INSTANCE.getConfig(); + config.setPublicKeys(singletonList("./target/test-classes/key.pub")); + config.setPrivateKeys(singletonList("./target/test-classes/key.key")); + ServiceLocator serviceLocator = ServiceLocator.create(); return new Nexus(serviceLocator); } diff --git a/nexus-app/src/test/java/com/github/nexus/config/ConfigHolderTest.java b/nexus-app/src/test/java/com/github/nexus/config/ConfigHolderTest.java new file mode 100644 index 0000000000..e342839a43 --- /dev/null +++ b/nexus-app/src/test/java/com/github/nexus/config/ConfigHolderTest.java @@ -0,0 +1,66 @@ +package com.github.nexus.config; + +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Field; + +import static com.github.nexus.config.ConfigHolder.INSTANCE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; + +public class ConfigHolderTest { + + @Before + public void init() throws Exception { + + final Field field = ConfigHolder.class.getDeclaredField("initialised"); + field.setAccessible(true); + field.set(INSTANCE, false); + + } + + @Test + public void getInstanceReturnsSingleton() { + + assertThat(ConfigHolder.getInstance()).isEqualTo(INSTANCE); + + } + + @Test + public void canOnlySetConfigurationOnce(){ + + final Configuration config = new Configuration(); + + INSTANCE.setConfiguration(config); + + final Throwable throwable = catchThrowable(() -> INSTANCE.setConfiguration(config)); + + assertThat(throwable) + .isInstanceOf(RuntimeException.class) + .hasMessage("Configuration already set"); + + } + + @Test + public void configCannotBeNull() { + + final Throwable throwable = catchThrowable(() -> INSTANCE.setConfiguration(null)); + + assertThat(throwable) + .isInstanceOf(NullPointerException.class); + + } + + @Test + public void getConfigReturnsSetConfig() { + + final Configuration config = new Configuration(); + + INSTANCE.setConfiguration(config); + + assertThat(INSTANCE.getConfig()).isEqualTo(config); + + } + +} diff --git a/nexus-app/src/test/java/com/github/nexus/enclave/keys/KeyManagerTest.java b/nexus-app/src/test/java/com/github/nexus/enclave/keys/KeyManagerTest.java index 4c78a83a7b..e212c84aa9 100644 --- a/nexus-app/src/test/java/com/github/nexus/enclave/keys/KeyManagerTest.java +++ b/nexus-app/src/test/java/com/github/nexus/enclave/keys/KeyManagerTest.java @@ -1,5 +1,6 @@ package com.github.nexus.enclave.keys; +import com.github.nexus.config.Configuration; import com.github.nexus.enclave.keys.model.Key; import com.github.nexus.enclave.keys.model.KeyPair; import com.github.nexus.encryption.NaclFacade; @@ -13,14 +14,12 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.Base64; -import java.util.Collections; import java.util.UUID; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.Collections.emptyList; -import static java.util.Collections.emptySet; -import static java.util.Collections.singletonList; +import static java.util.Collections.*; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; import static org.mockito.Mockito.doReturn; @@ -50,16 +49,29 @@ public void init() throws IOException { this.keygenPath = Files.createTempDirectory(UUID.randomUUID().toString()); - this.naclFacade = mock(NaclFacade.class); + final byte[] privateKeyJson = Json.createObjectBuilder() + .add("type", "unlocked") + .add("data", Json.createObjectBuilder().add("bytes", keyPair.getPrivateKey().toString())) + .build() + .toString() + .getBytes(UTF_8); + + Files.write(keygenPath.resolve("key.pub"), keyPair.getPublicKey().toString().getBytes(UTF_8), StandardOpenOption.CREATE_NEW); + Files.write(keygenPath.resolve("key.key"), privateKeyJson, StandardOpenOption.CREATE_NEW); - this.keyManager = new KeyManagerImpl(keygenPath.toString(), naclFacade, Collections.singleton(keyPair)); + final Configuration configuration = new Configuration(); + configuration.setPublicKeys(singletonList(keygenPath.resolve("key.pub").toString())); + configuration.setPrivateKeys(singletonList(keygenPath.resolve("key.key").toString())); + + this.naclFacade = mock(NaclFacade.class); + this.keyManager = new KeyManagerImpl(keygenPath.toString(), naclFacade, configuration); } @Test public void initialisedWithNoKeys() { - this.keyManager = new KeyManagerImpl(keygenPath.toString(), naclFacade); + this.keyManager = new KeyManagerImpl(keygenPath.toString(), naclFacade, new Configuration()); assertThat(keyManager).extracting("ourKeys").containsExactly(emptySet()); } diff --git a/nexus-app/src/test/resources/key.key b/nexus-app/src/test/resources/key.key new file mode 100644 index 0000000000..4afa75e346 --- /dev/null +++ b/nexus-app/src/test/resources/key.key @@ -0,0 +1 @@ +{"data":{"bytes":"yAWAJjwPqUtNVlqGjSrBmr1/iIkghuOh1803Yzx9jLM="},"type":"unlocked"} \ No newline at end of file diff --git a/nexus-app/src/test/resources/key.pub b/nexus-app/src/test/resources/key.pub new file mode 100644 index 0000000000..879f0b4b03 --- /dev/null +++ b/nexus-app/src/test/resources/key.pub @@ -0,0 +1 @@ +/+UuD63zItL1EbjxkKUljMgG8Z1w0AJ8pNOR4iq2yQc= \ No newline at end of file