Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration holder #57

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions nexus-app/src/main/java/com/github/nexus/app/Launcher.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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();

Expand Down
34 changes: 34 additions & 0 deletions nexus-app/src/main/java/com/github/nexus/config/ConfigHolder.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
33 changes: 33 additions & 0 deletions nexus-app/src/main/java/com/github/nexus/config/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.nexus.config;

import java.util.ArrayList;
import java.util.List;

public class Configuration {

private List<String> publicKeys;

private List<String> privateKeys;

public Configuration() {
this.publicKeys = new ArrayList<>();
this.privateKeys = new ArrayList<>();
}

public List<String> getPublicKeys() {
return publicKeys;
}

public void setPublicKeys(final List<String> publicKeys) {
this.publicKeys = publicKeys;
}

public List<String> getPrivateKeys() {
return privateKeys;
}

public void setPrivateKeys(final List<String> privateKeys) {
this.privateKeys = privateKeys;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -35,7 +37,11 @@ public class KeyManagerImpl implements KeyManager {
private final String baseKeygenPath;

public KeyManagerImpl(final String baseKeygenPath, final NaclFacade nacl, final List<Path> publicKeyPaths, final List<Path> 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(
Expand All @@ -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<KeyPair> 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
Expand Down
7 changes: 6 additions & 1 deletion nexus-app/src/main/resources/nexus-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@
<constructor-arg ref="nacl" />
</bean>

<bean id="configHolder" class="com.github.nexus.config.ConfigHolder" factory-method="getInstance" />

<bean name="keyManager" class="com.github.nexus.enclave.keys.KeyManagerImpl">
<constructor-arg value="." />
<constructor-arg value="./" />
<constructor-arg ref="nacl" />
<constructor-arg>
<bean factory-bean="configHolder" factory-method="getConfig" />
</constructor-arg>
</bean>

<bean name="nacl" class="com.github.nexus.encryption.Kalium">
Expand Down
29 changes: 20 additions & 9 deletions nexus-app/src/test/java/com/github/nexus/NexusIT.java
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
1 change: 1 addition & 0 deletions nexus-app/src/test/resources/key.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"bytes":"yAWAJjwPqUtNVlqGjSrBmr1/iIkghuOh1803Yzx9jLM="},"type":"unlocked"}
1 change: 1 addition & 0 deletions nexus-app/src/test/resources/key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/+UuD63zItL1EbjxkKUljMgG8Z1w0AJ8pNOR4iq2yQc=