Load config values from 1Password with this module.
Project description
A convenient way to load your app configuration from 1Password.
This project is licensed under the terms of the MIT license.
Quick Start
import asyncio
import os
from configator import load_config
from pydantic import BaseModel
class DatabaseConfig(BaseModel):
host: str
port: int
username: str
password: str
class AppConfig(BaseModel):
api_key: str
debug: bool
timeout: int
class Config(BaseModel):
db: DatabaseConfig
app: AppConfig
debug: bool = False
async def main():
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
cfg: Config = await load_config(
schema=Config,
token=token,
vault="REPO whatever",
item="whatever-develop",
)
assert cfg.db.port == 5432
asyncio.run(main())
Developer Mode
For local development, you can override configuration values using a .env file by setting the CONFIGATOR_DEV_MODE environment variable:
export CONFIGATOR_DEV_MODE=1
When developer mode is enabled, values are loaded with the following priority (highest to lowest):
.envfile- Environment variables
- 1Password values (via initialization parameters)
Without developer mode, the standard priority applies (1Password values take precedence over environment variables and .env files).
This feature works with the provided common configuration models (PostgresConfig, SentryConfig). For your own config schemas, you can simply extend ConfigatorSettings to get this behavior.
Installation
uv add "git+https://github.com/Utiligize/configator@v3000.0.0"
or, if you like the bleeding edge:
uv add "git+https://github.com/Utiligize/configator"
For information on how to authenticate uv with GitHub, see https://docs.astral.sh/uv/concepts/authentication/git/.
For information on how to use private repos in GitHub Actions, see https://docs.astral.sh/uv/guides/integration/github/#private-repos. If you create a fine-grained access token, it simply needs the "Content" read permission.
Writing Config Classes
Define your app's config as a class deriving from Pydantic's BaseModel. The field names will be matched against the 1Password item field titles, and the values loaded from them. The field names are treated as lower snake case, and item field names in 1Password are converted accordingly when matching. For example, a Python model with a field called sentry_key will match a 1Password item with a field title of SENTRY_KEY or sentry-key. It is therefore important to ensure that field names are unique, at least within sections.
Nested models are loaded from separate sections in the 1Password item. Fields in these nested models can have the same name as fields in other sections. Fields in the base config class are found by name, no matter their section (but the intention is for them to be added without one), so the names of these must be unique in the full model.
Supported Features
- Basic types (
str,int,float, evencomplex) are simply parsed from the string in 1Password. decimal.Decimalis also supported and should usually be preferred overfloat.- Booleans are special: since any string is truthy in Python, a
boolmust have one of 8 (case-insensitive) values:- "true", "1", "yes", and "on" are interpreted as
True. - "false", "0", "no", and "off" are interpreted as
False. - any other value for a field defined as
boolwill raise aValueError.
- "true", "1", "yes", and "on" are interpreted as
- Collections (
dict,list,set) are loaded by interpreting the string value in 1Password as JSON and passing that object to the constructor. This means that a set can be constructed from what looks like a list, for example. - Any string starting with
op://will be resolved recursively (up to a depth of 10 links).
Planned Features
- Providing access to extra fields in the config item when
model_config = ConfigDict(extra='allow')is specified in the input model. See https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.extra.
Unsupported Features
-
Typed collections are sadly not supported, because it confuses the
issubclassmatching of fields. This means that fields in your config model must be defined as e.g. plaindict, notdict[str, str]. -
OptionalandUnionfields are not supported, i.e. you cannot do either offoo: str | None = None bar: Optional[str] baz: int | float
because it confuses the hydrator, who won't know which constructor to call or will try to initialize
None. -
While
defaultvalues are supported,default_factoryis not. -
Basic Python types
bytesandbytearraymay work but are not officially supported.
Development
Setup
uv sync
Lint and Format
just lint
Run Tests
just test
Run Failed Tests
just test-failed
◼️◼️◼️
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file configator_op-3000.3.0.tar.gz.
File metadata
- Download URL: configator_op-3000.3.0.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37e41801323de0b260d49571e8ff7c02d357ee5ab3891e2ec7932c9fb98ef8df
|
|
| MD5 |
e0990e2cd0fa19c8ecc196dce84023e8
|
|
| BLAKE2b-256 |
d3fdde8dc85e79dd986c1d6bf85f70050a1698e045e06d877f0af1da3f68ad7b
|
File details
Details for the file configator_op-3000.3.0-py3-none-any.whl.
File metadata
- Download URL: configator_op-3000.3.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77e8af53db2c5cbcaf3a0fcc3351d640f99cff3691f8956cc9042302f6701a65
|
|
| MD5 |
1cfe729144c3e0aa19a8e087b977289d
|
|
| BLAKE2b-256 |
1450b0a9c161b91895da42aad81997959f481939818f27f9d12fe52818cbff94
|