Search in sources :

Example 11 with ConfigKeyPair

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

the class KeyGenCommandTest method updateNoOutputFileDefined.

@Test
public void updateNoOutputFileDefined() {
    String filename = "";
    ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
    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()));
    assertThat(exitCode).isZero();
    verify(keyGeneratorFactory).create(refEq(null), any(EncryptorConfig.class));
    verify(keyGenerator).generate(filename, null, null);
    verify(configFileUpdaterWriter).updateAndWriteToCLI(List.of(keyData), null, config);
    verify(keyDataMarshaller).marshal(configKeyPair);
}
Also used : ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) Test(org.junit.Test)

Example 12 with ConfigKeyPair

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

the class KeyGenCommandTest method onlyMulipleOutputFilesProvided.

@Test
public void onlyMulipleOutputFilesProvided() throws Exception {
    List<String> optionVariations = List.of("--keyout", "-filename");
    List<String> valueVariations = List.of("myfile", "myotherfile", "yetanother");
    ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
    valueVariations.forEach(filename -> {
        when(keyGenerator.generate(filename, null, null)).thenReturn(configKeyPair);
    });
    when(keyGeneratorFactory.create(refEq(null), any(EncryptorConfig.class))).thenReturn(keyGenerator);
    for (String option : optionVariations) {
        String arg = option.concat("=").concat(String.join(",", valueVariations));
        int exitCode = commandLine.execute(arg);
        assertThat(exitCode).isZero();
        CommandLine.ParseResult parseResult = commandLine.getParseResult();
        assertThat(parseResult).isNotNull();
        assertThat(parseResult.matchedArgs()).hasSize(1);
        assertThat(parseResult.hasMatchedOption(option));
        assertThat(parseResult.unmatched()).isEmpty();
        CliResult result = commandLine.getExecutionResult();
        assertThat(result).isNotNull();
        assertThat(result.isSuppressStartup()).isTrue();
        assertThat(result.getConfig()).isNotPresent();
    }
    verify(keyDataMarshaller, times(optionVariations.size() * valueVariations.size())).marshal(configKeyPair);
    verify(keyGeneratorFactory, times(optionVariations.size())).create(refEq(null), any(EncryptorConfig.class));
    valueVariations.forEach(filename -> {
        verify(keyGenerator, times(optionVariations.size())).generate(filename, 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 13 with ConfigKeyPair

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

the class KeyGenCommandTest method vaultUrlProvidedOnCommandLine.

@Test
public void vaultUrlProvidedOnCommandLine() {
    ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
    when(keyGenerator.generate("", null, null)).thenReturn(configKeyPair);
    when(keyGeneratorFactory.create(any(AzureKeyVaultConfig.class), any(EncryptorConfig.class))).thenReturn(keyGenerator);
    int outcome = commandLine.execute("--vault.type=AZURE", "--vault.url=someurl");
    assertThat(outcome).isZero();
    executionExceptionHandler.getExceptions().forEach(Throwable::printStackTrace);
    assertThat(executionExceptionHandler.getExceptions()).isEmpty();
    verify(keyGenerator).generate("", null, null);
    verify(keyGeneratorFactory).create(any(AzureKeyVaultConfig.class), any(EncryptorConfig.class));
    verify(keyDataMarshaller).marshal(configKeyPair);
}
Also used : ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) Test(org.junit.Test)

Example 14 with ConfigKeyPair

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

the class CliKeyPasswordResolver method getSingleKeyPassword.

protected void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor) {
    final boolean isInline = KeyDataUtil.isInline(keyPair);
    final boolean isFilesystem = KeyDataUtil.isFileSystem(keyPair);
    if (!isInline && !isFilesystem) {
        // some other key type that doesn't use passwords, skip
        return;
    }
    final boolean isLocked = KeyDataUtil.isLocked(keyPair);
    if (isLocked) {
        ConfigKeyPair configKeyPair = KeyDataUtil.unmarshal(keyPair, keyEncryptor);
        int currentAttemptNumber = MAX_PASSWORD_ATTEMPTS;
        while (currentAttemptNumber > 0) {
            if (Objects.isNull(configKeyPair.getPassword()) || configKeyPair.getPassword().length == 0 || Optional.ofNullable(configKeyPair.getPrivateKey()).filter(s -> s.contains("NACL_FAILURE")).isPresent()) {
                final String attemptOutput = "Attempt " + (MAX_PASSWORD_ATTEMPTS - currentAttemptNumber + 1) + " of " + MAX_PASSWORD_ATTEMPTS + ".";
                System.out.printf("Password for key[%s] missing or invalid.", keyNumber);
                System.out.println();
                System.out.printf("%s Enter a password for the key", attemptOutput);
                System.out.println();
                final char[] pass = passwordReader.readPasswordFromConsole();
                configKeyPair.withPassword(pass);
                keyPair.setPassword(pass);
            }
            currentAttemptNumber--;
        }
    }
}
Also used : ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair)

Example 15 with ConfigKeyPair

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

the class SendIT method senderAndRecipientOnSameNode.

@Test
public void senderAndRecipientOnSameNode() throws UnsupportedEncodingException {
    // Node C has 2 keys, use them both
    final String[] recipientPublicKeys = ExecutionContext.currentContext().getConfigs().stream().filter(c -> c.getAlias() == NodeAlias.C).findFirst().map(ConfigDescriptor::getAllKeys).get().stream().map(ConfigKeyPair::getPublicKey).toArray(String[]::new);
    final Party party = partyHelper.findByAlias(NodeAlias.C);
    final byte[] transactionData = utils.createTransactionData();
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setFrom(recipientPublicKeys[0]);
    sendRequest.setTo(recipientPublicKeys[1]);
    sendRequest.setPayload(transactionData);
    final Response response = party.getRestClient().target(party.getQ2TUri()).path(SEND_PATH).request().post(Entity.entity(sendRequest, MIME_TYPE_JSON_2_1));
    final SendResponse result = response.readEntity(SendResponse.class);
    assertThat(result.getKey()).isNotNull().isNotBlank();
    assertThat(result.getManagedParties()).containsExactlyInAnyOrder(recipientPublicKeys);
    assertThat(result.getSenderKey()).isEqualTo(recipientPublicKeys[0]);
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(201);
    URI location = response.getLocation();
    {
        final Response checkPersistedTxnResponse = party.getRestClient().target(location).request().accept(MIME_TYPE_JSON_2_1).get();
        assertThat(checkPersistedTxnResponse.getStatus()).isEqualTo(200);
        ReceiveResponse receiveResponse = checkPersistedTxnResponse.readEntity(ReceiveResponse.class);
        assertThat(receiveResponse.getPayload()).isEqualTo(transactionData);
        assertThat(receiveResponse.getManagedParties()).containsExactlyInAnyOrder(recipientPublicKeys);
    }
    {
        String encodedId = URLEncoder.encode(result.getKey(), StandardCharsets.UTF_8.toString());
        Stream.of(party).map(Party::getRestClientWebTarget).map(target -> target.path("transaction")).map(target -> target.path(encodedId)).map(target -> target.request().accept(MIME_TYPE_JSON_2_1).get()).forEach(r -> {
            assertThat(r.getStatus()).isEqualTo(200);
            ReceiveResponse receiveResponse = r.readEntity(ReceiveResponse.class);
            assertThat(receiveResponse.getManagedParties()).containsExactlyInAnyOrder(recipientPublicKeys);
            assertThat(receiveResponse.getSenderKey()).isEqualTo(recipientPublicKeys[0]);
        });
    }
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) ExecutionContext(suite.ExecutionContext) SendResponse(com.quorum.tessera.api.SendResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) PartyHelper(com.quorum.tessera.test.PartyHelper) ServerConfig(com.quorum.tessera.config.ServerConfig) RestUtils(com.quorum.tessera.test.rest.RestUtils) Utils.generateValidButUnknownPublicKey(transaction.utils.Utils.generateValidButUnknownPublicKey) StandardCharsets(java.nio.charset.StandardCharsets) Json(jakarta.json.Json) Entity(jakarta.ws.rs.client.Entity) Response(jakarta.ws.rs.core.Response) URLEncoder(java.net.URLEncoder) SendRequest(com.quorum.tessera.api.SendRequest) Stream(java.util.stream.Stream) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) MIME_TYPE_JSON_2_1(com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1) NodeAlias(suite.NodeAlias) URI(java.net.URI) ConfigDescriptor(config.ConfigDescriptor) Party(com.quorum.tessera.test.Party) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) ConfigDescriptor(config.ConfigDescriptor) URI(java.net.URI) 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