gpt4 book ai didi

javascript - 使用 aws Secret Manager 配置 knexfile.js

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:55 27 4
gpt4 key购买 nike

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

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"
}
},
};

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

最佳答案

现在这在 Knex 中成为可能。您可以将异步函数传递给配置

async function getConfig() {
return new Promise((resolve, reject) => {
client.getSecretValue({ SecretId: 'SECRETID' }, function(
err,
data
) {
if (err) {
console.log('secretsErr', err);
reject(err);
} else {
console.log('Secrets Manager call successful');
if ('SecretString' in data) {
let secret = data.SecretString;
secret = JSON.parse(secret);
const config = {
user: secret.DbUser,
password: secret.DbPassword,
server: secret.DbServer,
database: secret.DbDatabase,
expirationChecker: () => false,
options: {
encrypt: true,
enableArithAbort: true
}
};
resolve(config);
} else {
console.log('no secret found');
reject();
}
}
});
}

let knex = require('knex')({
client: 'mssql',
connection: async function() {
return await getConfig();
}
});

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

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