Search in sources :

Example 1 with StreamConsumer

use of exec.StreamConsumer in project tessera by ConsenSys.

the class AwsKeyVaultIT method keyGenerationRequestCreateSecretCallToAws.

@Test
public void keyGenerationRequestCreateSecretCallToAws() throws Exception {
    List<String> nodesToGenerateKeysFor = List.of("nodeA", "nodeB", "nodeC");
    final List<String> args = new ExecArgsBuilder().withStartScript(startScript).withClassPathItem(distDirectory).withArg("-keygen").withArg("-keygenvaulttype", "AWS").withArg("-filename", String.join(",", nodesToGenerateKeysFor)).withArg("-keygenvaulturl", keyVaultUrl).build();
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    processBuilder.environment().putAll(env());
    processBuilder.redirectErrorStream(false);
    Process process = processBuilder.start();
    executorService.submit(new StreamConsumer(process.getInputStream(), LOGGER::info));
    executorService.submit(new StreamConsumer(process.getErrorStream(), LOGGER::error));
    process.waitFor();
    assertThat(process.exitValue()).isZero();
    final String apiTarget = "secretsmanager.CreateSecret";
    assertThat(httpHandler.getRequests()).containsOnlyKeys(apiTarget);
    assertThat(httpHandler.getRequests().get(apiTarget)).hasSize(6);
    List<JsonObject> requests = httpHandler.getRequests().get(apiTarget);
    List<String> expectedNames = nodesToGenerateKeysFor.stream().flatMap(n -> Stream.of(n.concat("Pub"), n.concat("Key"))).collect(Collectors.toList());
    assertThat(requests.stream().map(j -> j.getString("Name")).collect(Collectors.toList())).containsExactlyInAnyOrderElementsOf(expectedNames);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(com.quorum.tessera.ssl.context.SSLContextBuilder) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) ServerConfig(com.quorum.tessera.config.ServerConfig) HttpRequest(java.net.http.HttpRequest) JaxbUtil(com.quorum.tessera.config.util.JaxbUtil) Map(java.util.Map) After(org.junit.After) JsonObject(jakarta.json.JsonObject) HttpClient(java.net.http.HttpClient) URI(java.net.URI) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) HttpResponse(java.net.http.HttpResponse) Before(org.junit.Before) ElUtil(com.quorum.tessera.test.util.ElUtil) Logger(org.slf4j.Logger) HttpsParameters(com.sun.net.httpserver.HttpsParameters) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) PortUtil(config.PortUtil) UUID(java.util.UUID) ExecArgsBuilder(exec.ExecArgsBuilder) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ExecUtils(exec.ExecUtils) Executors(java.util.concurrent.Executors) Json(jakarta.json.Json) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) UriBuilder(jakarta.ws.rs.core.UriBuilder) Paths(java.nio.file.Paths) Optional(java.util.Optional) Config(com.quorum.tessera.config.Config) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) HttpsServer(com.sun.net.httpserver.HttpsServer) StreamConsumer(exec.StreamConsumer) StreamConsumer(exec.StreamConsumer) JsonObject(jakarta.json.JsonObject) ExecArgsBuilder(exec.ExecArgsBuilder) Test(org.junit.Test)

Example 2 with StreamConsumer

use of exec.StreamConsumer in project tessera by ConsenSys.

the class AzureKeyVaultIT method doStuff.

@Test
public void doStuff() throws Exception {
    Map<String, Object> params = Map.of("azureKeyVaultUrl", keyVaultUrl);
    Path tempTesseraConfig = ElUtil.createTempFileFromTemplate(getClass().getResource("/vault/tessera-azure-config.json"), params);
    List<String> args = new ExecArgsBuilder().withStartScript(startScript).withClassPathItem(distDirectory).withArg("-configfile", tempTesseraConfig.toString()).withArg("-pidfile", pid.toAbsolutePath().toString()).withArg("-jdbc.autoCreateTables", "true").build();
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    processBuilder.environment().putAll(env());
    processBuilder.redirectErrorStream(true);
    Process process = processBuilder.start();
    executorService.submit(new StreamConsumer(process.getInputStream()));
    executorService.submit(() -> {
        int exitCode = process.waitFor();
        assertThat(exitCode).describedAs("Tessera node exited with code %d", exitCode).isEqualTo(0);
        return null;
    });
    final Config config = JaxbUtil.unmarshal(Files.newInputStream(tempTesseraConfig), Config.class);
    final URI bindingUrl = UriBuilder.fromUri(config.getP2PServerConfig().getBindingUri()).path("upcheck").build();
    HttpClient httpClient = HttpClient.newHttpClient();
    final HttpRequest request = HttpRequest.newBuilder().uri(bindingUrl).GET().build();
    CountDownLatch startUpLatch = new CountDownLatch(1);
    executorService.submit(() -> {
        while (true) {
            try {
                HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
                if (response.statusCode() == 200) {
                    startUpLatch.countDown();
                }
            } catch (InterruptedException | IOException e) {
            }
        }
    });
    assertThat(startUpLatch.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(httpHandler.getCounter()).isEqualTo(2);
}
Also used : Path(java.nio.file.Path) HttpRequest(java.net.http.HttpRequest) StreamConsumer(exec.StreamConsumer) Config(com.quorum.tessera.config.Config) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) HttpClient(java.net.http.HttpClient) ExecArgsBuilder(exec.ExecArgsBuilder) Test(org.junit.Test)

Example 3 with StreamConsumer

