Amazon SQS キューにおけるサーバー側の暗号化の使用 - Amazon Simple Queue Service

Amazon SQS キューにおけるサーバー側の暗号化の使用

Amazon SQS キューにサーバーサイドの暗号化 (SSE)を追加する場合にはAWS SDK for Java を使用します。各キューは AWS Key Management Service (AWS KMS) KMS キーを使用してデータ暗号化キーを生成します。この例では AWS Amazon SQS のマネージド KMS キーを使用します。

SSE および KMS キーのロールの使用における詳しい情報については、「Amazon SQS での保管中の暗号化」をご参照ください。

既存のキューに SSE を追加

既存のキューにサーバー側の暗号化を有効にする場合、SetQueueAttributes メソッドで KmsMasterKeyId 属性を設定します。

以下のコード例では、AWS KMS key を Amazon SQS の AWS マネージド KMS キーとして設定します。また、この例では、AWS KMS key 再利用期間を 140 秒に設定します。

サンプルコードを実行する前に AWS の認証情報を設定したことをご確認ください。詳細については、AWS SDK for Java 2.x デベロッパーガイドの「ディベロップメントのAWS 認証情報とリージョンのセットアップ」を参照してください。

public static void addEncryption(String queueName, String kmsMasterKeyAlias) { SqsClient sqsClient = SqsClient.create(); GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder() .queueName(queueName) .build(); GetQueueUrlResponse getQueueUrlResponse; try { getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest); } catch (QueueDoesNotExistException e) { LOGGER.error(e.getMessage(), e); throw new RuntimeException(e); } String queueUrl = getQueueUrlResponse.queueUrl(); Map<QueueAttributeName, String> attributes = Map.of( QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias, QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140" // Set the data key reuse period to 140 seconds. ); // This is how long SQS can reuse the data key before requesting a new one from KMS. SetQueueAttributesRequest attRequest = SetQueueAttributesRequest.builder() .queueUrl(queueUrl) .attributes(attributes) .build(); try { sqsClient.setQueueAttributes(attRequest); LOGGER.info("The attributes have been applied to {}", queueName); } catch (InvalidAttributeNameException | InvalidAttributeValueException e) { LOGGER.error(e.getMessage(), e); throw new RuntimeException(e); } finally { sqsClient.close(); } }

キューのSSEの無効化

既存のキューに対してサーバーサイドの暗号化を無効にするには、SetQueueAttributes メソッドを使用して、KmsMasterKeyId属性を空の文字列に設定します。

重要

null は、の有効な値ではありません。KmsMasterKeyId

SSEを使用してキューを作成する

キューの作成時にSSEを有効にするには、API メソッドKmsMasterKeyIdに属性CreateQueueを追加します。

以下の例では、SSE を有効にして新しいキューを作成する方法を示します。このキューは、Amazon SQS の AWS マネージド KMS キーを使用します。また、この例では、AWS KMS key 再利用期間を 160 秒に設定します。

サンプルコードを実行する前に AWS の認証情報を設定したことをご確認ください。詳細については、AWS SDK for Java 2.x デベロッパーガイドの「ディベロップメントの AWS 認証情報とリージョンのセットアップ」を参照してください。

// Create an SqsClient for the specified Region. SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build(); // Create a hashmap for the attributes. Add the key alias and reuse period to the hashmap. HashMap<QueueAttributeName, String> attributes = new HashMap<QueueAttributeName, String>(); final String kmsMasterKeyAlias = "alias/aws/sqs"; // the alias of the AWS managed KMS key for Amazon SQS. attributes.put(QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias); attributes.put(QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140"); // Add the attributes to the CreateQueueRequest. CreateQueueRequest createQueueRequest = CreateQueueRequest.builder() .queueName(queueName) .attributes(attributes) .build(); sqsClient.createQueue(createQueueRequest);

SSE属性の取得

キュー属性の取得の詳細については、Amazon Simple キューサービス API リファレンスをご参照ください。

特定のキューの KMS キー ID またはデータキーの再利用期間を取得する場合、GetQueueAttributes メソッドを実行して KmsMasterKeyId および KmsDataKeyReusePeriodSeconds 値を取得します。