Skip to main content

Connect to Vault with a configuration file

Project description

secretsmith

The secretsmith Python package allows connecting to Vault or OpenBao, with support for several authentication methods, including using a token or AppRole.

It also provides a simple wrapper to query secrets from a kv2 store.

This is a high-level wrapper around hvac.

At Nasqueron, we use this package to avoid writing boilerplate code in each application that needs to interact with Vault or OpenBao to:

  • read a configuration file to determine login parameters
  • query a simple password from kv2 store from a path

When more and more applications need to interact with Vault or OpenBao, and use the same authentication methods, the same patterns to query secrets, to maintain this wrapper high-level library becomes useful.

Login

Secretsmith uses the hvac library to connect to Vault or OpenBao.

If nothing is specified, it will try to connect to Vault using the environment variables VAULT_ADDR and VAULT_TOKEN, or reading a token file at the default path. Especially convenient during the development workflow.

When it's ready to be deployed, write a configuration file explaining how to connect to Vault or OpenBao.

How to use in code?

Call secretsmith.login() with the path to the configuration file:

import secretsmith

VAULT_CONFIG_PATH = '/path/to/config.yaml'

vault_client = secretsmith.login(config_path=VAULT_CONFIG_PATH)

Then, you can use the client as a hvac library Vault client.

We provide helper methods for common tasks, but you can also directly use hvac.

Configuration file

Secretsmith uses a YAML configuration file to determine the login parameters:

vault:
  server:
    url: https://127.0.0.1:8200
  auth:
    token: hvs.000000000000000000000000 

When using AppRole, the configuration file will look like:

vault:
  server:
    url: https://127.0.0.1:8200
    verify: /path/to/ca.pem
  auth:
    method: approle
    role_id: e5a7b66e-5d08-da9c-7075-71984634b882
    secret_id: 841771dc-11c9-bbc7-bcac-6a3945a69cd9

The format is based on the Vault execution module for SaltStack.

The following parameters are supported:

  • server - a block to specify the Vault or OpenBao server parameters
    • url - the URL
    • verify - the path to a CA certificate to verify the server's certificate
    • namespace - the namespace to use (by default, will follow environment)
  • auth - a block to specify the authentication method and parameters
    • method - what authentication backend to use, by default 'token'

Additional parameters are supported in the auth block depending on the authentication method.

When the method is token:

  • token - the token to use
  • token_file - alternatively, the path to a file containing the token

When the method is approle: - role_id - the AppRole role ID (required) - secret_id - the AppRole secret ID (optional)

Querying secrets

For kv2, we also provide helper methods for more common use cases.

If you store a password in the password field of the 'secret/app/db' path:

import secretsmith
from secretsmith.vault import secrets

vault_client = secretsmith.login()
password = secrets.get_password(vault_client, "secret", "app/db")

To get the full k/v store at the 'secret/app/db' path:

secret = secrets.read_secret(vault_client, "secret", "app/db")

If you also store custom metadata, you can use:

secret, metadata = secrets.read_secret_with_custom_metadata(vault_client, "secret", "app/db")

In all those examples, you need to replace "secret" by your kv2 mount point. The "secret" mount point is the default one if you didn't configure Vault.

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

secretsmith-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

secretsmith-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file secretsmith-0.1.0.tar.gz.

File metadata

  • Download URL: secretsmith-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for secretsmith-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5ba364c55e2ea82bb5dcad047aa13926e80751bc10c73df0ed4f90552501a863
MD5 fa2bcb461a3b8feebf7b249e79820c32
BLAKE2b-256 7f18c50a06c2c34cc9ec0e308751a392d89ad295db532dd7517f47f93fa4589e

See more details on using hashes here.

File details

Details for the file secretsmith-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: secretsmith-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for secretsmith-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e873c187a0f26c2c8fae964eb07bbb7a2960107edfc25f87e20cb3f74dc2d43
MD5 79521a1d6b24c1cffb9d6196ad8aa2f1
BLAKE2b-256 0ef4d88888a9b5868fed70d9ea6101d31b8c50a478b5c2e4125ca880c41dc27a

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