【问题标题】:Azure REST API list key vault secrets has maxresults limited to 25Azure REST API 列表密钥保管库机密的 maxresults 限制为 25
【发布时间】:2021-07-20 20:50:36
【问题描述】:

我正在尝试获取每个密钥保管库中所有机密的列表,并且我正在此 URL 上使用 Microsoft 的文档。 https://docs.microsoft.com/en-us/rest/api/keyvault/getsecrets/getsecrets#secretlistresult

它指出,如果您不设置 maxresults,它将默认为 25。但是,当我尝试将其设置为高于 25 时,它会在我的 powershell 脚本中引发此错误。

{"error":{"code":"BadParameter","message":"invalid maxresults"}}

从文档来看,端点似乎不包含任何分页或获取超过 25 个随机机密的方法。这似乎使端点变得毫无用处,因为没有办法过滤列表。

我用来获取列表的命令是这个。

$uri = ""https://$($Vault).vault.azure.net/secrets?api-version=7.1&maxresults=26""
Invoke-RestMethod -Method Get -Uri $uri -Headers $headers

【问题讨论】:

  • 响应正文中没有nextLink
  • 它返回一个值字段,其中包含 25 个秘密中每个秘密的 id 和属性列表。
  • 我很愚蠢,nextLink 被隐藏了,因为我将输出格式化为表格。

标签: azure powershell rest azure-keyvault


【解决方案1】:

正如 Gaurav 在评论中所说,您需要使用nextLink 才能获得下一页的结果。

我的测试代码是do-until:

$LoginUrl = "https://login.microsoft.com"
$RresourceUrl = "https://vault.azure.net/.default"
$ClientID = ""
$ClientSecret = ""
$TenantName = ""

# Compose REST request.
$Body = @{ grant_type = "client_credentials"; scope = $RresourceUrl; client_id = $ClientID; client_secret = $ClientSecret }     
$OAuth = Invoke-RestMethod -Method Post -Uri $LoginUrl/$TenantName/oauth2/v2.0/token -Body $Body

# Check if authentication was successfull.
if ($OAuth.access_token) {
    # Format headers.
    $HeaderParams = @{
        'Content-Type'  = "application\json"
        'Authorization' = "Bearer $($OAuth.access_token)"
    }

    # Create an empty array to store the result.
    $QueryResults = @()
    
    $Uri = "https://<your-key-vault-name>.vault.azure.net/secrets?api-version=7.1"
    # Invoke REST method and fetch data until there are no pages left.
    do {
        
        $Results = Invoke-RestMethod -Headers $HeaderParams -Uri $Uri -UseBasicParsing -Method "GET" -ContentType "application/json"
        $Results.nextLink
        if ($Results.value) {
            $QueryResults += $Results.value
        }
        else {
            $QueryResults += $Results
        }
        $Uri = $Results.nextLink
    } until (!($Uri))

    # Return the result.
    $QueryResults
}
else {
    Write-Error "No Access Token"
}

【讨论】:

  • 如果我的回复有帮助,请采纳,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
  • 2021-04-02
  • 1970-01-01
相关资源
最近更新 更多