简体   繁体   English

如何在 AWS Lambda function 上调用 ssl 端点

[英]How to call ssl endpoint fon AWS Lambda function

I have an AWS Lambda function that gets triggered by the Alexa command.我有一个由 Alexa 命令触发的 AWS Lambda function。 On every Alexa command, I want to call an external API endpoint to do the desired operation.在每个 Alexa 命令上,我想调用外部 API 端点来执行所需的操作。

I have a jar that calls the API and has the below externalService class with invokeCommand function.我有一个 jar 调用 API 并具有以下外部服务 class 和 invokeCommand ZC1C425268E68385D1AB507。 The jar has been added as a dependency under my Java project. jar 已添加为我的 Java 项目下的依赖项。

if(value.equals("something")) {
   externalService.invokeCommand();
   }

invokeCommand calls the external API which is protected by SSL certificate and throws an error 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. 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
   }

However, I don't think I can do the same with Lambda which is not allowing me to call SSL secured endpoint.但是,我认为我不能对 Lambda 做同样的事情,这不允许我调用 SSL 安全端点。

  • Is there a way I can package my jar with the trustStore?有没有办法让我在 trustStore 中使用 package 我的 jar?
  • Is there a way to run a jar with command-line option in AWS Lambda just like procfile does in Elastic Beanstalk.有没有办法在 AWS Lambda 中使用命令行选项运行 jar,就像 Elastic Beanstalk 中的 procfile 一样。

I believe one option would be to package the certificates into the Jar/zip for your Lambda function itself which would put them on your classpath.我相信一种选择是将 package 证书放入您的 Lambda function 本身的 Jar/zip 中,这会将它们放在您的类路径中。 Once they are on your classpath you can set them up programmatically using一旦它们在您的类路径上,您可以使用编程方式设置它们

    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");

Where you'd define KEYSTORE_FILE like你在哪里定义 KEYSTORE_FILE 像

URL trustStoreResource = LdapConfig.class.getResource( "/keystore.jks" );
String KEYSTORE_FILE= trustStoreResource.toURI().getPath();
System.setProperty("javax.net.ssl.keyStore", KEYSTORE_FILE);

You have too create truststore programitically See below code for reference您也必须以编程方式创建信任库 请参阅下面的代码以供参考

        // 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");

This you can test locally as well on aws lambda.您也可以在 aws lambda 上进行本地测试。 Hope this will solve the issue希望这能解决问题

  • You can keep you certificate in s3 or keep it in resources of lambda directory您可以将证书保存在 s3 中,也可以将其保存在 lambda 目录的资源中

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

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