Preferred method of private key storage (cloudfront signed urls)

I’ve been writing an app that makes use of CloudFront signed URLs with a private S3 bucket as the origin. What’s the best way of storing the CloudFront private key?

The SDK examples here just say to store it on disk: Signing Amazon CloudFront URLs with AWS SDK for PHP Version 3 - AWS SDK for PHP

I know there are lots of no-no’s when it comes to the storage of private keys. Since this is part of an EC2 ASG, I don’t want to just put the key in code or as part of the disk image - that just seems like it’s asking for trouble.

So what’s the preferred method here? I’m sure there’s more than one way to skin this cat…


Related discussions:

One solution I can think of is storing it on a private S3 bucket, and grant access using bucket policy to the role your EC2 instances have. It may not be the best solution.

Someone on the ##aws IRC channel mentioned something similar to that. The difficulty is that the key still needs to be stored on disk after it’s read from S3. I suppose I could have it read from S3 in my init script and then store it somewhere in /etc/ ?

Yup, write a script to copy the file from S3, put that script into user data so that the instance will download it while startup. Since the instance use its role to authenticate the download action, no credential information needed in your script

Why does it need to be on disk? Just fetch the bits from S3 or parameter store/secrets manager and store them in memory when the app initializes. I would consider the key to be application config, not something the instance should worry about fetching.

A good option will be to use Parameter Store or Secrets Manager.

Because AWS PHP SDK need the pem file to generate signed url

I agree.

/u/richardfan1126 Parameter Store / Secrets Manager is more elegant than using S3 and they were designed for this exact problem.

Yup, as long as the file size doesn’t exceed Secrets Manager limit, using it would be better than S3. However, OP still need to grab the key and save as file as AWS SDK need a pem file to sign url

I can store it in php://memoory/ if necessary, I think. I’ll check out secrets man ager and parameter store.

I use parameter store for similar. Some bash fragments for doing this:

get:

echo -n -e $(aws ssm get-parameters 
  --names "/${User}/rsa/public" \
  --with-decryption \
  --region ${AWS::Region} \
  --query 'Parameters[0].Value') \
  | sed -e 's/^"//' -e 's/"$//' > /home/ubuntu/.ssh/id_rsa.pub 

put:

aws ssm put-parameter \
  --name "/${User}/rsa/public" \
  --type "SecureString" \
  --value "$(cat /home/ubuntu/.ssh/id_rsa.pub)" \
  --overwrite \
  --region ${AWS::Region}

Use an instance profile to give the machine permission to access the necessary parameters, and have the ‘get’ command in your userdata.