Unified Kubernetes authentication toolkit - supports KubeConfig, In-Cluster, OIDC, and OpenShift OAuth authentication
Project description
Kube AuthKit - Kubernetes Authentication Toolkit
A lightweight Python library that provides unified authentication for OpenShift and Kubernetes clusters. This library simplifies authentication by supporting multiple methods through a single, consistent interface.
Features
-
Universal Authentication Support
- Standard Kubernetes KubeConfig (~/.kube/config)
- In-Cluster Service Account (for Pods and Notebooks)
- OIDC (OpenID Connect) with multiple flows
- OpenShift OAuth
-
Auto-Detection: Automatically detects and uses the best authentication method for your environment
-
Multiple OIDC Flows
- Authorization Code Flow with PKCE (for interactive apps)
- Device Code Flow (for CLI tools and headless environments)
- Client Credentials Flow (for service-to-service authentication)
-
Token Management
- Automatic token refresh
- Optional persistent storage via system keyring
- Secure in-memory storage by default
-
Security First
- TLS verification enabled by default
- No sensitive data in logs
- Minimal dependencies
Installation
pip install kube-authkit
For optional keyring support (persistent token storage):
pip install kube-authkit[keyring]
Quick Start
Automatic Authentication (Recommended)
The library automatically detects your environment and chooses the appropriate authentication method:
from kube_authkit import get_k8s_client
from kubernetes import client
# Auto-detect environment and authenticate
api_client = get_k8s_client()
# Use with standard Kubernetes client
v1 = client.CoreV1Api(api_client)
pods = v1.list_pod_for_all_namespaces()
print(f"Found {len(pods.items)} pods")
This works seamlessly whether you're running:
- Locally with ~/.kube/config
- Inside a Kubernetes Pod or OpenShift Notebook (using Service Account)
- With OIDC credentials in environment variables
Explicit OIDC Authentication
For CLI tools or when you need explicit control:
from kube_authkit import get_k8s_client, AuthConfig
config = AuthConfig(
method="oidc",
oidc_issuer="https://keycloak.example.com/auth/realms/myrealm",
client_id="my-cli-tool",
use_device_flow=True # Good for headless/CLI environments
)
# This will print: "Visit https://... and enter code: ABCD-EFGH"
api_client = get_k8s_client(config)
Interactive Browser-Based Authentication
For notebooks or interactive applications:
from kube_authkit import get_k8s_client, AuthConfig
config = AuthConfig(
method="oidc",
oidc_issuer="https://keycloak.example.com/auth/realms/myrealm",
client_id="my-app",
use_device_flow=False # Use Authorization Code Flow (opens browser)
)
# Browser will open for authentication
api_client = get_k8s_client(config)
Persistent Token Storage
Store refresh tokens securely in your system keyring:
from kube_authkit import get_k8s_client, AuthConfig
config = AuthConfig(
method="oidc",
oidc_issuer="https://keycloak.example.com/auth/realms/myrealm",
client_id="my-app",
use_keyring=True # Store tokens in system keyring
)
# First run: Interactive authentication
# Subsequent runs: Uses stored refresh token automatically
api_client = get_k8s_client(config)
Advanced: Customize Client Configuration
For advanced use cases where you need to customize the Kubernetes client configuration before creating the client:
from kube_authkit import get_k8s_config
from kubernetes import client
# Get just the configuration (without creating ApiClient yet)
k8s_config = get_k8s_config()
# Customize configuration as needed
k8s_config.debug = True # Enable debug logging
k8s_config.verify_ssl = False # Disable SSL verification (dev only)
# Create client with customized configuration
api_client = client.ApiClient(k8s_config)
v1 = client.CoreV1Api(api_client)
This is useful when you need:
- Custom debug settings
- SSL/TLS configuration
- Multiple clients with the same authentication but different settings
- To inspect the configuration before using it
Configuration
AuthConfig Options
| Parameter | Type | Default | Description |
|---|---|---|---|
method |
str | "auto" | Authentication method: "auto", "kubeconfig", "incluster", "oidc", "openshift" |
k8s_api_host |
str | None | Kubernetes API server URL (auto-detected if not provided) |
oidc_issuer |
str | None | OIDC issuer URL (required for OIDC) |
client_id |
str | None | OIDC client ID (required for OIDC) |
client_secret |
str | None | OIDC client secret (for confidential clients) |
scopes |
list | ["openid"] | OIDC scopes to request |
use_device_flow |
bool | False | Use Device Code Flow instead of Authorization Code Flow |
use_keyring |
bool | False | Store refresh tokens in system keyring |
ca_cert |
str | None | Path to custom CA certificate bundle |
verify_ssl |
bool | True | Verify SSL certificates (disable only for development) |
Environment Variables
The library respects these environment variables:
KUBECONFIG: Path to kubeconfig fileKUBERNETES_SERVICE_HOST: Auto-detected in-cluster (set by Kubernetes)AUTHKIT_OIDC_ISSUER: OIDC issuer URLAUTHKIT_CLIENT_ID: OIDC client IDAUTHKIT_CLIENT_SECRET: OIDC client secretAUTHKIT_TOKEN: Bearer token for authenticationAUTHKIT_API_HOST: Kubernetes API server URLOPENSHIFT_TOKEN: Legacy OpenShift OAuth token (useAUTHKIT_TOKENinstead)
Architecture
This library uses the Strategy Pattern to provide a unified interface across different authentication methods:
AuthFactory (auto-detection)
├── KubeConfigStrategy (~/.kube/config)
├── InClusterStrategy (Service Account)
├── OIDCStrategy (OpenID Connect)
└── OpenShiftOAuthStrategy (OpenShift OAuth)
Each strategy implements the same interface, making it easy to add new authentication methods in the future.
Security Considerations
- TLS Verification: Enabled by default. Only disable for development/testing.
- Token Storage: In-memory by default. Use keyring for persistence across sessions.
- Logging: No sensitive data (tokens, secrets) is ever logged.
- Dependencies: Minimal dependency footprint to reduce supply chain risk.
Development
Setup Development Environment
# Clone repository
git clone https://github.com/openshift/kube-authkit.git
cd kube-authkit
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install with dev dependencies
pip install -e ".[dev]"
Running Tests
# Run all tests with coverage
pytest
# Run specific test file
pytest tests/test_config.py
# Run with verbose output
pytest -v
# Type checking
mypy src/kube_authkit
# Code formatting
black src/ tests/
ruff check src/ tests/
# Security scanning
bandit -r src/
Examples
See the examples/ directory for complete examples:
auto_auth.py- Simple auto-detectionoidc_device_flow.py- CLI tool with device flowoidc_auth_code.py- Interactive browser-based authnotebook_usage.py- Jupyter notebook exampleexplicit_config.py- All configuration optionscustom_ca.py- Custom CA certificate
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
Apache License 2.0 - see LICENSE for details.
Support
- Issues: https://github.com/openshift/kube-authkit/issues
- Documentation: https://github.com/openshift/kube-authkit#readme
Acknowledgments
This library wraps and extends the official Kubernetes Python Client to provide simplified authentication workflows for OpenShift AI and Kubernetes environments.
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
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 kube_authkit-0.2.0.tar.gz.
File metadata
- Download URL: kube_authkit-0.2.0.tar.gz
- Upload date:
- Size: 106.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b91b65dc059bbd0a83b26b6cb9622fbf7fcce79a9dde334b8a6e96646c0ddc8e
|
|
| MD5 |
fbba8879411710849a4450e4a4bca5cb
|
|
| BLAKE2b-256 |
307117bc56b1f34b8be7d951f3bdaf3b040a1f4d9eb1978d34d342b43717e2f0
|
Provenance
The following attestation bundles were made for kube_authkit-0.2.0.tar.gz:
Publisher:
publish.yml on opendatahub-io/kube-authkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kube_authkit-0.2.0.tar.gz -
Subject digest:
b91b65dc059bbd0a83b26b6cb9622fbf7fcce79a9dde334b8a6e96646c0ddc8e - Sigstore transparency entry: 831175540
- Sigstore integration time:
-
Permalink:
opendatahub-io/kube-authkit@05a09d82f4a89c9ca465581f76e24449a0fdd92d -
Branch / Tag:
refs/tags/0.2.0 - Owner: https://github.com/opendatahub-io
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@05a09d82f4a89c9ca465581f76e24449a0fdd92d -
Trigger Event:
release
-
Statement type:
File details
Details for the file kube_authkit-0.2.0-py3-none-any.whl.
File metadata
- Download URL: kube_authkit-0.2.0-py3-none-any.whl
- Upload date:
- Size: 31.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab7cb0d59d31a631688c0cdb80e4a8dc27dc09f1153e80975fd5f76557f40a26
|
|
| MD5 |
fa7abd44dede626966dbcfc4bd977b53
|
|
| BLAKE2b-256 |
bb217d1c1286adacd0f6c1d7b5c69e512d1cd475c97c780a1c3b983d1d247b0b
|
Provenance
The following attestation bundles were made for kube_authkit-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on opendatahub-io/kube-authkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kube_authkit-0.2.0-py3-none-any.whl -
Subject digest:
ab7cb0d59d31a631688c0cdb80e4a8dc27dc09f1153e80975fd5f76557f40a26 - Sigstore transparency entry: 831175544
- Sigstore integration time:
-
Permalink:
opendatahub-io/kube-authkit@05a09d82f4a89c9ca465581f76e24449a0fdd92d -
Branch / Tag:
refs/tags/0.2.0 - Owner: https://github.com/opendatahub-io
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@05a09d82f4a89c9ca465581f76e24449a0fdd92d -
Trigger Event:
release
-
Statement type: