AwsEncryptionSdk.Keyring.KmsClient behaviour (AWS Encryption SDK v0.7.0)
View SourceBehaviour for AWS KMS client implementations.
This module defines the interface for KMS operations required by AWS KMS keyrings.
Implementations must provide generate_data_key/5, encrypt/5, and decrypt/5.
Implementations
AwsEncryptionSdk.Keyring.KmsClient.ExAws- Production client using ExAwsAwsEncryptionSdk.Keyring.KmsClient.Mock- Test mock for unit testing
Example
# Using ExAws client
{:ok, client} = KmsClient.ExAws.new(region: "us-east-1")
{:ok, result} = KmsClient.ExAws.generate_data_key(
client,
"arn:aws:kms:us-east-1:123456789012:key/abc123",
32,
%{"purpose" => "encryption"},
[]
)
Summary
Types
Result of Decrypt operation.
Result of Encrypt operation.
Encryption context - key-value pairs for additional authenticated data
Result of GenerateDataKey operation.
Grant tokens for temporary permissions
AWS KMS key identifier (ARN, alias ARN, alias name, or key ID)
KMS operation error with descriptive information
Callbacks
Decrypts data that was encrypted with a KMS key.
Encrypts data using a KMS key.
Generates a unique data key for encryption.
Types
Result of Decrypt operation.
:plaintext- The decrypted data:key_id- The ARN of the KMS key that was used
Result of Encrypt operation.
:ciphertext- The encrypted data (ciphertext blob):key_id- The ARN of the KMS key that was used
Encryption context - key-value pairs for additional authenticated data
@type generate_data_key_result() :: %{ plaintext: binary(), ciphertext: binary(), key_id: String.t() }
Result of GenerateDataKey operation.
:plaintext- The plaintext data key (unencrypted):ciphertext- The encrypted data key (ciphertext blob):key_id- The ARN of the KMS key that was used
@type grant_tokens() :: [String.t()]
Grant tokens for temporary permissions
@type key_id() :: String.t()
AWS KMS key identifier (ARN, alias ARN, alias name, or key ID)
@type kms_error() :: {:kms_error, atom(), String.t()} | {:http_error, integer(), String.t()} | {:connection_error, term()}
KMS operation error with descriptive information
Callbacks
@callback decrypt( client :: struct(), key_id :: key_id(), ciphertext :: binary(), encryption_context :: encryption_context(), grant_tokens :: grant_tokens() ) :: {:ok, decrypt_result()} | {:error, kms_error()}
Decrypts data that was encrypted with a KMS key.
Calls the AWS KMS Decrypt API to decrypt the provided ciphertext.
Parameters
client- The KMS client structkey_id- KMS key identifier (must match the key used for encryption)ciphertext- Encrypted data (ciphertext blob from Encrypt or GenerateDataKey)encryption_context- Must match the context used during encryptiongrant_tokens- Optional grant tokens for temporary permissions
Returns
{:ok, result}with plaintext and key_id{:error, reason}on failure
@callback encrypt( client :: struct(), key_id :: key_id(), plaintext :: binary(), encryption_context :: encryption_context(), grant_tokens :: grant_tokens() ) :: {:ok, encrypt_result()} | {:error, kms_error()}
Encrypts data using a KMS key.
Calls the AWS KMS Encrypt API to encrypt the provided plaintext.
Parameters
client- The KMS client structkey_id- KMS key identifier (ARN, alias, or key ID)plaintext- Data to encrypt (max 4096 bytes for direct encryption)encryption_context- Key-value pairs bound to the ciphertextgrant_tokens- Optional grant tokens for temporary permissions
Returns
{:ok, result}with ciphertext and key_id{:error, reason}on failure
@callback generate_data_key( client :: struct(), key_id :: key_id(), number_of_bytes :: pos_integer(), encryption_context :: encryption_context(), grant_tokens :: grant_tokens() ) :: {:ok, generate_data_key_result()} | {:error, kms_error()}
Generates a unique data key for encryption.
Calls the AWS KMS GenerateDataKey API to create a new data key. Returns both the plaintext key (for immediate use) and the encrypted key (for storage).
Parameters
client- The KMS client structkey_id- KMS key identifier (ARN, alias, or key ID)number_of_bytes- Length of the data key in bytes (typically 32 for AES-256)encryption_context- Key-value pairs bound to the ciphertextgrant_tokens- Optional grant tokens for temporary permissions
Returns
{:ok, result}with plaintext, ciphertext, and key_id{:error, reason}on failure