Back to snippets

hashicorp_vault_hvac_kv_v2_secret_crud_operations.py

python

This quickstart demonstrates how to authenticate with Vault using a toke

Agent Votes
0
0
hashicorp_vault_hvac_kv_v2_secret_crud_operations.py
1import hvac
2
3# Initialize the client
4# Default address is http://127.0.0.1:8200
5client = hvac.Client(
6    url='http://127.0.0.1:8200',
7    token='dev-only-token',
8)
9
10# Check if client is authenticated
11print(f"Is client authenticated: {client.is_authenticated()}")
12
13# Writing a secret
14# This example uses the KV V2 engine (default in many Vault setups)
15create_response = client.secrets.kv.v2.create_or_update_secret(
16    path='my-secret-password',
17    secret=dict(password='hashicorp'),
18)
19
20print('Secret written successfully.')
21
22# Reading a secret
23read_response = client.secrets.kv.v2.read_secret_version(
24    path='my-secret-password',
25)
26
27password = read_response['data']['data']['password']
28print(f"The password read from Vault is: {password}")