1616import com .fasterxml .jackson .databind .Module ;
1717
1818import javax .net .ssl .SSLContext ;
19+ import javax .net .ssl .TrustManagerFactory ;
20+ import java .io .ByteArrayInputStream ;
1921import java .lang .reflect .InvocationTargetException ;
22+ import java .security .KeyStore ;
23+ import java .security .cert .Certificate ;
24+ import java .security .cert .CertificateFactory ;
2025import java .util .*;
2126import java .util .concurrent .Executor ;
2227import java .util .stream .Collectors ;
@@ -30,6 +35,9 @@ public class ArangoConfig {
3035 private String password ;
3136 private String jwt ;
3237 private Boolean useSsl ;
38+ private Optional <String > sslCertValue ;
39+ private Optional <String > sslAlgorithm ;
40+ private String sslProtocol ;
3341 private SSLContext sslContext ;
3442 private Boolean verifyHost ;
3543 private Integer chunkSize ;
@@ -69,6 +77,9 @@ public void loadProperties(final ArangoConfigProperties properties) {
6977 // FIXME: make jwt field Optional
7078 jwt = properties .getJwt ().orElse (null );
7179 useSsl = properties .getUseSsl ().orElse (ArangoDefaults .DEFAULT_USE_SSL );
80+ sslCertValue = properties .getSslCertValue ();
81+ sslAlgorithm = properties .getSslAlgorithm ();
82+ sslProtocol = properties .getSslProtocol ().orElse (ArangoDefaults .DEFAULT_SSL_PROTOCOL );
7283 verifyHost = properties .getVerifyHost ().orElse (ArangoDefaults .DEFAULT_VERIFY_HOST );
7384 chunkSize = properties .getChunkSize ().orElse (ArangoDefaults .DEFAULT_CHUNK_SIZE );
7485 pipelining = properties .getPipelining ().orElse (ArangoDefaults .DEFAULT_PIPELINING );
@@ -151,7 +162,22 @@ public void setUseSsl(Boolean useSsl) {
151162 this .useSsl = useSsl ;
152163 }
153164
165+ public void setSslCertValue (String sslCertValue ) {
166+ this .sslCertValue = Optional .ofNullable (sslCertValue );
167+ }
168+
169+ public void setSslAlgorithm (String sslAlgorithm ) {
170+ this .sslAlgorithm = Optional .ofNullable (sslAlgorithm );
171+ }
172+
173+ public void setSslProtocol (String sslProtocol ) {
174+ this .sslProtocol = sslProtocol ;
175+ }
176+
154177 public SSLContext getSslContext () {
178+ if (sslContext == null ) {
179+ sslContext = createSslContext ();
180+ }
155181 return sslContext ;
156182 }
157183
@@ -342,4 +368,26 @@ public ProtocolConfig getProtocolConfig() {
342368 public void setProtocolConfig (ProtocolConfig protocolConfig ) {
343369 this .protocolConfig = protocolConfig ;
344370 }
371+
372+ private SSLContext createSslContext () {
373+ try {
374+ if (sslCertValue .isPresent ()) {
375+ ByteArrayInputStream is = new ByteArrayInputStream (Base64 .getDecoder ().decode (sslCertValue .get ()));
376+ Certificate cert = CertificateFactory .getInstance ("X.509" ).generateCertificate (is );
377+ KeyStore ks = KeyStore .getInstance (KeyStore .getDefaultType ());
378+ ks .load (null );
379+ ks .setCertificateEntry ("arangodb" , cert );
380+ TrustManagerFactory tmf = TrustManagerFactory .getInstance (sslAlgorithm .orElseGet (TrustManagerFactory ::getDefaultAlgorithm ));
381+ tmf .init (ks );
382+ SSLContext sc = SSLContext .getInstance (sslProtocol );
383+ sc .init (null , tmf .getTrustManagers (), null );
384+ return sc ;
385+ } else {
386+ return SSLContext .getDefault ();
387+ }
388+ } catch (Exception e ) {
389+ throw new RuntimeException (e );
390+ }
391+ }
392+
345393}
0 commit comments