[英]How to call ssl endpoint fon AWS Lambda function
我有一個由 Alexa 命令觸發的 AWS Lambda function。 在每個 Alexa 命令上,我想調用外部 API 端點來執行所需的操作。
我有一個 jar 調用 API 並具有以下外部服務 class 和 invokeCommand ZC1C425268E68385D1AB507。 jar 已添加為我的 Java 項目下的依賴項。
if(value.equals("something")) {
externalService.invokeCommand();
}
invokeCommand 調用受 SSL 證書保護的外部 API 並拋出錯誤
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
In Elastic Beanstalk I can create a zip folder with Jar, Keystore, and procfile and I can supply keystore as an argument in the procfile which will run the jar with the keystore and that'll allow access to SSL secured endpoint.
if(value.equals("something")) {
externalService.invokeCommand(); // error on AWS Lambda
}
但是,我認為我不能對 Lambda 做同樣的事情,這不允許我調用 SSL 安全端點。
我相信一種選擇是將 package 證書放入您的 Lambda function 本身的 Jar/zip 中,這會將它們放在您的類路徑中。 一旦它們在您的類路徑上,您可以使用編程方式設置它們
System.setProperty("javax.net.ssl.keyStore", KEYSTORE_FILE);
System.setProperty("javax.net.ssl.keyStorePassword", KEYSTORE);
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.trustStore", TRUSTSTORE_FILE);
System.setProperty("javax.net.ssl.trustStorePassword", TRUSTSTORE);
System.setProperty("javax.net.ssl.trustStoreType", "JKS");
你在哪里定義 KEYSTORE_FILE 像
URL trustStoreResource = LdapConfig.class.getResource( "/keystore.jks" );
String KEYSTORE_FILE= trustStoreResource.toURI().getPath();
System.setProperty("javax.net.ssl.keyStore", KEYSTORE_FILE);
您也必須以編程方式創建信任庫 請參閱下面的代碼以供參考
// Declare path of trust store and create file
String trustStorePath = "/tmp/trust";
// try creating above directory and path if you get error no such file
// Create Truststore using Key store api
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
// locate the default truststore
String filename = System.getProperty("java.home")
+ "/lib/security/cacerts".replace('/', File.separatorChar);
try (FileInputStream fis = new FileInputStream(filename)) {
keyStore.load(fis, "changeit".toCharArray());
}
// Add Certificate to Key store
CertificateFactory certF = CertificateFactory.getInstance("X.509");
Certificate cert = certF.generateCertificate(new FileInputStream("your certificate path"));
keyStore.setCertificateEntry("any alias", cert);
// Write Key Store
try (FileOutputStream out = new FileOutputStream(trustStoreFile)) {
keyStore.store(out, "changeit".toCharArray());
}
// Set Certificates to System properties
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
您也可以在 aws lambda 上進行本地測試。 希望這能解決問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.