Search in sources :

Example 1 with KeyVaultConfigValidations

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();
    }
}
Also used : KeyVaultConfigValidations(com.quorum.tessera.context.KeyVaultConfigValidations) ConstraintViolation(jakarta.validation.ConstraintViolation) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Test(org.junit.Test)

Example 2 with KeyVaultConfigValidations

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;
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) PublicKey(com.quorum.tessera.encryption.PublicKey) Client(jakarta.ws.rs.client.Client) java.util(java.util) Logger(org.slf4j.Logger) KeyEncryptorFactory(com.quorum.tessera.config.keys.KeyEncryptorFactory) LoggerFactory(org.slf4j.LoggerFactory) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Collectors(java.util.stream.Collectors) RestClientFactory(com.quorum.tessera.context.RestClientFactory) KeyDataUtil(com.quorum.tessera.config.util.KeyDataUtil) Stream(java.util.stream.Stream) com.quorum.tessera.config(com.quorum.tessera.config) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Enclave(com.quorum.tessera.enclave.Enclave) URI(java.net.URI) KeyVaultConfigValidations(com.quorum.tessera.context.KeyVaultConfigValidations) KeyVaultConfigValidations(com.quorum.tessera.context.KeyVaultConfigValidations) PublicKey(com.quorum.tessera.encryption.PublicKey) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) URI(java.net.URI) Enclave(com.quorum.tessera.enclave.Enclave) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Client(jakarta.ws.rs.client.Client) RuntimeContext(com.quorum.tessera.context.RuntimeContext)

Aggregations

KeyVaultConfigValidations (com.quorum.tessera.context.KeyVaultConfigValidations)2 ConstraintViolation (jakarta.validation.ConstraintViolation)2 ConstraintViolationException (jakarta.validation.ConstraintViolationException)2 com.quorum.tessera.config (com.quorum.tessera.config)1 ConfigKeyPair (com.quorum.tessera.config.keypairs.ConfigKeyPair)1 KeyEncryptor (com.quorum.tessera.config.keys.KeyEncryptor)1 KeyEncryptorFactory (com.quorum.tessera.config.keys.KeyEncryptorFactory)1 KeyDataUtil (com.quorum.tessera.config.util.KeyDataUtil)1 RestClientFactory (com.quorum.tessera.context.RestClientFactory)1 RuntimeContext (com.quorum.tessera.context.RuntimeContext)1 Enclave (com.quorum.tessera.enclave.Enclave)1 PublicKey (com.quorum.tessera.encryption.PublicKey)1 Client (jakarta.ws.rs.client.Client)1 URI (java.net.URI)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1