🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Hashicorp Vault: Sample Python Code

Write a python function called “insert” which would use vault api to store password called “devopsschool” and Write a python function called “display” to read a password using token.

Install python

Install PIP

$ apt install python3-pip

$ python3 vault-read-write.py

import hvac
# Initialize the Vault client
def initialize_vault_client(vault_addr, token):
client = hvac.Client(url=vault_addr, token=token)
return client
# Function to store a password in Vault
def insert(vault_addr, token, password):
try:
client = initialize_vault_client(vault_addr, token)
# Write the password to Vault
client.secrets.kv.v2.create_or_update_secret(
path='passwords',
secret=dict(devopsschool=password)
)
return True, "Password stored successfully"
except Exception as e:
return False, f"Error storing password: {str(e)}"
# Function to read a password from Vault using a token
def display(vault_addr, token):
try:
client = initialize_vault_client(vault_addr, token)
# Read the password from Vault
result = client.secrets.kv.v2.read_secret_version(path='passwords')
if result is not None and 'data' in result:
password = result['data']['data'].get('devopsschool', None)
if password:
return True, password
return False, "Password not found"
except Exception as e:
return False, f"Error reading password: {str(e)}"
# Example usage
if __name__ == "__main__":
vault_addr = "http://127.0.0.1:8200"
token = "hvs.BSu6lCAKvGRotgSI3FvsdLje"
password = "ILOVEBHARAT"
# Store the password
success, message = insert(vault_addr, token, password)
print(message)
# Retrieve the password
success, retrieved_password = display(vault_addr, token)
if success:
print(f"Retrieved password: {retrieved_password}")
else:
print(retrieved_password)
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x