Key services¶
In production, the primary key usually lives in a key service, where it cannot be exported and every use is authorized and logged. Each integration is a separate distribution with the same shape:
You build the client. Credentials, region, endpoints, and TLS are configured the way the vendor documents them. The provider never reads credentials, files, or environment variables, and never imports the vendor SDK itself.
Every wrap is bound to its envelope. The provider passes a digest of the envelope header to the key service as authenticated data, so a wrapped key cannot be moved into another envelope.
Envelopes record the exact key version. They keep opening after the key rotates, and
rewrapmoves them to the new version.Errors are normalized. Throttling and transient failures become retryable
ProviderErrors that the policy’s bounded retries handle. SDK messages, endpoints, and payloads never reach the caller.
The examples below use Policy.enterprise, which requires an audit sink; see
Audit and observability for audit_chain.
AWS Key Management Service¶
python -m pip install --pre "cryptography-suite-aws-kms[boto3]==4.0.0rc1"
import boto3
from botocore.config import Config
from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_aws_kms import PROVIDER_ID, AwsKmsProvider
kms = boto3.client(
"kms",
region_name="eu-west-1",
config=Config(connect_timeout=3, read_timeout=5, retries={"total_max_attempts": 1}),
)
protector = Protector(
provider=AwsKmsProvider(kms),
policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
primary_key=KeyRef(PROVIDER_ID, "alias/payments"),
audit_sink=audit_chain,
)
Item |
Value |
|---|---|
Keys |
Symmetric encryption keys ( |
Recorded version |
The key ARN. AWS rotates key material behind the same ARN, so rotation needs no rewrap. |
Binding |
Encryption context |
To seal |
|
To open |
|
Health check |
|
boto3 sets timeouts and retries per client, not per call, so the client
must make exactly one attempt per call: retries={"total_max_attempts": 1}.
The provider refuses any other client, because its retries would multiply
the policy’s. (botocore’s max_attempts counts retries:
{"max_attempts": 1} means two attempts.) The policy’s deadline
(provider_timeout_ms, 30 seconds by default) is checked between attempts
but cannot interrupt a call in flight, so keep connect_timeout plus
read_timeout within it. grant_tokens= passes KMS grant tokens.
Google Cloud KMS¶
python -m pip install --pre "cryptography-suite-gcp-kms[google]==4.0.0rc1"
from google.cloud import kms
from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_gcp_kms import PROVIDER_ID, GcpKmsProvider
key = "projects/acme/locations/europe-west4/keyRings/data/cryptoKeys/payments"
protector = Protector(
provider=GcpKmsProvider(kms.KeyManagementServiceClient(), health_key=key),
policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
primary_key=KeyRef(PROVIDER_ID, key),
audit_sink=audit_chain,
)
Item |
Value |
|---|---|
Keys |
|
Recorded version |
The CryptoKeyVersion resource name. After rotation, |
Binding |
Additional authenticated data |
To seal |
|
To open |
|
Health check |
|
Every call passes retry=None and a timeout of the time left before the
operation’s deadline, so the library’s own retries (up to 60 seconds per call
by default) never apply.
With a post-quantum key, encapsulation runs locally on the version’s cached
public key and needs ML-KEM in pyca/cryptography; unwrapping calls
Decapsulate, so the private key never leaves Cloud KMS and the reader needs
no ML-KEM support.
pq_version = (
"projects/acme/locations/europe-west4/keyRings/data/cryptoKeys/archive-pq"
"/cryptoKeyVersions/1"
)
pq_key = KeyRef(PROVIDER_ID, pq_version.rsplit("/cryptoKeyVersions/", 1)[0], pq_version)
Azure Key Vault and Managed HSM¶
python -m pip install --pre "cryptography-suite-azure-keyvault[azure]==4.0.0rc1"
from azure.identity import DefaultAzureCredential
from azure.keyvault.keys import KeyClient
from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_azure_keyvault import PROVIDER_ID, AzureKeyVaultProvider
client = KeyClient(
vault_url="https://acme-payments.vault.azure.net",
credential=DefaultAzureCredential(),
)
protector = Protector(
provider=AzureKeyVaultProvider(client),
policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
primary_key=KeyRef(PROVIDER_ID, "payments"),
audit_sink=audit_chain,
)
Item |
Value |
|---|---|
Keys |
|
Recorded version |
The key version. After rotation, |
Binding |
RSA-OAEP-256 has no associated data, so the data key is wrapped together with a digest of the header, checked in constant time on unwrap. |
To seal |
Key permissions |
To open |
Key permission |
Health check |
Key permission |
The built-in role Key Vault Crypto User (or Managed HSM Crypto User) grants all of these; a custom role can split sealing from opening. Every call turns the SDK’s retry policy off and carries connection and read timeouts of the time left before the deadline, whatever retry policy the client was built with. Disabled, expired, and not-yet-valid versions are reported as disabled, so opening falls back to another recipient.
HashiCorp Vault Transit¶
python -m pip install --pre "cryptography-suite-hashicorp-vault[hvac]==4.0.0rc1"
import hvac
from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_hashicorp_vault import PROVIDER_ID, VaultTransitProvider
client = hvac.Client(url="https://vault.internal:8200", token=vault_token, timeout=5)
protector = Protector(
provider=VaultTransitProvider(client, mount_point="transit", health_key="payments"),
policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
primary_key=KeyRef(PROVIDER_ID, "payments"),
audit_sink=audit_chain,
)
Item |
Value |
|---|---|
Keys |
Transit keys of type |
Recorded version |
The key version. After |
Binding |
Transit |
To seal |
|
To open |
|
Health check |
|
path "transit/keys/payments" { capabilities = ["read"] }
path "transit/encrypt/payments" { capabilities = ["update"] }
path "transit/decrypt/payments" { capabilities = ["update"] }
hvac sets its timeout per client (30 seconds by default), so keep it within the policy’s deadline. Key names must follow Vault’s own grammar; any other name is refused before a request is built.
Several services at once¶
A Protector can hold several providers. Every envelope is wrapped for every
recipient key, so a second region, a second cloud, or an offline escrow key
can open it when the first is unavailable:
from cryptography_suite_aws_kms import PROVIDER_ID as AWS
from cryptography_suite_gcp_kms import PROVIDER_ID as GCP
protector = Protector(
providers=[AwsKmsProvider(kms), GcpKmsProvider(gcp_client)],
policy=Policy.enterprise(approved_providers=[AWS, GCP]),
primary_key=KeyRef(AWS, "alias/payments"),
additional_keys=[KeyRef(GCP, gcp_key)],
audit_sink=audit_chain,
)
Check a provider¶
verify_provider runs the contract every provider must meet against a real
test key: binding enforced, tampering rejected, wraps randomized, versions
immutable, and more. Run it once against each environment before relying on
it:
from cryptography_suite.providers.conformance import verify_provider
verify_provider(AwsKmsProvider(kms), KeyRef(AWS, "alias/conformance-test"))
Writing your own provider is covered in Custom providers.