gpt4 book ai didi

amazon-web-services - Localstack throws 请求中包含的安全 token 无效

转载 作者:行者123 更新时间:2023-12-04 14:07:59 25 4
gpt4 key购买 nike

我使用 Localstack 和 Testcontainers((testcontainers:localstack:1.15.2)) 进行集成测试,并在测试设置中设置 secret ,如下所示:
代码示例

 import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
import com.amazonaws.services.secretsmanager.model.CreateSecretRequest;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SECRETSMANAGER;

public class QueueServiceTest {

DockerImageName localstackImage = DockerImageName.parse("localstack/localstack:0.11.3");
@Rule
public LocalStackContainer localstack = new LocalStackContainer(localstackImage)
.withServices(SECRETSMANAGER).withEnv("LOCALSTACK_HOSTNAME", "localhost").withEnv("HOSTNAME", "localhost");
@Test
public void someTestMethod() {
AWSSecretsManager secretsManager = AWSSecretsManagerClientBuilder.standard()
.withCredentials(localstack.getDefaultCredentialsProvider()).withRegion(localstack.getRegion())
.build();

String secretString = "usrnme";
CreateSecretRequest request = new CreateSecretRequest().withName("test")
.withSecretString(secretString)
.withRequestCredentialsProvider(localstack.getDefaultCredentialsProvider());
secretsManager.createSecret(request);
}

}
现在测试因错误而崩溃:

com.amazonaws.services.secretsmanager.model.AWSSecretsManagerException:The security token included in the request is invalid. (Service:AWSSecretsManager; Status Code: 400; Error Code:UnrecognizedClientException; Request ID:314b0dee-69ed-4b08-9cd0-2618b8e14b25; Proxy: null)

atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1819)atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1403)atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1372)atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1145)atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802)atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770)atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744)atcom.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704)atcom.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686)atcom.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550)atcom.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530)atcom.amazonaws.services.secretsmanager.AWSSecretsManagerClient.doInvoke(AWSSecretsManagerClient.java:2625)atcom.amazonaws.services.secretsmanager.AWSSecretsManagerClient.invoke(AWSSecretsManagerClient.java:2594)atcom.amazonaws.services.secretsmanager.AWSSecretsManagerClient.invoke(AWSSecretsManagerClient.java:2583)atcom.amazonaws.services.secretsmanager.AWSSecretsManagerClient.executeCreateSecret(AWSSecretsManagerClient.java:557)atcom.amazonaws.services.secretsmanager.AWSSecretsManagerClient.createSecret(AWSSecretsManagerClient.java:528)


我想我缺少一些参数,谁能帮我弄清楚。

最佳答案

AWSSecretsManagerClientBuilder 的端点配置不见了。现在您的客户端以真正的 AWS 端点为目标,例如:https://secretsmanager.us-east-1.amazonaws.com:443

public class LocalStackSecretsManagerTest {

DockerImageName localstackImage = DockerImageName.parse("localstack/localstack:0.11.3");

@Rule
public LocalStackContainer localstack = new LocalStackContainer(localstackImage)
.withServices(SECRETSMANAGER)
.withEnv("LOCALSTACK_HOSTNAME", "localhost")
.withEnv("HOSTNAME", "localhost");

@Test
void someTestMethod() {
AWSSecretsManager secretsManager = AWSSecretsManagerClientBuilder.standard()
.withCredentials(localstack.getDefaultCredentialsProvider())
.withEndpointConfiguration(localstack.getEndpointConfiguration(SECRETSMANAGER)) // this is the important line
.build();

String secretString = "usrnme";

CreateSecretRequest request = new CreateSecretRequest()
.withName("test")
.withSecretString(secretString);

secretsManager.createSecret(request);
}
}
指定端点时,您可以删除区域配置。
附加 .withRequestCredentialsProvider(localstack.getDefaultCredentialsProvider());CreateSecretRequest 是多余的,仅当您想根据 CreateSecretRequest 覆盖凭据提供程序时才需要.

关于amazon-web-services - Localstack throws 请求中包含的安全 token 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66811893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com