Skip to main content

GPG based env file encryptor utility.

Project description

PyEnv-Encrypt

GPG based env file encryptor utility.

PyEnv-Encrypt selectively encrypts and decrypts the fields of config files keeping the keys readable. For example, consider an environment variable file .env with the following values.

SOMEVAR="hello world"
ANOTHERVAR="test"

Running pyenc .env will encrypt the text fields into:

SOMEVAR="!ENC#~ hQGMA8pKDfwTzwbdAQv/c0/3Had47hxV6zuNmkBuOjv3bSGGGWzLGHVAN7ryL3tx =Y8Nr"
ANOTHERVAR="!ENC#~ hQGMA8pKDfwTzwbdAQv+JRBiVJB3rFqjONyXbBuN6pwzfHkHR43rbSIGX0o/B0zU =ljcz"

Features

  • Encryption support for the following file types.
    • .env
    • yaml
    • json
  • Recursive update of all text fields.
  • Automatic decision of encryption or decryption based on file contents.

Installation

To use, clone this repo and install with pip.

git clone git+https://github.com/akhlakm/pyenv-encrypt.git
cd pyenv-encrypt
pip install -e .

Alternatively, use the following for packaging (for example, in your requirements.txt file).

pip install git+https://github.com/akhlakm/pyenv-encrypt.git

Dependencies

The gpg utility must be installed in your system. GPG comes built-in with most versions of Linux OS. For Mac, use homebrew: brew install gpg.

See the official installation instructions for more info. Run the following command to check if GPG is installed.

gpg --version

Python dependencies:

  • python-dotenv
  • pyyaml

Commandline Usage

After installation, use the pyenc command to encrypt or decrypt your config/env files directly from terminal.

pyenc .env

Multiple files can also be processed.

pyenc .env vault.yaml data.json

pyenc will toggle between encryption and decryption. To force encryption or decryption specify -e or -d respectively.

pyenc -e .env vault.yaml data.json

Use As A Python Module

import os
from pyenv_enc import enc

# User ID for GPG
USERID = os.environ.get("USER")

# Check if a encryption key-pair exists for the userid,
# or, create a new one.
enc.check_gpg_pubkey(USERID)

# Encrypt a value
text = "hello world"
encrypted = enc.gpg_encrypt(USERID, text)
print(encrypted)

# Decrypt a value
decrypted = enc.gpg_decrypt(encrypted)
print(decrypted)

# Recursively encrypt the string fields of a dictionary.
# This is useful to encrypt JSON, YAML, TOML files.
mydict = {
    "key1": 1234,
    "key2": "hello world",
    "key3": ["hello", "world"],
    "key4": {"foo": "bar"}
}
encrypted_dict = enc.encrypt_data(USERID, mydict)
print(encrypted_dict)

# Decrypt the dictionary.
print(enc.decrypt_data(encrypted_dict))

About

LICENSE MIT Copyright 2023 Akhlak Mahmood

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

pyenv-encrypt-0.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

pyenv_encrypt-0.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pyenv-encrypt-0.0.1.tar.gz.

File metadata

  • Download URL: pyenv-encrypt-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for pyenv-encrypt-0.0.1.tar.gz
Algorithm Hash digest
SHA256 db5beb09e10a768927e0450ac78ac0f426bf5801a464832781d730a77245a4d3
MD5 f20650855db584cac70e41d14bbc18e3
BLAKE2b-256 86ad1065e9d7030f43636cc56cf57827bb8e42d466b2b1e2ea8231e69a39cb68

See more details on using hashes here.

File details

Details for the file pyenv_encrypt-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pyenv_encrypt-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52959340103895ad1acf152941fa702e7d192cbbb5ad61971dde55d7fa0032b4
MD5 3c187e5529ea68392d501604f0076bc1
BLAKE2b-256 d0695e096ac45bc2f5f760be1bfcee0bd92d28267286dc1959743276948ad713

See more details on using hashes here.

Supported by

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