mirror of
https://github.com/hanzoai/python-sdk.git
synced 2026-10-26 11:49:36 +00:00
kms.hanzo.ai runs luxfi/kms, which has never served an /api/* route. This
SDK was sending Infisical's: /api/v3/secrets/raw for every secret operation,
/api/v3/auth/login for user auth, /api/v1/auth/kubernetes-auth/login for k8s.
Only one call in the whole package — /v1/kms/auth/login — was ever real.
It stayed invisible because older luxfi/kms builds embedded a console SPA
behind a root catch-all that answered every unmatched path with 200
text/html. A wrong URL came back as a JSON decode error, which reads like a
parsing bug in the client rather than "this endpoint does not exist." That
catch-all is gone (cmd/kms/main.go ends in notFoundJSON), so those paths now
return honest JSON 404s and the SDK fails outright.
The route table is not the only thing that was wrong. The data model was
Infisical's too — project_id, workspace, secret version, secret comment,
shared-vs-personal type — and luxfi/kms has none of those concepts. It keys
one value by (org, path, name, env) in ZapDB at kms/secrets/{path}/{env}/{name}
and a write upserts it in place. Half-migrating would have left response
models that cannot validate what the server sends, so the vocabulary moves
with the paths:
list_secrets(path, env) -> names GET /v1/kms/orgs/{org}/secrets
get_secret(path, name, env) GET /v1/kms/orgs/{org}/secrets/{path}/{name}
put_secret(path, name, value, env) POST /v1/kms/orgs/{org}/secrets (create AND replace)
delete_secret(path, name, env) DELETE .../secrets/{path}/{name}
health() GET /v1/kms/healthz
Two server behaviors now have one home, hanzo_kms/routes.py, shared by the
sync and async clients so they stay mirror images:
- The server splits the trailing path at its LAST slash into (path, name),
so each segment is escaped individually. Escaping the joined string
encodes the separators away and the server reads one long name. A name
containing "/" is rejected outright: it would be written under one key
and read back under another, so the write looks like it succeeded and
the read never finds it.
- There is no versioned read. get_secret(version=N) raises
VersionUnsupportedError rather than quietly returning the current value.
org is new and required — it scopes both the URL and the JWT owner claim.
Constructor field, HANZO_KMS_ORG, defaults to "hanzo".
Auth collapses to what the server actually offers: client credentials
exchanged at /v1/kms/auth/login, or a pre-issued IAM bearer token. The AWS,
Azure, GCP, Kubernetes and SRP methods were Infisical's and none were served.
That also fixes a divergence where the async client silently ignored
HANZO_KMS_TOKEN — env parsing is now one function.
Callers migrated with it: hanzo-cli's `hanzo kms`, the hanzo-tools-kms MCP
tool, and hanzo-tools-auth's session client, all of which were building the
removed auth models and calling the removed create/update pair. Each now
constructs KMSClient() and lets it read the environment.
Tests: pkg/hanzo-kms/tests pins the wire shape — no request URL may contain
"/api/", per-segment escaping survives the server's last-slash split, sync
and async emit byte-identical request lines, and a decoder rejects the
200-HTML/JSON-404 shapes instead of reading them as empty results. Wired into
hanzo-packages-ci without `|| true`, so a regression turns CI red instead of
waiting for a decode error in production.
hanzo-kms 1.1.0 -> 1.1.1.
Co-authored-by: Hanzo Dev <[email protected]>
125 lines
4.8 KiB
Python
125 lines
4.8 KiB
Python
"""Hanzo KMS CLI — read and write secrets non-interactively.
|
|
|
|
A secret is (org, path, name, env). The org comes from ``--org`` /
|
|
``HANZO_KMS_ORG`` (default ``hanzo``); path and name are positional.
|
|
|
|
Usage:
|
|
# Machine identity from the environment
|
|
export HANZO_KMS_CLIENT_ID=... HANZO_KMS_CLIENT_SECRET=...
|
|
python -m hanzo_kms get providers/lux deploy-mnemonic --env prod
|
|
|
|
# Or a pre-issued IAM bearer token
|
|
HANZO_KMS_TOKEN=... python -m hanzo_kms list --path providers/lux --env prod
|
|
|
|
python -m hanzo_kms set providers/lux deploy-mnemonic "word word ..." --env prod
|
|
python -m hanzo_kms delete providers/lux deploy-mnemonic --env prod
|
|
python -m hanzo_kms export --path providers/lux --env prod --format json
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
|
|
from .client import KMSClient
|
|
from .models import settings_from_env
|
|
from .routes import DEFAULT_ENV
|
|
|
|
|
|
def _client(args: argparse.Namespace) -> KMSClient:
|
|
"""Build a client from the environment, overlaid with CLI flags."""
|
|
settings = settings_from_env()
|
|
if args.url:
|
|
settings.site_url = args.url
|
|
if args.org:
|
|
settings.org = args.org
|
|
if args.token:
|
|
settings.access_token = args.token
|
|
if args.client_id:
|
|
settings.client_id = args.client_id
|
|
if args.client_secret:
|
|
settings.client_secret = args.client_secret
|
|
|
|
if not (settings.access_token or (settings.client_id and settings.client_secret)):
|
|
print(
|
|
"Error: no auth configured. Set HANZO_KMS_TOKEN, or "
|
|
"HANZO_KMS_CLIENT_ID and HANZO_KMS_CLIENT_SECRET.",
|
|
file=sys.stderr,
|
|
)
|
|
sys.exit(1)
|
|
|
|
return KMSClient(settings, debug=args.debug)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(prog="hanzo-kms", description="Hanzo KMS CLI")
|
|
parser.add_argument("--url", help="KMS URL (default: https://kms.hanzo.ai)")
|
|
parser.add_argument("--org", help="Organization (default: hanzo)")
|
|
parser.add_argument("--token", help="Pre-issued IAM bearer token")
|
|
parser.add_argument("--client-id", help="Machine identity client ID")
|
|
parser.add_argument("--client-secret", help="Machine identity client secret")
|
|
parser.add_argument("--debug", action="store_true")
|
|
|
|
sub = parser.add_subparsers(dest="command")
|
|
|
|
list_p = sub.add_parser("list", help="List secret names at a path")
|
|
list_p.add_argument("--path", default="", help="Secret path")
|
|
list_p.add_argument("--env", default=DEFAULT_ENV, help=f"Environment (default: {DEFAULT_ENV})")
|
|
|
|
get_p = sub.add_parser("get", help="Print a secret value")
|
|
get_p.add_argument("path", help="Secret path, e.g. providers/lux")
|
|
get_p.add_argument("name", help="Secret name")
|
|
get_p.add_argument("--env", default=DEFAULT_ENV, help=f"Environment (default: {DEFAULT_ENV})")
|
|
|
|
set_p = sub.add_parser("set", help="Create or replace a secret")
|
|
set_p.add_argument("path", help="Secret path")
|
|
set_p.add_argument("name", help="Secret name")
|
|
set_p.add_argument("value", help="Secret value")
|
|
set_p.add_argument("--env", default=DEFAULT_ENV, help=f"Environment (default: {DEFAULT_ENV})")
|
|
|
|
del_p = sub.add_parser("delete", help="Delete a secret")
|
|
del_p.add_argument("path", help="Secret path")
|
|
del_p.add_argument("name", help="Secret name")
|
|
del_p.add_argument("--env", default=DEFAULT_ENV, help=f"Environment (default: {DEFAULT_ENV})")
|
|
|
|
export_p = sub.add_parser("export", help="Export every secret at a path")
|
|
export_p.add_argument("--path", default="", help="Secret path")
|
|
export_p.add_argument("--env", default=DEFAULT_ENV, help=f"Environment (default: {DEFAULT_ENV})")
|
|
export_p.add_argument("--format", choices=["env", "json"], default="env")
|
|
|
|
args = parser.parse_args()
|
|
if not args.command:
|
|
parser.print_help()
|
|
sys.exit(1)
|
|
|
|
client = _client(args)
|
|
try:
|
|
if args.command == "list":
|
|
for name in client.list_secrets(args.path, args.env):
|
|
print(name)
|
|
|
|
elif args.command == "get":
|
|
print(client.get_secret(args.path, args.name, args.env))
|
|
|
|
elif args.command == "set":
|
|
client.put_secret(args.path, args.name, args.value, args.env)
|
|
print(f"set {args.path}/{args.name} ({args.env})")
|
|
|
|
elif args.command == "delete":
|
|
client.delete_secret(args.path, args.name, args.env)
|
|
print(f"deleted {args.path}/{args.name} ({args.env})")
|
|
|
|
elif args.command == "export":
|
|
names = client.list_secrets(args.path, args.env)
|
|
values = {name: client.get_secret(args.path, name, args.env) for name in names}
|
|
if args.format == "json":
|
|
print(json.dumps(values, indent=2))
|
|
else:
|
|
for name, value in values.items():
|
|
print(f"{name}={value}")
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|