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

Feature/disable auto discovery #477

Merged
merged 7 commits into from Sep 26, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public void buildOptions() {
"unixSocketFile",
"useWhiteList",
"server.sslConfig.clientTrustCertificates",
"server.sslConfig.serverTrustCertificates"
"server.sslConfig.serverTrustCertificates",
"disablePeerDiscovery"
);

final Map<String, Class> results = OverrideUtil.buildConfigOptions();
Expand Down Expand Up @@ -304,7 +305,7 @@ public void convertTo() {
@Test
public void initialiseNestedObjects() {

Config config = new Config(null, null, null, null, null, null, true);
Config config = new Config(null, null, null, null, null, null, true,true);

OverrideUtil.initialiseNestedObjects(config);

Expand All @@ -315,6 +316,7 @@ public void initialiseNestedObjects() {
assertThat(config.getKeys()).isNotNull();
assertThat(config.getPeers()).isEmpty();
assertThat(config.getAlwaysSendTo()).isEmpty();
assertThat(config.isDisablePeerDiscovery()).isTrue();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public Config build() {
forwardingKeys = Collections.emptyList();
}

return new Config(jdbcConfig, serverConfig, peerList, keyData, forwardingKeys, toPath(workDir, unixSocketFile), useWhiteList);
return new Config(jdbcConfig, serverConfig, peerList, keyData, forwardingKeys, toPath(workDir, unixSocketFile), useWhiteList,false);
}

}
13 changes: 11 additions & 2 deletions config/src/main/java/com/quorum/tessera/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,33 @@ public class Config extends ConfigItem {
@XmlAttribute
private final boolean useWhiteList;

@XmlAttribute
private final boolean disablePeerDiscovery;

public Config(final JdbcConfig jdbcConfig,
final ServerConfig serverConfig,
final List<Peer> peers,
final KeyConfiguration keyConfiguration,
final List<String> alwaysSendTo,
final Path unixSocketFile,
final boolean useWhiteList) {
final boolean useWhiteList,
final boolean disablePeerDiscovery) {
this.jdbcConfig = jdbcConfig;
this.serverConfig = serverConfig;
this.peers = peers;
this.keys = keyConfiguration;
this.alwaysSendTo = alwaysSendTo;
this.unixSocketFile = unixSocketFile;
this.useWhiteList = useWhiteList;
this.disablePeerDiscovery = disablePeerDiscovery;
}

private static Config create() {
return new Config();
}

private Config() {
this(null, null, null, null, null, null, false);
this(null, null, null, null, null, null, false,false);
}

public JdbcConfig getJdbcConfig() {
Expand Down Expand Up @@ -108,4 +113,8 @@ public boolean isUseWhiteList() {
return this.useWhiteList;
}

public boolean isDisablePeerDiscovery() {
return disablePeerDiscovery;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
public class JaxbConfigFactory implements ConfigFactory {

private static final Set<PosixFilePermission> NEW_PASSWORD_FILE_PERMS = Stream
.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE)
.collect(Collectors.toSet());
.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE)
.collect(Collectors.toSet());

@Override
public Config create(final InputStream configData, final List<KeyData> newKeys) {

Expand All @@ -33,12 +33,12 @@ public Config create(final InputStream configData, final List<KeyData> newKeys)
if (Objects.nonNull(config.getKeys()) && !newKeys.isEmpty()) {
try {
final List<String> newPasswords = newKeys
.stream()
.map(KeyData::getConfig)
.map(KeyDataConfig::getPassword)
.map(Optional::ofNullable)
.map(pass -> pass.orElse(""))
.collect(Collectors.toList());
.stream()
.map(KeyData::getConfig)
.map(KeyDataConfig::getPassword)
.map(Optional::ofNullable)
.map(pass -> pass.orElse(""))
.collect(Collectors.toList());

if (config.getKeys().getPasswords() != null) {
config.getKeys().getPasswords().addAll(newPasswords);
Expand All @@ -47,11 +47,11 @@ public Config create(final InputStream configData, final List<KeyData> newKeys)
Files.write(config.getKeys().getPasswordFile(), newPasswords, APPEND);
} else if (!newPasswords.stream().allMatch(""::equals)) {
final List<String> existingPasswords = config
.getKeys()
.getKeyData()
.stream()
.map(k -> "")
.collect(Collectors.toList());
.getKeys()
.getKeyData()
.stream()
.map(k -> "")
.collect(Collectors.toList());
existingPasswords.addAll(newPasswords);

this.createFile(Paths.get("passwords.txt"));
Expand All @@ -66,16 +66,17 @@ public Config create(final InputStream configData, final List<KeyData> newKeys)

}

if(createdNewPasswordFile) {
if (createdNewPasswordFile) {
//return a new object with the password file set
return new Config(
config.getJdbcConfig(),
config.getServerConfig(),
config.getPeers(),
new KeyConfiguration(Paths.get("passwords.txt"), null, config.getKeys().getKeyData()),
config.getAlwaysSendTo(),
config.getUnixSocketFile(),
config.isUseWhiteList()
config.getJdbcConfig(),
config.getServerConfig(),
config.getPeers(),
new KeyConfiguration(Paths.get("passwords.txt"), null, config.getKeys().getKeyData()),
config.getAlwaysSendTo(),
config.getUnixSocketFile(),
config.isUseWhiteList(),
config.isDisablePeerDiscovery()
);
} else {
//leave config untouched since it wasn't needed to make a new one
Expand All @@ -86,7 +87,7 @@ public Config create(final InputStream configData, final List<KeyData> newKeys)
//create a file if it doesn't exist and set the permissions to be only
// read/write for the creator
private void createFile(final Path fileToMake) throws IOException {
if(Files.notExists(fileToMake)) {
if (Files.notExists(fileToMake)) {
Files.createFile(fileToMake);
Files.setPosixFilePermissions(fileToMake, NEW_PASSWORD_FILE_PERMS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void invalidAlwaysSendTo() {

List<String> alwaysSendTo = Arrays.asList("BOGUS");

Config config = new Config(null, null, null, null, alwaysSendTo, null, false);
Config config = new Config(null, null, null, null, alwaysSendTo, null, false,false);

Set<ConstraintViolation<Config>> violations = validator.validateProperty(config, "alwaysSendTo");

Expand All @@ -120,7 +120,7 @@ public void validAlwaysSendTo() {

List<String> alwaysSendTo = Arrays.asList(value);

Config config = new Config(null, null, null, null, alwaysSendTo, null, false);
Config config = new Config(null, null, null, null, alwaysSendTo, null, false,false);

Set<ConstraintViolation<Config>> violations = validator.validateProperty(config, "alwaysSendTo");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quorum.tessera.api.grpc;

import com.quorum.tessera.node.AutoDiscoveryDisabledException;
import io.grpc.stub.StreamObserver;
import java.util.Objects;
import javax.validation.ConstraintViolationException;
Expand All @@ -23,6 +24,9 @@ public void handle(StreamObserverCallback callback) {
observer.onNext(r);
observer.onCompleted();

} catch(AutoDiscoveryDisabledException ex) {
observer.onError(io.grpc.Status.PERMISSION_DENIED
.withDescription(ex.getMessage()).asRuntimeException());
} catch (ConstraintViolationException validationError) {
observer.onError(io.grpc.Status.INVALID_ARGUMENT
.withCause(validationError)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quorum.tessera.api.grpc;

import com.quorum.tessera.node.AutoDiscoveryDisabledException;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
Expand All @@ -15,6 +16,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class StreamObserverTemplateTest {

Expand Down Expand Up @@ -76,7 +78,6 @@ public void executeValidationError() {
@Test
public void executeOtherError() {


Throwable exception = new Throwable("OUCH");

template.handle(() -> {
Expand All @@ -87,4 +88,34 @@ public void executeOtherError() {
verify(observer).onError(exception);

}


@Test
public void executeAutoDiscoveryDisabled() {

List<StatusRuntimeException> results = new ArrayList<>();
doAnswer((iom) -> {
results.add(iom.getArgument(0));
return null;
}).when(observer)
.onError(any(StatusRuntimeException.class));

final String exceptionMessage = "Sorry Dave I cant let you do that";

AutoDiscoveryDisabledException exception = mock(AutoDiscoveryDisabledException.class);
when(exception.getMessage()).thenReturn(exceptionMessage);


template.handle(() -> {
throw exception;
});

StatusRuntimeException result = results.stream().findAny().get();

assertThat(result.getStatus().getCode()).isEqualTo(Status.PERMISSION_DENIED.getCode());

assertThat(result.getStatus().getDescription()).isEqualTo(exceptionMessage);

verify(observer).onError(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.quorum.tessera.api.exception;

import com.quorum.tessera.node.AutoDiscoveryDisabledException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class AutoDiscoveryDisabledExceptionMapper implements ExceptionMapper<AutoDiscoveryDisabledException> {

@Override
public Response toResponse(AutoDiscoveryDisabledException exception) {
return Response.status(Response.Status.FORBIDDEN)
.entity(exception.getMessage())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.quorum.tessera.api.exception;

import com.quorum.tessera.node.AutoDiscoveryDisabledException;
import javax.ws.rs.core.Response;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;

public class AutoDiscoveryDisabledExceptionMapperTest {

private AutoDiscoveryDisabledExceptionMapper mapper;

@Before
public void onSetUp() {
mapper = new AutoDiscoveryDisabledExceptionMapper();
}

@Test
public void handleAutoDiscoveryDisabledException() {
String message = ".. all outta gum";
AutoDiscoveryDisabledException exception =
new AutoDiscoveryDisabledException(message);

Response result = mapper.toResponse(exception);

assertThat(result.getStatus()).isEqualTo(403);
assertThat(result.getEntity()).isEqualTo(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Config config() {

Path unixSocketFile = mock(Path.class);

Config config = new Config(jdbcConfig,serverConfig,Collections.EMPTY_LIST,keyConfiguration,Collections.EMPTY_LIST,unixSocketFile,false);
Config config = new Config(jdbcConfig,serverConfig,Collections.EMPTY_LIST,keyConfiguration,Collections.EMPTY_LIST,unixSocketFile,false,false);

return config;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.quorum.tessera.core.config;

import com.quorum.tessera.config.Config;

public interface ConfigService {

Config getConfig();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.quorum.tessera.core.config;

import com.quorum.tessera.config.Config;
import java.util.Objects;


public class ConfigServiceImpl implements ConfigService {

private final Config config;

public ConfigServiceImpl(Config initialConfig) {
this.config = Objects.requireNonNull(initialConfig);
}

@Override
public Config getConfig() {
return config;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.quorum.tessera.node;

import com.quorum.tessera.exception.TesseraException;

public class AutoDiscoveryDisabledException extends TesseraException {

public AutoDiscoveryDisabledException(String message) {
super(message);
}

}
Loading