Search in sources :

Example 6 with ConfigKeyPair

use of com.quorum.tessera.config.keypairs.ConfigKeyPair in project tessera by ConsenSys.

the class DefaultKeyVaultConfigValidationsTest method validCase.

@Test
public void validCase() {
    KeyConfiguration keyConfiguration = mock(KeyConfiguration.class);
    // Not ideal. Having to use config object in tests to apply validation rules.
    KeyVaultConfig keyVaultConfig = new DefaultKeyVaultConfig() {

        {
            setKeyVaultType(KeyVaultType.AZURE);
        }
    };
    List<KeyVaultConfig> keyVaultConfigList = Arrays.asList(mock(KeyVaultConfig.class));
    when(keyConfiguration.getKeyVaultConfigs()).thenReturn(keyVaultConfigList);
    ConfigKeyPair keyPair = new AzureVaultKeyPair("publicKeyId", "privateKeyId", null, null);
    List<ConfigKeyPair> keyPairs = Arrays.asList(keyPair);
    Set<ConstraintViolation<?>> results = validator.validate(keyConfiguration, keyPairs);
    assertThat(results).isEmpty();
}
Also used : KeyVaultConfig(com.quorum.tessera.config.KeyVaultConfig) DefaultKeyVaultConfig(com.quorum.tessera.config.DefaultKeyVaultConfig) DefaultKeyVaultConfig(com.quorum.tessera.config.DefaultKeyVaultConfig) KeyConfiguration(com.quorum.tessera.config.KeyConfiguration) ConstraintViolation(jakarta.validation.ConstraintViolation) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) AzureVaultKeyPair(com.quorum.tessera.config.keypairs.AzureVaultKeyPair) Test(org.junit.Test)

Example 7 with ConfigKeyPair

use of com.quorum.tessera.config.keypairs.ConfigKeyPair in project tessera by ConsenSys.

the class RuntimeContextProviderTest method provides.

@Test
public void provides() {
    Config confg = createMockConfig();
    try (var mockedStaticConfigFactory = mockStatic(ConfigFactory.class);
        var mockStaticRestClientFactory = mockStatic(RestClientFactory.class);
        var mockStaticKeyDataUtil = mockStatic(KeyDataUtil.class);
        var mockStaticEnclave = mockStatic(Enclave.class)) {
        Enclave enclave = mock(Enclave.class);
        mockStaticEnclave.when(Enclave::create).thenReturn(enclave);
        ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
        when(configKeyPair.getPublicKey()).thenReturn(Base64.getEncoder().encodeToString("PublicKey".getBytes()));
        when(configKeyPair.getPrivateKey()).thenReturn(Base64.getEncoder().encodeToString("PrivateKey".getBytes()));
        mockStaticKeyDataUtil.when(() -> KeyDataUtil.unmarshal(any(KeyData.class), any(KeyEncryptor.class))).thenReturn(configKeyPair);
        RestClientFactory restClientFactory = mock(RestClientFactory.class);
        when(restClientFactory.buildFrom(any(ServerConfig.class))).thenReturn(mock(Client.class));
        mockStaticRestClientFactory.when(RestClientFactory::create).thenReturn(restClientFactory);
        ConfigFactory configFactory = mock(ConfigFactory.class);
        when(configFactory.getConfig()).thenReturn(confg);
        mockedStaticConfigFactory.when(ConfigFactory::create).thenReturn(configFactory);
        RuntimeContext runtimeContext = RuntimeContextProvider.provider();
        assertThat(runtimeContext).isNotNull().isSameAs(RuntimeContextProvider.provider());
        mockedStaticConfigFactory.verify(ConfigFactory::create);
        mockedStaticConfigFactory.verifyNoMoreInteractions();
        mockStaticRestClientFactory.verify(RestClientFactory::create);
        mockedStaticConfigFactory.verifyNoMoreInteractions();
        mockStaticKeyDataUtil.verify(() -> KeyDataUtil.unmarshal(any(KeyData.class), any(KeyEncryptor.class)));
        mockStaticKeyDataUtil.verifyNoMoreInteractions();
        mockStaticEnclave.verify(Enclave::create);
        mockStaticEnclave.verifyNoMoreInteractions();
        verify(enclave).getPublicKeys();
        verifyNoMoreInteractions(enclave);
    }
}
Also used : Enclave(com.quorum.tessera.enclave.Enclave) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) RestClientFactory(com.quorum.tessera.context.RestClientFactory) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) Client(jakarta.ws.rs.client.Client) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 8 with ConfigKeyPair

