use of com.quorum.tessera.context.KeyVaultConfigValidations in project tessera by ConsenSys.
the class RuntimeContextProviderTest method providesHasVaultValidationFailures.
@Test
public void providesHasVaultValidationFailures() {
Config confg = createMockConfig();
try (var mockedStaticConfigFactory = mockStatic(ConfigFactory.class);
var mockStaticKeyVaultConfigValidations = mockStatic(KeyVaultConfigValidations.class)) {
KeyVaultConfigValidations keyVaultConfigValidations = mock(KeyVaultConfigValidations.class);
ConstraintViolation constraintViolation = mock(ConstraintViolation.class);
when(keyVaultConfigValidations.validate(any(), anyList())).thenReturn(Set.of(constraintViolation));
mockStaticKeyVaultConfigValidations.when(KeyVaultConfigValidations::create).thenReturn(keyVaultConfigValidations);
ConfigFactory configFactory = mock(ConfigFactory.class);
when(configFactory.getConfig()).thenReturn(confg);
mockedStaticConfigFactory.when(ConfigFactory::create).thenReturn(configFactory);
Throwable ex = catchThrowable(() -> RuntimeContextProvider.provider());
assertThat(ex).isExactlyInstanceOf(ConstraintViolationException.class);
ConstraintViolationException constraintViolationException = (ConstraintViolationException) ex;
assertThat(constraintViolationException.getConstraintViolations()).containsExactly(constraintViolation);
mockedStaticConfigFactory.verify(ConfigFactory::create);
mockedStaticConfigFactory.verifyNoMoreInteractions();
mockedStaticConfigFactory.verifyNoMoreInteractions();
}
}
use of com.quorum.tessera.context.KeyVaultConfigValidations in project tessera by ConsenSys.
the class RuntimeContextProvider method provider.
public static RuntimeContext provider() {
LOGGER.debug("Providing RuntimeContext");
RuntimeContextHolder contextHolder = RuntimeContextHolder.INSTANCE;
if (contextHolder.getContext().isPresent()) {
LOGGER.debug("Found stored RuntimeContext instance");
return contextHolder.getContext().get();
}
Config config = ConfigFactory.create().getConfig();
EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()).orElse(new EncryptorConfig() {
{
setType(EncryptorType.NACL);
}
});
KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig);
final KeyVaultConfigValidations vaultConfigValidation = KeyVaultConfigValidations.create();
final RuntimeContextBuilder runtimeContextBuilder = RuntimeContextBuilder.create();
if (Objects.nonNull(config.getKeys())) {
List<ConfigKeyPair> configKeyPairs = config.getKeys().getKeyData().stream().map(o -> KeyDataUtil.unmarshal(o, keyEncryptor)).collect(Collectors.toList());
Set<ConstraintViolation<?>> violations = vaultConfigValidation.validate(config.getKeys(), configKeyPairs);
if (!violations.isEmpty()) {
LOGGER.debug("Constraint violations {}", violations);
throw new ConstraintViolationException(violations);
}
final Enclave enclave = Enclave.create();
runtimeContextBuilder.withKeys(enclave.getPublicKeys());
}
List<ServerConfig> servers = config.getServerConfigs();
ServerConfig p2pServerContext = servers.stream().filter(s -> s.getApp() == AppType.P2P).findFirst().orElseThrow(() -> new IllegalStateException("No P2P server configured"));
Client p2pClient = RestClientFactory.create().buildFrom(p2pServerContext);
List<PublicKey> alwaysSendTo = Stream.of(config).map(Config::getAlwaysSendTo).filter(Objects::nonNull).flatMap(List::stream).map(Base64.getDecoder()::decode).map(PublicKey::from).collect(Collectors.toList());
RuntimeContext context = runtimeContextBuilder.withP2pServerUri(config.getP2PServerConfig().getServerUri()).withP2pClient(p2pClient).withKeyEncryptor(keyEncryptor).withDisablePeerDiscovery(config.isDisablePeerDiscovery()).withRemoteKeyValidation(config.getFeatures().isEnableRemoteKeyValidation()).withEnhancedPrivacy(config.getFeatures().isEnablePrivacyEnhancements()).withPeers(config.getPeers().stream().map(Peer::getUrl).map(URI::create).collect(Collectors.toList())).withAlwaysSendTo(alwaysSendTo).withUseWhiteList(config.isUseWhiteList()).withRecoveryMode(config.isRecoveryMode()).withMultiplePrivateStates(config.getFeatures().isEnableMultiplePrivateStates()).withClientMode(config.getClientMode()).build();
contextHolder.setContext(context);
return context;
}
Aggregations