Skip to content

Commit 160c1e3

Browse files
committed
add support for default truststore
1 parent 74b882c commit 160c1e3

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

vcloud-director/src/main/java/brooklyn/networking/vclouddirector/CustomSSLSocketFactory.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
/*
2-
* *******************************************************
3-
* Copyright VMware, Inc. 2010-2013. All Rights Reserved.
4-
* *******************************************************
5-
*
6-
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
7-
* WARRANTIES OR CONDITIONS # OF ANY KIND, WHETHER ORAL OR WRITTEN,
8-
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY # DISCLAIMS ANY IMPLIED
9-
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY # QUALITY,
10-
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
11-
*/
121
package brooklyn.networking.vclouddirector;
132

143
import java.io.FileInputStream;

vcloud-director/src/main/java/brooklyn/networking/vclouddirector/FakeSSLSocketFactory.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
/*
2-
* *******************************************************
3-
* Copyright VMware, Inc. 2010-2013. All Rights Reserved.
4-
* *******************************************************
5-
*
6-
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
7-
* WARRANTIES OR CONDITIONS # OF ANY KIND, WHETHER ORAL OR WRITTEN,
8-
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY # DISCLAIMS ANY IMPLIED
9-
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY # QUALITY,
10-
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
11-
*/
121
package brooklyn.networking.vclouddirector;
132

143
import java.security.KeyManagementException;

vcloud-director/src/main/java/brooklyn/networking/vclouddirector/NatService.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.google.common.base.Preconditions.checkNotNull;
44

5+
import java.io.File;
56
import java.net.InetAddress;
67
import java.util.ArrayList;
78
import java.util.Iterator;
@@ -47,6 +48,8 @@
4748
import brooklyn.util.exceptions.Exceptions;
4849
import brooklyn.util.guava.Maybe;
4950
import brooklyn.util.net.Protocol;
51+
import brooklyn.util.os.Os;
52+
import brooklyn.util.text.Strings;
5053
import brooklyn.util.time.Duration;
5154
import 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();

vcloud-director/src/test/java/brooklyn/networking/vclouddirector/SecureNatServiceLiveTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
* brooklyn.location.named.canopy-vCHS.credential=pa55w0rd
3030
* brooklyn.location.named.canopy-vCHS.advancednetworking.vcloud.network.id=041e176a-befc-4b28-89e2-3c5343ff4d12
3131
* brooklyn.location.named.canopy-vCHS.advancednetworking.vcloud.network.publicip=23.92.230.21
32-
* brooklyn.location.named.canopy-vCHS.trustStore=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/security/cacerts
3332
* brooklyn.location.named.canopy-vCHS.trustStorePassword=changeit
3433
*
3534
* brooklyn.location.named.canopy-TAI=jclouds:vcloud-director:https://svdc.it-solutions.atos.net/api
@@ -98,8 +97,9 @@ private NatService.Builder newServiceBuilder(JcloudsLocation loc) {
9897
throw Exceptions.propagate(e);
9998
}
10099

101-
String trustStore = (String) loc.getAllConfigBag().getStringKey("trustStore");
100+
String trustStore = (String) loc.getAllConfigBag().getStringKey("trustStore"); // if null, it will use default trustore
102101
String trustStorePassword = (String) loc.getAllConfigBag().getStringKey("trustStorePassword");
102+
assertNotNull(trustStorePassword, "trustStorePassword not set on location " + loc);
103103

104104
return NatService.builder()
105105
.identity(loc.getIdentity())

0 commit comments

Comments
 (0)