Sec - Tiny Python library for using secrets
Sec is a tiny Python library for using secrets. Simple to its core, Sec exposes just one function and offers no configurations options.
If you are developing web applications, then by most chances your application uses some sort of "secret" information (e.g. database passwords, API keys etc.) which hopefully 🙏 is not kept into the code base.
Since this kind of information is not kept in the database, it resides in an external place like a file (e.g. /run/secrets/aws-key) or an environment variable (e.g. DATABASE_URL).
All Sec does is provide a single, unique interface for accessing these information from a Python application.
Installation
You can install sec with uv:
uv add sec
For local development, install the project and dev tools with:
uv sync --group dev
Quality checks use Ruff and pytest:
uv run ruff check .
uv run ruff format --check .
uv run pytest
Requirements
Sec requires Python 3.8 (or greater) to work.
API Documentation
load(name, fallback=None)
The load method of Sec attempts to load the contents of a secret, based on a given name, in the following order:
- Load the contents of
/run/secrets/{name}(nameis lowercased here) - Load the contents of the path found in the environment variable
{name}_FILE(nameis uppercased here) - Load the content of the environment variable
{name}(nameis uppercased here) - Load the content of the
.envfile in the current working directory - Return the value of the
fallbackargument if provided, orNone
Quick Start Example
First, let's create some secret files
$ echo "supersecret" > /run/secrets/mystiko
$ export MYSECRET_FILE=/run/secrets/mystiko
$ export ANOTHER_SECRET=hello
$ echo "DATABASE_URL=postgresql://user:password@localhost/app" > .env
Next, let's open up the Python interpreter and load these secrets in our application.
>>> import sec
>>> sec.load('mystiko')
'supersecret'
>>> sec.load('mysecret')
'supersecret'
>>> sec.load('another_secret')
'hello'
>>> sec.load('database_url')
'postgresql://user:password@localhost/app'
The .env support is intentionally dependency-free and understands simple .env
files, including blank lines, comments, export KEY=value entries, quoted
values, and UTF-8 content such as emoji or accented characters.
Use Cases
Docker Swarm Secrets
Docker Secrets lets services running on Docker Swarm get exclusive access to secret information that are encrypted at rest.
Although this feature is amazing, it cannot be used outside of Docker Swarm (e.g. in Docker on your local machine) so developers tend to create hacks and workarounds around this issue.
This is where sec comes into play. The following application code will work the same in production with Docker Secrets and in development with environment variables instead.
import sec
# The following line will work the same in development and production
database_url = sec.load('database_url')
Below you can see the corresponding Docker files that we set up to run the above application.
docker-compose.yml
version: "3.6"
services:
web:
image: company/app
secrets:
- database-url
secrets:
settings:
external:
name: database-url
docker-compose.override.yml
version: "3.6"
services:
web:
build: .
volumes:
- .:/usr/src/app
environment:
DATABASE_URL: postgresql://user:password@postgres
postgres:
image: postgres:latest
secrets:
settings:
external:
name: database-url
License
Sec is MIT Licensed.
Metadata
Release files for sec 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sec-1.0.0.tar.gz | 5.8 kB | Without provenance | Details for sec-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sec-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for sec-1.0.0-py3-none-any.whl |
Total release size: 10.6 kB
Release files / sec-1.0.0.tar.gz
| Download URL | sec-1.0.0.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5289c3efa5944458c59f47952da5e8b50776a2a6883781e75ffe2c58f7071920
|
|
BLAKE2b-256 checksum How to use checksums |
a5fe49e0469204b41acd9b4041bd4df4ab7e42102e7ca670e8d417e5f3897fb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / sec-1.0.0-py3-none-any.whl
| Download URL | sec-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4ad7c3d55276f4a43d90cad47c58571f05041748490064ca07093d0cc69c4dd5
|
|
BLAKE2b-256 checksum How to use checksums |
cc3461e4d39e0d1068974fd8ebfa767bdbfd4ac5b98a052c0a12f46f40074584
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for sec 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.