问题描述

在使用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中添加访问授权

  1. 进入Azure Key Vault页面
  2. 选择要操作的Key Value
  3. 点击 Access Policy
  4. 赋予Secret Permissions权限

三:调用AAD Token 接口获取到正确的Token

同样,使用CURL命令调用AAD Token API,获取第四步的Authorization Token

在Windows中,POST请求的Body内容可以通过 --data “parameter1=value1&parameter2=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

【Azure Developer】使用 CURL 获取 Key Vault 中 Secrets 中的值的更多相关文章

  1. 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID

    问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...

  2. 【Azure Developer】完成算法第4版书中,第一节基础编码中的数组函数 histogrm()

    问题描述 算法 Algorithms (第四版)书中,第1章:基础编程模型第15题: 结果: 编写一个静态方法 histogram(), 接受一个整型数组a[] 和一个整数M为参数,并返回一个大小为M ...

  3. 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code

    问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...

  4. 【Azure Developer】记录一次使用Java Azure Key Vault Secret示例代码生成的Jar包,单独运行出现 no main manifest attribute, in target/demo-1.0-SNAPSHOT.jar 错误消息

    问题描述 创建一个Java Console程序,用于使用Azure Key Vault Secret.在VS Code中能正常Debug,但是通过mvn clean package打包为jar文件后, ...

  5. Azure Key Vault (2) 使用Azure Portal创建和查看Azure Key Vault

    <Windows Azure Platform 系列文章目录> 请注意: 文本仅简单介绍如何在Azure Portal创建和创建Key Vault,如果需要结合Application做二次 ...

  6. Azure Key Vault (1) 入门

    <Windows Azure Platform 系列文章目录> 为什么要使用Azure Key Vault? 我们假设在微软云Azure上有1个场景,在Windows VM里面有1个.NE ...

  7. 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)

    关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...

  8. 【Azure Developer - 密钥保管库 】使用 Python Azure SDK 实现从 Azure Key Vault Certificate 中下载证书(PEM文件)

    问题描述 在Azure Key Vault中,我们可以从Azure门户中下载证书PEM文件到本地. 可以通过OpenSSL把PFX文件转换到PEM文件.然后用TXT方式查看内容,操作步骤如下图: Op ...

  9. 【Azure 环境】Azure Key Vault (密钥保管库)中所保管的Keys, Secrets,Certificates是否可以实现数据粒度的权限控制呢?

    问题描述 Key Vault (密钥保管库) 能不能针对用户授权实现指定用户只能访问某个或某些特定的key? 如当前有两个用户(User1, User2),在Key Vault中有10个Key,Use ...

随机推荐

  1. Python_Learn,Python背景的介绍

    一.计算机程序的运行方式 机器语言编写的程序可以在计算机上直接运行,而汇编语言和高级余语言写的程序(通常称为源程序)则需要"翻译"成机器语言才能运行.源程序"翻译&quo ...

  2. 如何理解Node.js和JavaScript的关系

    一.Javascript的引擎 浏览器一般有两个引擎,一个是Html引擎,一个是脚本引擎. JavaScript是一种脚本语言,最初用于浏览器的动态显示,方便操作页面数据和内容.但实际上,它也可以在浏 ...

  3. springMVC和struts2的区别有哪些?

    (1)springmvc的入口是一个servlet即前端控制器(DispatchServlet),而struts2入口是一个filter过虑器(StrutsPrepareAndExecuteFilte ...

  4. python模块相互依赖的解决方案

    第一种:将相互依赖的文件中的其中一个文件的代码移植到另一个文件中... 第二种:将 import .... 或 from ... import 语句的位置移动到def函数内部,由于import和fro ...

  5. 什么时候使用session?什么时候使用application?

    application:程序全局变量对象,对每个用户每个页面都有效session:用户全局变量,对于该用户的所有操作过程都有效

  6. thymeleaf调用静态类

    为啥需要调用本地静态方法 Thymeleaf模板里面有时候需要使用本地静态常量,要是每次都在控制层传一遍很麻烦 我们需要在Thymeleaf模板我们使用比如 1代表一年级,2代表二年级,3代表三年级等 ...

  7. 转载:2017百度春季实习生五道编程题[全AC]

    装载至:https://blog.csdn.net/zmdsjtu/article/details/70880761 1[编程题]买帽子 时间限制:1秒空间限制:32768K度度熊想去商场买一顶帽子, ...

  8. springboot使用自定义注解和反射实现一个简单的支付

    优点: 未使用if else,就算以后增加支付类型,也不用改动之前代码 只需要新写一个支付类,给添加自定义注解@Pay 首先: 定义自定义注解 Pay 定义 CMBPay ICBCPay 两种支付 根 ...

  9. jsp技术之JSLT技术<c:if text="">判断

    JSLT的c:if标签 作用:用来进行判断的 语法: <c:if test="判断条件,使用EL表达式进行判断"> 如果判断为true,这里的内容会生效:如果为fals ...

  10. 深入Linux 内核架构之 CFS

    linux内核分析--CFS(完全公平调度算法)   1.1 CFS原理 cfs定义了一种新的模型,它给cfs_rq(cfs的run queue)中的每一个进程安排一个虚拟时钟,vruntime.如果 ...