在没有PowerShell的情况下在Azure Key Vault中设置密钥

时间:2016-05-28 14:14:32

标签: azure azure-keyvault

如何在不使用PowerShell的情况下在Azure Key Vault中设置机密。我们使用Azure Key Vault安全地存储连接字符串和一些其他应用程序机密。我们可以使用PowerShell脚本添加秘密,但我想知道是否有另一种方法可以在Azure KeyVault中添加密钥,最好使用API​​。我们实际上需要提供一个管理工具,应用程序管理员可以使用该工具在密钥库中添加/修改机密。

3 个答案:

答案 0 :(得分:1)

Microsoft确实为此提供了REST API。您可以查看here

下面是一个PowerShell脚本,向您展示如何使用该API创建密钥。

int number = (int)(Math.random() *(second - first) + first);

我不知道您使用的编程语言,因此我使用PowerShell因为它易于测试。该脚本是从C#代码转换而来,因此可以很容易地转换回C#。如果您不喜欢提示行为,则可以将凭证与安全字符串一起使用。对于其他编程语言,您可以使用相应的ADAL。如果ADAL不适用于该编程语言,则可以使用OAuth2。

答案 1 :(得分:1)

您现在可以通过Azure门户添加密钥和密钥,而无需使用PowerShell。

答案 2 :(得分:1)

这个问题已经很老了,我想为遇到这个问题的人增加一个新的角度)...

您现在还可以使用ARM模板存储机密信息,虽然已经有一段时间了,但是在很大程度上很难找到相关文档(我第一次工作时花了一些时间来查找它!),但是这是azure快速入门模板中的一个方便示例:

https://github.com/Azure/azure-quickstart-templates/blob/master/201-key-vault-secret-create/azuredeploy.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "keyVaultName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Key Vault"
      }
    },
    "tenantId": {
      "type": "string",
      "metadata": {
        "description": "Tenant Id for the subscription and use assigned access to the vault. Available from the Get-AzureRMSubscription PowerShell cmdlet"
      }
    },
    "accessPolicies": {
      "type": "array",
      "defaultValue": "{}",
      "metadata": {
        "description": "Access policies object {\"tenantId\":\"\",\"objectId\":\"\",\"permissions\":{\"keys\":[\"\"],\"secrets\":[\"\"]}}"
      }
    },
    "vaultSku": {
      "type": "string",
      "defaultValue": "Standard",
      "allowedValues": [
        "Standard",
        "Premium"
      ],
      "metadata": {
        "description": "SKU for the vault"
      }
    },
    "enabledForDeployment": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies if the vault is enabled for VM or Service Fabric deployment"
      }
    },
    "enabledForTemplateDeployment": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies if the vault is enabled for ARM template deployment"
      }
    },
    "enableVaultForVolumeEncryption": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies if the vault is enabled for volume encryption"
      }
    },
    "secretsObject": {
      "type": "secureObject",
      "defaultValue": "{}",
      "metadata": {
        "description": "all secrets {\"secretName\":\"\",\"secretValue\":\"\"} wrapped in a secure object"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.KeyVault/vaults",
      "name": "[parameters('keyVaultName')]",
      "apiVersion": "2015-06-01",
      "location": "[parameters('location')]",
      "tags": {
        "displayName": "KeyVault"
      },
      "properties": {
        "enabledForDeployment": "[parameters('enabledForDeployment')]",
        "enabledForTemplateDeployment": "[parameters('enabledForTemplateDeployment')]",
        "enabledForVolumeEncryption": "[parameters('enableVaultForVolumeEncryption')]",
        "tenantId": "[parameters('tenantId')]",
        "accessPolicies": "[parameters('accessPolicies')]",
        "sku": {
          "name": "[parameters('vaultSku')]",
          "family": "A"
        }
      }
    },
    {
      "type": "Microsoft.KeyVault/vaults/secrets",
      "name": "[concat(parameters('keyVaultName'), '/', parameters('secretsObject').secrets[copyIndex()].secretName)]",
      "apiVersion": "2015-06-01",
      "properties": {
        "value": "[parameters('secretsObject').secrets[copyIndex()].secretValue]"
      },
      "dependsOn": [
        "[concat('Microsoft.KeyVault/vaults/', parameters('keyVaultName'))]"
      ],
      "copy": {
        "name": "secretsCopy",
        "count": "[length(parameters('secretsObject').secrets)]"
      }
    }
  ]
}
相关问题