简体   繁体   English

将证书添加到AWS EC2客户端

[英]Add certificate to AWS EC2 client

I'm using the EC2 java classes. 我正在使用EC2 Java类。 To connect to an AWS account successfully, I have to add a certificate (.crt file) to the local Java cacerts file, using the keytool command, which works well when the code runs locally. 要成功连接到AWS账户,我必须使用keytool命令将证书(.crt文件)添加到本地Java cacerts文件中,该命令在代码在本地运行时效果很好。
The problem is I need the code to run on a server, in which I have no access to the cacerts file, so I'm trying to add the certificate programatically, using the ClientConfiguration class. 问题是我需要代码在无法访问cacerts文件的服务器上运行,因此我试图使用ClientConfiguration类以编程方式添加证书。 I uploaded my cacerts file to the server, the program reads it as a KeyStore object, and creates an SSLSocketFactory instance: 我将cacerts文件上传到服务器,程序将其读取为KeyStore对象,并创建一个SSLSocketFactory实例:

SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, "<password>");
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.getApacheHttpClientConfig().setSslSocketFactory(socketFactory);
AWSCredentials credentialsProvider = new BasicAWSCredentials("<key>", "<private key>");
AmazonEC2 ec2 = new AmazonEC2Client(credentialsProvider, clientConfig);

But it still produces the HTTP Status 500 - Unable to execute HTTP request: peer not authenticated error. 但是它仍然会产生HTTP Status 500 - Unable to execute HTTP request: peer not authenticated错误。
I have checked that the added certificate alias can be read successfully, with the keystore's getCertificate method. 我检查了是否可以使用密钥库的getCertificate方法成功读取添加的证书别名。
The "<password>" sent to the SSLSocketFactory constructor is the keystore's password. 发送到SSLSocketFactory构造函数的"<password>"是密钥库的密码。
Is that the correct way to add a certificate to EC2? 这是向EC2添加证书的正确方法吗?

The problem was that the password parameter to the SSLSocketFactory constructor was not needed. 问题是不需要SSLSocketFactory构造函数的password参数。 Supplying the password to the KeyStore constructor is enough, and for SSLSocketFactory it's simply: KeyStore构造函数提供密码就足够了,对于SSLSocketFactory来说,这很简单:

SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM