Skip to content

Commit

Permalink
enhancement: Switch to gRPC channel builder to support Unix domain so…
Browse files Browse the repository at this point in the history
…ckets (#98)

Support for creating UDS connections has been added in the upstream
project if `Grpc.newChannelBuilder` is used instead of
`ManagedChannelBuilder`.

Signed-off-by: Charith Ellawala <[email protected]>
  • Loading branch information
charithe committed Jun 26, 2024
1 parent 9b93c93 commit 197ed6b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/main/java/dev/cerbos/sdk/CerbosClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

package dev.cerbos.sdk;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
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.SslContextBuilder;
import io.grpc.*;
import io.grpc.netty.shaded.io.netty.handler.ssl.util.InsecureTrustManagerFactory;

import javax.net.ssl.SSLException;
import java.io.InputStream;
import java.time.Duration;

Expand Down Expand Up @@ -78,35 +73,30 @@ private ManagedChannel buildChannel() throws InvalidClientConfigurationException

ManagedChannelBuilder<?> channelBuilder = null;
if (plaintext) {
channelBuilder = ManagedChannelBuilder.forTarget(target).usePlaintext();
channelBuilder = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create());
} else {
SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
TlsChannelCredentials.Builder tlsCredentials = TlsChannelCredentials.newBuilder();
if (insecure) {
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
tlsCredentials.trustManager(InsecureTrustManagerFactory.INSTANCE.getTrustManagers());
}

if (caCertificate != null) {
try {
sslContextBuilder.trustManager(caCertificate);
tlsCredentials.trustManager(caCertificate);
} catch (Exception e) {
throw new InvalidClientConfigurationException("Failed to set CA trust root", e);
}
}

if (tlsCertificate != null && tlsKey != null) {
try {
sslContextBuilder.keyManager(tlsCertificate, tlsKey);
tlsCredentials.keyManager(tlsCertificate, tlsKey);
} catch (Exception e) {
throw new InvalidClientConfigurationException("Failed to set TLS credentials", e);
}
}

try {
channelBuilder =
NettyChannelBuilder.forTarget(target).sslContext(sslContextBuilder.build());
} catch (SSLException e) {
throw new InvalidClientConfigurationException("Failed to build SSL context", e);
}
channelBuilder = Grpc.newChannelBuilder(target, tlsCredentials.build());
}

if (!isEmptyString(authority)) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/dev/cerbos/sdk/CerbosBlockingClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CerbosBlockingClientTest extends CerbosClientTests {
private static final Logger LOG = LoggerFactory.getLogger(CerbosBlockingClientTest.class);

@Container
private static final CerbosContainer cerbosContainer =
new CerbosContainer("dev")
Expand All @@ -28,6 +29,7 @@ class CerbosBlockingClientTest extends CerbosClientTests {
.withCommand("server", "--config=/config/config.yaml")
.withLogConsumer(new Slf4jLogConsumer(LOG));


@BeforeAll
public void initClient() throws CerbosClientBuilder.InvalidClientConfigurationException {
String target = cerbosContainer.getTarget();
Expand Down

0 comments on commit 197ed6b

Please sign in to comment.