Skip to main content

Configator logo depicting a cool gator

A convenient way to load your app configuration from 1Password.

Ruff CI status Quality Gate Status Test Coverage Lines of Code

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):

  1. .env file
  2. Environment variables
  3. 1Password values (via initialization parameters)

Without developer mode, the standard priority applies (1Password values take precedence over environment variables and .env files).

⚠️ Never enable developer mode in production. A stray .env file would silently override vetted secrets. Configator guards against this: if CONFIGATOR_DEV_MODE is set while ENVIRONMENT (or, as a fallback, APP_ENV) resolves to production — any value case-insensitively starting with product, e.g. product or production — instantiating a config model raises a RuntimeError, as does load_config() with a ConfigatorSettings schema, before any 1Password request. Production deployments must ensure CONFIGATOR_DEV_MODE is unset and that no .env file ships in production images.

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, even complex) are simply parsed from the string in 1Password.

  • decimal.Decimal is also supported and should usually be preferred over float.

  • Booleans are special: since any string is truthy in Python, a bool must 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 bool will raise a ValueError.
  • Collections (dict, list, set, tuple) 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. Typed collections such as list[str] or dict[str, int] work too; Pydantic validates the item types.

  • Literal fields with str or int members load the member whose string form equals the value in 1Password, so level: Literal[1, 2] reads "2" as 2. Any other value is a ConfigInvalidError naming the allowed members. Literals with members of other types (e.g. bool or enum members) are refused.

  • NewType and type statement aliases (e.g. UserId = NewType("UserId", str), type Port = int) load as the type they stand for. Parameterized aliases (type Pair[T] = tuple[T, T]) are not supported.

  • Optional and union fields (str | None, Optional[int], int | str) are supported:

    • An optional field loads exactly like its inner type when the value in 1Password is non-empty.
    • An empty value in 1Password makes an optional field None, even if the schema gives it a non-None default. This lets the 1Password item switch a setting off. A field that is absent from the item still falls back to its default, and without a default it is still a ConfigInvalidError.
    • A union of several types tries them in the order they are declared and takes the first that parses, using the same parsing rules as a plain field of that type (JSON for collections, the 8 boolean strings for bool, member matching for Literal). The parsed value must also pass that member's own validation, so list[int] | list[str] reads ["1"] as [1] and ["a"] as ["a"], and Annotated[int, Field(gt=0)] | str reads "-1" as "-1". Put the most specific type first: port: int | str reads "8080" as 8080, but port: str | int reads it as "8080", because a str accepts anything.
    • If no member parses the value, loading fails with a ConfigInvalidError naming the types tried.
    class AppConfig(BaseModel):
        timeout: int | None = 30  # absent: 30, empty: None, "60": 60
        hosts: list[str] | None = None  # '["a", "b"]': ["a", "b"]
        port: int | str = 8080  # "9000": 9000, "http": "http"
    
  • Any string starting with op:// will be resolved recursively (up to a depth of 10 links). All references in the config item are resolved together, one batched request per level of nesting, so a schema with many referencing fields costs a handful of 1Password requests rather than one per field. The request count for each load is emitted as an info log line.

  • Every 1Password call is retried individually on transient failures (3 attempts, with no further attempt scheduled more than 10 seconds after the first), so a retry costs one request rather than a full reload. The cap bounds retrying, not a single hung request — the SDK exposes no request timeout. Rate-limit errors are never retried: they are logged and raised immediately, because the hourly read budget can be up to an hour from resetting and each retry would spend a request against a budget that is already empty.

Planned Features

Unsupported Features

  • A nested model (section) inside a union, e.g. database: DatabaseConfig | None, is not supported and fails with a ConfigInvalidError, because a section is either in the 1Password item or not.
  • While default values are supported, default_factory is not.
  • Basic Python types bytes and bytearray may work but are not officially supported.

PostgreSQL

PostgresConfig reads the libpq-style fields PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE, PGSSLMODE (default require) and PGSSLROOTCERT (default unset). dsn() builds the connection string; dsn_redacted() builds the same string with the password masked, for logging.

