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.2.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.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cubesdk-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 04aac186bc3ffa245ceaff2b1862df91bbb6d456150d0d0c6167040a031b4df5
MD5 3803e3e0cd5009632eef6ec5a2c25f21
BLAKE2b-256 24b6d458020ecc81b6f0e3d3809276e06595df2b636f93a001a65ca02ba5ea3a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cubesdk-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f7913e2908c959df2972297f76e3027201151e7f98ae3dd945162adc41c99d6c
MD5 f99c55c66c90df56345705bf103f6c37
BLAKE2b-256 d2af760de0f3091b40d6b905ac648b55e43afbaef0bc7c2fe27963fba63894e1

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