vault-access-tool/vault_tool.py

91 lines
3.2 KiB
Python
Raw Normal View History

2024-12-16 16:34:09 +00:00
"""
title: Hashicorp Vault Tool
author: Josh Knapp
version: 0.4.5
"""
2024-12-12 16:02:39 +00:00
import subprocess
import os
2024-12-16 16:34:09 +00:00
import hvac
import urllib3
from pydantic import BaseModel, Field
2024-12-12 16:02:39 +00:00
2024-12-16 16:34:09 +00:00
# Try to import hvac, install if not present
try:
import hvac
except ImportError:
print("hvac package not found. Attempting to install...")
2024-12-12 16:02:39 +00:00
try:
2024-12-16 16:34:09 +00:00
subprocess.check_call([sys.executable, "-m", "pip", "install", "hvac"])
2024-12-12 16:02:39 +00:00
import hvac
2024-12-12 17:17:00 +00:00
2024-12-16 16:34:09 +00:00
print("hvac package installed successfully")
except subprocess.CalledProcessError as e:
print(f"Failed to install hvac package: {str(e)}")
2024-12-12 16:02:39 +00:00
2024-12-16 16:34:09 +00:00
class Tools:
class Valves(BaseModel):
VAULT_ADDR: str = Field(
default="",
description="The Web Address for the Vault Server",
)
TLS_VERIFY: bool = Field(
default="False",
description="Check the TLS Certificate for the Vault Server",
)
2024-12-12 16:02:39 +00:00
2024-12-16 16:34:09 +00:00
def __init__(self):
self.valves = self.Valves()
pass
2024-12-12 16:02:39 +00:00
2024-12-16 16:34:09 +00:00
def get_vault_secret(self, path: str, token: str) -> str:
"""
Read secrets stored within HashiCorp Vault based on the path provided by the user.
:param token: Vault authentication token
:param path: Path to the secret in Vault
:return: The value of the secret or the error message if one is produced
2024-12-12 16:02:39 +00:00
2024-12-16 16:34:09 +00:00
"""
# Check if Vault Address has been set
if not self.valves.VAULT_ADDR:
return "The Vault Address has not been set. Please define it in the Tool's Valves"
# Check if a Token is set
if not token:
return f"No token defined, please either provide one in the prompt or via an Environment Variable {token}"
vault_addr = self.valves.VAULT_ADDR
# Attempt to connect to HashiCorp Vault
try:
if self.valves.TLS_VERIFY == True:
client = hvac.Client(
url=vault_addr, token=token, verify=self.valves.TLS_VERIFY
)
else:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
client = hvac.Client(
url=vault_addr, token=token, verify=self.valves.TLS_VERIFY
)
# Check Authentication
2024-12-12 16:02:39 +00:00
if not client.is_authenticated():
2024-12-16 16:34:09 +00:00
return "Failed to authenticate with Vault"
2024-12-12 16:02:39 +00:00
# Split path to separate the key if it exists
2024-12-12 17:17:00 +00:00
path_parts = path.rsplit(".", 1)
2024-12-12 16:02:39 +00:00
secret_path = path_parts[0]
2024-12-12 17:17:00 +00:00
key = path_parts[1] if len(path_parts) > 1 else "value"
2024-12-12 16:02:39 +00:00
try:
2024-12-16 16:34:09 +00:00
secret = client.read(secret_path)
if secret and "data" in secret:
secret_data = secret["data"]
if key in secret_data:
return secret_data[key]
2024-12-12 16:02:39 +00:00
else:
2024-12-16 16:34:09 +00:00
return f"Key '{key}' not found in KV v1 secret path. Check your secret path and ensure the token used has the appropriate permissions."
2024-12-12 16:02:39 +00:00
except Exception as e:
2024-12-16 16:34:09 +00:00
print(f"KV v1 attempt failed: {str(e)}")
return f"Secret not found at path: {path}"
2024-12-12 16:02:39 +00:00
except Exception as e:
2024-12-16 16:34:09 +00:00
return f"Error connecting to Vault: {str(e)}"