教程:通过 JavaScript 将 Azure 密钥保管库与虚拟机配合使用
Azure Key Vault 可帮助你保护密钥、机密和证书,例如 API 密钥和数据库连接字符串。
在本教程中,将会设置 Node.js 应用程序以使用 Azure 资源的托管标识从 Azure 密钥保管库读取信息。 你将学习如何执行以下操作:
- 创建密钥保管库
- 在 Key Vault 中存储机密
- 创建一个 Azure Linux 虚拟机
- 为虚拟机启用托管标识
- 授予所需的权限,让控制台应用程序从 Key Vault 读取数据
- 从 Key Vault 检索机密
在开始之前,请阅读 Key Vault 的基本概念。
如果没有 Azure 订阅,请创建一个试用版版订阅。
对于 Windows、Mac 和 Linux:
- Git
- 本教程要求在本地运行 Azure CLI。 必须安装 Azure CLI 2.0.4 或更高版本。 运行
az --version
即可查找版本。 如果需要安装或升级 CLI,请参阅安装 Azure CLI 2.0。
若要使用 Azure CLI 登录到 Azure,请输入:
az cloud set -n AzureChinaCloud
az login
# az cloud set -n AzureCloud //means return to Public Azure.
本快速入门使用预先创建的 Azure 密钥保管库。 可以遵循 Azure CLI 快速入门、Azure PowerShell 快速入门或 Azure 门户快速入门中的步骤创建 Key Vault。
或者,也可运行这些 Azure CLI 或 Azure PowerShell 命令。
重要
每个密钥保管库必须具有唯一的名称。 在以下示例中,将 <your-unique-keyvault-name> 替换为密钥保管库的名称。
az group create --name "myResourceGroup" -l "ChinaEast"
az keyvault create --name "<your-unique-keyvault-name>" -g "myResourceGroup" --enable-rbac-authorization
让我们创建一个名为 mySecret 的机密,其值为 Success!。 机密可以是密码、SQL 连接字符串,或者需要安全保存的、可供应用程序使用的其他任何信息。
若要将机密添加到新创建的密钥保管库,请使用以下命令:
az keyvault secret set --vault-name "<your-unique-keyvault-name>" --name "mySecret" --value "Success!"
使用以下方法之一创建名为 myVM 的 VM:
Linux | Windows |
---|---|
Azure CLI | Azure CLI |
PowerShell | PowerShell |
Azure 门户 | Azure 门户 |
若要使用 Azure CLI 创建 Linux VM,请使用 az vm create 命令。 以下示例添加一个名为 azureuser 的用户帐户。 --generate-ssh-keys
参数用来自动生成一个 SSH 密钥,并将其放置在默认密钥位置 ( ~/.ssh) 中。
az vm create \
--resource-group myResourceGroup \
--name myVM \
--image Ubuntu2204 \
--admin-username azureuser \
--generate-ssh-keys
请记录输出中 publicIpAddress
的值。
使用 Azure CLI az vm identity assign 命令为虚拟机创建系统分配的标识:
az vm identity assign --name "myVM" --resource-group "myResourceGroup"
记下以下代码中显示的系统分配的标识。 以上命令的输出为:
{
"systemAssignedIdentity": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"userAssignedIdentities": {}
}
现在可以运行以下命令,将前面创建的标识权限分配到 Key Vault:
az keyvault set-policy --name "<your-unique-keyvault-name>" --object-id "<systemAssignedIdentity>" --secret-permissions get list
若要登录到虚拟机,请按照连接并登录到运行 Linux 的 Azure 虚拟机或连接并登录到运行 Windows 的 Azure 虚拟机中的说明操作。
若要登录到 Linux VM,可以将 ssh 命令与在<创建虚拟机>步骤中提供的 publicIpAddress 配合使用:
ssh azureuser@<PublicIpAddress>
在虚拟机上,安装我们将在 JavaScript 脚本中使用的两个 npm 库:@azure/keyvault-secrets 和 @azure/identity。
在 SSH 终端中,使用以下命令安装 Node.js 和 npm:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - && \ sudo apt-get install -y nodejs
创建应用目录并初始化 Node.js 包:
mkdir app && cd app && npm init -y
使用
npm
安装 Azure 服务包:npm install @azure/keyvault-secrets @azure/identity
在
app
目录中的虚拟机上,创建名为 index.js 的 JavaScript 文件。touch index.js
使用 Nano 文本编辑器打开该文件:
nano index.js
复制以下代码(请将 <your-unique-keyvault-name> 替换为你的密钥保管库的名称)并将其粘贴到 Nano 编辑器中:
// index.js const { SecretClient } = require("@azure/keyvault-secrets"); const { DefaultAzureCredential } = require("@azure/identity"); // Your Azure Key Vault name and secret name const keyVaultName = "<your-unique-keyvault-name>"; const keyVaultUri = `https://${keyVaultName}.vault.azure.cn`; const secretName = "mySecret"; // Authenticate to Azure const credential = new DefaultAzureCredential(); const client = new SecretClient(keyVaultUri, credential); // Get Secret with Azure SDK for JS const getSecret = async (secretName) => { return (await client.getSecret(secretName)).value; } getSecret(secretName).then(secretValue => { console.log(`The value of secret '${secretName}' in '${keyVaultName}' is: '${secretValue}'`); }).catch(err => { console.log(err); })
使用 Ctrl + x 保存该文件。
当系统询问
Save modified buffer?
时,请输入 y。当系统询问
File Name to Write: index.js
时,请按 Enter 键。
最后,运行 index.js。 如果一切正常,应返回机密值:
node index.js
The value of secret 'mySecret' in '<your-unique-keyvault-name>' is: 'Success!'
不再需要本教程中创建的虚拟机和 Key Vault 时,请将其删除。 只需删除它们所属的资源组,即可快速执行此操作:
az group delete -g myResourceGroup