credchain 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- credchain-0.1.0/.gitattributes +10 -0
- credchain-0.1.0/.github/workflows/publish-test.yml +40 -0
- credchain-0.1.0/.github/workflows/publish.yml +42 -0
- credchain-0.1.0/.github/workflows/run-static.yml +58 -0
- credchain-0.1.0/.github/workflows/run-tests.yml +51 -0
- credchain-0.1.0/.gitignore +24 -0
- credchain-0.1.0/.pre-commit-config.yaml +41 -0
- credchain-0.1.0/CONTRIBUTING.md +124 -0
- credchain-0.1.0/LICENSE +21 -0
- credchain-0.1.0/PKG-INFO +160 -0
- credchain-0.1.0/README.md +148 -0
- credchain-0.1.0/credchain/__init__.py +38 -0
- credchain-0.1.0/credchain/chain.py +38 -0
- credchain-0.1.0/credchain/env.py +50 -0
- credchain-0.1.0/credchain/file.py +65 -0
- credchain-0.1.0/credchain/keyring.py +115 -0
- credchain-0.1.0/credchain/protocol.py +5 -0
- credchain-0.1.0/credchain/py.typed +0 -0
- credchain-0.1.0/pyproject.toml +91 -0
- credchain-0.1.0/tests/__init__.py +0 -0
- credchain-0.1.0/tests/test_chain.py +47 -0
- credchain-0.1.0/tests/test_env.py +36 -0
- credchain-0.1.0/tests/test_file.py +41 -0
- credchain-0.1.0/tests/test_keyring.py +110 -0
- credchain-0.1.0/tests/test_no_secrets_in_logs.py +64 -0
- credchain-0.1.0/uv.lock +1080 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Store and check out text files as LF on every platform, overriding a local
|
|
2
|
+
# core.autocrlf=true that would otherwise produce phantom EOL-only diffs.
|
|
3
|
+
# text=auto leaves detected binaries untouched; eol=lf pins the *working tree*.
|
|
4
|
+
* text=auto eol=lf
|
|
5
|
+
|
|
6
|
+
# cmd.exe reads batch files by byte offset, so LF-only endings break labels
|
|
7
|
+
# and goto. Do NOT add *.ps1 here: PowerShell is cross-platform, and CRLF
|
|
8
|
+
# breaks the shebang of a .ps1 meant to run on Linux/macOS.
|
|
9
|
+
*.bat text eol=crlf
|
|
10
|
+
*.cmd text eol=crlf
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
name: Publish to TestPyPI
|
|
2
|
+
on:
|
|
3
|
+
workflow_dispatch:
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
test-pypi-publish:
|
|
7
|
+
name: Upload to TestPyPI
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
permissions:
|
|
10
|
+
id-token: write # For PyPI's trusted publishing
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout
|
|
13
|
+
uses: actions/checkout@v5
|
|
14
|
+
with:
|
|
15
|
+
fetch-depth: 0
|
|
16
|
+
fetch-tags: true
|
|
17
|
+
|
|
18
|
+
- name: Install uv
|
|
19
|
+
uses: astral-sh/setup-uv@v6
|
|
20
|
+
|
|
21
|
+
- name: Build
|
|
22
|
+
run: uv build
|
|
23
|
+
|
|
24
|
+
- name: Check version format
|
|
25
|
+
run: |
|
|
26
|
+
# Extract version from the built package
|
|
27
|
+
VERSION=$(ls dist/*.whl | head -n1 | sed -E 's/.*-([0-9].*)-.*/\1/')
|
|
28
|
+
echo "Package version: $VERSION"
|
|
29
|
+
|
|
30
|
+
# Check if version contains dev/post markers (development version)
|
|
31
|
+
if [[ "$VERSION" =~ (dev|post.*dev) ]]; then
|
|
32
|
+
echo "❌ ERROR: Cannot publish development version: $VERSION"
|
|
33
|
+
echo "Please create a proper Git tag first (e.g., 'git tag v0.1.0')"
|
|
34
|
+
exit 1
|
|
35
|
+
fi
|
|
36
|
+
|
|
37
|
+
echo "✅ Version $VERSION is valid for publishing"
|
|
38
|
+
|
|
39
|
+
- name: Publish to TestPyPI
|
|
40
|
+
run: uv publish --publish-url https://test.pypi.org/legacy/ -v dist/*
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
on:
|
|
3
|
+
release:
|
|
4
|
+
types: [published]
|
|
5
|
+
workflow_dispatch:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
pypi-publish:
|
|
9
|
+
name: Upload to PyPI
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
permissions:
|
|
12
|
+
id-token: write # For PyPI's trusted publishing
|
|
13
|
+
steps:
|
|
14
|
+
- name: Checkout
|
|
15
|
+
uses: actions/checkout@v5
|
|
16
|
+
with:
|
|
17
|
+
fetch-depth: 0
|
|
18
|
+
fetch-tags: true
|
|
19
|
+
|
|
20
|
+
- name: Install uv
|
|
21
|
+
uses: astral-sh/setup-uv@v6
|
|
22
|
+
|
|
23
|
+
- name: Build
|
|
24
|
+
run: uv build
|
|
25
|
+
|
|
26
|
+
- name: Check version format
|
|
27
|
+
run: |
|
|
28
|
+
# Extract version from the built package
|
|
29
|
+
VERSION=$(ls dist/*.whl | head -n1 | sed -E 's/.*-([0-9].*)-.*/\1/')
|
|
30
|
+
echo "Package version: $VERSION"
|
|
31
|
+
|
|
32
|
+
# Check if version contains dev/post markers (development version)
|
|
33
|
+
if [[ "$VERSION" =~ (dev|post.*dev) ]]; then
|
|
34
|
+
echo "❌ ERROR: Cannot publish development version: $VERSION"
|
|
35
|
+
echo "Please create a proper Git tag first (e.g., 'git tag v0.1.0')"
|
|
36
|
+
exit 1
|
|
37
|
+
fi
|
|
38
|
+
|
|
39
|
+
echo "✅ Version $VERSION is valid for publishing"
|
|
40
|
+
|
|
41
|
+
- name: Publish to PyPI
|
|
42
|
+
run: uv publish -v dist/*
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
name: Run static analysis
|
|
2
|
+
|
|
3
|
+
env:
|
|
4
|
+
# Enable colored output
|
|
5
|
+
PY_COLORS: 1
|
|
6
|
+
|
|
7
|
+
on:
|
|
8
|
+
push:
|
|
9
|
+
branches: ["main"]
|
|
10
|
+
paths:
|
|
11
|
+
- "src/**"
|
|
12
|
+
- "tests/**"
|
|
13
|
+
- "uv.lock"
|
|
14
|
+
- "pyproject.toml"
|
|
15
|
+
- ".github/workflows/**"
|
|
16
|
+
|
|
17
|
+
# Run on all pull requests because these checks are required and will block merges otherwise
|
|
18
|
+
pull_request:
|
|
19
|
+
|
|
20
|
+
workflow_dispatch:
|
|
21
|
+
|
|
22
|
+
permissions:
|
|
23
|
+
contents: read
|
|
24
|
+
|
|
25
|
+
jobs:
|
|
26
|
+
static_analysis:
|
|
27
|
+
timeout-minutes: 2
|
|
28
|
+
|
|
29
|
+
runs-on: ubuntu-latest
|
|
30
|
+
|
|
31
|
+
steps:
|
|
32
|
+
- uses: actions/checkout@v5
|
|
33
|
+
- name: Install uv
|
|
34
|
+
uses: astral-sh/setup-uv@v6
|
|
35
|
+
with:
|
|
36
|
+
enable-cache: true
|
|
37
|
+
cache-dependency-glob: "uv.lock"
|
|
38
|
+
|
|
39
|
+
- name: Install dependencies
|
|
40
|
+
run: uv sync
|
|
41
|
+
|
|
42
|
+
- name: Check lockfile is up to date
|
|
43
|
+
run: |
|
|
44
|
+
if ! uv lock --check; then
|
|
45
|
+
echo "❌ Lockfile is out of date!"
|
|
46
|
+
echo "To update the lockfile, run 'uv lock'."
|
|
47
|
+
exit 1
|
|
48
|
+
fi
|
|
49
|
+
echo "✅ Lockfile is up to date"
|
|
50
|
+
|
|
51
|
+
- name: Lint
|
|
52
|
+
run: uv run ruff check src tests
|
|
53
|
+
|
|
54
|
+
- name: Check formatting
|
|
55
|
+
run: uv run ruff format --check src tests
|
|
56
|
+
|
|
57
|
+
- name: Type checking
|
|
58
|
+
run: uv run ty check src
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
name: Run tests
|
|
2
|
+
|
|
3
|
+
env:
|
|
4
|
+
# Enable colored output
|
|
5
|
+
PY_COLORS: 1
|
|
6
|
+
|
|
7
|
+
on:
|
|
8
|
+
push:
|
|
9
|
+
branches: ["main"]
|
|
10
|
+
paths:
|
|
11
|
+
- "src/**"
|
|
12
|
+
- "tests/**"
|
|
13
|
+
- "uv.lock"
|
|
14
|
+
- "pyproject.toml"
|
|
15
|
+
- ".github/workflows/**"
|
|
16
|
+
|
|
17
|
+
# Run on all pull requests because these checks are required and will block merges otherwise
|
|
18
|
+
pull_request:
|
|
19
|
+
|
|
20
|
+
workflow_dispatch:
|
|
21
|
+
|
|
22
|
+
permissions:
|
|
23
|
+
contents: read
|
|
24
|
+
|
|
25
|
+
jobs:
|
|
26
|
+
run_tests:
|
|
27
|
+
name: "Run tests: Python ${{ matrix.python-version }} on ${{ matrix.os }}"
|
|
28
|
+
runs-on: ${{ matrix.os }}
|
|
29
|
+
strategy:
|
|
30
|
+
matrix:
|
|
31
|
+
os: [ubuntu-latest, windows-latest]
|
|
32
|
+
python-version: ["3.10"]
|
|
33
|
+
fail-fast: false
|
|
34
|
+
timeout-minutes: 5
|
|
35
|
+
|
|
36
|
+
steps:
|
|
37
|
+
- uses: actions/checkout@v5
|
|
38
|
+
|
|
39
|
+
- name: Install uv
|
|
40
|
+
uses: astral-sh/setup-uv@v6
|
|
41
|
+
with:
|
|
42
|
+
enable-cache: true
|
|
43
|
+
cache-dependency-glob: "uv.lock"
|
|
44
|
+
python-version: ${{ matrix.python-version }}
|
|
45
|
+
|
|
46
|
+
- name: Install dependencies
|
|
47
|
+
# Run with frozen to use the current lockfile; static checks will determine if it needs updating
|
|
48
|
+
run: uv sync --frozen
|
|
49
|
+
|
|
50
|
+
- name: Run tests
|
|
51
|
+
run: uv run pytest
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Python-generated files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[oc]
|
|
4
|
+
build/
|
|
5
|
+
dist/
|
|
6
|
+
wheels/
|
|
7
|
+
*.egg-info
|
|
8
|
+
|
|
9
|
+
# Virtual environments
|
|
10
|
+
.venv/
|
|
11
|
+
|
|
12
|
+
# Log files
|
|
13
|
+
*.log
|
|
14
|
+
|
|
15
|
+
# Coverage reports
|
|
16
|
+
.coverage
|
|
17
|
+
htmlcov/
|
|
18
|
+
.pytest_cache/
|
|
19
|
+
|
|
20
|
+
# VSCode settings
|
|
21
|
+
.vscode/PythonImportHelper-v2-Completion.json
|
|
22
|
+
|
|
23
|
+
# Claude Code local settings (tool permissions, personal overrides — not shared)
|
|
24
|
+
.claude/settings.local.json
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
fail_fast: false
|
|
2
|
+
|
|
3
|
+
repos:
|
|
4
|
+
- repo: https://github.com/abravalheri/validate-pyproject
|
|
5
|
+
rev: v0.24.1
|
|
6
|
+
hooks:
|
|
7
|
+
- id: validate-pyproject
|
|
8
|
+
|
|
9
|
+
- repo: https://github.com/pre-commit/mirrors-prettier
|
|
10
|
+
rev: v3.1.0
|
|
11
|
+
hooks:
|
|
12
|
+
- id: prettier
|
|
13
|
+
types_or: [yaml, json]
|
|
14
|
+
|
|
15
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
16
|
+
# Ruff version.
|
|
17
|
+
rev: v0.15.12
|
|
18
|
+
hooks:
|
|
19
|
+
# Run the linter.
|
|
20
|
+
- id: ruff-check
|
|
21
|
+
args: [--fix, --exit-non-zero-on-fix]
|
|
22
|
+
# Run the formatter.
|
|
23
|
+
- id: ruff-format
|
|
24
|
+
|
|
25
|
+
- repo: local
|
|
26
|
+
hooks:
|
|
27
|
+
- id: ty
|
|
28
|
+
name: ty check
|
|
29
|
+
entry: uv run ty check
|
|
30
|
+
language: system
|
|
31
|
+
types: [python]
|
|
32
|
+
files: ^src/|^tests/
|
|
33
|
+
pass_filenames: false
|
|
34
|
+
require_serial: true
|
|
35
|
+
|
|
36
|
+
- repo: https://github.com/codespell-project/codespell
|
|
37
|
+
rev: v2.4.1
|
|
38
|
+
hooks:
|
|
39
|
+
- id: codespell # See pyproject.toml for args
|
|
40
|
+
additional_dependencies:
|
|
41
|
+
- tomli
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Contributing <!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
Thank you for your interest in contributing to `credchain`! This document
|
|
4
|
+
provides guidelines for setting up a development environment, making changes,
|
|
5
|
+
and testing them locally before pushing to GitHub.
|
|
6
|
+
|
|
7
|
+
- [Development Setup](#development-setup)
|
|
8
|
+
- [Prerequisites](#prerequisites)
|
|
9
|
+
- [Clone and install](#clone-and-install)
|
|
10
|
+
- [Code Quality](#code-quality)
|
|
11
|
+
- [Enable automatic execution on git commit](#enable-automatic-execution-on-git-commit)
|
|
12
|
+
- [Manual execution](#manual-execution)
|
|
13
|
+
- [Testing](#testing)
|
|
14
|
+
- [Release Process](#release-process)
|
|
15
|
+
- [Testing a release first](#testing-a-release-first)
|
|
16
|
+
- [Building Packages Locally](#building-packages-locally)
|
|
17
|
+
- [Dependency Management](#dependency-management)
|
|
18
|
+
|
|
19
|
+
## Development Setup
|
|
20
|
+
|
|
21
|
+
### Prerequisites
|
|
22
|
+
|
|
23
|
+
- [Python 3.10](https://www.python.org/downloads) or later
|
|
24
|
+
- [uv](https://docs.astral.sh/uv/) — fast Python package manager
|
|
25
|
+
|
|
26
|
+
### Clone and install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
git clone https://github.com/stephaneberle9/python-credchain.git
|
|
30
|
+
cd python-credchain
|
|
31
|
+
uv sync
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Code Quality
|
|
35
|
+
|
|
36
|
+
This project uses `pre-commit` hooks for static checks to maintain high code
|
|
37
|
+
quality standards:
|
|
38
|
+
|
|
39
|
+
| Hook | Purpose |
|
|
40
|
+
| ---- | ------- |
|
|
41
|
+
| `ruff-check` | Python linting (with auto-fix) |
|
|
42
|
+
| `ruff-format` | Python code formatting |
|
|
43
|
+
|
|
44
|
+
### Enable automatic execution on git commit
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
uv run pre-commit install
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Manual execution
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# Run all checks on all files
|
|
54
|
+
uv run pre-commit run --all-files
|
|
55
|
+
|
|
56
|
+
# Run individual tools
|
|
57
|
+
uv run ruff format src tests # Code formatting
|
|
58
|
+
uv run ruff check --fix src tests # Linting with auto-fix
|
|
59
|
+
uv run ty check src # Type checking
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Testing
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Run all tests with coverage
|
|
66
|
+
uv run pytest
|
|
67
|
+
|
|
68
|
+
# Run a specific test file
|
|
69
|
+
uv run pytest tests/test_chain.py
|
|
70
|
+
|
|
71
|
+
# Run with verbose output
|
|
72
|
+
uv run pytest -v
|
|
73
|
+
|
|
74
|
+
# Show only a coverage summary (skip HTML report)
|
|
75
|
+
uv run pytest --cov-report=term-missing --no-cov-on-fail
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Coverage is collected from `src/credchain` and an HTML report is written
|
|
79
|
+
to `htmlcov/`.
|
|
80
|
+
|
|
81
|
+
## Release Process
|
|
82
|
+
|
|
83
|
+
1. Ensure all changes are committed and the `main` branch is up to date.
|
|
84
|
+
|
|
85
|
+
2. Create and push a version tag:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
git tag v0.1.0
|
|
89
|
+
git push origin v0.1.0
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
3. Create a GitHub release from the tag and add release notes. The
|
|
93
|
+
`publish.yml` workflow triggers automatically and uploads the package to
|
|
94
|
+
PyPI.
|
|
95
|
+
|
|
96
|
+
The package version is derived automatically from the git tag by
|
|
97
|
+
[uv-dynamic-versioning](https://github.com/nicoddemus/uv-dynamic-versioning).
|
|
98
|
+
|
|
99
|
+
### Testing a release first
|
|
100
|
+
|
|
101
|
+
To publish to TestPyPI before a real release, trigger the `publish-test.yml`
|
|
102
|
+
workflow manually from the **Actions** tab on GitHub (select **Run workflow**).
|
|
103
|
+
|
|
104
|
+
## Building Packages Locally
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
uv build
|
|
108
|
+
ls dist/
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
This produces both a wheel (`.whl`) and a source distribution (`.tar.gz`).
|
|
112
|
+
|
|
113
|
+
## Dependency Management
|
|
114
|
+
|
|
115
|
+
Dependencies are pinned in `uv.lock` for reproducible installs. After adding or
|
|
116
|
+
changing dependencies in `pyproject.toml`:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
uv lock # update the lockfile
|
|
120
|
+
uv sync # sync your environment to the new lockfile
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
> [!IMPORTANT]
|
|
124
|
+
> Always commit both `pyproject.toml` and `uv.lock` together.
|
credchain-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stephan Eberle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
credchain-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: credchain
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Credential provider chain: read usernames, passwords and tokens from environment variables, the OS keychain or secret files, with automatic fallback.
|
|
5
|
+
Author-email: Stephan Eberle <stephaneberle9@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Provides-Extra: keyring
|
|
10
|
+
Requires-Dist: keyring>=25.0.0; extra == 'keyring'
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# credchain <!-- omit in toc -->
|
|
14
|
+
|
|
15
|
+
Read the credentials an application needs — a username and password, or an
|
|
16
|
+
API token — from environment variables, the OS keychain or secret files, with
|
|
17
|
+
automatic fallback through a priority chain.
|
|
18
|
+
|
|
19
|
+
`credchain` has no framework dependency. FastMCP servers that also take
|
|
20
|
+
credentials from HTTP headers use
|
|
21
|
+
[fastmcp-creds](https://github.com/stephaneberle9/fastmcp-creds), which builds
|
|
22
|
+
on it.
|
|
23
|
+
|
|
24
|
+
- [Installation](#installation)
|
|
25
|
+
- [Usage](#usage)
|
|
26
|
+
- [Credential provider chain](#credential-provider-chain)
|
|
27
|
+
- [Environment variables](#environment-variables)
|
|
28
|
+
- [OS keychain](#os-keychain)
|
|
29
|
+
- [Secret files](#secret-files)
|
|
30
|
+
- [Logging](#logging)
|
|
31
|
+
- [API Reference](#api-reference)
|
|
32
|
+
- [`CredentialsProvider`](#credentialsprovider)
|
|
33
|
+
- [`CredentialsProviderChain`](#credentialsproviderchain)
|
|
34
|
+
- [`EnvironmentCredentialsProvider`](#environmentcredentialsprovider)
|
|
35
|
+
- [`KeyringCredentialsProvider`](#keyringcredentialsprovider)
|
|
36
|
+
- [`FileCredentialsProvider`](#filecredentialsprovider)
|
|
37
|
+
- [Contributing](#contributing)
|
|
38
|
+
- [License](#license)
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
The package is not published to PyPI yet. Install it from its Git repository
|
|
43
|
+
or a local checkout:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
uv add "credchain @ git+https://github.com/stephaneberle9/python-credchain.git"
|
|
47
|
+
# with the OS keychain provider:
|
|
48
|
+
uv add "credchain[keyring] @ git+https://github.com/stephaneberle9/python-credchain.git"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Requires Python 3.10+.
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
### Credential provider chain
|
|
56
|
+
|
|
57
|
+
Compose providers in priority order. The chain tries each in sequence and
|
|
58
|
+
returns the first result where both username and password (or token) are
|
|
59
|
+
non-empty.
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from credchain import CredentialsProviderChain, EnvironmentCredentialsProvider
|
|
63
|
+
from credchain.keyring import KeyringCredentialsProvider # optional dep
|
|
64
|
+
|
|
65
|
+
chain = CredentialsProviderChain([
|
|
66
|
+
EnvironmentCredentialsProvider("MY_USERNAME", "MY_PASSWORD"),
|
|
67
|
+
KeyringCredentialsProvider("my-service"),
|
|
68
|
+
])
|
|
69
|
+
|
|
70
|
+
username, password = chain.get_credentials()
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
A provider that raises is logged (exception type only) and skipped.
|
|
74
|
+
|
|
75
|
+
### Environment variables
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from credchain import EnvironmentCredentialsProvider
|
|
79
|
+
|
|
80
|
+
provider = EnvironmentCredentialsProvider("MY_USERNAME", "MY_PASSWORD")
|
|
81
|
+
token_provider = EnvironmentCredentialsProvider.for_token("MY_API_TOKEN")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### OS keychain
|
|
85
|
+
|
|
86
|
+
Reads credentials stored in the OS keychain via
|
|
87
|
+
[`keyring`](https://pypi.org/project/keyring/). Requires `credchain[keyring]`.
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from credchain.keyring import KeyringCredentialsProvider
|
|
91
|
+
|
|
92
|
+
provider = KeyringCredentialsProvider("my-service") # "username" + "password" entries
|
|
93
|
+
provider = KeyringCredentialsProvider.for_token("my-service") # one "token" entry
|
|
94
|
+
provider = KeyringCredentialsProvider("my-service", timeout=10)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The keychain lookup is bounded by a timeout (default 5 seconds), so a locked
|
|
98
|
+
or unresponsive keychain, e.g. one waiting for an unlock prompt in a headless
|
|
99
|
+
session, cannot hang the caller. On timeout, a warning is logged and
|
|
100
|
+
`(None, None)` is returned, so a chain falls through to its next provider.
|
|
101
|
+
|
|
102
|
+
### Secret files
|
|
103
|
+
|
|
104
|
+
Reads one value per file, such as a secret mounted into a container. One
|
|
105
|
+
trailing line break is removed; a missing file means no credentials.
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from credchain import FileCredentialsProvider
|
|
109
|
+
|
|
110
|
+
provider = FileCredentialsProvider("/run/secrets/username", "/run/secrets/password")
|
|
111
|
+
token_provider = FileCredentialsProvider.for_token("/run/secrets/api-token")
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Logging
|
|
115
|
+
|
|
116
|
+
Providers log at debug level where they looked and whether they found
|
|
117
|
+
something, never a credential or any part of one: in token mode the token is
|
|
118
|
+
returned in both positions, so even a "username" can be a secret. Errors are
|
|
119
|
+
logged with their exception type only, because messages may quote values.
|
|
120
|
+
|
|
121
|
+
## API Reference
|
|
122
|
+
|
|
123
|
+
### `CredentialsProvider`
|
|
124
|
+
|
|
125
|
+
A `Protocol`: any object with a
|
|
126
|
+
`get_credentials() -> tuple[str | None, str | None]` method satisfies it.
|
|
127
|
+
|
|
128
|
+
### `CredentialsProviderChain`
|
|
129
|
+
|
|
130
|
+
`CredentialsProviderChain(providers)` — tries each provider in order and
|
|
131
|
+
returns the first result where both values are non-empty, else
|
|
132
|
+
`(None, None)`.
|
|
133
|
+
|
|
134
|
+
### `EnvironmentCredentialsProvider`
|
|
135
|
+
|
|
136
|
+
`EnvironmentCredentialsProvider(username_env_var_name, password_env_var_name)`
|
|
137
|
+
reads two environment variables;
|
|
138
|
+
`EnvironmentCredentialsProvider.for_token(token_env_var_name)` reads one and
|
|
139
|
+
returns it as both elements.
|
|
140
|
+
|
|
141
|
+
### `KeyringCredentialsProvider`
|
|
142
|
+
|
|
143
|
+
`KeyringCredentialsProvider(service, username_key="username", password_key="password", timeout=5.0)`
|
|
144
|
+
reads two keychain entries of `service`;
|
|
145
|
+
`KeyringCredentialsProvider.for_token(service, token_key="token", timeout=5.0)`
|
|
146
|
+
reads one. `timeout=None` waits indefinitely. Requires `credchain[keyring]`.
|
|
147
|
+
|
|
148
|
+
### `FileCredentialsProvider`
|
|
149
|
+
|
|
150
|
+
`FileCredentialsProvider(username_file, password_file)` reads two files;
|
|
151
|
+
`FileCredentialsProvider.for_token(token_file)` reads one. Errors other than a
|
|
152
|
+
missing file propagate.
|
|
153
|
+
|
|
154
|
+
## Contributing
|
|
155
|
+
|
|
156
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
MIT — see [LICENSE](LICENSE).
|