|
| 1 | +## Customer Managed Key (CMK) support |
| 2 | + |
| 3 | +This folder contains CloudFormation templates to help create the IAM Policies and KMS Keys to enable CMK in an |
| 4 | +Amazon Managed Service for Apache Flink application. |
| 5 | + |
| 6 | +**Important**: Setting up CMK following the Principle of Least Privilege requires setting up at least two IAM Users or Roles |
| 7 | +and executing a number of steps, [described below](#setting-up-cmk-on-an-application), in order and with different users. |
| 8 | +This process cannot be automated using a simple CFN template. |
| 9 | +The provided CFN templates help create the required IAM Policies and KMS Key with the correct policy. The full process |
| 10 | +requires additional steps which can be done manually, using the AWS CLI/API, the Console, or any automation tool. |
| 11 | + |
| 12 | + |
| 13 | +For further information, see the service documentation: [Key Management in Amazon Managed Service for Apache Flink](https://docs.aws.amazon.com/managed-flink/latest/java/key-management-flink.html). |
| 14 | + |
| 15 | +### Background |
| 16 | + |
| 17 | +#### Actors |
| 18 | + |
| 19 | +To use CMK following the Principle of Least Privilege implies multiple and separate actors: |
| 20 | + |
| 21 | +1. The KMS **Customer Managed Key** (CMK), which comprises a Key Policy and the actual Key Material used for encryption. |
| 22 | +2. A **Key Administrator** IAM Role or User, who creates and manages the CMK. |
| 23 | +3. An **Operator** IAM Role or User, who uses the Managed Flink API (`kinesisanalyticsv2`) to control the lifecycle of the application, including configuring it and enabling CMK. |
| 24 | +4. The Managed Flink **Application** itself, which has an IAM Service Role attached to control the permissions of the application. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +#### Dependencies between components |
| 29 | + |
| 30 | +Some of these components refer to other components, causing dependencies: |
| 31 | + |
| 32 | +* The *Operator* IAM Policy contains a reference to the *Application* and the *CMK* ARNs. |
| 33 | +* The *CMK Key Policy* contains a reference to the *Operator* User or Role, the *Key Administrator* User or Role, and the *Application* ARN. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +Because of these dependencies, setting up CMK for an application must be done in a precise order, as described in the following section. |
| 38 | + |
| 39 | + |
| 40 | +### Setting up CMK on an application |
| 41 | + |
| 42 | +1. Create the *Operator's Base IAM Policy* using the [msf-base-operator-iam-policy.yaml](./msf-base-operator-iam-policy.yaml) CFN template. |
| 43 | +2. Attach the Base Policy to the Operator's User or Role. |
| 44 | +3. If the application doesn't exist yet, use the Operator's credentials to create an application _without_ CMK. |
| 45 | +4. Create the *Key Administrator's IAM Policy* using [kms-key-administrator-policy.yaml](./kms-key-administrator-policy.yaml) (see Note 3 below). |
| 46 | +5. Attach the Policy to the IAM User/Role you are going to use to manage the CMK key. |
| 47 | +6. Use the Key Administrator's credentials to create the CMK, using [msf-kms-key.yaml](./msf-kms-key.yaml). The template will ask you to provide the application name, the Operator's Role/User, and the Key Administrator's Role/User ARNs. |
| 48 | +7. Create the *Operator's CMK IAM Policy* using [msf-cmk-operator-iam-policy.yaml](./msf-cmk-operator-iam-policy.yaml). |
| 49 | +8. Attach the new policy to the Operator's User/Role without removing the Base policy. The template will ask you to provide the application name and the CMK ID. |
| 50 | +9. Use the Operator's credentials to update the application, enabling CMK with the key you created in step 6. |
| 51 | + |
| 52 | +#### ⚠️ Important notes |
| 53 | + |
| 54 | +Note 1: No CFN template is provided for steps 2, 3, 5, 7, and 9. These steps depend on your setup and whether you are using IAM Users or Roles as Operator and Key Administrator. You can use the AWS CLI, the Console, or any automation tool for these steps. Refer to the *Using Customer Managed Key in Amazon Managed Service for Apache Flink* service documentation page for setting CMK on an existing application. |
| 55 | + |
| 56 | +Note 2: The *Operator's Base IAM Policy* defined by [msf-base-operator-iam-policy.yaml](./msf-base-operator-iam-policy.yaml) gives the Operator broad permissions to control any application in the account and attach any service role to the application. You can restrict this policy with further conditions as required. |
| 57 | + |
| 58 | +Note 3: The CFN template [msf-kms-key.yaml](./msf-kms-key.yaml) which creates the CMK must be run using the Key Administrator role/user. This user must have at minimum the permissions defined by the Key Administrator IAM Policy created using the [kms-key-administrator-policy.yaml](./kms-key-administrator-policy.yaml) CFN template. It must also have any permissions required to create the stack, for example to upload the template to S3 if you are creating the stack from the console. |
| 59 | + |
| 60 | +### Testing CMK |
| 61 | + |
| 62 | +Setting up CMK following the Principle of Least Privilege, as described above, makes it difficult to simply test CMK where more lenient security policies can be applied, such as in non-production environments. |
| 63 | +If you want to use CMK with a single user having broad permissions acting both as *Operator* and *Key Administrator*, you can apply the following Key Policy when you create the CMK Key. |
| 64 | + |
| 65 | + |
| 66 | +```JSON |
| 67 | +{ |
| 68 | + "Version": "2012-10-17", |
| 69 | + "Id": "key-policy-permissive", |
| 70 | + "Statement": [ |
| 71 | + { |
| 72 | + "Sid": "Allow any KMS action to Admin", |
| 73 | + "Effect": "Allow", |
| 74 | + "Principal": { |
| 75 | + "AWS": "arn:aws:iam::<account-id>:role/Admin" |
| 76 | + }, |
| 77 | + "Action": "kms:*", |
| 78 | + "Resource": "*" |
| 79 | + }, |
| 80 | + { |
| 81 | + "Sid": "Allow any KMS action to Managed Flink", |
| 82 | + "Effect": "Allow", |
| 83 | + "Principal": { |
| 84 | + "Service": [ |
| 85 | + "kinesisanalytics.amazonaws.com", |
| 86 | + "infrastructure.kinesisanalytics.amazonaws.com" |
| 87 | + ] |
| 88 | + }, |
| 89 | + "Action":[ "kms:DescribeKey", "kms:Decrypt", "kms:GenerateDataKey", "kms:GenerateDataKeyWithoutPlaintext", "kms:CreateGrant" ], |
| 90 | + "Resource": "*" |
| 91 | + } |
| 92 | + ] |
| 93 | +} |
| 94 | + |
| 95 | +``` |
| 96 | + |
| 97 | +⚠️ This key policy should not be used in production environments or environments containing sensitive data. |
0 commit comments