Encoded private key in user data

Base64 encoding does not protect a private key stored in user data.

Description

Keep private keys out of an EC2 launch template’s UserData. Anyone who can read the template or instance user data can recover a Base64-encoded key.

Potential impact

An exposed key can still authenticate to systems that trust it.

Remediation

Replace an exposed key at its consumers and remove trust in the old one. Have the application retrieve any required secret at runtime with a narrowly scoped instance role. Check logs and deployment artifacts for copies.

Examples

Supply ImageId and a compatible InstanceType. The first value imitates a private-key header. The second uses Fn::Base64 to encode a startup script containing no secret.

Before

json
{
  "Parameters": {
    "ImageId": {
      "Type": "AWS::EC2::Image::Id"
    },
    "InstanceType": {
      "Type": "String"
    }
  },
  "Resources": {
    "myLaunchConfig3": {
      "Type": "AWS::EC2::LaunchTemplate",
      "Properties": {
        "LaunchTemplateData": {
          "ImageId": {
            "Ref": "ImageId"
          },
          "InstanceType": {
            "Ref": "InstanceType"
          },
          "UserData": "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpzb21lS2V5"
        }
      }
    }
  }
}

After

json
{
  "Parameters": {
    "ImageId": {
      "Type": "AWS::EC2::Image::Id"
    },
    "InstanceType": {
      "Type": "String"
    }
  },
  "Resources": {
    "myLaunchConfig3": {
      "Type": "AWS::EC2::LaunchTemplate",
      "Properties": {
        "LaunchTemplateData": {
          "ImageId": {
            "Ref": "ImageId"
          },
          "InstanceType": {
            "Ref": "InstanceType"
          },
          "UserData": {
            "Fn::Base64": "#!/bin/sh\necho bootstrap only\n"
          }
        }
      }
    }
  }
}

References