Skip to content

Instantly share code, notes, and snippets.

@nicksnell
Created April 6, 2023 17:14
Show Gist options
  • Save nicksnell/cd73bf67a666002dc5f787fef0c6ecbc to your computer and use it in GitHub Desktop.
Save nicksnell/cd73bf67a666002dc5f787fef0c6ecbc to your computer and use it in GitHub Desktop.
Decode a KMS value using a specific key
import base64
import boto3
import os
encoded = os.environ.get("ENCODED")
key_arn = os.environ.get("KEY")
profile = os.environ.get("PROFILE")
if key_arn is None or encoded is None:
raise Exception("You must provide a key & encoded values!")
session = boto3.session.Session(profile_name=profile)
kms = session.client("kms")
meta = kms.decrypt(
KeyId=key_arn,
CiphertextBlob=base64.b64decode(encoded)
)
plaintext = meta["Plaintext"]
print(plaintext.decode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment