kiarina-lib-cloudflare-auth
A Python library for Cloudflare authentication with configuration management using pydantic-settings-manager.
Features
- Configuration Management: Use
pydantic-settings-managerfor flexible configuration - Type Safety: Full type hints and Pydantic validation
- Secure Credential Handling: API tokens are protected using
SecretStr - Multiple Configurations: Support for multiple named configurations (e.g., different accounts)
- Environment Variable Support: Configure via environment variables with
KIARINA_LIB_CLOUDFLARE_AUTH_prefix
Installation
pip install kiarina-lib-cloudflare-auth
Quick Start
Basic Usage
from kiarina.lib.cloudflare.auth import CloudflareAuthSettings, settings_manager
# Configure Cloudflare authentication
settings_manager.user_config = {
"default": {
"account_id": "your-account-id",
"api_token": "your-api-token"
}
}
# Get settings
settings = settings_manager.settings
print(f"Account ID: {settings.account_id}")
print(f"API Token: {settings.api_token.get_secret_value()}") # Access secret value
Environment Variable Configuration
Configure authentication using environment variables:
export KIARINA_LIB_CLOUDFLARE_AUTH_ACCOUNT_ID="your-account-id"
export KIARINA_LIB_CLOUDFLARE_AUTH_API_TOKEN="your-api-token"
from kiarina.lib.cloudflare.auth import settings_manager
# Settings are automatically loaded from environment variables
settings = settings_manager.settings
print(f"Account ID: {settings.account_id}")
Multiple Configurations
Manage multiple Cloudflare accounts:
from kiarina.lib.cloudflare.auth import settings_manager
# Configure multiple accounts
settings_manager.user_config = {
"production": {
"account_id": "prod-account-id",
"api_token": "prod-api-token"
},
"staging": {
"account_id": "staging-account-id",
"api_token": "staging-api-token"
}
}
# Switch between configurations
settings_manager.active_key = "production"
prod_settings = settings_manager.settings
print(f"Production Account: {prod_settings.account_id}")
settings_manager.active_key = "staging"
staging_settings = settings_manager.settings
print(f"Staging Account: {staging_settings.account_id}")
Configuration
This library uses pydantic-settings-manager for flexible configuration management.
CloudflareAuthSettings
The CloudflareAuthSettings class provides the following configuration fields:
| Field | Type | Required | Description |
|---|---|---|---|
account_id |
str |
Yes | Cloudflare account ID |
api_token |
SecretStr |
Yes | Cloudflare API token (masked in logs) |
Environment Variables
All settings can be configured via environment variables with the KIARINA_LIB_CLOUDFLARE_AUTH_ prefix:
# Account ID
export KIARINA_LIB_CLOUDFLARE_AUTH_ACCOUNT_ID="your-account-id"
# API Token (will be automatically wrapped in SecretStr)
export KIARINA_LIB_CLOUDFLARE_AUTH_API_TOKEN="your-api-token"
Programmatic Configuration
from pydantic import SecretStr
from kiarina.lib.cloudflare.auth import CloudflareAuthSettings, settings_manager
# Direct settings object
settings = CloudflareAuthSettings(
account_id="your-account-id",
api_token=SecretStr("your-api-token")
)
# Via settings manager
settings_manager.user_config = {
"default": {
"account_id": "your-account-id",
"api_token": "your-api-token" # Automatically converted to SecretStr
}
}
Runtime Overrides
from kiarina.lib.cloudflare.auth import settings_manager
# Override specific settings at runtime
settings_manager.cli_args = {
"account_id": "override-account-id"
}
settings = settings_manager.settings
print(f"Account ID: {settings.account_id}") # Uses overridden value
Security
API Token Protection
API tokens are stored using Pydantic's SecretStr type, which provides the following security benefits:
- Masked in logs: Tokens are displayed as
**********in string representations - Prevents accidental exposure: Tokens won't appear in debug output or error messages
- Explicit access required: Must use
.get_secret_value()to access the actual token
from kiarina.lib.cloudflare.auth import settings_manager
settings = settings_manager.settings
# Token is masked in string representation
print(settings) # api_token=SecretStr('**********')
# Explicit access to get the actual token
token = settings.api_token.get_secret_value()
API Reference
CloudflareAuthSettings
class CloudflareAuthSettings(BaseSettings):
account_id: str
api_token: SecretStr
Pydantic settings model for Cloudflare authentication.
Fields:
account_id(str): Cloudflare account IDapi_token(SecretStr): Cloudflare API token (protected)
settings_manager
settings_manager: SettingsManager[CloudflareAuthSettings]
Global settings manager instance for Cloudflare authentication. See: pydantic-settings-manager
Development
Prerequisites
- Python 3.12+
Setup
# Clone the repository
git clone https://github.com/kiarina/kiarina-python.git
cd kiarina-python
# Setup development environment
mise run setup
Running Tests
# Run format, lint, type checks and tests
mise run package kiarina-lib-cloudflare-auth
# Coverage report
mise run package:test kiarina-lib-cloudflare-auth --coverage
Dependencies
- pydantic-settings - Settings management
- pydantic-settings-manager - Advanced settings management
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
This is a personal project, but contributions are welcome! Please feel free to submit issues or pull requests.
Related Projects
- kiarina-python - The main monorepo containing this package
- pydantic-settings-manager - Configuration management library used by this package
Metadata
Release files for kiarina-lib-cloudflare-auth 1.37.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 |
|---|---|---|---|---|
| kiarina_lib_cloudflare_auth-1.37.0.tar.gz | 4.6 kB | Without provenance | Details for kiarina_lib_cloudflare_auth-1.37.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kiarina_lib_cloudflare_auth-1.37.0-py3-none-any.whl | Python 3 | none | any | Details for kiarina_lib_cloudflare_auth-1.37.0-py3-none-any.whl |
Total release size: 9.0 kB
Release files / kiarina_lib_cloudflare_auth-1.37.0.tar.gz
| Download URL | kiarina_lib_cloudflare_auth-1.37.0.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c9b10f94105f3bb1336281d4f99762a38fa554fbc590663026c0fa6f0370cd13
|
|
BLAKE2b-256 checksum How to use checksums |
a9e296e1a5f3b85b1e1513fc6f0f84e0fdc29f0e8f0af9becee426640400b119
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / kiarina_lib_cloudflare_auth-1.37.0-py3-none-any.whl
| Download URL | kiarina_lib_cloudflare_auth-1.37.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d72929917b4a430fb4c0ba8236d388b410ada9a3d4ec4245b1e0f4704780dc2
|
|
BLAKE2b-256 checksum How to use checksums |
0ddcd719923aa1afbe7f1874e5b85bcd42ee1e1a5e2f1ba4205e0c2e5ff81904
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Security for kiarina-lib-cloudflare-auth 1.37.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.