Python中Azure Keyvault的循环变量和设置机密



所以我正在创建一个Python脚本,该脚本将机密导入Azure。我有24个密钥库的价值观和秘密,但我需要一个更好的方式来写这篇文章,我很挣扎。到目前为止,我拥有的是:

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

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

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)

相关内容

  • 没有找到相关文章

最新更新