Skip to main content

Google Cloud KMS Key Provider for Swarmauri

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_keyprovider_gcpkms


Swarmauri GCP KMS Key Provider

Google Cloud KMS-backed key provider for the Swarmauri framework. It exposes Cloud KMS asymmetric and symmetric keys through the common IKeyProvider interface so agents can sign, verify, encrypt, decrypt, wrap, and unwrap data without leaving Swarmauri.

Optional Canonicalization Extras

  • cbor – installs cbor2 to enable canonical CBOR utilities where workflows require deterministic binary encoding.

Features

  • Use Cloud KMS asymmetric keys for RSA/EC signing and verification while receiving RFC 7517 JWKS payloads for downstream services.
  • Perform RSA-OAEP wrapping/unwrapping of data encryption keys and AES-256 encryption/decryption with hardware-backed material.
  • Publish JWKS documents from Cloud KMS public keys, including caching and TTL-based refresh to minimize API calls.
  • Generate random bytes and derive key material via HKDF with SHA-256 for envelope encryption scenarios.
  • Destroy individual key versions via the Cloud KMS REST API when performing decommissioning workflows.

Prerequisites

  • Python 3.10 or newer.
  • google-auth and requests (installed automatically) plus network access to Google Cloud KMS endpoints.
  • A Google Cloud project with the KMS API enabled, along with a key ring (key_ring_id) in your chosen location (location_id).
  • Service account or workload identity with permissions such as cloudkms.cryptoKeys.get, cloudkms.cryptoKeyVersions.useToSign, cloudkms.cryptoKeyVersions.useToDecrypt, cloudkms.cryptoKeys.list, and cloudkms.keyRings.get.
  • Application Default Credentials available to the runtime (e.g., GOOGLE_APPLICATION_CREDENTIALS, workload identity, or Cloud Run default service account).

Installation

# pip
pip install swarmauri_keyprovider_gcpkms

# poetry
poetry add swarmauri_keyprovider_gcpkms

# uv (pyproject-based projects)
uv add swarmauri_keyprovider_gcpkms

# Extras for CBOR canonicalization
pip install "swarmauri_keyprovider_gcpkms[cbor]"

Quickstart: Sign and Verify with Cloud KMS

import asyncio
from swarmauri_keyprovider_gcpkms import GcpKmsKeyProvider
from swarmauri_core.key_providers.types import KeySpec, KeyUse


async def main() -> None:
    provider = GcpKmsKeyProvider(
        project_id="my-project",
        location_id="us-central1",
        key_ring_id="swarmauri",
    )

    key_ref = await provider.get_key(
        kid="projects/my-project/locations/us-central1/keyRings/swarmauri/cryptoKeys/jwt-key",
        version=None,
    )

    message = b"payload to sign"
    signature = await provider.sign(key_ref.kid, message, alg=JWAAlg.RS256)
    await provider.verify(key_ref.kid, message, signature, alg=JWAAlg.RS256)

    jwk = await provider.get_public_jwk(key_ref.kid, key_ref.version)
    print("Public JWK", jwk)


if __name__ == "__main__":
    asyncio.run(main())

Encrypt and Wrap Data Keys

import asyncio
from swarmauri_keyprovider_gcpkms import GcpKmsKeyProvider


async def encrypt_documents() -> None:
    provider = GcpKmsKeyProvider(
        project_id="my-project",
        location_id="us-east1",
        key_ring_id="data-protection",
    )

    dek = await provider.random_bytes(32)
    aad = b"swarmauri::tenant-a"

    ciphertext = await provider.encrypt(
        kid="projects/my-project/locations/us-east1/keyRings/data-protection/cryptoKeys/primary",
        plaintext=b"secret payload",
        associated_data=aad,
    )

    wrapped = await provider.wrap(
        kid="projects/my-project/locations/us-east1/keyRings/data-protection/cryptoKeys/wrapping",
        plaintext=dek,
    )

    unwrapped = await provider.unwrap(
        kid="projects/my-project/locations/us-east1/keyRings/data-protection/cryptoKeys/wrapping",
        ciphertext=wrapped,
    )
    assert unwrapped == dek


# asyncio.run(encrypt_documents())

Operational Tips

  • The provider caches public keys (_pub_cache) for 5 minutes; call get_public_jwk(..., force=True) if you rotate Cloud KMS key versions and need instant propagation.
  • Use explicit key version names when destroying or disabling keys: projects/.../cryptoKeys/<name>/cryptoKeyVersions/<n>.
  • Cloud KMS rotation is controlled outside the provider (per key configuration). Combine the provider with IAM rotation settings to enforce regular key versioning.
  • For auditability, inspect the tags field on returned KeyRef objects—they include algorithm purpose and key type hints derived from Cloud KMS metadata.

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

swarmauri_keyprovider_gcpkms-0.9.3.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

swarmauri_keyprovider_gcpkms-0.9.3-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file swarmauri_keyprovider_gcpkms-0.9.3.tar.gz.

File metadata

  • Download URL: swarmauri_keyprovider_gcpkms-0.9.3.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_keyprovider_gcpkms-0.9.3.tar.gz
Algorithm Hash digest
SHA256 ed89deb0ad7b6599ee8a87d9ce5c0051f84b9dc46b8d13cfc57d5ad3932c6409
MD5 951702273bf6e47306d34069a0600f6d
BLAKE2b-256 d02c1d65072bf1bd41e72860d387aff5a43ce0904ce898a45864e907f231055f

See more details on using hashes here.

File details

Details for the file swarmauri_keyprovider_gcpkms-0.9.3-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_keyprovider_gcpkms-0.9.3-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_keyprovider_gcpkms-0.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d9b772649ea52cc5ad33967a08852d3006c00642dd2e8fb311ed67d8e78883ac
MD5 4850b0aa4047f79a97eb8a23cce83abb
BLAKE2b-256 f6fa959cd787150253163ee6cc73f709bd98b71763201daa69285a9a979a8b76

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