惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Schneier on Security
Schneier on Security
雷峰网
雷峰网
C
Check Point Blog
K
Kaspersky official blog
T
Tor Project blog
S
Securelist
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
SecWiki News
SecWiki News
Attack and Defense Labs
Attack and Defense Labs
B
Blog
H
Help Net Security
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Google DeepMind News
Google DeepMind News
I
InfoQ
F
Full Disclosure
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
小众软件
小众软件
腾讯CDC
L
LangChain Blog
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
O
OpenAI News
P
Privacy International News Feed
A
About on SuperTechFans
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
M
MIT News - Artificial intelligence
Project Zero
Project Zero
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Last Watchdog
The Last Watchdog
Hacker News: Ask HN
Hacker News: Ask HN
S
Secure Thoughts
Spread Privacy
Spread Privacy
N
News | PayPal Newsroom
G
GRAHAM CLULEY
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - 衡子

Ubuntu24.04更改SSH端口 记录安装过程 Azure CLI创建管理员用户 创建VMSS中的instance 创建不带公网IP的VM Azure LSv3系列VM 自动挂载NVMe本地磁盘 Azure AD访问Azure Storage Azure AD SSO with Google Cloud Identity 通过VM SWAP OS DISK升级VM 通过API获取Azure KeyVault Securet Azure Front Door添加自定义域名 VM间网络PPS和带宽测试 Azure获取access token的方法 VM间记录时延 Windows Terminal的一些配置 安装hping Azure解除不再使用Directory的关联 使用VSCode Remote Containers功能实现开发环境统一 Azure AKS容器网络详解
Azure Linux VM使用Managed Identity获取Key-vault的Secret
衡子 · 2022-05-27 · via 博客园 - 衡子

在程序开发中,如何管理密码或Key,是一个开发人员都会碰到的问题。Azure的Managed Identity为基于Azure的开发提供了一种安全方便的方法。

Azure的Managed Identity为应用程序提供一个标识,可以在连接到支持 Azure Active Directory (Azure AD) 身份验证的资源时使用。应用程序可以使用托管标识来获取 Azure AD 令牌,而开发人员无需管理凭据。

前面几篇blog中介绍了如何获取Azure的Access Token,这里介绍通过Managed Identity,在Azure VM内方便获取Access Token的方法。这为基于Azure的一些应用提供了一个新的获取token的思路。

1 创建managed identity

az group create -n idtest -l westus2

az identity create -g idtest -n vhid01

2 创建基于managed identity的VM

subnet_id=$(az network vnet subnet list \

--vnet-name default-uswest2-vnet \

-g networkwatcherrg | jq -r .[0].id)

nsg_id=$(az network nsg show -g networkwatcherrg \

-n default-uswest2-vnet-vlan11-nsg-westus2 | jq -r .id)

az vm create -n idtest01 -g idtest -l westus2 \

--accelerated-networking true \

--image OpenLogic:CentOS:7_9:latest \

--os-disk-size-gb 128 \

--priority spot \

--size Standard_D2s_v5 \

--assign-identity vhid01 \

--subnet $subnet_id \

--nsg $nsg_id \

--authentication-type ssh \

--admin-username hengwei \

--public-ip-sku Standard \

--ssh-key-values ~/.ssh/id_rsa.pub --no-wait

查看VM的identity属性:

$ az vm show -n idtest01 -g idtest | jq .identity

{

"principalId": null,

"tenantId": null,

"type": "UserAssigned",

"userAssignedIdentities": {

"/subscriptions/xxxx/resourceGroups/idtest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/vhid01": {

"clientId": "xxxxxxx-8af6-4dde-97fc-xxxxxxx",

"principalId": "xxxxxxx-b2fc-4c12-984c-xxxxxxx"

}

}

}

3 对已有VM添加managed identity

创建无User identity的VM

az vm create -n idtest02 -g idtest -l westus2 \

--accelerated-networking true \

--image OpenLogic:CentOS:7_9:latest \

--os-disk-size-gb 128 \

--priority spot \

--size Standard_D2s_v5 \

--subnet $subnet_id \

--nsg $nsg_id \

--authentication-type ssh \

--admin-username hengwei \

--public-ip-sku Standard \

--ssh-key-values ~/.ssh/id_rsa.pub --no-wait

对于创建的VM idtest02不包含identity的属性。

$ az vm show -n idtest02 -g idtest | jq .identity

null

对于已经创建的VM,可以通过update VM的方法,添加managed identity:

$ az vm identity assign -g idtest -n idtest02 --identities vhid01

查看Identity属性:

$ az vm show -n idtest02 -g idtest | jq .identity

{

"principalId": null,

"tenantId": null,

"type": "UserAssigned",

"userAssignedIdentities": {

"/subscriptions/xxxx/resourceGroups/idtest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/vhid01": {

"clientId": "xxxx-8af6-4dde-97fc-xxxx",

"principalId": "xxxx-b2fc-4c12-984c-xxxx"

}

}

}

4 在VM内获取Access Token

在VM中可以获取这个id各个scope的access Token。创建如下的脚本,并且将clientId更换成对于managed Identity的clientId,这个脚本可以获得KeyVault的AccessToken:

az identity show -n vhid01 -g idtest | jq .clientId

"xxxx-8af6-4dde-97fc-xxxx"

#!/bin/bash

curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net&client_id= xxxx-8af6-4dde-97fc-xxxx -H Metadata:true -s | jq -r .access_token

5 创建KeyVault的secret

用下面的cli创建keyvault的securet:

kvname="vhkvtest01"

rg="idtest"

az keyvault create -n $kvname -g $rg -l westus2 \

--no-self-perms --enable-soft-delete false

pid=$(az identity show -g idtest -n vhid01 \

| jq -r .principalId)

az_user=$(az account show -n dn-china-ats-weiheng \

| jq -r .user.name)

az_user_id=$(az ad user list --upn $az_user \

| jq -r .[].objectId)

az keyvault set-policy -n $kvname -g $rg \

--secret-permissions all --object-id $pid

az keyvault set-policy -n $kvname -g $rg \

--secret-permissions all --object-id $az_user_id

s_name='whkey'

s_value='aaaaaa'

az keyvault secret set -n $s_name \

--vault-name $kvname --value $s_value

6 在VM中通过managed Identity访问KeyVault的Securet

#!/bin/bash

token=$(./getKeyVaultToken.sh)

curl -s "https://vhkvtest01.vault.azure.net/secrets/whkey?api-version=2016-10-01" -H "Authorization: Bearer $token"

7 通过manage identity查看相关资源

类似的,下面这个脚本可以获得Azure management的AccessToken:

#!/bin/bash

curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/' -H Metadata:true -s | jq -r .access_token

subId="xxxx-xxxx-xxxx"

token=$(./getKeyManageToken.sh)

curl -s -H "Authorization: Bearer $token" \

"https://management.azure.com/subscriptions?api-version=2020-01-01" | jq .

curl -s -H "Authorization: Bearer $token" \

"https://management.azure.com/subscriptions/$subId/resourcegroups?api-version=2021-04-01" \

| jq .value[].name

curl -s -H "Authorization: Bearer $token" \

"https://management.azure.com/subscriptions/$subId/providers/Microsoft.Storage/storageAccounts?api-version=2021-09-01" | jq .value