Skip to main content

Cloudflare client library for kiarina namespace

Project description

kiarina-lib-cloudflare

A Python library for Cloudflare authentication with configuration management using pydantic-settings-manager.

Features

  • Configuration Management: Use pydantic-settings-manager for 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_ prefix

Installation

pip install kiarina-lib-cloudflare

Quick Start

Basic Usage

from kiarina.lib.cloudflare import CloudflareSettings, 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_ACCOUNT_ID="your-account-id"
export KIARINA_LIB_CLOUDFLARE_API_TOKEN="your-api-token"
from kiarina.lib.cloudflare 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 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.

CloudflareSettings

The CloudflareSettings 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_ prefix:

# Account ID
export KIARINA_LIB_CLOUDFLARE_ACCOUNT_ID="your-account-id"

# API Token (will be automatically wrapped in SecretStr)
export KIARINA_LIB_CLOUDFLARE_API_TOKEN="your-api-token"

Programmatic Configuration

from pydantic import SecretStr
from kiarina.lib.cloudflare import CloudflareSettings, settings_manager

# Direct settings object
settings = CloudflareSettings(
    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 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 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

CloudflareSettings

class CloudflareSettings(BaseSettings):
    account_id: str
    api_token: SecretStr

Pydantic settings model for Cloudflare authentication.

Fields:

  • account_id (str): Cloudflare account ID
  • api_token (SecretStr): Cloudflare API token (protected)

settings_manager

settings_manager: SettingsManager[CloudflareSettings]

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

# Coverage report
mise run package:test kiarina-lib-cloudflare --coverage

Dependencies

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

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

kiarina_lib_cloudflare-2.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kiarina_lib_cloudflare-2.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_lib_cloudflare-2.0.0.tar.gz.

File metadata

  • Download URL: kiarina_lib_cloudflare-2.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kiarina_lib_cloudflare-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3fd7e556aaed49b42a070804e43d447d9537fcbd470b06141dc73f6acbc2f0d4
MD5 b3891ae1b9d655c42433cdfe36d5cf29
BLAKE2b-256 1b0c985abd53aff8acb9d2e7693f2d367763afa322a7ab3f96cc8deba506625d

See more details on using hashes here.

File details

Details for the file kiarina_lib_cloudflare-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_lib_cloudflare-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df0d9f4b0e1ea0f1925d3eaecef3dacdbbd046a1ac2fbc884b41e1df9ca94d41
MD5 31f0fca72adfed2fc18b2be68637065c
BLAKE2b-256 83928fc93136c1510e377690d2cb04c3395cf6a4ab24459d7bb73e25422d6f9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page