【问题标题】:Retrieve multiple secret values key vault检索多个秘密值密钥保管库
【发布时间】:2020-12-10 06:42:30
【问题描述】:

有没有办法通过 HTTP 方法 GET 从 azure key vault 中检索多个秘密值?我正在使用链接 https://{keyvaultname}.vault.azure.net/secrets/{keysecretname}?api-version=7.1

我一次只能检索一个值。那么有没有办法使用 HTTP 方法来获取所有的秘密值呢?

【问题讨论】:

  • @CarlZhao 嗨,我阅读了给出的答案,但该方法使用的是我不熟悉的 node.js,而且据我所知,它只返回一个秘密名称列表而不是秘密值

标签: azure http postman azure-keyvault


【解决方案1】:

目前,没有 Azure Vault API 操作可让您从 Azure Vault 检索所有机密及其各自的值。

但是,您可以使用 postman 来协调所有机密的检索,方法是利用 Collection Runner 以及控制逻辑来定义运行哪个请求以及何时运行。 我参考了这个community post 并创建了一个邮递员集合,可以帮助您检索所有秘密。

我自己在我的个人 Azure 订阅中的 Key Vault 上对此进行了测试,它就像一个魅力。请确保在 Collection Runner 中有一个空白的 Postman Environment 来运行此 Collection。

希望这会有所帮助。如果您遇到任何问题,请告诉我。

{
"info": {
    "_postman_id": "c7298583-a343-47f3-b608-73547da45d5e",
    "name": "Azure Vault Secrets",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
    {
        "name": "Retrieve All Secret Keys",
        "event": [
            {
                "listen": "test",
                "script": {
                    "id": "af71963c-adc5-4688-aa55-5fdae1aea154",
                    "exec": [
                        "// Function to extract last element i.e. the Secret Key Name from the secrets URL",
                        "const getLastItem = thePath => thePath.substring(thePath.lastIndexOf('/') + 1);",
                        "",
                        "// Parse the response Body",
                        "var jsonData = pm.response.json();",
                        "",
                        "// Map the secrets URL from the element 'id' presnet in response",
                        "var secretUrllist = _.map(jsonData.value, 'id');",
                        "",
                        "// Initialize an empty array to store the secret Key name",
                        "var secretList = [];",
                        "",
                        "// Populate the array and extract the last element from the URL",
                        "_.forEach(secretUrllist, function(value){",
                        "    secretList.push(getLastItem(value));",
                        "});",
                        "",
                        "// Set the secretList",
                        "pm.environment.set('secretList',JSON.stringify(secretList));",
                        "",
                        "// Set the next index of the array for secretList ",
                        "pm.environment.set('nextIndex', 0);",
                        "",
                        "// Set the active secret Key name to fetch the secret Value for",
                        "pm.environment.set('activeSecret', secretList[0]);",
                        ""
                    ],
                    "type": "text/javascript"
                }
            }
        ],
        "request": {
            "auth": {
                "type": "noauth"
            },
            "method": "GET",
            "header": [],
            "url": {
                "raw": "{{vaultBaseUrl}}/secrets?api-version=7.1",
                "host": [
                    "{{vaultBaseUrl}}"
                ],
                "path": [
                    "secrets"
                ],
                "query": [
                    {
                        "key": "api-version",
                        "value": "7.1"
                    }
                ]
            }
        },
        "response": []
    },
    {
        "name": "Retrieve All Secret Values",
        "event": [
            {
                "listen": "test",
                "script": {
                    "id": "7d6e8591-9c9b-4a97-92f3-a24059fa8750",
                    "exec": [
                        "let secretList = JSON.parse(pm.environment.get('secretList')),",
                        "    // Increment the next Index",
                        "    nextIndex = parseInt(pm.environment.get('nextIndex')) + 1;",
                        "",
                        "",
                        "// In case secret values have been fetched for all requests then we're done here",
                        "// time to end the collection run and clean up the environment and activeSecret",
                        "if (secretList.length === nextIndex) {",
                        "    pm.environment.set('nextIndex', 0);",
                        "    pm.environment.set('activeSecret', secretList[0]);",
                        "",
                        "    postman.setNextRequest(null);",
                        "}",
                        "else {",
                        "    let activeSecret = secretList[nextIndex];",
                        "    pm.environment.set('nextIndex', nextIndex);",
                        "    pm.environment.set('activeSecret', activeSecret);",
                        "",
                        "    // Now run the Retrieve All Secret Values request again to get the secret value",
                        "    // for the next request",
                        "    postman.setNextRequest(\"Retrieve All Secret Values\");",
                        "}",
                        "",
                        ""
                    ],
                    "type": "text/javascript"
                }
            }
        ],
        "request": {
            "auth": {
                "type": "noauth"
            },
            "method": "GET",
            "header": [],
            "url": {
                "raw": "{{vaultBaseUrl}}/secrets/{{activeSecret}}?api-version=7.1",
                "host": [
                    "{{vaultBaseUrl}}"
                ],
                "path": [
                    "secrets",
                    "{{activeSecret}}"
                ],
                "query": [
                    {
                        "key": "api-version",
                        "value": "7.1"
                    }
                ]
            }
        },
        "response": []
    }
],
"protocolProfileBehavior": {}

}

【讨论】:

  • 介意解释一下“定义运行哪个请求以及何时运行”是什么意思?
  • 这就是在 Request 中实现的控制逻辑,叫做 postman 集合中的“Retrieve All Secret Values”。在我们检索到保险库中存在的所有秘密之前,该请求将被执行。下面是您在 Postman 中导入提供的集合后将看到的代码的 sn-p:
  • if (secretList.length === nextIndex) { pm.environment.set('nextIndex', 0); pm.environment.set('activeSecret', secretList[0]); postman.setNextRequest(null); } else { 让 activeSecret = secretList[nextIndex]; pm.environment.set('nextIndex', nextIndex); pm.environment.set('activeSecret', activeSecret); // 现在再次运行 Retrieve All Secret Values 请求以获取秘密值 // 用于下一个请求 postman.setNextRequest("Retrieve All Secret Values"); }
猜你喜欢
  • 2020-08-28
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2019-12-03
  • 2018-07-31
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
相关资源
最近更新 更多