Skip to main content

CubeSDK: Secure secret management client

Project description

CubeSDK

Python SDK for Cube secret management with built-in caching, environment switching, and automatic error handling. Website: https://usecube.co

Features

  • 🔐 Secure Secret Management - Retrieve secrets from Cube with service token authentication
  • Built-in Caching - TTL-based caching to reduce API calls and improve performance
  • 🌍 Environment Support - Switch between environments (development, staging, production) seamlessly
  • 🔄 Auto-refresh - Automatic cache invalidation and refresh capabilities
  • 🛡️ Error Handling - Comprehensive exception handling for authentication, authorization, and API errors
  • 📦 Simple API - Clean and intuitive interface for secret management

Installation

Install CubeSDK using pip:

pip install cubesdk

Quick Start

from cubesdk import CubeSDK

# Initialize client with a service token and default environment
client = CubeSDK(
    service_token="st_dev_xxx",
    default_env="development",
    cache_ttl=120,  # Cache secrets for 2 minutes
)

# Get a single secret
db_password = client.get_secret("DB_PASSWORD")

# Get all secrets
secrets = client.get_secrets()

# Switch environment
client.switch_env("staging")
api_key = client.get_secret("API_KEY")

# Force refresh cache
client.refresh()

Configuration

The CubeSDK client accepts the following parameters:

  • service_token (str, required): Your Cube service token for authentication
  • api_version (str, optional): API version to use (defaults to environment variable or configured value)
  • default_env (str, optional): Default environment name (default: "development")
  • base_url (str, optional): Base URL for the Cube API (defaults to environment variable or configured value)
  • timeout (int, optional): Request timeout in seconds (default: 5)
  • cache_ttl (int, optional): Cache time-to-live in seconds (default: 60)

API Reference

get_secret(key: str) -> str

Retrieve a single secret by key. The secret is cached based on the current environment and cache TTL.

password = client.get_secret("DB_PASSWORD")

get_secrets() -> Dict[str, str]

Retrieve all secrets for the current environment. All secrets are cached automatically.

all_secrets = client.get_secrets()
# Returns: {"DB_PASSWORD": "secret123", "API_KEY": "key456", ...}

switch_env(environment: str)

Switch to a different environment. This clears the cache to ensure fresh secrets are fetched.

client.switch_env("staging")

refresh()

Force refresh all cached secrets by clearing the cache.

client.refresh()

Error Handling

The SDK provides specific exception types for different error scenarios:

from cubesdk import CubeSDK
from app.exceptions import (
    AuthenticationError,
    AuthorizationError,
    SecretNotFoundError,
    APIError,
)

try:
    secret = client.get_secret("MY_SECRET")
except AuthenticationError:
    print("Invalid or expired service token")
except AuthorizationError:
    print("Access denied for this environment")
except SecretNotFoundError:
    print("Secret not found in the current environment")
except APIError as e:
    print(f"API error: {e}")

Examples

Basic Usage

from cubesdk import CubeSDK

client = CubeSDK(
    service_token="st_dev_xxx",
    default_env="development",
    cache_ttl=300,  # 5 minutes
)

# Get secrets
db_host = client.get_secret("DB_HOST")
db_password = client.get_secret("DB_PASSWORD")
api_key = client.get_secret("API_KEY")

Environment Switching

# Start with development
client = CubeSDK(
    service_token="st_dev_xxx",
    default_env="development",
)

prod_secret = client.get_secret("SECRET_KEY")

# Switch to staging
client.switch_env("staging")
staging_secret = client.get_secret("SECRET_KEY")

Batch Secret Retrieval

# Get all secrets at once (more efficient for multiple secrets)
secrets = client.get_secrets()
db_config = {
    "host": secrets["DB_HOST"],
    "port": secrets["DB_PORT"],
    "password": secrets["DB_PASSWORD"],
}

Custom Configuration

client = CubeSDK(
    service_token="st_dev_xxx",
    default_env="development",
    base_url="https://api.cube.example.com",
    timeout=10,
    cache_ttl=600,  # 10 minutes
    api_version="v1",
)

Requirements

  • Python 3.9+
  • requests>=2.31.0

License

MIT

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

cubesdk-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

cubesdk-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file cubesdk-0.1.1.tar.gz.

File metadata

  • Download URL: cubesdk-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for cubesdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9115b4b14d65563708316715fe326b8b71b8107e3168edbe92d62c54279e1b63
MD5 72b5899fa440b4b82ba299c5b3cf93b8
BLAKE2b-256 33a9137b39bcbb729e83fac1c401fd4ed31ea37585f6ccc1e9db90b80aaa5a2c

See more details on using hashes here.

File details

Details for the file cubesdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cubesdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for cubesdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad2a4c88a22a0d3cca824d25a1ecb2178c48451c292edf5f2aec91a839bb0645
MD5 bfd29db1d95bb62ccb90c3838dcea757
BLAKE2b-256 0e46d04c87c746d483bb9e1bb9fdd5b6172680a35a556fdf93b532b2c5336764

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