22
33import static com .google .common .base .Preconditions .checkNotNull ;
44
5+ import java .io .File ;
56import java .net .InetAddress ;
67import java .util .ArrayList ;
78import java .util .Iterator ;
4748import brooklyn .util .exceptions .Exceptions ;
4849import brooklyn .util .guava .Maybe ;
4950import brooklyn .util .net .Protocol ;
51+ import brooklyn .util .os .Os ;
52+ import brooklyn .util .text .Strings ;
5053import brooklyn .util .time .Duration ;
5154import brooklyn .util .time .Time ;
5255
@@ -477,6 +480,11 @@ protected VcloudClient newVcloudClient() {
477480 }
478481
479482 protected VcloudClient newVcloudClient (String endpoint , String identity , String credential , String trustStore , String trustStorePassword , Level logLevel ) {
483+
484+ if (trustStore == null ) {
485+ trustStore = getDefaultTrustStore ();
486+ }
487+
480488 try {
481489 if (logLevel != null ) {
482490 // Logging is extremely verbose at INFO - it logs in full every http request/response (including payload).
@@ -491,16 +499,18 @@ protected VcloudClient newVcloudClient(String endpoint, String identity, String
491499 try {
492500 vcloudClient = new VcloudClient (endpoint , version );
493501 LOG .debug ("VCloudClient - trying login to {} using {}" , endpoint , version );
494- vcloudClient .login (identity , credential );
495502
496503 // Performing Certificate Validation
497- if (trustStore != null && trustStorePassword != null ) {
504+ if (Strings .isNonBlank (trustStorePassword )) {
505+ LOG .debug ("Registering HTTPS scheme using trustStore ='{}' with trustStorePassword = '{}'" , trustStore , trustStorePassword );
498506 vcloudClient .registerScheme ("https" , 443 , CustomSSLSocketFactory .getInstance (trustStore , trustStorePassword ));
499507 } else {
500- LOG .warn ("Ignoring the Certificate Validation using FakeSSLSocketFactory" );
501- vcloudClient .registerScheme ("https" , 443 , FakeSSLSocketFactory .getInstance ());
508+ LOG .warn ("Registering HTTPS scheme using FakeSSLSocketFactory, as trustStore ='{}' with trustorePassword = '{}' are not valid." ,
509+ trustStore , Strings .isBlank (trustStorePassword ) ? "empty" : trustStorePassword );
510+ vcloudClient .registerScheme ("https" , 443 , FakeSSLSocketFactory .getInstance ());
502511 }
503512
513+ vcloudClient .login (identity , credential );
504514 versionFound = true ;
505515 LOG .info ("VCloudClient - Logged into {} using version {}" , endpoint , version );
506516 break ;
@@ -517,6 +527,23 @@ protected VcloudClient newVcloudClient(String endpoint, String identity, String
517527 }
518528 }
519529
530+ /**
531+ * http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#InstallationAndCustomization
532+ *
533+ * @return the default truststore, jssecacerts, if it exists. Otherwise, cacerts
534+ */
535+ private String getDefaultTrustStore () {
536+ String trustStore ;
537+ String trustStoreFolder = Os .mergePaths (System .getProperty ("java.home" ), "lib" , "security" );
538+ trustStore = Os .mergePaths (trustStoreFolder , "jssecacerts" );
539+ if (!new File (trustStore ).exists ()) {
540+ trustStore = Os .mergePaths (trustStoreFolder , "cacerts" );
541+ } else {
542+ throw new IllegalStateException ("Cannot find a valid default truststore (jssecacerts or cacerts) in " + trustStoreFolder );
543+ }
544+ return trustStore ;
545+ }
546+
520547 private GatewayNatRuleType generateGatewayNatRule (Protocol protocol , HostAndPort original ,
521548 HostAndPort translated , ReferenceType interfaceRef ) {
522549 GatewayNatRuleType gatewayNatRule = new GatewayNatRuleType ();
0 commit comments