avatarCndro

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2399

Abstract

our account must be able to perform this or you might rather do that manually by going to the Azure page and search for key vaults.</p><figure id="bc21"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Ct3G3uPOVu3i-D6DDV0bbA.png"><figcaption></figcaption></figure><p id="ba94">While at the Key Vault page, click on create and then fill in the necessary details. Navigate through each tab, while at the Access Policy tab, ensure to add the application you want to work with here.</p><figure id="7e1a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fl2SqTfUM-237_RPbSLeqg.png"><figcaption></figcaption></figure><p id="a88f">To create our key vault using python without going to the Azure portal we can use the following code.</p><blockquote id="658a"><p>Use the az group create command to create a resource group:</p></blockquote><div id="7545"><pre>az <span class="hljs-keyword">group</span> <span class="hljs-title">create</span> — name KeyVault-PythonQS-rg — <span class="hljs-keyword">location</span> <span class="hljs-title">eastus</span></pre></div><blockquote id="a4e5"><p>Use az kyvault create to create the key vault:</p></blockquote><div id="93e7"><pre>az keyvault create --name <span class="hljs-tag"><your-unique-keyvault-name></span> --resource-<span class="hljs-keyword">group</span> <span class="hljs-title">KeyVault-PythonQS-rg</span></pre></div><blockquote id="9a32"><p>Grant access to your key vault</p></blockquote><div id="6259"><pre><span class="hljs-string">az</span> <span class="hljs-string">keyvault</span> <span class="hljs-built_in">set-policy</span> <span class="hljs-built_in">--name</span> <<span class="hljs-string">YourKeyVaultName</span>> <span class="hljs-built_in">--upn</span> <span class="hljs-string">user</span>@<span class="hljs-string">domain</span>.<span class="hljs-string">com</span> <span class="hljs-built_in">--secret-permissions</span> <span class="hljs-string">delete</span> <span class="hljs-string">get</span> <span class="hljs-string">list</span> <span class="hljs-string">set</span></pre></div><p id="1a38">To proceed, we then ensure to set our environment variable, most especially for values we’ll be using inside our code; KeyVaultName, client_secret, client_id, tenant_id. We can set it using the following command.</p><div id="bc8f"><pre><span class="hljs-built_in">export</span> <span class="hlj

Options

s-attribute">KEY_VAULT_NAME</span>=<your-key-vault-name></pre></div><p id="cad2"><b>Sample code to Set our secret</b></p><p id="58a0">The following code sample demonstrates how to create a client, set a secret, retrieve a secret</p><div id="c966"><pre>import os from azure<span class="hljs-selector-class">.keyvault</span><span class="hljs-selector-class">.secrets</span> import SecretClient from azure<span class="hljs-selector-class">.identity</span> import ClientSecretCredential

KeyVaultName = os<span class="hljs-selector-class">.environ</span><span class="hljs-selector-attr">[<span class="hljs-string">'KEY_VAULT_NAME'</span>]</span> client_id = os<span class="hljs-selector-class">.environ</span><span class="hljs-selector-attr">[<span class="hljs-string">'client_id'</span>]</span> client_secret = os<span class="hljs-selector-class">.environ</span><span class="hljs-selector-attr">[<span class="hljs-string">'client_secret'</span>]</span> tenant_id = os<span class="hljs-selector-class">.environ</span><span class="hljs-selector-attr">[<span class="hljs-string">'tenant_id'</span>]</span>

