0

所以我正在创建一个将机密导入 Azure 的 Python 脚本。我有 24 个 keyvault 值和秘密,但我需要一种更好的方法来编写这个并且我正在苦苦挣扎。这是我到目前为止所拥有的:

import os
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential

#keyvault name
keyVaultName = os.environ["XXXXXXXX"]

#keyvault URI
KVUri = f"https://{keyVaultName}.vault.azure.net"

#get credentials from already signed in user
credential = DefaultAzureCredential()

#store user credentials and keyvault URI
client = SecretClient(vault_url=KVUri, credential=credential)

#define keyvault secret names and values
secretName1 = input("app-id-example1")
secretValue1 = input("1234")

secretName2 = input("app-id-example2")
secretValue2 = input("5678")

secretName3 = input("app-id-example3")
secretValue3 = input("9101")

secretName4 = input("app-id-example4")
secretValue4 = input("1213")

#set keyvault secret names and values in Azure
client.set_secret(secretName1, secretValue1)
client.set_secret(secretName2, secretValue2)
client.set_secret(secretName3, secretValue3)
client.set_secret(secretName4, secretValue4)

print(" done.")

print(f"Retrieving your secret from {keyVaultName}.")

retrieved_secret = client.get_secret(secretName1, secretName2, secretName3, secretName4)

print(f"Your secret is '{retrieved_secret.value}'.")

print(" done.")

我希望块文本更小我不知道我是如何查看 dict 键值对的,但我不确定它是如何工作的。

有人可以帮忙吗。

编辑:我开发了一个 python 脚本,允许您为 1 个密钥库创建多个秘密 - https://github.com/TechyTish/AzurePy/blob/main/create-azure-secrets-README.md

4

1 回答 1

0

您可以使用字典中的键值对尝试类似的操作。

my_dict = {}
no_of_secrets = 24

for i in range(no_of_secrets):
  secretName = input("app-id-example ")
  secretValue = input("Secret Value ")
  my_dict[secretName] = secretValue

for Key,Value in my_dict.items():
  client.set_secret(Key, Value)
于 2022-01-22T11:11:22.763 回答