use of com.quorum.tessera.config.keypairs.ConfigKeyPair 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)

Example 9 with ConfigKeyPair

use of com.quorum.tessera.config.keypairs.ConfigKeyPair in project tessera by ConsenSys.

the class KeyGenCommandTest method noArgsProvided.

@Test
public void noArgsProvided() throws Exception {
    ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
    when(keyGenerator.generate("", null, null)).thenReturn(configKeyPair);
    when(keyGeneratorFactory.create(refEq(null), any(EncryptorConfig.class))).thenReturn(keyGenerator);
    int exitCode = commandLine.execute();
    assertThat(exitCode).isZero();
    CommandLine.ParseResult parseResult = commandLine.getParseResult();
    assertThat(parseResult).isNotNull();
    assertThat(parseResult.matchedArgs()).isEmpty();
    assertThat(parseResult.unmatched()).isEmpty();
    CliResult result = commandLine.getExecutionResult();
    assertThat(result).isNotNull();
    assertThat(result.isSuppressStartup()).isTrue();
    assertThat(result.getConfig()).isNotPresent();
    assertThat(result.getStatus()).isEqualTo(0);
    verify(keyDataMarshaller).marshal(configKeyPair);
    verify(keyGeneratorFactory).create(refEq(null), any(EncryptorConfig.class));
    verify(keyGenerator).generate("", null, null);
}
Also used : CommandLine(picocli.CommandLine) CliResult(com.quorum.tessera.cli.CliResult) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) Test(org.junit.Test)

Example 10 with ConfigKeyPair

use of com.quorum.tessera.config.keypairs.ConfigKeyPair in project tessera by ConsenSys.

the class KeyGenCommandTest method updateFileStuffWithOutputFile.

@Test
public void updateFileStuffWithOutputFile() throws Exception {
    String filename = "";
    char[] password = "I LOVE SPARROWS".toCharArray();
    ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
    when(configKeyPair.getPassword()).thenReturn(password);
    when(keyGenerator.generate(filename, null, null)).thenReturn(configKeyPair);
    when(keyGeneratorFactory.create(refEq(null), any(EncryptorConfig.class))).thenReturn(keyGenerator);
    Config config = mock(Config.class);
    KeyConfiguration keyConfiguration = mock(KeyConfiguration.class);
    KeyData keyData = mock(KeyData.class);
    when(keyDataMarshaller.marshal(configKeyPair)).thenReturn(keyData);
    when(config.getKeys()).thenReturn(keyConfiguration);
    commandLine.registerConverter(Config.class, value -> config);
    int exitCode = commandLine.execute("--configfile=".concat(filename), "--vault.type=".concat(KeyVaultType.AZURE.name()), "--configout=".concat("config.out"), "--pwdout=".concat("pwd.out"));
    assertThat(exitCode).isZero();
    verify(keyGeneratorFactory).create(refEq(null), any(EncryptorConfig.class));
    verify(keyGenerator).generate(filename, null, null);
    verify(configFileUpdaterWriter).updateAndWrite(List.of(keyData), null, config, Paths.get("config.out"));
    verify(keyDataMarshaller).marshal(configKeyPair);
    verify(passwordFileUpdaterWriter).updateAndWrite(List.of(password), config, Paths.get("pwd.out"));
}
Also used : ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) Test(org.junit.Test)

Aggregations

ConfigKeyPair (com.quorum.tessera.config.keypairs.ConfigKeyPair)16 Test (org.junit.Test)11 Stream (java.util.stream.Stream)5 CliResult (com.quorum.tessera.cli.CliResult)4 Response (jakarta.ws.rs.core.Response)4 Collectors (java.util.stream.Collectors)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 CommandLine (picocli.CommandLine)4 NodeAlias (suite.NodeAlias)4 com.quorum.tessera.config (com.quorum.tessera.config)3 ServerConfig (com.quorum.tessera.config.ServerConfig)3 KeyEncryptor (com.quorum.tessera.config.keys.KeyEncryptor)3 ConfigDescriptor (config.ConfigDescriptor)3 Json (jakarta.json.Json)3 ConstraintViolation (jakarta.validation.ConstraintViolation)3 Client (jakarta.ws.rs.client.Client)3 ReceiveResponse (com.quorum.tessera.api.ReceiveResponse)2 SendRequest (com.quorum.tessera.api.SendRequest)2 SendResponse (com.quorum.tessera.api.SendResponse)2 AzureVaultKeyPair (com.quorum.tessera.config.keypairs.AzureVaultKeyPair)2