我是Node.js
的新手,我一直在使用第三方提供商的示例项目,我正在尝试使用Azure Key Vault来存储配置值。
在执行其余操作之前,我无法获得wait
的进程。我会尽量详细说明。
sample project有一个名为agent.js
的文件,它是起始页/文件。在line 16(agent_config = require('./config/config.js')[process.env.LP_ACCOUNT][process.env.LP_USER]
)上,它调用带有值的配置文件。我正在尝试使用Key Vault设置这些值。我已经尝试了很多调用函数的组合,甚至实现了async / await
,但agent_config
的值总是包含一个[Promise]
对象,而不是Key Vault返回的数据。
如果我是对的,这是因为Key Vault本身也使用async / await
,并且在返回Key Vault值之前返回配置文件。
如何在这种情况下添加/实施Key Vault?
这是我尝试过的:
第一次更新agent.js到
let agent_config = {};
try {
agent_config = require('./config/config.js')['123']['accountName'];
} catch (ex) {
log.warn(`[agent.js] Error loading config: ${ex}`)
}
console.log(agent_config);
./config/config.就是
const KeyVault = require('azure-keyvault');
const msRestAzure = require('ms-rest-azure');
const KEY_VAULT_URI = 'https://' + '{my vault}' + '.vault.azure.net/' || process.env['KEY_VAULT_URI'];
function getValue(secretName, secretVersion) {
msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' }).then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
module.exports = {
'123': {
'accountName': {
accountId: getValue('mySecretName', '')
}
}
};
结果
{accountsId:undefined}
使getValue成为一个async
函数并将其包装在另一个函数周围(尝试没有包装但也没有工作)
./config/config.就是
const KeyVault = require('azure-keyvault');
const msRestAzure = require('ms-rest-azure');
const KEY_VAULT_URI = 'https://' + '{my vault}' + '.vault.azure.net/' || process.env['KEY_VAULT_URI'];
async function getValue(secretName, secretVersion) {
msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' }).then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
async function config() {
module.exports = {
'123': {
'accountName': {
accountId: await getValue('mySecretName', '')
}
}
};
}
config();
结果
{}
使getValue成为一个async
函数并将其包装在另一个函数周围(尝试没有包装但也没有工作)
./config/config.就是
const KeyVault = require('azure-keyvault');
const msRestAzure = require('ms-rest-azure');
const KEY_VAULT_URI = 'https://' + '{my vault}' + '.vault.azure.net/' || process.env['KEY_VAULT_URI'];
async function getValue(secretName, secretVersion) {
return msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' })
.then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
return client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
module.exports = {
'123': {
'accountName': {
accountId: getValue('mySecretName', '')
}
}
};
config();
结果
{accountId:{<pending>}}
我尝试了许多其他方式,如module.exports = async (value) =< {...}
(通过其他问题/解决方案找到但没有成功。
我开始认为我需要在agent.js
上做一些“等待”,但我还没有找到关于此的好消息。
任何帮助都会很棒!
一个问题是你的getValue
函数没有返回任何东西,因为你的返回需要是明确的。
(没有承诺被退回,没有什么可以等待的)
async function getValue(secretName, secretVersion) {
return msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' })
.then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
return client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
你也可以使用箭头函数逃脱不太明确的回报。
const getValue = async (secretName, secretVersion) =>
msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' })
.then(credentials => {
const client = new KeyVault.KeyVaultClient(credentials);
return client.getSecret(KEY_VAULT_URI, secretName, secretVersion)
.then(response => response.Value);
});
引入Azure Key Vault读取(异步)意味着您的整个配置读取都是异步的。你无能为力。这意味着使用配置的代码需要适当地处理它。首先导出一个将返回配置的异步函数。
async function getConfig() {
return {
'123': {
'accountName': {
accountId: await getValue('mySecretName', '')
}
}
};
}
module.exports = getConfig;
在代理程序代码中,您可以调用该函数。这意味着您的代理程序代码也需要包含在函数中,所以可能是这样的......
const Bot = require('./bot/bot.js');
const getConfig = require('./config/config.js');
getConfig().then(agentConfig => {
const agent = new Bot(agentConfig);
agent.on(Bot.const.CONNECTED, data => {
log.info(`[agent.js] CONNECTED ${JSON.stringify(data)}`);
});
});