問題描述
在使用CURL通過REST API獲取Azure Key Vaualt的Secrets值,提示Missing Token, 問如何來生成正確的Token呢?
# curl 命令 curl -k --request GET -H "Content-type: application/json;charset=UTF-8" -s https://<your key vault name>.vault.azure.cn/secrets/<secrets name >/<Secrets version number b38a011e4a82a8830b401af1a2384e72 # 錯誤訊息 {"error":{"code":"Unauthorized","message":"AKV10000: Request is missing a Bearer or PoP token
問題分析
通過-v 輸出的更詳細錯誤顯示 401 Unauthorized,在curl傳送的請求中缺少了 Authorization Header。而如果通過瀏覽器F12(開發者工具)獲取到訪問Key Vault Secret的Netwrok Trace獲取的Authorization還是會遇見錯誤。
錯誤訊息為:
{"error":{"code":"Unauthorized","message":"AKV10022: Invalid audience. Expected https://vault.azure.cn, found: https://management.core.chinacloudapi.cn/."}}
所以為了獲取正確的Token:
一:需要在Azure AD中“註冊應用”
二:在Azure Key Vault的Access Policy中新增訪問授權
三:呼叫AAD Token 介面獲取到正確的Token
操作步驟
一:在Azure AD中“註冊應用”
進入 Azure AD App registrations 頁面( https://portal.azure.cn/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps),點選 “New registration”新增新的註冊應用,輸入名稱後註冊。
成功後,一定要記住一點。複製出 Application(Client) ID, Directory (tenant) ID, 外加 在Certificates & Secrets頁面中新增的Client Secrets. (將在第三步中使用)
二:在Azure Key Vault的Access Policy中新增訪問授權
- 進入Azure Key Vault頁面
- 選擇要操作的Key Value
- 點選 Access Policy
- 賦予Secret Permissions許可權
三:呼叫AAD Token 介面獲取到正確的Token
同樣,使用CURL命令呼叫AAD Token API,獲取第四步的Authorization Token
在Windows中,POST請求的Body內容可以通過 --data “parameter1=value1¶meter2=value2”的格式傳遞。所以獲取Token的CLUR命令為:
curl -k --request POST -H 'Content-Type: application/x-www-form-urlencoded'
--data "grant_type=client_credentials&resource=https://vault.azure.cn&client_secret=your secret value&client_id=your aad client id"
-s https://login.chinacloudapi.cn/<your tenant id >/oauth2/token
四:呼叫Key Vault Secrets介面獲取Secret
從第三步中獲取Token,放入獲取Secrets的Header中。命令為:
curl -k --request GET -H "Content-type: application/json;charset=UTF-8"
-H "Authorization:Bearer <REPLACE CONTENT ey*********************>"
-s https://<your key vault name>.vault.azure.cn/secrets/<secrets name >/<Secrets version number b38a011e4a82a8830b401af1a2384e72?api-version=7.3
附錄一:curl命令的引數設定
C:\>curl -h Usage: curl [options...] <url> -d, --data <data> HTTP POST data -f, --fail Fail silently (no output at all) on HTTP errors -h, --help <category> Get help for commands -i, --include Include protocol response headers in the output -o, --output <file> Write to file instead of stdout -O, --remote-name Write output to a file named as the remote file -s, --silent Silent mode -T, --upload-file <file> Transfer local FILE to destination -u, --user <user:password> Server user and password -A, --user-agent <name> Send User-Agent <name> to server -v, --verbose Make the operation more talkative -V, --version Show version number and quit This is not the full help, this menu is stripped into categories. Use "--help category" to get an overview of all categories. For all options use the manual or "--help all".
參考文件
Azure Key Vault REST API - Get Secret: https://docs.microsoft.com/zh-cn/rest/api/keyvault/secrets/get-secret/get-secret