How exactly do encryption permissions work for SNS and SMS?

, ,

When I use the default aws/sqs KMS key, I can send messages to the queue.

But when I try sending messages from SNS to SQS using the default KMS aws/sqs, it then fails with an error KMS.AccessDeniedException.

It’s very odd. Sometimes it’s like the message receiver (SQS) needs to have access; other times it’s like the message sender needs to have access.

I run into similar oddness when publishing messages to SNS using API Gateway.

-–

The documentation is exceedingly unclear about the permissions.

Who or what needs to have what permissions to the encryption keys in order to send messages in SNS and SQS?

The short answer is that the service SNS needs to be able to decrypt and generate data keys (plaintext and not).

The exact answer is this policy (or a superset of it, natch) must be associated with the key in question:

{
    "Sid": "Allow Amazon SNS to use this key",
    "Effect": "Allow",
    "Principal": {
        "Service": "sns.amazonaws.com"
    },
    "Action": [
        "kms:Decrypt",
        "kms:GenerateDataKey*"
    ],
    "Resource": "*"
}

…which makes me think that the default key is out.

Pretty sure they both need to have access. The publisher of the SNS topic needs to be able to encrypt them, the subscriber to the SNS topic with a SQS, will need to be able to decrypt them.

+1 for this. I just had this issue with SNS/SQS encryption for splunk. Key policy needs to allow additional services to decrypt and gen data key.

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-subscribe-queue-sns-topic.html

Which links to: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-key-management.html#compatibility-with-aws-services

This states

Several AWS services act as event sources that can send events to Amazon SQS queues. To allow these event sources to work with encrypted queues, you must create a customer managed CMK and add permissions in the key policy for the service to use the required AWS KMS API methods.

Also make sure you take note of:

While you can configure global permissions to send to and receive from Amazon SQS, AWS KMS requires explicitly naming the full ARN of CMKs in specific regions in the Resource section of an IAM policy.

When a SQS queue with SSE receives a message,

SQS basically says: “Hey KMS I got a message from X, here’s my Key Id, pls encrypt for me”.

KMS says “Ok but first let me take a look at your Key’s policy to see if X has the permission to encrypt messages to your queue”

SQS says “OK”

KMS says “Because your default kms/sqs key policy does not give X permission, I will return AccessDenied”.

I finally figured this out: it’s caller (sender or receiver). While the caller doesn’t call KMS directly, SQS does so with their permissions when sending/receiving (and it never uses it’s own “Service” permissions for anything to do with encryption).

The default aws/sqs KMS key policy is

{
“Version”: “2012-10-17”,
“Id”: “auto-sqs-1”,
“Statement”: [
{
“Sid”: “Allow access through Simple Queue Service (SQS) for all principals in the account that are authorized to use SQS”,
“Effect”: “Allow”,
“Principal”: {
“AWS”: “"
},
“Action”: [
“kms:Encrypt”,
“kms:Decrypt”,
"kms:ReEncrypt
”,
“kms:GenerateDataKey*”,
“kms:CreateGrant”,
“kms:DescribeKey”
],
“Resource”: “",
“Condition”: {
“StringEquals”: {
“kms:ViaService”: “sqs.us-east-1.amazonaws.com”,
“kms:CallerAccount”: “853263162156”
}
}
},
{
“Sid”: “Allow direct access to key metadata to the account”,
“Effect”: “Allow”,
“Principal”: {
“AWS”: “arn:aws:iam::<account_id>:root”
},
“Action”: [
"kms:Describe
”,
“kms:Get*”,
“kms:List*”,
“kms:RevokeGrant”
],
“Resource”: “*”
}
]
}

So if you want SNS to publish (which has no kms:CallerAccount), you have to create a custom KMS key. A generic policy would be:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "kms:*",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "kms:ViaService": "sqs.us-east-1.amazonaws.com"
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<account_id>:root"
            },
            "Action": "kms:*",
            "Resource": "*"
        }
    ]
}

This allows anyone SQS thinks should encrypt to encrypt.

The only downside with this generic policy that it allows anyone who knows your key id to use it for their own SQS queues (causing a billing problem, not a confidentiality problem).

If you wanted to more exact, you’d basically just have to repeat the same thing you use for your sqs:SendMessage resource-based and identity-based policies.


For SNS, I believe the caller needs Encrypt permissions and the SNS service needs decrypt permissions.