PGSSLROOTCERT names the CA certificates to trust, which verify-ca and verify-full need:

  • Unset or empty: the DSN carries only sslmode.
  • A file path: added as sslrootcert=<path>, URL-encoded. Configator does not check that the file exists; the driver reports a missing file.
  • system: the OS CA bundle. Configator resolves it to a path, because asyncpg takes system as a file name, unlike libpq 16+. It uses SSL_CERT_FILE if that is non-empty, else the first of /etc/ssl/certs/ca-certificates.crt (Debian, Ubuntu), /etc/pki/tls/certs/ca-bundle.crt (RHEL) and /etc/ssl/cert.pem (Alpine, macOS) that is a regular file. If none does, dsn() and dsn_redacted() raise ConfigInvalidError naming the paths tried.
from configator.models import PostgresConfig

cfg = PostgresConfig(PGHOST="db.example.com", PGSSLMODE="verify-full", PGSSLROOTCERT="system")
cfg.dsn_redacted()
# 'postgresql://postgres:**********@db.example.com:5432/postgres?sslmode=verify-full&sslrootcert=/etc/ssl/certs/ca-certificates.crt'

Error Handling

Every failure load_config reports about 1Password or the config item derives from ConfigatorError, so one except clause catches them all — the developer-mode production guard being the one deliberate exception, described at the end of this section. Below the base sit two types that answer the question a caller has to make a decision on — is the config wrong, or is 1Password simply not answering?

Exception Meaning What a caller should do
ConfigUnavailableError 1Password could not be reached or would not answer: authentication failure, network or TLS error, rate limiting, an empty vault or item listing, or a reference that could not be resolved. The config that is there may well be fine. A service that keeps a last-good config snapshot may boot from it rather than fail.
ConfigInvalidError The item was read, but does not fit the schema: a field with no value and no default, a value that will not parse as its annotated type, a field type Configator cannot load, malformed JSON in a collection field, a Pydantic validation failure, or a reference chain deeper than 10 links. Fail loudly. Retrying and falling back to an older snapshot both serve stale config over a real, unfixed error; someone has to correct the 1Password item or the schema.

An empty vault or item listing counts as unavailable rather than invalid, because a de-permissioned or rotated service-account token looks exactly like a vault that is not there.

Where a failure originates in an underlying exception — an SDK error, a parse failure, a Pydantic validation error — it is chained as __cause__, so the original message and traceback survive. Failures Configator detects itself have no __cause__: a vault or item absent from a listing, a required field with no value, a reference the response omits, and the depth guard all raise on their own. Treat __cause__ as optional:

from configator import ConfigInvalidError, ConfigUnavailableError, load_config

try:
    cfg = await load_config(schema=Config, token=token, vault=vault, item=item)
except ConfigUnavailableError as exc:
    log.warning("1Password unavailable, booting from snapshot: %s", exc.__cause__ or exc)
    cfg = load_snapshot()
except ConfigInvalidError:
    log.exception("config item does not fit the schema")
    raise

The developer-mode production guard described above is deliberately not part of this hierarchy: it still raises a plain RuntimeError, because it is a refusal to start rather than a report about the config item.

Development

Setup

uv sync

Lint and Format

just lint

Run Tests

just test

Run Failed Tests

just test-failed

◼️◼️◼️

Metadata

Release files for configator-op 3000.9.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 configator-op 3000.9.0
File Size Uploaded Provenance Actions
configator_op-3000.9.0.tar.gz 41.0 kB Without provenance Details for configator_op-3000.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for configator-op 3000.9.0
File Interpreter ABI Platform Actions
configator_op-3000.9.0-py3-none-any.whl Python 3 none any Details for configator_op-3000.9.0-py3-none-any.whl

Total release size: 60.5 kB

Release files / configator_op-3000.9.0.tar.gz

Download URL configator_op-3000.9.0.tar.gz
Size 41.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2afbb9d27d3e9e2be9129a094164d470770a2db0d55b2f6cb22043929065d191
BLAKE2b-256 checksum
How to use checksums
7d7c60ceab46c034da08e6a50cd10b304375aa4e312f2f6411a7796ddbae0eba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / configator_op-3000.9.0-py3-none-any.whl

Download URL configator_op-3000.9.0-py3-none-any.whl
Size 19.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81f3aaddc7bea72b1706e2cc16c915feb96a5e89c6271e1a7b65ccf0be2f4db5
BLAKE2b-256 checksum
How to use checksums
05edf3aa3f8d421d9ffa9aceee55b585b2906b3f090d5f4ca0b4144c40fe7cda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for configator-op 3000.9.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.

Release history Release notifications | RSS feed

This release

3000.9.0 This release

2 release files No provenance

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