使用PowerShell从Azure应用配置获取键值对

6pp0gazn  于 2023-10-22  发布在  Shell
关注(0)|答案(6)|浏览(165)

AZ模块中没有任何小工具可以从Azure应用配置中获取键值对。除了AZ CLI,还有其他选择吗?有没有一种方法可以与PowerShell的服务交互?

vsnjm48y

vsnjm48y1#

目前没有PowerShell支持从应用程序配置中获取键值。在PowerShell中调用Azure CLI是正确的方法。Az.AppConfiguration模块只支持管理操作(例如,创建应用程序配置存储等)。
请求在GitHub https://github.com/Azure/AppConfiguration/issues/267中跟踪。

dgenwo3n

dgenwo3n2#

嘿,我有点迟到了,但我一直在寻找同样的东西,我最终创建了自己的解决方案,因为给定的答案只涵盖了管理任务,而不是数据检索。
看看这个repo
它可以获取密钥值并解析秘密。
它还可以解析值中引用的键。
它在后台使用Aztek,所以你需要先安装它。

35g0bw71

35g0bw713#

下面是我的解决方案,使用应用程序配置连接字符串进行身份验证。您对返回对象的.items属性感兴趣。在分页的情况下,也会有一个next链接。有关详细信息,请参阅https://learn.microsoft.com/azure/azure-app-configuration/rest-api-key-value上的文档
键可以包含斜杠字符,因此在构建$RequestUri时需要使用[System.Net.WebUtility]::UrlEncode($Key)对其进行URL编码。

  1. function Invoke-AppConfigRequest {
  2. param(
  3. [Parameter(Mandatory = $true)] [string] $ConnectionString, # 'Endpoint=...;Id=...;Secret=...'
  4. [Parameter(Mandatory = $true)] [string] $RequestUri, # '/kv?api-version=1.0&key=some-url-encoded-key&label=*'
  5. [Parameter(Mandatory = $false)] [string] $Method = 'GET', # 'GET', 'POST'
  6. [Parameter(Mandatory = $false)] [object] $Body = $null # Accepts [object] to avoid implicit conversion of $null to empty string
  7. )
  8. $ConnectionStringValues = $ConnectionString -split ';' | ForEach-Object { $Tokens = $_ -split '=',2; @{ Key = $Tokens[0]; Value = $Tokens[1] } }
  9. $Endpoint = ($ConnectionStringValues | Where-Object { $_.Key -eq 'Endpoint' }).Value
  10. $Credential = ($ConnectionStringValues | Where-Object { $_.Key -eq 'Id' }).Value
  11. $Secret = ($ConnectionStringValues | Where-Object { $_.Key -eq 'Secret' }).Value
  12. if ([string]::IsNullOrWhitespace($Endpoint) -or [string]::IsNullOrWhitespace($Credential) -or [string]::IsNullOrWhitespace($Secret)) {
  13. throw "Invalid App Configuration connection string"
  14. }
  15. $UtcNow = (Get-Date).ToUniversalTime().ToString('ddd, d MMM yyyy HH:mm:ss \G\M\T')
  16. $EndpointHost = $Endpoint -replace '^https?://(.*)$','$1'
  17. $ContentHash = [Convert]::ToBase64String(
  18. [System.Security.Cryptography.HashAlgorithm]::Create('sha256').ComputeHash(
  19. [System.Text.Encoding]::UTF8.GetBytes($(if ($Body -ne $null) { "$Body" } else { '' }))
  20. )
  21. )
  22. $StringToSign = "$Method`n$RequestUri`n$UtcNow;$EndpointHost;$ContentHash"
  23. $HmacSha256 = New-Object System.Security.Cryptography.HMACSHA256
  24. $HmacSha256.Key = [Convert]::FromBase64String($Secret)
  25. $Signature = [Convert]::ToBase64String(
  26. $HmacSha256.ComputeHash(
  27. [System.Text.Encoding]::UTF8.GetBytes($StringToSign)
  28. )
  29. )
  30. $Headers = @{
  31. 'Host' = $EndpointHost;
  32. 'x-ms-date' = $UtcNow;
  33. 'x-ms-content-sha256' = $ContentHash;
  34. 'Accept' = 'application/vnd.microsoft.appconfig.kv+json, application/json, application/problem+json';
  35. 'Authorization' = "HMAC-SHA256 Credential=$Credential&SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=$Signature";
  36. }
  37. $Uri = "$Endpoint$RequestUri"
  38. $Response = Invoke-WebRequest -Method $Method -Uri $Uri -Headers $Headers -Body $Body
  39. if ($Response.StatusCode -eq 200) {
  40. [System.Text.Encoding]::UTF8.GetString($Response.Content) | ConvertFrom-Json
  41. }
  42. }

示例调用:

  1. function Get-AppConfigKeyValue {
  2. param(
  3. [Parameter(Mandatory = $true)] [string] $ConnectionString,
  4. [Parameter(Mandatory = $true)] [string] $Key,
  5. [Parameter(Mandatory = $false)] [string] $Label = ''
  6. )
  7. $UrlEncodedKey = [System.Net.WebUtility]::UrlEncode($Key)
  8. $UrlEncodedLabel = [System.Net.WebUtility]::UrlEncode($Label)
  9. # https://learn.microsoft.com/azure/azure-app-configuration/rest-api-key-value
  10. $Method = 'GET'
  11. $ApiVersion = '1.0'
  12. $RequestUri = '/kv'
  13. #if (![string]::IsNullOrWhitespace($UrlEncodedKey)) {
  14. # $RequestUri += "/$UrlEncodedKey" # Strict key/label matching, no support for wildcards like *.
  15. #}
  16. $RequestUri += "?api-version=$ApiVersion"
  17. if (![string]::IsNullOrWhitespace($UrlEncodedKey)) {
  18. $RequestUri += "&key=$UrlEncodedKey" # Key filter, accepts "*" to match all keys.
  19. }
  20. if (![string]::IsNullOrWhitespace($UrlEncodedLabel)) {
  21. $RequestUri += "&label=$UrlEncodedLabel" # Label filter, accepts "*" to match all labels.
  22. } else {
  23. $RequestUri += "&label=%00" # Matches KV without a label.
  24. }
  25. (Invoke-AppConfigRequest -ConnectionString $ConnectionString -RequestUri $RequestUri).items
  26. }
展开查看全部
qybjjes1

qybjjes14#

Azure CLI有一个命令az appconfig kv list,可用于列出应用程序配置中的所有键值。关于用法和示例的更多细节可以在这里找到。

e4yzc0pl

e4yzc0pl5#

新版本的az.appconfiguration模块提供了此功能。首先安装模块,如果您已经安装了它,请使用-Force开关:

  1. install-module -name az.appconfiguration -Force

接下来,您可以使用端点查询应用配置存储:

  1. Get-AzAppConfigurationKeyValue -Endpoint "https://appconfigname.azconfig.io"

您还可以使用标签和通配符:

  1. Get-AzAppConfigurationKeyValue -Endpoint "https://appconfigname.azconfig.io" -Label "dev" -key "serverWeb*"

注意:当我试图获取所有KeyValues时,我遇到了一些错误,我怀疑它检索的值有一个最大值。使用标签和关键字过滤器,它工作得很好。

ki0zmccv

ki0zmccv6#

查看Az.AppConfiguration模块。我不能保证它的质量或多么完整,但它就在那里。Az模块看起来不像是作为依赖项包含的。
您可以从PowerShell Gallery下载:

  1. Install-Module -Name Az.AppConfiguration -Repository PSGallery -Scope CurrentUser

相关问题