问题描述
我们在应用程序中使用多个信任库,并且需要一种方法来为 AWS SDK(S3 客户端实现)设置特定信任库以连接到 S3 服务器。 目前,当我们尝试连接到 S3 服务器时,我们遇到了证书链异常。原因是相应的根证书不在默认信任库中。 相反,我们希望为 AWS SDK(S3 客户端实现)明确设置正确的信任库。
我们查看了 AWS SDK 库,但无法弄清楚如何设置自定义信任库而不是通过 jvm 属性。
解决方法
我从来没有做过这样的事情。但是 - 从 here 应该是可能的。所以如果你有自己的 CA,你可以在 AWS 端注册密钥,这样密钥/证书就会被接受。
另一方面,我不能 100% 确定我是否正确解决了您的问题......通常,当我使用 AWS S3 时,我会通过 https 从我的客户端连接到他们的主机。您能否详细说明该 S3 客户端和服务器?也许还有其他解决方案 - 如果是这样 - 我会编辑答案。
,我找到了向 HTTP 请求添加自定义密钥库的解决方案。想法是以编程方式导入所有密钥库并将其添加为 apache 客户端配置的一部分。下面是使用的代码片段。
private static AmazonS3 createClient(Properties prop) throws KeyManagementException,UnrecoverableKeyException,NoSuchAlgorithmException,KeyStoreException,CertificateException,IOException {
AWSCredentials credentials = new BasicAWSCredentials(prop.getProperty("access.key"),prop.getProperty("secret.key"));
ClientConfiguration clientConfiguration = new ClientConfiguration();
if(prop.getProperty("proxy.host")!=null && !prop.getProperty("proxy.host").equalsIgnoreCase(""))
clientConfiguration.setProxyHost(prop.getProperty("proxy.host"));
if(prop.getProperty("proxy.port")!=null && !prop.getProperty("proxy.port").equalsIgnoreCase(""))
clientConfiguration.setProxyPort(Integer.parseInt(prop.getProperty("proxy.port")));
if(prop.getProperty("proxy.username")!=null && !prop.getProperty("proxy.username").equalsIgnoreCase(""))
clientConfiguration.setProxyUsername(prop.getProperty("proxy.username"));
if(prop.getProperty("proxy.password")!=null && !prop.getProperty("proxy.password").equalsIgnoreCase(""))
clientConfiguration.setProxyPassword(prop.getProperty("proxy.password"));
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null,null);
CertificateFactory fact = CertificateFactory.getInstance("X.509");
FileInputStream is = new FileInputStream ("entrust_g2_ca.cer");
X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
is.close();
keyStore.setCertificateEntry("ssl-ca",cer);
TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmFactory.init(keyStore);
TrustManager[] tms = tmFactory.getTrustManagers();
SecureRandom sr = getRandomWithoutSeed();
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null,tms,sr);
SdkTLSSocketFactory sslSocketFactory = new SdkTLSSocketFactory(sslContext,null);
clientConfiguration.getApacheHttpClientConfig().setSslSocketFactory(sslSocketFactory);
AmazonS3ClientBuilder s3Client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new EndpointConfiguration(prop.getProperty("s3.endpoint"),prop.getProperty("s3.region")))
.withClientConfiguration(clientConfiguration);
if(credentials!=null) {
s3Client.withCredentials(new AWSStaticCredentialsProvider(credentials));
}
return s3Client.build();
}