11
11
import com .nimbusds .oauth2 .sdk .id .ClientID ;
12
12
import com .nimbusds .oauth2 .sdk .id .Issuer ;
13
13
import 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 .*;
18
15
import io .opentdf .platform .wellknownconfiguration .GetWellKnownConfigurationRequest ;
19
16
import io .opentdf .platform .wellknownconfiguration .GetWellKnownConfigurationResponse ;
20
17
import io .opentdf .platform .wellknownconfiguration .WellKnownServiceGrpc ;
18
+ import nl .altindag .ssl .SSLFactory ;
19
+ import nl .altindag .ssl .pem .util .PemUtils ;
21
20
import org .slf4j .Logger ;
22
21
import org .slf4j .LoggerFactory ;
23
22
23
+ import javax .net .ssl .X509ExtendedTrustManager ;
24
+ import java .io .File ;
25
+ import java .io .FileInputStream ;
24
26
import java .io .IOException ;
27
+ import java .io .InputStream ;
28
+ import java .nio .file .Path ;
29
+ import java .util .ArrayList ;
30
+ import java .util .List ;
25
31
import java .util .UUID ;
26
32
27
33
/**
@@ -32,6 +38,7 @@ public class SDKBuilder {
32
38
private String platformEndpoint = null ;
33
39
private ClientAuthentication clientAuth = null ;
34
40
private Boolean usePlainText ;
41
+ private SSLFactory sslFactory ;
35
42
36
43
private static final Logger logger = LoggerFactory .getLogger (SDKBuilder .class );
37
44
@@ -44,6 +51,47 @@ public static SDKBuilder newBuilder() {
44
51
return builder ;
45
52
}
46
53
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
+
47
95
public SDKBuilder platformEndpoint (String platformEndpoint ) {
48
96
this .platformEndpoint = platformEndpoint ;
49
97
return this ;
@@ -104,12 +152,16 @@ private GRPCAuthInterceptor getGrpcAuthInterceptor(RSAKey rsaKey) {
104
152
Issuer issuer = new Issuer (platformIssuer );
105
153
OIDCProviderMetadata providerMetadata ;
106
154
try {
107
- providerMetadata = OIDCProviderMetadata .resolve (issuer );
155
+ providerMetadata = OIDCProviderMetadata .resolve (issuer , httpRequest -> {
156
+ if (sslFactory !=null ) {
157
+ httpRequest .setSSLSocketFactory (sslFactory .getSslSocketFactory ());
158
+ }
159
+ });
108
160
} catch (IOException | GeneralException e ) {
109
161
throw new SDKException ("Error resolving the OIDC provider metadata" , e );
110
162
}
111
163
112
- return new GRPCAuthInterceptor (clientAuth , rsaKey , providerMetadata .getTokenEndpointURI ());
164
+ return new GRPCAuthInterceptor (clientAuth , rsaKey , providerMetadata .getTokenEndpointURI (), sslFactory );
113
165
}
114
166
115
167
SDK .Services buildServices () {
@@ -141,12 +193,21 @@ public SDK build() {
141
193
* @return {@type ManagedChannelBuilder<?>} configured with the SDK options
142
194
*/
143
195
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
+ }
146
203
147
204
if (usePlainText ) {
148
205
channelBuilder = channelBuilder .usePlaintext ();
149
206
}
150
207
return channelBuilder ;
151
208
}
209
+
210
+ SSLFactory getSslFactory (){
211
+ return this .sslFactory ;
212
+ }
152
213
}
0 commit comments