Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to disable TLS #356

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ public class ClientConfiguration {
private final String endpoints;
private final SessionCredentialsProvider sessionCredentialsProvider;
private final Duration requestTimeout;
private final boolean sslEnabled;

/**
* The caller is supposed to have validated the arguments and handled throwing exceptions or
* logging warnings already, so we avoid repeating args check here.
*/
ClientConfiguration(String endpoints, SessionCredentialsProvider sessionCredentialsProvider,
Duration requestTimeout) {
Duration requestTimeout, boolean sslEnabled) {
this.endpoints = endpoints;
this.sessionCredentialsProvider = sessionCredentialsProvider;
this.requestTimeout = requestTimeout;
this.sslEnabled = sslEnabled;
}

public static ClientConfigurationBuilder newBuilder() {
Expand All @@ -54,4 +56,8 @@ public Optional<SessionCredentialsProvider> getCredentialsProvider() {
public Duration getRequestTimeout() {
return requestTimeout;
}

public boolean isSslEnabled() {
return sslEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ClientConfigurationBuilder {
private String endpoints;
private SessionCredentialsProvider sessionCredentialsProvider = null;
private Duration requestTimeout = Duration.ofSeconds(3);
private boolean sslEnabled = true;

/**
* Configure the access point with which the SDK should communicate.
Expand Down Expand Up @@ -70,6 +71,17 @@ public ClientConfigurationBuilder setRequestTimeout(Duration requestTimeout) {
return this;
}

/**
* Enable or disable the use of Secure Sockets Layer (SSL) for network transport.
*
* @param sslEnabled A boolean value indicating whether SSL should be enabled or not.
* @return The {@link ClientConfigurationBuilder} instance, to allow for method chaining.
*/
public ClientConfigurationBuilder enableSsl(boolean sslEnabled) {
this.sslEnabled = sslEnabled;
return this;
}

/**
* Finalize the build of {@link ClientConfiguration}.
*
Expand All @@ -78,6 +90,6 @@ public ClientConfigurationBuilder setRequestTimeout(Duration requestTimeout) {
public ClientConfiguration build() {
checkNotNull(endpoints, "endpoints should not be null");
checkNotNull(requestTimeout, "requestTimeout should not be null");
return new ClientConfiguration(endpoints, sessionCredentialsProvider, requestTimeout);
return new ClientConfiguration(endpoints, sessionCredentialsProvider, requestTimeout, sslEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,56 @@

public interface Client {
/**
* @return endpoints.
* Retrieve Endpoints Information
*
* @return the endpoints associated with this client.
*/
Endpoints getEndpoints();

/**
* Get the unique client identifier for each client.
* Get Unique Client Identifier
*
* <p>Get the unique client identifier for each client.
*
* @return unique client identifier.
* @return a unique client identifier.
*/
ClientId getClientId();

/**
* @return signature for tls
* Get TLS Signature
*
* @return the signature for TLS (Transport Layer Security).
* @throws Exception if an error occurs during the signature generation process.
*/
Metadata sign() throws Exception;

/**
* Send heart beat to remote {@link Endpoints}.
* Check SSL Status
*
* <p>Check if SSL (Secure Sockets Layer) is enabled.
*
* @return a boolean value indicating whether SSL is enabled or not.
*/
boolean isSslEnabled();

/**
* Send Heartbeat
*
* <p> Send a heartbeat to the remote endpoint.
*/
void doHeartbeat();

/**
* Sync settings to remote.
* Sync Settings
*
* <p>Synchronize client settings with the remote endpoint.
*/
void syncSettings();

/**
* Do some stats for client.
* Do Statistics
*
* <p>Perform some statistics for the client.
*/
void doStats();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ public Metadata sign() throws NoSuchAlgorithmException, InvalidKeyException {
return Signature.sign(clientConfiguration, clientId);
}

@Override
public boolean isSslEnabled() {
return clientConfiguration.isSslEnabled();
}

/**
* Send heartbeat data to the appointed endpoint
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ private void clearIdleRpcClients() throws InterruptedException {
}

/**
* Return the RPC client by remote {@link Endpoints}, would create the client automatically if it does not exist.
*
* <p>In case of the occasion that {@link RpcClient} is garbage collected before shutdown when invoked
* concurrently, lock here is essential.
* Obtain the RPC client by remote {@link Endpoints}, if it does not already exist, it will be created
* automatically.
*
* @param endpoints remote endpoints.
* @return RPC client.
Expand All @@ -181,7 +179,7 @@ private RpcClient getRpcClient(Endpoints endpoints) throws ClientException {
return rpcClient;
}
try {
rpcClient = new RpcClientImpl(endpoints);
rpcClient = new RpcClientImpl(endpoints, client.isSslEnabled());
} catch (SSLException e) {
log.error("Failed to get RPC client, endpoints={}, clientId={}", endpoints, client.getClientId(), e);
throw new ClientException("Failed to generate RPC client", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
import io.grpc.netty.shaded.io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
Expand Down Expand Up @@ -91,10 +92,18 @@ public synchronized void reset(Metric metric) {
return;
}
final Endpoints endpoints = metric.getEndpoints();
final SslContext sslContext = GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
final NettyChannelBuilder channelBuilder = NettyChannelBuilder.forTarget(endpoints.getGrpcTarget())
.sslContext(sslContext).intercept(new AuthInterceptor(clientConfiguration, clientId));
.intercept(new AuthInterceptor(clientConfiguration, clientId));

if (clientConfiguration.isSslEnabled()) {
final SslContextBuilder builder = GrpcSslContexts.forClient();
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
SslContext sslContext = builder.build();
channelBuilder.sslContext(sslContext);
} else {
channelBuilder.usePlaintext();
}

final List<InetSocketAddress> socketAddresses = endpoints.toSocketAddresses();
if (null != socketAddresses) {
IpNameResolverFactory metricResolverFactory = new IpNameResolverFactory(socketAddresses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,22 @@ public class RpcClientImpl implements RpcClient {
private long activityNanoTime;

@SuppressWarnings("deprecation")
public RpcClientImpl(Endpoints endpoints) throws SSLException {
final SslContextBuilder builder = GrpcSslContexts.forClient();
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
SslContext sslContext = builder.build();

public RpcClientImpl(Endpoints endpoints, boolean sslEnabled) throws SSLException {
final NettyChannelBuilder channelBuilder =
NettyChannelBuilder.forTarget(endpoints.getGrpcTarget())
.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT_MILLIS)
.maxInboundMessageSize(GRPC_MAX_MESSAGE_SIZE)
.intercept(LoggingInterceptor.getInstance())
.sslContext(sslContext);
.intercept(LoggingInterceptor.getInstance());

if (sslEnabled) {
final SslContextBuilder builder = GrpcSslContexts.forClient();
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
SslContext sslContext = builder.build();
channelBuilder.sslContext(sslContext);
} else {
channelBuilder.usePlaintext();
}

// Disable grpc's auto-retry here.
channelBuilder.disableRetry();
final List<InetSocketAddress> socketAddresses = endpoints.toSocketAddresses();
Expand Down