如何使用 Azure 应用配置 REST API

How to use Azure App Configuration REST API

试图找出如何使用 Azure AppConfiguration REST API(主要是检索和创建键值)。到目前为止,我找到了两个信息来源:Configuration Stores REST API docs and this GitHub repo 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 端点的 URI {StoreName}.azconfig.io/kv/...,并且必须有更灵活的方法来检索数据。但我无法让它发挥作用。我试着关注 instructions。我尝试使用 AAD 令牌向此 URI 发出请求,就像我对第一种 API 所做的那样。在这两种情况下,我都会收到 401 身份验证错误。 谁能分享一些详细的工作示例(Powershell、Postman)?任何帮助将不胜感激。

https://management.azure.com/是Azure Resource ManagementAPI,而azconfig.io是App Configuration自己的API.

我认为你应该使用 App Configuration 自己的 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