Skip to main content

pydantic-kms-secrets

PyPI version Upload Python Package codecov

Utility to decrypt and encrypt secrets using AWS KMS keys that also integrates with pydantic models which allows for encrypted values to be stored in .env files and be decrypted at runtime.

Installation

From PyPi:

$ pip install pydantic-kms-secrets

Usage

CLI

Encrypt a secret:

$ pks -k your-kms-key-id -v my-secret-password -e

Decrypt a secret:

$ pks -k your-kms-key-id -v your-encrypted-secret -d

Help docs:

$ pks --help
usage: pks [-h] [-k KEY_ID] -v VALUE [-e] [-d]

Tool to encrypt and decrypt secrets via a KMS key

optional arguments:
  -h, --help            show this help message and exit
  -k KEY_ID, --key-id KEY_ID
                        ID of the KMS key to use
  -v VALUE, --value VALUE
                        The value to be encrypted
  -e, --encrypt         Set to encrypt value
  -d, --decrypt         Set to decrypt value

Pydantic

Pydantic KMS Secrets is able to integrate and add functionality on top of Pydantic's dotenv extension by allowing you to store encrypted values in your .env files and decrypt them at runtime. A basic implementation would look something like:

Pydantic Settings Model

from pydantic import BaseSettings
from pydantic_kms_secrets import KMSSecretStr, decrypt_kms_secrets


class Settings(BaseSettings):
    env: str
    secrets_kms_key_id: str  # This model attribute must exist to decrypt secrets
    kms_secret_1: KMSSecretStr
    kms_secret_2: KMSSecretStr

    class Config:
        env_file = ".env"

# Don't forget to call decrypt_kms_secrets, if you don't the secrets will not be decrypted
settings = decrypt_kms_secrets(Settings())

.env File

ENV="prod"

SECRETS_KMS_KEY_ID="your-kms-key-id"  # This environment variable must be set to decrypt secrets
KMS_SECRET_1="my-first-encrypted-secret"
KMS_SECRET_2="my-second-encrypted-secret"

KMSSecretStr Class

The KMSSecretStr class is almost identical to the SecretStr type in pydantic.

# This example uses the settings value from the python example above

# Standard access methods will not display the secret
print(settings)
#> env='prod' secrets_kms_key_id='your-kms-key-id' kms_secret_1=KMSSecretStr('**********') kms_secret_2=KMSSecretStr('**********')
print(settings.kms_secret_1)
#> **********
print(settings.dict())
"""
{
    'env': 'prod',
    'secret_kms_key_id': 'your-kms-key-id',
    'kms_secret_1': KMSSecretStr('**********'),
    'kms_secret_2': KMSSecretStr('**********'),
}
"""
print(settings.json())
#> {"env": "prod", "secret_kms_key_id": "your-kms-key-id", "kms_secret_1": "**********", "kms_secret_2": "**********"}

# Use get_secret_value method to see the secret's content.
print(settings.kms_secret_1.get_secret_value())
#> my-first-encrypted-secret
print(settings.kms_secret_2.get_secret_value())
#> my-second-encrypted-secret

Metadata

Release files for pydantic-kms-secrets 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-kms-secrets 0.3.0
File Size Uploaded
pydantic_kms_secrets-0.3.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-kms-secrets 0.3.0
File Interpreter ABI Platform
pydantic_kms_secrets-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.5 kB

Release files / pydantic_kms_secrets-0.3.0.tar.gz

Download URL pydantic_kms_secrets-0.3.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f1f84c574198f7defb1cb378839de7fbad4386154e6af1f284fe9a884cf3c455
BLAKE2b-256 checksum
How to use checksums
8ef426541c4916a0b571997e90db31653d1152e50ebd36b06002a603306aa283
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.11

Release files / pydantic_kms_secrets-0.3.0-py3-none-any.whl

Download URL pydantic_kms_secrets-0.3.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1e746320332a85f4a7b64bb005377a70df5153f40e40ad4ba0a715d996bf96b
BLAKE2b-256 checksum
How to use checksums
94c58b4ee3cddccd69ce4670c591d0cca024d2ceb8d1d5e3726eb84de2582c5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.11

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page