1

我正在创建 AWS Lambda,它将使用来自私有队列(在客户端服务器中)的数据。它需要添加一些受信任的证书。在本地,我执行了以下命令:

keytool -import -v -trustcacerts -alias "clientcert" -file "..\client.cer" -keystore cacerts -keypass changeit -storepass changeit

它工作正常。现在我已经在 aws 控制台中上传了我的 lambda 函数,我得到了同样的错误,如下所示:

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

谁能建议我如何在控制台中将受信任的证书添加到 aws lambda

4

1 回答 1

1

您也必须以编程方式创建信任库 请参阅下面的代码以供参考

        // 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 上进行本地测试。希望这能解决问题

于 2021-05-27T19:57:33.717 回答