Skip to main content

A wrapper for handling HCP Vault Secrets on an app-by-app basis

Project description

hcp-vault-secrets-wrapper

Simple HCP Vault Secrets Wrapper library

Approach

  • Simple HTTP client implementation in generic python

Required Variables

  • Client ID and Client Secret: each application requires a unique client ID and secret. Keep these values secret.
  • Client Org, App and Project IDs, which can be fetched from the secrets fetching URL provided by HCP:
    • Example: https://api.cloud.hashicorp.com/secrets/2023-11-28/organizations/XXXX/projects/YYYY/apps/ZZZZ/secrets:open
      • XXXX is the organization ID (a UUID)
      • YYYY is the project ID (also a UUID)
      • ZZZZ is the application ID (should be human-readble)

Implementation

  • Use the following pattern to setup an HCP Vault connection for an application
# Keep Secret
HCP_CLIENT_ID = getenv("HCP_CLIENT_ID") or None
HCP_CLIENT_SECRET = getenv("HCP_CLIENT_SECRET") or None

# Not Secret - get from HCP Vault app screen:
HCP_ORG_ID = getenv("HCP_ORG_ID") or None
HCP_PROJECT_ID = getenv("HCP_PROJECT_ID") or None
HCP_APP_ID = getenv("HCP_APP_ID") or None

# Setup secrets manage setup
secrets_mgr = HCPVaultClient(HCP_CLIENT_ID, HCP_CLIENT_SECRET, HCP_ORG_ID, HCP_PROJECT_ID, HCP_APP_ID)
# Fetch secrets
secrets_data = secrets_mgr.fetch_secrets() # Returns object with secrets from HCP
# Access static secrets:
some_secret = secrets_data["SOME_SECRET"]
# Access dynamic secrets and set environ variable 
from os import environ
environ["AWS_ACCESS_KEY_ID"] = secrets_data["SOME_SECRET_AWS"]["values"]["access_key_id"]
environ["AWS_SECRET_ACCESS_KEY"] = secrets_data["SOME_SECRET_AWS"]["values"]["secret_access_key"]
environ["AWS_SESSION_TOKEN"] = secrets_data["SOME_SECRET_AWS"]["values"]["session_token"]

Refresh secrets

  • The code contains two class variables that cache the secrets to avoid over-querying of secrets from HCP.
    • By default, secrets are cached for 25 minutes (or can be controlled by the refresh_timeout_min constructor variable)
  • Call fetch_secrets to either pull new secrets (if we exceed the refresh timeout) OR return the last cached secrets.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hcp_vault_secrets_wrapper-0.7.5.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hcp_vault_secrets_wrapper-0.7.5-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file hcp_vault_secrets_wrapper-0.7.5.tar.gz.

File metadata

File hashes

Hashes for hcp_vault_secrets_wrapper-0.7.5.tar.gz
Algorithm Hash digest
SHA256 22d3aa8654c7d56a7e33b92b0f4061880a5dce2a429d4685a7e824428c08f99c
MD5 1bfaffec0a3e370b2f12990e180a248c
BLAKE2b-256 bda5496d46e1c4057d7a0b80257fb76c8087af8157a9bbc54070c06a91eb75c3

See more details on using hashes here.

File details

Details for the file hcp_vault_secrets_wrapper-0.7.5-py3-none-any.whl.

File metadata

File hashes

Hashes for hcp_vault_secrets_wrapper-0.7.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b52e9bcf08ef268f0bd4416769116adb3fa071925da112190b223f8c5c021ac9
MD5 60126f0dc04dbb0393a537697b36367e
BLAKE2b-256 de3f61e4a36324311afb200a98aea87b2fa5eec808b2199773ccf5ba395a8274

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page