1111import com .nimbusds .oauth2 .sdk .id .ClientID ;
1212import com .nimbusds .oauth2 .sdk .id .Issuer ;
1313import com .nimbusds .openid .connect .sdk .op .OIDCProviderMetadata ;
14- import io .grpc .ManagedChannel ;
15- import io .grpc .ManagedChannelBuilder ;
16- import io .grpc .Status ;
17- import io .grpc .StatusRuntimeException ;
14+ import io .grpc .*;
1815import io .opentdf .platform .wellknownconfiguration .GetWellKnownConfigurationRequest ;
1916import io .opentdf .platform .wellknownconfiguration .GetWellKnownConfigurationResponse ;
2017import io .opentdf .platform .wellknownconfiguration .WellKnownServiceGrpc ;
18+ import nl .altindag .ssl .SSLFactory ;
19+ import nl .altindag .ssl .pem .util .PemUtils ;
2120import org .slf4j .Logger ;
2221import org .slf4j .LoggerFactory ;
2322
23+ import javax .net .ssl .X509ExtendedTrustManager ;
24+ import java .io .File ;
25+ import java .io .FileInputStream ;
2426import java .io .IOException ;
27+ import java .io .InputStream ;
28+ import java .nio .file .Path ;
29+ import java .util .ArrayList ;
30+ import java .util .List ;
2531import java .util .UUID ;
2632
2733/**
@@ -32,6 +38,7 @@ public class SDKBuilder {
3238 private String platformEndpoint = null ;
3339 private ClientAuthentication clientAuth = null ;
3440 private Boolean usePlainText ;
41+ private SSLFactory sslFactory ;
3542
3643 private static final Logger logger = LoggerFactory .getLogger (SDKBuilder .class );
3744
@@ -44,6 +51,47 @@ public static SDKBuilder newBuilder() {
4451 return builder ;
4552 }
4653
54+ public SDKBuilder sslFactory (SSLFactory sslFactory ) {
55+ this .sslFactory = sslFactory ;
56+ return this ;
57+ }
58+
59+ /**
60+ * Add SSL Context with trusted certs from certDirPath
61+ * @param certsDirPath Path to a directory containing .pem or .crt trusted certs
62+ * @return
63+ */
64+ public SDKBuilder sslFactoryFromDirectory (String certsDirPath ) throws Exception {
65+ File certsDir = new File (certsDirPath );
66+ File [] certFiles =
67+ certsDir .listFiles ((dir , name ) -> name .endsWith (".pem" ) || name .endsWith (".crt" ));
68+ logger .info ("Loading certificates from: " + certsDir .getAbsolutePath ());
69+ List <InputStream > certStreams = new ArrayList <>();
70+ for (File certFile : certFiles ) {
71+ certStreams .add (new FileInputStream (certFile ));
72+ }
73+ X509ExtendedTrustManager trustManager =
74+ PemUtils .loadTrustMaterial (certStreams .toArray (new InputStream [0 ]));
75+ this .sslFactory =
76+ SSLFactory .builder ().withDefaultTrustMaterial ().withSystemTrustMaterial ()
77+ .withTrustMaterial (trustManager ).build ();
78+ return this ;
79+ }
80+
81+ /**
82+ * Add SSL Context with default system trust material + certs contained in a Java keystore
83+ * @param keystorePath Path to keystore
84+ * @param keystorePassword Password to keystore
85+ * @return
86+ */
87+ public SDKBuilder sslFactoryFromKeyStore (String keystorePath , String keystorePassword ) {
88+ this .sslFactory =
89+ SSLFactory .builder ().withDefaultTrustMaterial ().withSystemTrustMaterial ()
90+ .withTrustMaterial (Path .of (keystorePath ), keystorePassword ==null ?
91+ "" .toCharArray () : keystorePassword .toCharArray ()).build ();
92+ return this ;
93+ }
94+
4795 public SDKBuilder platformEndpoint (String platformEndpoint ) {
4896 this .platformEndpoint = platformEndpoint ;
4997 return this ;
@@ -104,12 +152,16 @@ private GRPCAuthInterceptor getGrpcAuthInterceptor(RSAKey rsaKey) {
104152 Issuer issuer = new Issuer (platformIssuer );
105153 OIDCProviderMetadata providerMetadata ;
106154 try {
107- providerMetadata = OIDCProviderMetadata .resolve (issuer );
155+ providerMetadata = OIDCProviderMetadata .resolve (issuer , httpRequest -> {
156+ if (sslFactory !=null ) {
157+ httpRequest .setSSLSocketFactory (sslFactory .getSslSocketFactory ());
158+ }
159+ });
108160 } catch (IOException | GeneralException e ) {
109161 throw new SDKException ("Error resolving the OIDC provider metadata" , e );
110162 }
111163
112- return new GRPCAuthInterceptor (clientAuth , rsaKey , providerMetadata .getTokenEndpointURI ());
164+ return new GRPCAuthInterceptor (clientAuth , rsaKey , providerMetadata .getTokenEndpointURI (), sslFactory );
113165 }
114166
115167 SDK .Services buildServices () {
@@ -141,12 +193,21 @@ public SDK build() {
141193 * @return {@type ManagedChannelBuilder<?>} configured with the SDK options
142194 */
143195 private ManagedChannelBuilder <?> getManagedChannelBuilder (String endpoint ) {
144- ManagedChannelBuilder <?> channelBuilder = ManagedChannelBuilder
145- .forTarget (endpoint );
196+ ManagedChannelBuilder <?> channelBuilder ;
197+ if (sslFactory != null ) {
198+ channelBuilder = Grpc .newChannelBuilder (endpoint , TlsChannelCredentials .newBuilder ()
199+ .trustManager (sslFactory .getTrustManager ().get ()).build ());
200+ }else {
201+ channelBuilder = ManagedChannelBuilder .forTarget (endpoint );
202+ }
146203
147204 if (usePlainText ) {
148205 channelBuilder = channelBuilder .usePlaintext ();
149206 }
150207 return channelBuilder ;
151208 }
209+
210+ SSLFactory getSslFactory (){
211+ return this .sslFactory ;
212+ }
152213}
0 commit comments