如何使用 Azure 应用程序配置 REST API



试图弄清楚如何使用Azure AppConfiguration REST API(主要用于检索和创建键值(。到目前为止,我找到了两个信息来源:Configuration Stores REST API 文档和这个 GitHub 存储库 Azure App Configuration。

这两个来源如何相互对应?他们显然描述了一些不同的AppConfig REST API。

我设法使用这种类型的 URI 和 AAD 授权从我的 AppConfig 存储中检索值https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppConfiguration/configurationStores/{configStoreName}/listKeyValue?api-version=2019-10-01但它只允许获取一个特定键的一个值。

另一种方法使用基于 AppConfig 终结点{StoreName}.azconfig.io/kv/...的 URI,并且必须具有更灵活的方法来检索数据。但我无法让它工作。我试图按照指示进行操作。我尝试使用 AAD 令牌向此 URI 发出请求,就像我对第一种类型的 API 所做的那样。在这两种情况下,我都收到 401 身份验证错误。 任何人都可以分享一些详细的工作示例(Powershell,Postman(吗?任何帮助将不胜感激。

https://management.azure.com/是Azure资源管理API,而 azconfig.io 是App Configuration自己的API。

我认为您应该使用应用程序配置自己的 API。但是,相同的 Azure AD 令牌不适用于此 API。需要使用resource=https://yourstorename.azconfig.ioscope=https://yourstorename.azconfig.io/.default请求另一个访问令牌,具体取决于使用的是 Azure AD 的 v1 还是 v2 令牌终结点。

使用脚本中的$headers对 API 调用进行身份验证:

function Sign-Request(
[string] $hostname,
[string] $method,      # GET, PUT, POST, DELETE
[string] $url,         # path+query
[string] $body,        # request body
[string] $credential,  # access key id
[string] $secret       # access key value (base64 encoded)
)
{  
$verb = $method.ToUpperInvariant()
$utcNow = (Get-Date).ToUniversalTime().ToString("R", [Globalization.DateTimeFormatInfo]::InvariantInfo)
$contentHash = Compute-SHA256Hash $body
$signedHeaders = "x-ms-date;host;x-ms-content-sha256";  # Semicolon separated header names
$stringToSign = $verb + "`n" +
$url + "`n" +
$utcNow + ";" + $hostname + ";" + $contentHash  # Semicolon separated signedHeaders values
$signature = Compute-HMACSHA256Hash $secret $stringToSign

# Return request headers
return @{
"x-ms-date" = $utcNow;
"x-ms-content-sha256" = $contentHash;
"Authorization" = "HMAC-SHA256 Credential=" + $credential + "&SignedHeaders=" + $signedHeaders + "&Signature=" + $signature
}
}
function Compute-SHA256Hash(
[string] $content
)
{
$sha256 = [System.Security.Cryptography.SHA256]::Create()
try {
return [Convert]::ToBase64String($sha256.ComputeHash([Text.Encoding]::ASCII.GetBytes($content)))
}
finally {
$sha256.Dispose()
}
}
function Compute-HMACSHA256Hash(
[string] $secret,      # base64 encoded
[string] $content
)
{
$hmac = [System.Security.Cryptography.HMACSHA256]::new([Convert]::FromBase64String($secret))
try {
return [Convert]::ToBase64String($hmac.ComputeHash([Text.Encoding]::ASCII.GetBytes($content)))
}
finally {
$hmac.Dispose()
}
}
# Stop if any error occurs
$ErrorActionPreference = "Stop"
$uri = [System.Uri]::new("https://{myconfig}.azconfig.io/kv?api-version=1.0")
$method = "GET"
$body = $null
$credential = "<Credential>"
$secret = "<Secret>"
$headers = Sign-Request $uri.Authority $method $uri.PathAndQuery $body $credential $secret

酱料:https://github.com/Azure/AppConfiguration/blob/master/docs/REST/authentication/hmac.md#JavaScript

最新更新