Review encoded key material in user data

Check whether user data contains an actual private key. Base64 encoding does not protect secrets.

Description

Check whether the PEM data in EC2 user data is a private key, public key or certificate. Anyone who can read a Base64-encoded private key can decode it.

Potential impact

An exposed private key can be used for authentication or signing wherever it is still trusted.

Remediation

Remove private keys from user data. If the application needs a secret, let its instance role read just that secret from AWS Secrets Manager. Replace an exposed key, remove trust in the old key and check existing instances, launch configurations and template versions for copies.

Examples

amazon.aws.ec2_launch_template accepts Base64-encoded user_data. Supply ami_id, security_group_ids and an existing key pair named default.

Before

yaml
- name: launch template with illustrative encoded key material
  amazon.aws.ec2_launch_template:
    name: special
    image_id: "{{ ami_id }}"
    key_name: default
    security_group_ids: "{{ security_group_ids }}"
    instance_type: t3.micro
    user_data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpzb21lS2V5

The first value illustrates a private-key header.

After

yaml
- name: launch template without embedded private key
  amazon.aws.ec2_launch_template:
    name: special
    image_id: "{{ ami_id }}"
    key_name: default
    security_group_ids: "{{ security_group_ids }}"
    instance_type: t3.micro
    user_data: IyEvYmluL3NoCmV4aXQgMAo=

The second value is a startup script that exits with exit 0. Configure the application to retrieve any secrets it needs.

References