Tiny helper to fetch Google Secret Manager secrets with optional service account keyfile support.
Project description
toolssecret
Tiny helper to fetch secrets from Google Secret Manager, with optional support for:
- ADC (Application Default Credentials) (default)
- A service account JSON keyfile (
path_keyfile) - A service account info dict (
keyfile_json) — useful for CI/CD where you inject JSON via env/secret manager - A pre-resolved credentials object (
credentials) — reuse credentials from e.g.toolsbq - A pre-built SM client (
client) — fastest option for repeated calls
It's designed so you can simply:
from toolssecret import get_secret
Install
From pypi
pip install toolssecret
Usage
1) Using ADC (Application Default Credentials)
from toolssecret import get_secret
value = get_secret(secret_name="api_key_test", project_id="myproject")
print(value)
2) Using a service account keyfile
from toolssecret import get_secret
value = get_secret(
secret_name="api_key_test",
project_id="myproject",
path_keyfile="~/.config/gcloud/sa-keys/myserviceaccount.json",
)
print(value)
Notes:
path_keyfilesupports~and environment variable expansion like$HOME/...(expanded by Python).
3) Using a service account info dict
This is useful when you keep the service account JSON in an environment variable or secret.
import json
import os
from toolssecret import get_secret
sa_info = json.loads(os.environ["GCP_SA_JSON"])
value = get_secret(
secret_name="api_key_test",
project_id="myproject",
keyfile_json=sa_info,
)
print(value)
4) Reusing credentials from toolsbq
If you already have a BigQuery client from toolsbq, you can reuse its credentials to avoid re-reading the SA file:
from toolsbq import bq_get_client
from toolssecret import get_secret
bq = bq_get_client(path_keyfile="~/key.json")
value = get_secret("my-secret", credentials=bq._credentials)
5) Pre-building an SM client for repeated calls
For hot loops or many secrets, pre-build the client once to avoid repeated gRPC channel setup:
from toolssecret import sm_get_client, get_secret
client = sm_get_client(path_keyfile="~/key.json")
s1 = get_secret("secret-a", client=client)
s2 = get_secret("secret-b", client=client)
s3 = get_secret("secret-c", client=client)
Project ID detection
If you omit project_id, toolssecret will try to detect it in this order:
GOOGLE_CLOUD_PROJECT,GCLOUD_PROJECT,GCP_PROJECTenv varskeyfile_json["project_id"](if provided)path_keyfile's embedded project_id (if provided)GOOGLE_APPLICATION_CREDENTIALSfile's project_id- ADC project detection
API
sm_get_client
sm_get_client(
*,
path_keyfile: str | None = None,
keyfile_json: dict | None = None,
credentials: Any | None = None,
) -> SecretManagerServiceClient
Build a reusable Secret Manager client. Credential priority:
credentials— pre-resolved credentials object (e.g. frombq_client._credentials)keyfile_json/path_keyfile— explicit SA credentials- RAM-ADC / env / ADC fallback
get_secret
get_secret(
secret_name: str,
*,
project_id: str | None = None,
version_id: str = "latest",
client: SecretManagerServiceClient | None = None,
credentials: Any | None = None,
path_keyfile: str | None = None,
keyfile_json: dict | None = None,
) -> str
Credential priority:
client— pre-built SM client (fastest for repeated calls)credentials— pre-resolved credentials objectkeyfile_json— SA key as dictpath_keyfile— path to SA JSON file- RAM-ADC / env / ADC fallback
Secrets are cached in-memory per process (cache key includes project, secret name, version, and credential fingerprint).
Security notes
- Avoid committing service account keyfiles to git.
- Prefer
keyfile_jsonsourced from a secure secret store (CI secrets, vault, etc.). toolssecretdoes not log secret values.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file toolssecret-0.1.3.tar.gz.
File metadata
- Download URL: toolssecret-0.1.3.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b087b494adfd986b98340b4fb62ee705f59c36ca91fab42f33a17f3756070bd0
|
|
| MD5 |
e332cf13bf387c604477a2ddc9a832c6
|
|
| BLAKE2b-256 |
a66cf4d330c2120b747f0564e8acd5fd9bbe3570bf6a62a3562134afa3958412
|
File details
Details for the file toolssecret-0.1.3-py3-none-any.whl.
File metadata
- Download URL: toolssecret-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95ae1547c94f954be7f7934cfe1a54569727c5d6ea7f1bd65962dbf93e54720f
|
|
| MD5 |
5110d1dbb2a166c1b4bcc2517b1fb81b
|
|
| BLAKE2b-256 |
2dd5511991030b5519170fb28283733e5ee140b6ccf1dd77ee1f565d6e72f5aa
|