簡體   English   中英

AWS Secrets Manager 證書問題

[英]AWS Secrets Manager Certificate issue

我正在嘗試運行一些 java 代碼來獲取一個來自 AWS Secrets manager 的秘密。 代碼非常基本。

    ClientConfiguration clientConfigurtion = new ClientConfiguration();
    clientConfigurtion.setProxyHost("myproxyhost");
    clientConfigurtion.setProxyPort(80);
    clientConfigurtion.setProxyUsername("XXX");
    clientConfigurtion.setProxyPassword("XXX");
    clientConfigurtion.setProxyProtocol(Protocol.HTTP);

    // Create a Secrets Manager client
    AWSSecretsManager client  = AWSSecretsManagerClientBuilder.standard()
            .withRegion(region).withClientConfiguration(clientConfigurtion)
            .build();

    // In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
    // See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    // We rethrow the exception by default.

    String decodedBinarySecret;
    GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest()
            .withSecretId(secretName);
    GetSecretValueResult getSecretValueResult = null;

    try {
        getSecretValueResult = client.getSecretValue(getSecretValueRequest);
    } catch (DecryptionFailureException e) {
        // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
        // Deal with the exception here, and/or rethrow at your discretion.
        throw e;
    } catch (InternalServiceErrorException e) {
        // An error occurred on the server side.
        // Deal with the exception here, and/or rethrow at your discretion.
        throw e;
    } catch (InvalidParameterException e) {
        // You provided an invalid value for a parameter.
        // Deal with the exception here, and/or rethrow at your discretion.
        throw e;
    } catch (InvalidRequestException e) {
        // You provided a parameter value that is not valid for the current state of the resource.
        // Deal with the exception here, and/or rethrow at your discretion.
        throw e;
    } catch (ResourceNotFoundException e) {
        // We can't find the resource that you asked for.
        // Deal with the exception here, and/or rethrow at your discretion.
        throw e;
    }

當我到達它實際獲取秘密值“getSecretValueResult = client.getSecretValue(getSecretValueRequest);”的那一行時我得到一個堆棧跟蹤。

在幾個地方,跟蹤包含此文本。

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

我認為這意味着我缺少一些證書,但我不知道該怎么做才能解決這個問題。

我在 Mac 上本地運行它。

非常感謝有關解決此證書錯誤的任何幫助。

我遇到了同樣的問題並且剛剛開始工作。 默認信任庫沒有https://secretsmanager.us-east-1.amazonaws.com的列表。 您可以看到 AWS 客戶端嘗試連接的 URL(在您的情況下可能略有不同)的方式是打開 java 系統屬性 javax.net.debug=all 您可以通過命令行或如果使用Maven 這樣做:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
            <systemPropertyVariables>
                <javax.net.ssl.trustStore>c:\\path to your cert truststore\\cacerts</javax.net.ssl.trustStore>
                <javax.net.ssl.trustStorePassword>changeit</javax.net.ssl.trustStorePassword>

                <javax.net.debug>all</javax.net.debug>
                
            </systemPropertyVariables>
            </configuration>
        </plugin>  

一旦您知道 AWS 客戶端嘗試與之進行 ssl 握手的 URL(在輸出/錯誤日志中搜索“*** 證書鏈”),您將看到如下內容:*** 證書鏈鏈 [0] = [ [ 版本:V3 主題:CN=secretsmanager.us-east-1.amazonaws.com

現在的問題是獲得這個證書。 在 Chrome 瀏覽器中拉出這個 URL https://secretsmanager.us-east-1.amazonaws.com你會得到一個錯誤,比如 Missing Authentication Token

然后只需按 F12,然后單擊安全選項卡並使用默認值下載該證書。

現在將該證書導入您的 Java 信任庫(如果您使用的是 DOS,我將使用 git bash shell 相應地更改路徑的格式):

$JAVA_HOME/bin/keytool -import -alias awsChromeCer2 -keystore /c/path to your keystore/cacerts -file /c/path to where you save the certificate/awsChromeCert2.cer

驗證它在那里:

$JAVA_HOME/bin/keytool -list -keystore /c/path to your keystore/cacerts | grep aws

當它要求輸入密碼時,默認值可能是:changeit

現在您應該能夠成功運行它而不會出現此異常:com.amazonaws.SdkClientException:無法執行 HTTP 請求:sun.security.validator.ValidatorException:PKIX 路徑構建失敗:sun.security.provider.certpath.SunCertPathBuilderException:無法找到到請求目標的有效認證路徑

相反,您將成功檢索您所追求的 AWS secretsmanager 秘密。 如果您正在使用 Maven,請確保您像我在上一節中所說的那樣定義了這些變量,以指向您的本地信任庫。 還要確保您的機器上設置了 AWS 憑證,但這是一個單獨的問題。

我注意到 secretsmanager 密鑰正在輪換,這意味着您必須在接近使用它的時間下載它們。 如果你想自動化,你可以做這樣的事情:

回聲退出| openssl s_client -showcerts -servername secretsmanager.us-east-1.amazonaws.com -connect secretsmanager.us-east-1.amazonaws.com:443 > SM_cacert.pem

要么編輯證書(刪除 -----END CERTIFICATE ----- 后面的所有文本,后面只有一個空行)

或者

使用來自 bash shell 的代碼編輯證書

awk 'split_after == 1 {n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1}{print > "rds-ca-" n ".pem"}' < SM_cacert .pem

將新證書導入您的信任庫

$JAVA_HOME/bin/keytool -import -alias awsFromOpenSsl -keystore /c/path to your truststore/cacerts -file /c/path to the new cert file/rds-ca-.pem

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM