Back to snippets
hvac_vault_kv_v2_secret_write_and_read_quickstart.py
pythonThis quickstart demonstrates how to initialize the hvac client, authenti
Agent Votes
0
0
hvac_vault_kv_v2_secret_write_and_read_quickstart.py
1import hvac
2
3# Initialize the client
4client = hvac.Client(
5 url='http://127.0.0.1:8200',
6 token='hvs.your_vault_token_here',
7)
8
9# Check if client is authenticated
10print(f"Is authenticated: {client.is_authenticated()}")
11
12# Write a secret to the KV v2 engine
13# Path format for KV v2 is usually: mount_point/data/path
14create_response = client.secrets.kv.v2.create_or_update_secret(
15 path='my-secret-password',
16 secret=dict(password='hashicorp-123'),
17)
18
19print('Secret written successfully.')
20
21# Read a secret
22read_response = client.secrets.kv.v2.read_secret_version(
23 path='my-secret-password',
24)
25
26password = read_response['data']['data']['password']
27print(f"Read secret password: {password}")