KMS

Encryption, key management, aliases, grants, real ECDH, key import.

fakecloud implements 56 of 56 KMS operations at 100% Smithy conformance.

Supported features

  • Symmetric keys — CreateKey, Encrypt, Decrypt, GenerateDataKey, ReEncrypt. Real AES-256-GCM envelope encryption with AWS-shaped ciphertext blobs (key id header + IV + ciphertext + tag).
  • Asymmetric keys — Sign, Verify, GetPublicKey across:
    • RSA — RSA_2048, RSA_3072, RSA_4096 with real RSA Sign/Verify/GetPublicKey, and real RSA-OAEP Encrypt/Decrypt/ReEncrypt for ENCRYPT_DECRYPT keys. As on AWS, the ciphertext is the raw RSA-OAEP output (one modulus long, interchangeable with ciphertext produced outside KMS with the public key), which names no key: Decrypt needs KeyId and EncryptionAlgorithm (ReEncrypt SourceKeyId and SourceEncryptionAlgorithm), and without the key the request is InvalidCiphertextException (the self-describing fakecloud-rsa: ciphertexts earlier fakecloud versions produced still open without it, so persisted data survives an upgrade)
    • ECC — ECC_NIST_P256, ECC_NIST_P384, ECC_NIST_P521, and ECC_SECG_P256K1 with real ECDSA Sign/Verify/GetPublicKey
  • Key management — DescribeKey, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion
  • Aliases — CRUD with alias/ prefix validation. Aliases are regional, as in AWS: the same alias name can exist independently in every region, ListAliases pages through the request region's aliases only, and an alias must point at a key in its own region. An alias name resolves in the caller's region and an alias ARN in the region it names, anywhere a key id/ARN is accepted (Encrypt, Decrypt, Sign, Verify, GenerateDataKey, ReEncrypt, grants, key policies, cross-service KmsKeyId, which resolves in the resource's region).
  • Grants - CreateGrant, RetireGrant, RevokeGrant, ListGrants (ListGrants / ListRetirableGrants name each grant's key by its key ARN, a replica's grant by the replica's). Under --iam strict a grant authorizes direct KMS calls as on AWS: its grantee (an IAM user, a role and its assumed-role sessions, in the key's account or another) may perform the grant's operations on the key without any IAM or key-policy permission, within its EncryptionContextSubset / EncryptionContextEquals constraints and its SourceArn constraint (which allows the grant's operations only on behalf of that resource, DescribeKey included and RetireGrant excepted, so a grant bound to a resource authorizes no direct API call); a grant to an AWS service principal (GranteeServicePrincipal, which needs a SourceArn constraint and a RetiringPrincipal or RetiringServicePrincipal; CreateGrant refuses a SourceArn off the model's ARN pattern and each invalid grantee/retiring combination with the ValidationException messages recorded against KMS, before it looks up the key) lets that service use the key when it acts itself for that resource through the KMS hook (ListGrants filters by GranteePrincipal or GranteeServicePrincipal, ListRetirableGrants by RetiringPrincipal or RetiringServicePrincipal); a grant naming an account only lets that account delegate through IAM policies; an explicit Deny in the key policy or an IAM policy still wins, an SCP that does not allow the action caps it, and the role's permissions boundary and session policies cap a grant to a role; RevokeGrant / RetireGrant remove the access at once. A grant names an IAM user or role grantee by its unique ID, as AWS does: a user or role deleted and recreated under the same name is a new principal the old grant does not cover, and once the grantee is gone ListGrants shows its unique ID (AIDA... / AROA...) as GranteePrincipal. Each key of a multi-Region set has its own grants and key policy: a bare key id names the replica in its region, and a multi-Region ciphertext is decrypted (directly or by a service) by the related key of the region of the call, under that key's policy and grants. A grant holding CreateGrant lets its grantee create only grants for some of its operations with constraints at least as strict. CreateGrant refuses encryption-context constraints (ValidationException) on a non-symmetric key or alongside an operation that takes no encryption context (only DescribeKey, RetireGrant and CreateGrant may join them). RetireGrant is allowed to the grant's retiring principal, to its grantee when the grant allows RetireGrant, and to principals of the key's account whose IAM policies allow it. See IAM enforcement.
  • Cross-account keys - a principal of another account names a key by its key ARN or alias ARN (key ARN only for CreateGrant, GetKeyRotationStatus, ListGrants, RevokeGrant, RetireGrant, which may also name its grant by GrantToken alone) for the operations AWS allows across accounts: the cryptographic operations, DescribeKey and GetPublicKey. The request is served in the key's account and authorized by its key policy plus the caller's IAM policies, or by a grant. A symmetric ciphertext sealed for such a caller names the key by its ARN, so its Decrypt needs no KeyId (also for a key with imported material). A key id or alias name always means a key of the caller's own account. ReEncrypt takes its source and destination keys from any accounts (the caller's or others', each by its ARN), authorized as kms:ReEncryptFrom on the source key and kms:ReEncryptTo on the destination key, each by that key's policy and grants; the ciphertext is opened in the source key's account and resealed in the destination key's.
  • Key rotation — automatic rotation flag (tracked), on-demand rotation
  • Key policies — PutKeyPolicy, GetKeyPolicy, ListKeyPolicies
  • Tags — on keys
  • Real ECDH — DeriveSharedSecret performs actual Elliptic Curve Diffie-Hellman
  • Key import — GetParametersForImport, ImportKeyMaterial with real key material handling
  • Custom key stores — CRUD (records only)
  • Key replica — ReplicateKey
  • Cross-service KMS hook - S3 (SSE-KMS), SQS, SNS, DynamoDB, Secrets Manager, SSM SecureString and the SES receipt-rule S3 action call into KMS for real encrypt/decrypt under the hood. Every call is recorded at /_fakecloud/kms/usage so test code can assert which service principal triggered which operation on which key with which encryption context. AWS-managed keys (aws/s3, aws/sqs, aws/sns, aws/dynamodb, aws/secretsmanager, aws/ssm, ...) exist from first use, one per account and region, which that region's alias/aws/<service> names. As on AWS, AWS-managed keys cannot be managed: PutKeyPolicy, ScheduleKeyDeletion, CancelKeyDeletion, EnableKey / DisableKey, the rotation operations, TagResource / UntagResource, UpdateKeyDescription, CreateGrant, CreateAlias / UpdateAlias targeting one, ReplicateKey, UpdatePrimaryRegion and the imported-material operations answer AccessDeniedException ("... because no resource-based policy allows the kms: action"). A resource can name another account's customer key by its key ARN or alias ARN (an SQS queue's KmsMasterKeyId, a secret's KmsKeyId): the key is used in its own account and region, and only when its key policy allows the caller (the calling IAM user or role, an assumed-role session matching its role, when the service knows it; else the resource's account, which an account-root or bare account-id principal names) or a grant on the key does (grantee principal, operations and EncryptionContextSubset / EncryptionContextEquals constraints honoured); otherwise the service reports KMS's AccessDeniedException naming the key. Another account's aws/<service> alias ARN creates no key there and is refused, as AWS-managed keys are never usable across accounts. A key id or alias name resolves in the resource's own account.
  • AWS-managed keys - the alias/aws/<service> keys of every service AWS has one for (acm, backup, codeartifact, codecommit, dms, dynamodb, ebs, ecr, elasticache, elasticfilesystem, es, glue, kafka, kinesis, kinesisvideo, lambda, rds, redshift, s3, secretsmanager, ses, sns, sqs, ssm, timestream, xray; another name such as alias/aws/logs is NotFoundException "Alias is not found.", as on AWS) carry the key policy and description AWS gives them, recorded from a real account. As on AWS, ListAliases lists the predefined aliases (aws/dynamodb, aws/ebs, aws/elasticfilesystem, aws/es, aws/glue, aws/kinesisvideo, aws/rds, aws/redshift, aws/s3, aws/ssm, aws/xray) without a TargetKeyId until first used; the key is created the first time it is used, directly (DescribeKey, Encrypt, ...) or by its service. GetKeyPolicy takes a key id or ARN (an alias is InvalidArnException). The policy grants Encrypt, Decrypt, ReEncrypt* and GenerateDataKey* only to principals of the owning account calling through the owning service (kms:ViaService = <service>.<region>.amazonaws.com, kms:CallerAccount = the account), and fakecloud evaluates it on every use: a direct Encrypt, Decrypt, GenerateDataKey* or ReEncrypt on alias/aws/s3 is refused with AccessDeniedException ("User: ... is not authorized to perform: kms:Encrypt on resource: because no resource-based policy allows the kms:Encrypt action") whatever the IAM mode, while each service keeps using its own key through the KMS hook, which calls KMS through the calling service (so one service cannot use another's aws/* key) or, for SES, as the SES service principal. The aws/lambda key also lets account principals decrypt ciphertexts whose encryption context names one of the account's functions, and aws/ses lets them decrypt the mail SES sealed, as their policies say. A key policy evaluated for a service's use of a key sees kms:ViaService, kms:CallerAccount (the caller's own account, not the account of the bucket or queue the service acts on), the request's kms:EncryptionContext:* keys and the caller's global keys (aws:PrincipalArn, which for an assumed-role session is its role, aws:PrincipalAccount, aws:PrincipalType, aws:userid, aws:PrincipalTag/*, aws:PrincipalOrgID), whatever the IAM mode; direct KMS API calls see kms:CallerAccount and kms:EncryptionContext:* under --iam strict.
  • Service ciphertexts - ciphertexts the hook seals bind the service's encryption context like KMS ciphertexts (S3 aws:s3:arn, SQS aws:sqs:arn, SNS aws:sns:topicArn, Secrets Manager SecretARN + SecretVersionId, SSM PARAMETER_ARN, DynamoDB aws:dynamodb:tableName + aws:dynamodb:subscriberId, Lambda aws:lambda:FunctionArn), so the KMS API decrypts one only with that context (InvalidCiphertextException otherwise). Context-free service ciphertexts persisted before this binding still decrypt after an upgrade. A failed KMS call is reported by each service with its own declared error (SQS KMS.AccessDeniedException / KMS.DisabledException / KMS.InvalidKeyUsageException / KMS.InvalidStateException / KMS.NotFoundException, SNS KMSAccessDenied / KMSDisabled / KMSInvalidState / KMSNotFound, S3 AccessDenied / KMS.<code>, SSM AccessDeniedException / InvalidKeyId, Secrets Manager AccessDeniedException / InvalidParameterException / EncryptionFailure / DecryptionFailure, DynamoDB CreateTable / UpdateTable AccessDeniedException / ValidationException), relaying KMS's message. A refusal names the identity the service acted for (the IAM user or assumed role that called it, the account root when the service acts for the account). Under --iam strict the caller's SCPs cap that key use too, as the KMS request is the caller's (kms:ViaService names the service): an SCP refusal is named as such in the message. Services that deliver into SQS as their own service principal (SNS subscriptions, EventBridge rule targets, S3 event notifications) need the queue key's policy to name that principal: a queue under the AWS-managed aws/sqs key refuses them, as on AWS, so SNS delivers nothing and counts AWS/SNS NumberOfNotificationsFailed (and uses the subscription's dead-letter queue), EventBridge delivers nothing, counts AWS/Events FailedInvocations and sends the event to the target's DeadLetterConfig queue (message attributes ERROR_CODE NO_PERMISSIONS, ERROR_MESSAGE, RULE_ARN, TARGET_ARN; InvocationsSentToDlq / InvocationsFailedToBeSentToDlq), and S3 refuses the notification configuration with InvalidArgument "Unable to validate the following destination configurations"; a customer key whose policy grants the principal works, and the account itself (or a role a service acts as: Scheduler, Pipes, Step Functions, Lambda destinations) still uses aws/sqs. SSE-SQS queues (SqsManagedSseEnabled, the default) encrypt bodies at rest with an SQS-owned key and never call KMS, so service principals deliver to them freely and no aws/sqs key is created for them. Secrets Manager encrypts every secret, one created without KmsKeyId under the region's aws/secretsmanager key (also when CloudFormation creates it); secrets stored in plaintext by an older fakecloud still read back. A stored ciphertext that does not open (disabled or deleted key, refused key) fails the read with the service's error, never reads as its ciphertext, and an SQS message keeps its seal when it moves to an unencrypted dead-letter queue, so it is still decrypted there.
  • Default keys on encrypted resources - a resource encrypted at rest without a named key reports the ARN of its service's AWS-managed key, a real key minted once per account and region that DescribeKey alias/aws/<service> resolves to: aws/rds for RDS instances and clusters, DocumentDB and Neptune clusters, aws/ebs for EBS volumes and snapshots (or the account's EBS default key when ModifyEbsDefaultKmsKeyId customized it), and aws/elasticfilesystem, aws/redshift, aws/dms, aws/kafka, aws/timestream, aws/codeartifact, aws/codecommit for their services. A key named by alias (resolved in the resource's region) or key id is reported as the ARN of the key it names. CloudFormation-provisioned resources report the same keys.
  • Service Quotas enforcement (opt-in): with "Customer Master Keys (CMKs)" (L-C2F1777E) enforced in Service Quotas, CreateKey and ReplicateKey are refused with LimitExceededException once the region holds that many customer managed keys (any key state, pending deletion included; AWS managed keys do not count). Off by default
  • Introspection

    • GET /_fakecloud/kms/usage — list every cross-service KMS call (operation, service principal, key ARN, encryption context). Useful for asserting that your code path actually triggered KMS encrypt/decrypt under the hood.

    Protocol

    JSON protocol. X-Amz-Target header, JSON body, JSON responses.

    Gotchas

    • Real AES-256-GCM, AWS-shaped ciphertext. Encrypt/Decrypt round-trip across process restarts as long as the same key material is available. Ciphertext layout mimics AWS (key id header, IV, ciphertext, GCM tag) so length and shape match what real callers expect, but blobs are not interchangeable with real AWS KMS.

    Source