Skip to main content

Decorators for Multi-Source Secret Retrieval

Project description

decorative-secrets

test PyPI version

This library implements decorators for mapping parameters passing sensitive data, such as authentication credentials, to alternate parameters indicating where the same credentials can be retrieved dynamically from a secrets manager and/or environment variables. The use of a decorator pattern to expose multiple authentication mechanisms is intended to facilitate development practices which maintain a semblance of harmony between security hygiene and test-driven development.

Currently, the following secret sources are supported:

  • Databricks Secrets (pip3 install decorative-secrets[databricks])
  • 1Password (pip3 install decorative-secrets[onepassword]): 1Password secrets are retrieved dynamically using the 1Password CLI under the hood. This mechanism can be used with the 1Password desktop client and CLI (including via SSO), in which case a popup window will prompt for authentication during execution, or can use a 1Password service account by setting the OP_SERVICE_ACCOUNT_TOKEN environment variable. The former is recommended for local development and testing, the latter for CI/CD and/or hosted applications.
  • Environment Variables

Future development will target support for AWS Secrets Manager, Google Cloud Secret Manager, and Azure Key Vault.

Installation

You can install decorative-secrets with pip:

pip3 install decorative-secrets

Example Usage

from functools import cache
from my_app_sdk.client import Client
from decorative_secrets.environment import apply_environment_arguments
from decorative_secrets.onepassword import apply_onepassword_arguments
from decorative_secrets.databricks import apply_databricks_secrets_arguments

@cache
@apply_environment_arguments(
  client_id="client_id_environment_variable",
  client_secret="client_secret_environment_variable",
)
@apply_databricks_secrets_arguments(
  client_id="client_id_databricks_secret",
  client_secret="client_secret_databricks_secret",
)
@apply_onepassword_arguments(
  client_id="client_id_onepassword",
  client_secret="client_secret_onepassword",
)
def get_client(
    client_id: str | None = None,
    client_secret: str = None,
    client_id_databricks_secret: tuple[str, str] | None = None,
    client_secret_databricks_secret: tuple[str, str] | None = None,
    client_id_onepassword: str | None = None,
    client_secret_onepassword: str | None = None,
    client_id_environment_variable: str | None = None,
    client_secret_environment_variable: str | None = None,
) -> Client:
    """
    This function is an example use of `decorative-secrets`.
    The returned client will authenticate with explicitly provided
    credentials if a `client_id` and `client_secret` are passed as arguments,
    otherwise, the same function call will first check to see if
    environment variables can be used, then will check to see if databricks
    secrets can be used, and lastly will check to see if 1password
    stored credentials can be obtained. In all cases where
    an argument other than `None` is passed, errors will be caught and
    accumulated for that parameter, but only raised if none of the
    successive mechanisms for retrieving a value for the parameter are
    successful.
    
    Parameters:
        client_id: An eplicitly passed OAuth 2 client ID
        client_secret: An explicitly passed OAuth 2 client secret
        client_id_databricks_secret: A databricks secrets scope and key
        from which to retrieve the client ID, if executed in a Databricks
        Runtime environment
        client_secret_databricks_secret: A databricks secrets scope and key
        from which to retrieve the client secret, if executed in a Databricks
        Runtime environment
        client_id_onepassword: A onepassword reference from which to retrieve
        the client ID. Note: the user will be prompted to login, if they have
        not already done so, unless a `OP_SERVICE_ACCOUNT_TOKEN` environment
        variable has been set, and/or both both an `OP_CONNECT_HOST` and
        `OP_CONNECT_TOKEN` environment variable have been set.
        client_secret_onepassword: A onepassword reference from which to retrieve
        the client secret. Note: the user will be prompted to login, if they have
        not already done so, unless a `OP_SERVICE_ACCOUNT_TOKEN` environment
        variable has been set, and/or both both an `OP_CONNECT_HOST` and
        `OP_CONNECT_TOKEN` environment variable have been set.
        client_id_environment_variable: An environment variable from which
        the client ID may be retrieved
        client_secret_environment_variable: An environment variable from which
        the client secret may be retrieved
    """
    return Client(
        oauth2_client_id=client_id,
        oauth2_client_secret=client_secret
    )


# Initialize an OAuth 2 Client
client: Client = get_client(
  client_id_databricks_secret=("client-scope", "client-id-key"),
  client_secret_databricks_secret=("client-scope", "client-secret-key"),
  client_id_onepassword="op://Vault Name/Client ID Item Name/username",
  client_secret_onepassword="op://Vault Name/Client Secret Item Name/credential",
  client_id_environment_variable="MY_APP_CLIENT_ID",
  client_secret_environment_variable="MY_APP_CLIENT_SECRET",
)

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

decorative_secrets-0.8.5.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

decorative_secrets-0.8.5-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file decorative_secrets-0.8.5.tar.gz.

File metadata

  • Download URL: decorative_secrets-0.8.5.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.3 cpython/3.12.3 HTTPX/0.28.1

File hashes

Hashes for decorative_secrets-0.8.5.tar.gz
Algorithm Hash digest
SHA256 6423c9c71a2fd7c78fabc0731dbf8d396218a504f870be87e4ada3a9a9cad415
MD5 55001a42b43022a26fd5c13f6b50665c
BLAKE2b-256 3f2f2558fa5fecbf4a34eb77a2b171f3677320cd7ded87c215b786ad9066ef8e

See more details on using hashes here.

File details

Details for the file decorative_secrets-0.8.5-py3-none-any.whl.

File metadata

  • Download URL: decorative_secrets-0.8.5-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.3 cpython/3.12.3 HTTPX/0.28.1

File hashes

Hashes for decorative_secrets-0.8.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6a1a4d5ca4e2ecba56a636765fac598f6476de5ac4fe83ad4201be36e6bac473
MD5 1e8b2fd59225450f43a811c682259fbc
BLAKE2b-256 2bdeb5eb1e9f41e670ee8d71a08e23101b358ed56f3e119095d2f168195b8bd9

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