AWS-EC2-Secrets-in-User-Data
Severity : Medium
Description: This control ensures that EC2 instances doesn’t expose passwords, private keys or other keys in the user data. Instance’s user data is metadata field that allows custom code to run after the instance is launch. The presence of these secrets in metadata could expose them to third party. Security best practices recommend to remove all secret from the user data of the instances.
Remediation Steps:
Perform following to remove the secrete from the user data and use SM :
Login to the AWS Management Console at https://console.aws.amazon.com.
Create the secret in the Secret Manager for instances :
Navigate to Secret Manager console.
Select Secrets and Store a new secret.
In navigation select Instances and the select the instance reported.
Under Secret Type, select other type secret and specify key value pair.
Select Next. Enter Name for secret and Description.
Create IAM role with secret access policy for instances:
Navigate to IAMÂ console.
Select Roles and select Create Role.
If EC2 instance role already created, Add Policy to get the secret as below. If role doesn’t exist Create Role for EC2 instances. The Policy in the role
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "secretsmanager:GetSecretValue" ], "Resource": <Key ARN in Secrete Manager>, "Effect": "Allow" } ] }
Â
Update existing instances to use above role and remove secret from user data :
Navigate to EC2Â console.
Navigate to Instances and select Instances.
Select the instance reported from the list of instance.
Select Action and then choose Security, Modify IAM Role.
For IAM role, if instance doesn’t have any role, Select the role created above. In case instances have a role and the key access policy above was added to old role, instance have the new updated policy.
Choose Save to save changes.
Select Action and then choose Select Action and then choose Instance Settings, Edit User data.
In the User data Find the secret and remove the secret.
Update the user data with following script to set the secret key and save it in environment variable
#!/bin/bash aws secretsmanager get-secret-value --region <aws-region> --secret-id <secret Key name>
Â
Important:
Reference :
Blue Hexagon Proprietary