use of exec.StreamConsumer in project tessera by ConsenSys.

the class AwsKeyVaultIT method tesseraStartupRequestsKeysWhosIdsAreConfigured.

@Test
public void tesseraStartupRequestsKeysWhosIdsAreConfigured() throws Exception {
    Map<String, Object> params = Map.of("awsSecretsManagerEndpoint", keyVaultUrl);
    Path tempTesseraConfig = ElUtil.createTempFileFromTemplate(getClass().getResource("/vault/tessera-aws-config.json"), params);
    List<String> args = new ExecArgsBuilder().withStartScript(startScript).withClassPathItem(distDirectory).withArg("-configfile", tempTesseraConfig.toString()).withArg("-pidfile", pid.toAbsolutePath().toString()).withArg("-jdbc.autoCreateTables", "true").build();
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    processBuilder.environment().putAll(env());
    processBuilder.redirectErrorStream(true);
    Process process = processBuilder.start();
    executorService.submit(new StreamConsumer(process.getInputStream(), LOGGER::info));
    executorService.submit(() -> {
        int exitCode = process.waitFor();
        assertThat(exitCode).describedAs("Tessera node exited with code %d", exitCode).isEqualTo(0);
        return null;
    });
    final Config config = JaxbUtil.unmarshal(Files.newInputStream(tempTesseraConfig), Config.class);
    final URI bindingUrl = Optional.of(config).map(Config::getP2PServerConfig).map(ServerConfig::getBindingUri).map(UriBuilder::fromUri).map(u -> u.path("upcheck")).map(UriBuilder::build).get();
    HttpClient httpClient = HttpClient.newHttpClient();
    final HttpRequest request = HttpRequest.newBuilder().uri(bindingUrl).GET().build();
    CountDownLatch startUpLatch = new CountDownLatch(1);
    executorService.submit(() -> {
        while (true) {
            try {
                HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
                if (response.statusCode() == 200) {
                    startUpLatch.countDown();
                }
            } catch (InterruptedException | IOException e) {
            }
        }
    });
    assertThat(startUpLatch.await(2, TimeUnit.MINUTES)).isTrue();
    assertThat(httpHandler.getCounter()).isEqualTo(2);
    List<JsonObject> requests = httpHandler.getRequests().get("secretsmanager.GetSecretValue");
    assertThat(requests).hasSize(2);
    List<String> secretIds = requests.stream().map(j -> j.getString("SecretId")).collect(Collectors.toList());
    assertThat(secretIds).containsExactlyInAnyOrder("secretIdPub", "secretIdKey");
}
Also used : Path(java.nio.file.Path) HttpRequest(java.net.http.HttpRequest) HttpURLConnection(java.net.HttpURLConnection) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(com.quorum.tessera.ssl.context.SSLContextBuilder) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) ServerConfig(com.quorum.tessera.config.ServerConfig) HttpRequest(java.net.http.HttpRequest) JaxbUtil(com.quorum.tessera.config.util.JaxbUtil) Map(java.util.Map) After(org.junit.After) JsonObject(jakarta.json.JsonObject) HttpClient(java.net.http.HttpClient) URI(java.net.URI) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) HttpResponse(java.net.http.HttpResponse) Before(org.junit.Before) ElUtil(com.quorum.tessera.test.util.ElUtil) Logger(org.slf4j.Logger) HttpsParameters(com.sun.net.httpserver.HttpsParameters) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) PortUtil(config.PortUtil) UUID(java.util.UUID) ExecArgsBuilder(exec.ExecArgsBuilder) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ExecUtils(exec.ExecUtils) Executors(java.util.concurrent.Executors) Json(jakarta.json.Json) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) UriBuilder(jakarta.ws.rs.core.UriBuilder) Paths(java.nio.file.Paths) Optional(java.util.Optional) Config(com.quorum.tessera.config.Config) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) HttpsServer(com.sun.net.httpserver.HttpsServer) StreamConsumer(exec.StreamConsumer) StreamConsumer(exec.StreamConsumer) ServerConfig(com.quorum.tessera.config.ServerConfig) Config(com.quorum.tessera.config.Config) JsonObject(jakarta.json.JsonObject) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ServerConfig(com.quorum.tessera.config.ServerConfig) HttpClient(java.net.http.HttpClient) JsonObject(jakarta.json.JsonObject) ExecArgsBuilder(exec.ExecArgsBuilder) Test(org.junit.Test)

Aggregations

Config (com.quorum.tessera.config.Config)3 ExecArgsBuilder (exec.ExecArgsBuilder)3 StreamConsumer (exec.StreamConsumer)3 IOException (java.io.IOException)3 URI (java.net.URI)3 HttpClient (java.net.http.HttpClient)3 HttpRequest (java.net.http.HttpRequest)3 Path (java.nio.file.Path)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ServerConfig (com.quorum.tessera.config.ServerConfig)2 JaxbUtil (com.quorum.tessera.config.util.JaxbUtil)2 SSLContextBuilder (com.quorum.tessera.ssl.context.SSLContextBuilder)2 ElUtil (com.quorum.tessera.test.util.ElUtil)2 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)2 HttpsParameters (com.sun.net.httpserver.HttpsParameters)2 HttpsServer (com.sun.net.httpserver.HttpsServer)2 PortUtil (config.PortUtil)2 ExecUtils (exec.ExecUtils)2 Json (jakarta.json.Json)2 JsonObject (jakarta.json.JsonObject)2