@@ -72,21 +72,28 @@ public void updatePassword(EntityStore entityStore) {
7272 disableInterface (entityStore , filterName , "InetInterface" );
7373 }
7474 } else if (key .equalsIgnoreCase ("cassandra_disablessl" )) {
75- if (passwordValue .equalsIgnoreCase ("true" )) {
76- disableCassandraSSL (entityStore );
77- }
75+ disableCassandraSSL (entityStore , passwordValue );
7876 } else if (key .startsWith ("cassandraCert" )) {
7977 try {
80- List <X509Certificate > certificates = certHelper .parseX509 (passwordValue );
81- int index = 0 ;
82- for (X509Certificate certificate : certificates ) {
83- String alias = importPublicCertificate (certificate , entityStore );
84- if (alias != null ) {
85- updateCassandraCert (entityStore , alias , index != 0 );
86- index ++;
78+ String pemKey = System .getenv ("cassandra_private_key" );
79+ String publicKey = System .getenv ("cassandra_public_key" );
80+ if ( pemKey != null && publicKey != null ) {
81+ PKCS12 pkcs12 = importCertAndKeyAndCA (entityStore , publicKey , passwordValue , pemKey , null );
82+ Trace .info ("Pem file alias name :" + pkcs12 .getAlias ());
83+ updateCassandraCertAndKey (entityStore , pkcs12 .getAlias (), pkcs12 .getCertificates ());
84+ }else {
85+ List <X509Certificate > certificates = certHelper .parseX509 (passwordValue );
86+
87+ int index = 0 ;
88+ for (X509Certificate certificate : certificates ) {
89+ String alias = importPublicCertificate (certificate , entityStore );
90+ if (alias != null ) {
91+ updateCassandraCert (entityStore , alias , index != 0 );
92+ index ++;
93+ }
8794 }
8895 }
89- } catch (CertificateException | FileNotFoundException e ) {
96+ } catch (Exception e ) {
9097 Trace .error ("Unable to add Cassandra certificate from Environment variable" , e );
9198 }
9299 } else if (key .startsWith ("certandkey_" )) {
@@ -375,22 +382,52 @@ public void updateCassandraPassword(EntityStore entityStore, char[] password) {
375382 entityStore .updateEntity (entity );
376383 }
377384
378- public void updateCassandraCert (EntityStore entityStore , String alias , boolean append ) {
385+
386+
387+ public void updateCassandraCertAndKey (EntityStore entityStore , String clientAuthAlias , Certificate [] certificates ) {
388+ Entity entity = getCassandraEntity (entityStore );
389+ boolean useSSL = entity .getBooleanValue ("useSSL" );
390+ if (useSSL ) {
391+
392+ String clientAuth = "sslCertificate" ;
393+ updateCertEntity (entityStore , entity , clientAuthAlias , clientAuth , false );
394+ String filedName = "sslTrustedCerts" ;
395+
396+ if ( certificates .length > 1 ){
397+ // Start from 1 To ignore public key associated with private key
398+ for (int i = 1 ; i < certificates .length ; i ++) {
399+ Certificate certificate = certificates [i ];
400+ String alias = Util .getAliasName ((X509Certificate ) certificate );
401+ updateCertEntity (entityStore , entity , alias , filedName , true );
402+ }
403+ }
404+ }
405+ }
406+
407+ public Entity getCassandraEntity (EntityStore entityStore ){
379408 String shorthandKey = "/[CassandraSettings]name=Cassandra Settings" ;
380- Entity entity = getEntity (entityStore , shorthandKey );
409+ return getEntity (entityStore , shorthandKey );
410+ }
411+
412+ public void updateCassandraCert (EntityStore entityStore , String alias , boolean append ) {
413+ Entity entity = getCassandraEntity (entityStore );
381414 boolean useSSL = entity .getBooleanValue ("useSSL" );
382415 if (useSSL ) {
383416 String filedName = "sslTrustedCerts" ;
384417 updateCertEntity (entityStore , entity , alias , filedName , append );
385418 }
386419 }
387420
388- public void disableCassandraSSL (EntityStore entityStore ) {
421+ public void disableCassandraSSL (EntityStore entityStore , String value ) {
389422 String shorthandKey = "/[CassandraSettings]name=Cassandra Settings" ;
390423 Entity entity = getEntity (entityStore , shorthandKey );
391- entity .setBooleanField ("useSSL" , false );
424+ boolean boolValue = Boolean .parseBoolean (value );
425+ entity .setBooleanField ("useSSL" , !boolValue );
392426 entityStore .updateEntity (entity );
393- Trace .info ("Disabled Cassandra SSL" );
427+ if (!boolValue )
428+ Trace .info ("Disabled Cassandra SSL" );
429+ else
430+ Trace .info ("Enabled Cassandra SSL" );
394431 }
395432
396433 // Supports both HTTP and HTTPS interfaces where interfaceType are InetInterface, SSLInterface
@@ -415,7 +452,6 @@ private String importPublicCertificate(X509Certificate certificate, EntityStore
415452 String escapedAlias = ShorthandKeyFinder .escapeFieldValue (alias );
416453 Entity certEntity = getCertEntity (entityStore , escapedAlias );
417454 Trace .info ("Alias :" + alias + "Escaped alias :" + escapedAlias );
418-
419455 if (certEntity == null ) {
420456 Trace .info ("Adding cert" );
421457 certEntity = EntityStoreDelegate .createDefaultedEntity (entityStore , "Certificate" );
@@ -498,14 +534,16 @@ private void updateCertEntity(EntityStore entityStore, Entity entity, String ali
498534 String certStoreDistinguishedName = espk .getFieldValueOfReferencedEntity ("dname" );
499535 Trace .info (" alias name from Gateway Cert store :" + certStoreDistinguishedName );
500536 if (certStoreDistinguishedName .equals (alias )) {
501- Trace .info ("Removing existing certs " + alias );
537+ Trace .info ("Removing existing cert as it matches the current cert " + alias );
502538 values .remove (value );
539+ continue ;
503540 }
504- Trace .info ("adding " + alias );
505- values .add (new Value (portableESPK ));
506541 }
542+ Trace .info ("adding " + alias );
543+ values .add (new Value (portableESPK ));
507544 field .setValues (values );
508545 } else {
546+ Trace .debug ("Replacing exising cert reference" );
509547 entity .setReferenceField (fieldName , portableESPK );
510548 }
511549 entityStore .updateEntity (entity );
0 commit comments