gpt4 book ai didi

javascript - 配置中缺少凭据,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1(存在凭据文件)

转载 作者:行者123 更新时间:2023-12-05 06:56:49 27 4
gpt4 key购买 nike

我试图在我的 cypress 测试中获取 AWS secret,但我一直收到 CredentialsError

Missing credentials in config, if using AWS_CONFIG_FILE, setAWS_SDK_LOAD_CONFIG=1

我确实有一个 ~/.aws/credentials 文件,其中设置了我的 aws_access_key_id 和 aws_secret_access_key。

在我的代码中,我将所有环境变量导出到一个文本文件,我可以看到 AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY 和 AWS_SESSION_TOKEN 的值。

我也尝试设置环境变量 AWS_SDK_LOAD_CONFIG=1 但仍然收到相同的消息。

我需要 ~/.aws/credentials 还是可以通过环境变量来完成?

谁能看到我遗漏了什么:(99% 的代码是亚马逊在 secret 管理器页面上提供的代码我只是想将响应包装在 Cypress 对象中。我已经删除了我的 AWS 详细信息和将它们替换为 REMOVED)

Cypress.Commands.add("aws_secret", () => {
// Use this code snippet in your app.
// If you need more information about configurations or implementing the sample code, visit the AWS docs:
// https://aws.amazon.com/developers/getting-started/nodejs/

cy.exec(`printenv >> envs.txt`);

// Load the AWS SDK
var AWS = require("aws-sdk"),
region = “REMOVED”,
secretName = “REMOVED”,
secret,
decodedBinarySecret;

// Create a Secrets Manager client
var client = new AWS.SecretsManager({
region: region,
});

// 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.

client.getSecretValue({ SecretId: secretName }, function (err, data) {
if (err) {
if (err.code === "DecryptionFailureException")
// 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 err;
else if (err.code === "InternalServiceErrorException")
// An error occurred on the server side.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === "InvalidParameterException")
// You provided an invalid value for a parameter.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === "InvalidRequestException")
// 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 err;
else if (err.code === "ResourceNotFoundException")
// We can't find the resource that you asked for.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
} else {
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
if ("SecretString" in data) {
secret = data.SecretString;
cy.log("the secret is " + secret);
} else {
let buff = new Buffer(data.SecretBinary, "base64");
decodedBinarySecret = buff.toString("ascii");
cy.log("the decodedBinarySecret is " + decodedBinarySecret);
}
}

// Your code goes here.
cy.wrap(
client.getSecretValue({ SecretId: "REMOVED" }).promise()
).as("key1");
});
});

最佳答案

您无需设置 AWS_SDK_LOAD_CONFIGenv 运行该过程并不能保证设置所有相同的环境变量。运行此脚本时如何设置环境变量。

您实际上可以在加载 AWS SDK 之前对此进行测试,添加

console.log('AccessKey: ', process.env.AWS_ACCESS_KEY_ID);

并验证它们是否正在打印。要在节点进程中作为环境变量传递,请执行:导出 AWS_ACCESS_KEY_ID=VALUE;节点./script.js

Docs on setting up Cypress environment variables

CYPRESS 为前缀导入:

export CYPRESS_AWS_ACCESS_KEY_ID=VALUE

关于javascript - 配置中缺少凭据,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1(存在凭据文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65041408/

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