我需要使用从秘密管理器获取的秘密来配置knexfile.js。
我从密钥管理器中检索密钥,并将其存储在密钥变量中,并在配置中使用它。

var AWS = require('aws-sdk'),
endpoint = "abcd",
region = "us-east-1",
secretName = "abcd",
secret,
binarySecretData;

var client = new AWS.SecretsManager({
endpoint: endpoint,
region: region
});
client.getSecretValue({
SecretId: secretName
}, function (err, data) {
if (err) {
if (err.code === 'ResourceNotFoundException')
    console.log("The requested secret " + secretName + " was not found");
else if (err.code === 'InvalidRequestException')
    console.log("The request was invalid due to: " + err.message);
else if (err.code === 'InvalidParameterException')
    console.log("The request had invalid params: " + err.message);
} else {
if (data.SecretString !== "") {
    secret = data.SecretString;
} else {
    binarySecretData = data.SecretBinary;
}
}
});
module.exports = {
development: {
    client: secret.localClient,
    connection: {
        host: secret.localHost,
        user: secret.localUser,
        password: secret.localPassword,
        database: secret.localDatabase,
        charset: "utf8"
    }
},
};


但是显示错误
    TypeError:无法读取未定义的属性“ localClient”

最佳答案

获取秘密是异步操作,因此,当您尝试从knexfile.js导出变量时,变量secret尚不存在。

启动虚拟机时,您可能应该首先获取要存储在本地的机密,然后在knexfile.js中同步读取它,例如从本地文件中同步读取它。

关于javascript - 使用AWS Secret Manager配置knexfile.js,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51112575/

10-09 17:54