KeyUri = (f<span class="hljs-string">"https://{KeyVaultName}.vault.azure.net"</span>) credentials = <span class="hljs-built_in">ClientSecretCredential</span>(client_id=client_id, client_secret=client_secret,tenant_id=tenant_id) secrets = <span class="hljs-built_in">SecretClient</span>(vault_url=KeyUri, credential=credentials) secretName=<span class="hljs-string">"username"</span> secretValue=<span class="hljs-string">"1233567"</span> new_secret = secrets<span class="hljs-selector-class">.set_secret</span>(secretName, secretValue) _sec= secrets<span class="hljs-selector-class">.get_secret</span>(secretName)<span class="hljs-selector-class">.value</span> <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(_sec)</span></span></pre></div><p id="85f9"><b>Conclusion</b></p><p id="5d37">That’s pretty much it. In this post, we have learned how to create Azure Key Vault, create a client, set a secret, and retrieve a secret. There’s a step-by-step video tutorial on this. Click <a href="https://www.youtube.com/watch?v=2zg2b7I8d4s">here</a> to watch. Thanks for reading this article. Stay tuned for more posts.</p><p id="e01c"><i>More content at <a href="http://plainenglish.io/"><b>plainenglish.io</b></a></i></p></article></body>

Azure Key Vault Secret Client Library for Python

Azure Key Vault helps to solve Secrets management by securely storing and controlling access to tokens, passwords, certificates, API keys, and other secrets. When you use Key Vault to store secrets, you avoid storing secrets in your code, which increases the security of your app. What type of secrets can be stored in an Azure key vault? It can store three types of items namely: secrets, keys, and certificates.

In today’s post, you will learn how to get started with Azure Key Vault Secret Client Library for Python. Let’s get started by performing the following operations;

Step 1: Install the Packages

  • Azure Identity

pip install azure-keyvault-secrets azure-identity

  • Azure CLI (using the link)

Azure Keyvaults secret

pip install azure-keyvault-secrets

Afterward, we will proceed to our code and work with the library.

Step 2: Sign in to Azure

We’ll sign in to azure using the command below after we’ve installed the necessary libraries. This command was run majorly for authentication. After running the command, it will load an Azure sign-in page.

az login

Step 3: Create a resource group and key vault

From here, we then create our resource group and key vault. To do this your account must be able to perform this or you might rather do that manually by going to the Azure page and search for key vaults.

While at the Key Vault page, click on create and then fill in the necessary details. Navigate through each tab, while at the Access Policy tab, ensure to add the application you want to work with here.

To create our key vault using python without going to the Azure portal we can use the following code.

Use the az group create command to create a resource group:

az group create — name KeyVault-PythonQS-rg — location eastus

Use az kyvault create to create the key vault:

az keyvault create --name <your-unique-keyvault-name> --resource-group KeyVault-PythonQS-rg

Grant access to your key vault

az keyvault set-policy --name <YourKeyVaultName> --upn user@domain.com --secret-permissions delete get list set

To proceed, we then ensure to set our environment variable, most especially for values we’ll be using inside our code; KeyVaultName, client_secret, client_id, tenant_id. We can set it using the following command.

export KEY_VAULT_NAME=<your-key-vault-name>

Sample code to Set our secret

The following code sample demonstrates how to create a client, set a secret, retrieve a secret

import os 
from azure.keyvault.secrets import SecretClient 
from azure.identity import ClientSecretCredential 
  
KeyVaultName = os.environ['KEY_VAULT_NAME'] 
client_id = os.environ['client_id'] 
client_secret = os.environ['client_secret'] 
tenant_id = os.environ['tenant_id'] 
  
KeyUri = (f"https://{KeyVaultName}.vault.azure.net") 
credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret,tenant_id=tenant_id) 
secrets = SecretClient(vault_url=KeyUri, credential=credentials) 
secretName="username" 
secretValue="1233567" 
new_secret = secrets.set_secret(secretName, secretValue) 
_sec= secrets.get_secret(secretName).value 
print(_sec)

Conclusion

That’s pretty much it. In this post, we have learned how to create Azure Key Vault, create a client, set a secret, and retrieve a secret. There’s a step-by-step video tutorial on this. Click here to watch. Thanks for reading this article. Stay tuned for more posts.

More content at plainenglish.io

Python
Programming
Data Science
Data Analysis
Cybersecurity
Recommended from ReadMedium