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

推荐订阅源

博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
小众软件
小众软件
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
A
About on SuperTechFans
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
博客园_首页
T
Troy Hunt's Blog
I
InfoQ
M
MIT News - Artificial intelligence
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
博客园 - 司徒正美
Cloudbric
Cloudbric
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
D
Docker
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
博客园 - 聂微东
S
Security Affairs
Security Archives - TechRepublic
Security Archives - TechRepublic
WordPress大学
WordPress大学
N
News and Events Feed by Topic
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Heimdal Security Blog
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
A
Arctic Wolf
J
Java Code Geeks
F
Full Disclosure
L
Lohrmann on Cybersecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Project Zero
Project Zero
GbyAI
GbyAI
B
Blog
爱范儿
爱范儿

博客园 - 衡子

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