0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

KMSで暗号化したSNS Topicに投入したメッセージをKMSで暗号化したSQS Queueに投入する

Posted at

はじめに

* こちらの手順に則って作業します。
* (けど、上記の手順は昔のコンソール用の手順)
* 構築が終わったら、Resource Policyをガチャガチャいじる予定

Custom CMKを作る

  1. KMS のコンソールにアクセス
  2. Create Keyをクリック
  3. Key typeはSymmetricを選択し、Key Material OriginはKMSを選択する
  4. AliasにMyCustomCMKを入力する
  5. Key AdministratorにIAM_ROLE_DEFAULT(自分で作ったIAM ROLEかIAM USER)を選択する
  6. Key Usage PermissionにIAM_ROLE_DEFAULT(キーの使用者に該当。検証用なので、Administratorと同一)を選択する
  7. Reviewで、SNSをPrincipal指定したStatementを追加する

{
    "Id": "key-consolepolicy-3",
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Enable IAM User Permissions",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::xxxxxxxxxxxx:root"
            },
            "Action": "kms:*",
            "Resource": "*"
        },
        {
            "Sid": "Allow access for Key Administrators",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam:: xxxxxxxxxxxx:role/IAM_ROLE_DEFAULT"
            },
            "Action": [
                "kms:Create*",
                "kms:Describe*",
                "kms:Enable*",
                "kms:List*",
                "kms:Put*",
                "kms:Update*",
                "kms:Revoke*",
                "kms:Disable*",
                "kms:Get*",
                "kms:Delete*",
                "kms:TagResource",
                "kms:UntagResource",
                "kms:ScheduleKeyDeletion",
                "kms:CancelKeyDeletion"
            ],
            "Resource": "*"
        },
        {
            "Sid": "Allow use of the key",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam:: xxxxxxxxxxxx:role/IAM_ROLE_DEFAULT"
            },
            "Action": [
                "kms:Encrypt",
                "kms:Decrypt",
                "kms:ReEncrypt*",
                "kms:GenerateDataKey*",
                "kms:DescribeKey"
            ],
            "Resource": "*"
        },
        {
            "Sid": "Allow attachment of persistent resources",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam:: xxxxxxxxxxxx:role/IAM_ROLE_DEFAULT"
            },
            "Action": [
                "kms:CreateGrant",
                "kms:ListGrants",
                "kms:RevokeGrant"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "kms:GrantIsForAWSResource": "true"
                }
            }
        },
        {
            "Sid": "Allow Amazon SNS to use this key",
            "Effect": "Allow",
            "Principal": {
                "Service": "sns.amazonaws.com"
            },
            "Action": [
                "kms:Decrypt",
                "kms:GenerateDataKey*"
            ],
            "Resource": "*"
        }
    ]
}

SSEが有効なSNS Topicを作る

  1. SNSのコンソールにアクセス
  2. Create Topicをクリックする
  3. Topic NameにMyEncryptedTopicを入力する
  4. EncryptionでEnable encryptionを選択し、フォームにalias/MyCustomCMK(さっき作ったCMKのalias)を入力する
  5. Create Topicをクリックする

SSEが有効なSQS Queueを作る

  1. SQSのコンソールにアクセス
  2. Create New Queueをクリックする
  3. Queue NameにMyEncryptedQueueを入力し、Configure Queueをクリックする
  4. SSE SettingsでUse SSEをチェックし、AWS KMS CMKでMyCustomCMK(さっき作ったCMKの名前)を選択する
  5. Create Queueをクリックする

SNS Topicへのサブスクライブ

  1. SQSのコンソールにアクセスする
  2. MyEncryptedQueueを選択、Queue ActionからSubscribe Queue to SNS Topicをクリックする
  3. Choose a TopicからMyEncryptedを選択する
  4. Subscribeをクリックする

SSEが有効なSNS Topicにメッセージをパブリッシュする

  1. SNSのコンソールにアクセスする
  2. TopicsからMyEncryptedTopicを選択し、Publish Messageをクリックする
  3. SubjectにTest Message Subject、Message BodyにTest Message Bodyを入力し、Publish Messageをクリックする

SSEが有効なSQS Queueにキューイングされていることを確認する

  1. SQSのコンソールにアクセスする
  2. MyEncryptedQueueを選択し、Queue ActionからView/Delete Messagesをクリックする
  3. Start Polling for Messagesをクリックし、Test Message Subjectがキューイングされていることを確認できる

(参考)各オブジェクトのJSON

sqs get-queue-attributes


{
    "Attributes": {
        "ApproximateNumberOfMessagesNotVisible": "0", 
        "Policy": "{\"Version\":\"2012-10-17\",\"Id\":\"arn:aws:sqs:ap-northeast-1: xxxxxxxxxxxx:MyEncryptedQueue/SQSDefaultPolicy\",\"Statement\":[{\"Sid\":\"Sid1594222624729\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:ap-northeast-1: xxxxxxxxxxxx:MyEncryptedQueue\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:ap-northeast-1: xxxxxxxxxxxx:MyEncryptedTopic\"}}}]}", 
        "KmsMasterKeyId": "arn:aws:kms:ap-northeast-1: xxxxxxxxxxxx:key/73f9e34b-a538-4049-91e0-yyyyyyyyyyyy", 
        "MessageRetentionPeriod": "345600", 
        "ApproximateNumberOfMessagesDelayed": "0", 
        "KmsDataKeyReusePeriodSeconds": "300", 
        "MaximumMessageSize": "262144", 
        "CreatedTimestamp": "1594222364", 
        "ApproximateNumberOfMessages": "1", 
        "ReceiveMessageWaitTimeSeconds": "0", 
        "DelaySeconds": "0", 
        "VisibilityTimeout": "30", 
        "LastModifiedTimestamp": "1594222626", 
        "QueueArn": "arn:aws:sqs:ap-northeast-1:xxxxxxxxxxxx:MyEncryptedQueue"
    }
}

sns get-topic-attributes


{
    "Attributes": {
        "KmsMasterKeyId": "arn:aws:kms:ap-northeast-1: xxxxxxxxxxxx:key/73f9e34b-a538-4049-91e0-yyyyyyyyyyyy", 
        "DisplayName": "", 
        "SubscriptionsDeleted": "0", 
        "EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}", 
        "Owner": "xxxxxxxxxxxx", 
        "SubscriptionsConfirmed": "1", 
        "Policy": "{\"Version\":\"2008-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:GetTopicAttributes\",\"SNS:SetTopicAttributes\",\"SNS:AddPermission\",\"SNS:RemovePermission\",\"SNS:DeleteTopic\",\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:Publish\",\"SNS:Receive\"],\"Resource\":\"arn:aws:sns:ap-northeast-1: xxxxxxxxxxxx:MyEncryptedTopic\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"xxxxxxxxxxxx\"}}}]}", 
        "TopicArn": "arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:MyEncryptedTopic", 
        "SubscriptionsPending": "0"
    }
}

おしまい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?