From 25f223a9767a4230fa73a5ca042f4e1f4107b008 Mon Sep 17 00:00:00 2001 From: magnusbechwind Date: Mon, 11 Mar 2024 10:45:06 +0100 Subject: [PATCH] possibility for using TLS --- .../concordium/sdk/examples/BlockStats.java | 20 +++++++++++++----- .../concordium/sdk/examples/FindAccount.java | 19 +++++++++++++---- .../sdk/examples/ValidatorStats.java | 21 +++++++++++++------ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/BlockStats.java b/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/BlockStats.java index 5913135a2..6760754c6 100644 --- a/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/BlockStats.java +++ b/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/BlockStats.java @@ -2,6 +2,7 @@ import com.concordium.sdk.ClientV2; import com.concordium.sdk.Connection; +import com.concordium.sdk.TLSConfig; import com.concordium.sdk.exceptions.ClientInitializationException; import com.concordium.sdk.requests.BlockQuery; import com.concordium.sdk.responses.BlockIdentifier; @@ -13,9 +14,9 @@ import com.concordium.sdk.responses.blocksummary.specialoutcomes.SpecialOutcome; import com.concordium.sdk.types.AbsoluteBlockHeight; import com.google.common.collect.ImmutableList; -import lombok.var; import picocli.CommandLine; +import java.io.File; import java.io.IOException; import java.net.URL; import java.time.ZonedDateTime; @@ -47,6 +48,12 @@ public class BlockStats implements Callable { defaultValue = "http://localhost:20002") private String endpoint; + @CommandLine.Option( + names = {"--tls"}, + description = "Path to the server certificate" + ) + private Optional tls; + @CommandLine.Option( names = {"--timeout"}, description = "GRPC request timeout in milliseconds.", @@ -68,12 +75,15 @@ public class BlockStats implements Callable { @Override public Integer call() throws IOException, ClientInitializationException { URL endpointUrl = new URL(this.endpoint); - Connection connection = Connection.newBuilder() + Connection.ConnectionBuilder connection = Connection.newBuilder() .host(endpointUrl.getHost()) .port(endpointUrl.getPort()) - .timeout(timeout) - .build(); - ClientV2 client = ClientV2.from(connection); + .timeout(timeout); + + if (tls.isPresent()) { + connection = connection.useTLS(TLSConfig.from(new File(tls.get()))); + } + ClientV2 client = ClientV2.from(connection.build()); AbsoluteBlockHeight start; if (fromString.isPresent()) { diff --git a/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/FindAccount.java b/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/FindAccount.java index 79a067ba9..ea0f5ece8 100644 --- a/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/FindAccount.java +++ b/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/FindAccount.java @@ -2,6 +2,7 @@ import com.concordium.sdk.ClientV2; import com.concordium.sdk.Connection; +import com.concordium.sdk.TLSConfig; import com.concordium.sdk.exceptions.ClientInitializationException; import com.concordium.sdk.requests.BlockQuery; import com.concordium.sdk.responses.FindAccountResponse; @@ -13,6 +14,7 @@ import com.concordium.sdk.types.AccountAddress; import picocli.CommandLine; +import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; @@ -32,6 +34,12 @@ public class FindAccount implements Callable { defaultValue = "http://localhost:20002") private String endpoint; + @CommandLine.Option( + names = {"--tls"}, + description = "Path to the server certificate" + ) + private Optional tls; + @CommandLine.Option( names = {"--timeout"}, description = "GRPC request timeout in milliseconds.", @@ -47,12 +55,15 @@ public class FindAccount implements Callable { @Override public Integer call() throws MalformedURLException, ClientInitializationException { URL endpointUrl = new URL(this.endpoint); - Connection connection = Connection.newBuilder() + Connection.ConnectionBuilder connection = Connection.newBuilder() .host(endpointUrl.getHost()) .port(endpointUrl.getPort()) - .timeout(timeout) - .build(); - ClientV2 client = ClientV2.from(connection); + .timeout(timeout); + + if (tls.isPresent()) { + connection = connection.useTLS(TLSConfig.from(new File(tls.get()))); + } + ClientV2 client = ClientV2.from(connection.build()); Optional response = client.findAccountCreation(AccountAddress.from(account)); diff --git a/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/ValidatorStats.java b/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/ValidatorStats.java index f7bc8f0e3..826155b90 100644 --- a/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/ValidatorStats.java +++ b/concordium-sdk-examples/src/main/java/com/concordium/sdk/examples/ValidatorStats.java @@ -2,6 +2,7 @@ import com.concordium.sdk.ClientV2; import com.concordium.sdk.Connection; +import com.concordium.sdk.TLSConfig; import com.concordium.sdk.exceptions.ClientInitializationException; import com.concordium.sdk.requests.BlockQuery; import com.concordium.sdk.responses.BakerId; @@ -9,10 +10,9 @@ import com.concordium.sdk.responses.poolstatus.BakerPoolStatus; import com.concordium.sdk.responses.poolstatus.CurrentPaydayStatus; import com.concordium.sdk.transactions.Hash; -import lombok.val; -import lombok.var; import picocli.CommandLine; +import java.io.File; import java.math.BigInteger; import java.net.MalformedURLException; import java.net.URL; @@ -36,6 +36,12 @@ public class ValidatorStats implements Callable { defaultValue = "http://localhost:20002") private String endpoint; + @CommandLine.Option( + names = {"--tls"}, + description = "Path to the server certificate" + ) + private Optional tls; + @CommandLine.Option( names = {"--timeout"}, description = "GRPC request timeout in milliseconds.", @@ -45,12 +51,15 @@ public class ValidatorStats implements Callable { @Override public Integer call() throws MalformedURLException, ClientInitializationException { URL endpointUrl = new URL(this.endpoint); - Connection connection = Connection.newBuilder() + Connection.ConnectionBuilder connection = Connection.newBuilder() .host(endpointUrl.getHost()) .port(endpointUrl.getPort()) - .timeout(timeout) - .build(); - ClientV2 client = ClientV2.from(connection); + .timeout(timeout); + + if (tls.isPresent()) { + connection = connection.useTLS(TLSConfig.from(new File(tls.get()))); + } + ClientV2 client = ClientV2.from(connection.build()); // BigInteger to avoid overflow BigInteger equity = BigInteger.ZERO;