AwsEncryptionSdk.Keyring.KmsKeyArn (AWS Encryption SDK v0.7.0)
View SourceAWS KMS Key ARN parsing, validation, and MRK matching utilities.
Implements the AWS Encryption SDK specification for KMS key identifiers:
- https://github.com/awslabs/aws-encryption-sdk-specification/blob/master/framework/aws-kms/aws-kms-key-arn.md
- https://github.com/awslabs/aws-encryption-sdk-specification/blob/master/framework/aws-kms/aws-kms-mrk-match-for-decrypt.md
ARN Format
AWS KMS ARNs follow the format:
arn:partition:kms:region:account:resource-type/resource-id
Example: arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab
Multi-Region Keys (MRK)
Multi-Region keys have resource IDs that start with mrk-. They can be used
interchangeably across regions for decrypt operations.
Summary
Functions
Checks if a string looks like an ARN (starts with "arn:").
Determines if a key identifier represents a Multi-Region Key (MRK).
Determines if two key identifiers match for decrypt purposes.
Parses an AWS KMS ARN string into a structured format.
Reconstructs an ARN string from a parsed KmsKeyArn struct.
Types
Functions
Checks if a string looks like an ARN (starts with "arn:").
Examples
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.arn?("arn:aws:kms:us-west-2:123:key/abc")
true
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.arn?("mrk-123")
false
Determines if a key identifier represents a Multi-Region Key (MRK).
Accepts either a parsed KmsKeyArn struct or a string identifier.
Parameters
arn_or_identifier- AKmsKeyArnstruct or string key identifier
Returns
trueif the identifier represents an MRKfalseotherwise
Rules
For ARN structs:
- Resource type "alias" always returns false
- Resource type "key" with ID starting with "mrk-" returns true
- Otherwise returns false
For string identifiers:
- Strings starting with "arn:" are parsed and checked as ARNs
- Strings starting with "alias/" return false
- Strings starting with "mrk-" return true
- All other strings return false
Examples
iex> {:ok, arn} = AwsEncryptionSdk.Keyring.KmsKeyArn.parse("arn:aws:kms:us-west-2:123:key/mrk-abc")
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.mrk?(arn)
true
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.mrk?("mrk-abc123")
true
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.mrk?("alias/my-key")
false
Determines if two key identifiers match for decrypt purposes.
This implements the AWS KMS MRK Match for Decrypt algorithm. Two identifiers match if:
- They are identical strings, OR
- Both are Multi-Region keys with the same partition, service, account, resource type, and resource ID (region may differ)
Parameters
identifier_a- First AWS KMS key identifier (ARN or raw ID)identifier_b- Second AWS KMS key identifier (ARN or raw ID)
Returns
trueif the identifiers match for decrypt purposesfalseotherwise
Examples
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.mrk_match?(
...> "arn:aws:kms:us-west-2:123:key/mrk-abc",
...> "arn:aws:kms:us-east-1:123:key/mrk-abc"
...> )
true
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.mrk_match?(
...> "arn:aws:kms:us-west-2:123:key/mrk-abc",
...> "arn:aws:kms:us-west-2:123:key/normal-key"
...> )
false
Parses an AWS KMS ARN string into a structured format.
Parameters
arn_string- A string containing an AWS KMS ARN
Returns
{:ok, t()}on successful parsing{:error, reason}on validation failure
Errors
{:error, :invalid_prefix}- ARN does not start with "arn"{:error, :empty_partition}- Partition component is empty{:error, :empty_service}- Service component is empty{:error, :invalid_service}- Service is not "kms"{:error, :empty_region}- Region component is empty{:error, :empty_account}- Account component is empty{:error, :invalid_resource_section}- Resource section missing "/" separator{:error, :empty_resource_type}- Resource type is empty{:error, :invalid_resource_type}- Resource type not "alias" or "key"{:error, :empty_resource_id}- Resource ID is empty
Examples
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.parse("arn:aws:kms:us-west-2:123456789012:key/1234abcd")
{:ok, %AwsEncryptionSdk.Keyring.KmsKeyArn{
partition: "aws",
service: "kms",
region: "us-west-2",
account: "123456789012",
resource_type: "key",
resource_id: "1234abcd"
}}
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.parse("invalid")
{:error, :invalid_prefix}
Reconstructs an ARN string from a parsed KmsKeyArn struct.
Examples
iex> {:ok, arn} = AwsEncryptionSdk.Keyring.KmsKeyArn.parse("arn:aws:kms:us-west-2:123:key/abc")
iex> AwsEncryptionSdk.Keyring.KmsKeyArn.to_string(arn)
"arn:aws:kms:us-west-2:123:key/abc"