Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best method for passing AWS credentials as user data to an EC2 instance?

Tags:

I have a job processing architecture based on AWS that requires EC2 instances query S3 and SQS. In order for running instances to have access to the API the credentials are sent as user data (-f) in the form of a base64 encoded shell script. For example:

$ cat ec2.sh ... export AWS_ACCOUNT_NUMBER='1111-1111-1111' export AWS_ACCESS_KEY_ID='0x0x0x0x0x0x0x0x0x0' ... $ zip -P 'secret-password' ec2.sh $ openssl enc -base64 -in ec2.zip 

Many instances are launched...

$ ec2run ami-a83fabc0 -n 20 -f ec2.zip 

Each instance decodes and decrypts ec2.zip using the 'secret-password' which is hard-coded into an init script. Although it does work, I have two issues with my approach.

  1. 'zip -P' is not very secure
  2. The password is hard-coded in the instance (it's always 'secret-password')

The method is very similar to the one described here

Is there a more elegant or accepted approach? Using gpg to encrypt the credentials and storing the private key on the instance to decrypt it is an approach I'm considering now but I'm unaware of any caveats. Can I use the AWS keypairs directly? Am I missing some super obvious part of the API?

like image 438
AdamK Avatar asked Mar 12 '09 23:03

AdamK


People also ask

What is the best method to give privilege to an EC2 instance to access other AWS?

You can use IAM to control how other users use resources in your AWS account, and you can use security groups to control access to your Amazon EC2 instances. You can choose to allow full use or limited use of your Amazon EC2 resources.

How do I pass my AWS credentials?

Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/ . In the navigation pane, choose Users. Choose the name of the user whose access keys you want to create, and then choose the Security credentials tab. In the Access keys section, choose Create access key.

What are the 3 different methods that you connect to a EC2 instance?

AWS support many ways to let you connect to your servers(EC2), we will introduce three methods : SSH, Instance Connect, System Manager and deep dive in EC2 Instance Connect and System Manager – Session Manager.

Which is the best practice to connect to an EC2 instance?

Ensure that no security group allows unrestricted inbound access on TCP port 6379 (Redis). Ensure that your EC2 instances do not reach the limit set by AWS for the number of vCPUs. Ensure default security groups restrict all public traffic to follow AWS security best practices.


2 Answers

You can store the credentials on the machine (or transfer, use, then remove them.)

You can transfer the credentials over a secure channel (e.g. using scp with non-interactive authentication e.g. key pair) so that you would not need to perform any custom encryption (only make sure that permissions are properly set to 0400 on the key file at all times, e.g. set the permissions on the master files and use scp -p)

If the above does not answer your question, please provide more specific details re. what your setup is and what you are trying to achieve. Are EC2 actions to be initiated on multiple nodes from a central location? Is SSH available between the multiple nodes and the central location? Etc.


EDIT

Have you considered parameterizing your AMI, requiring those who instantiate your AMI to first populate the user data (ec2-run-instances -f user-data-file) with their AWS keys? Your AMI can then dynamically retrieve these per-instance parameters from http://169.254.169.254/1.0/user-data.


UPDATE

OK, here goes a security-minded comparison of the various approaches discussed so far:

  1. Security of data when stored in the AMI user-data unencrypted
    • low
    • clear-text data is accessible to any user who manages to log onto the AMI and has access to telnet, curl, wget, etc. (can access clear-text http://169.254.169.254/1.0/user-data)
    • you are vulnerable to proxy request attacks (e.g. attacker asks the Apache that may or may not be running on the AMI to get and forward the clear-text http://169.254.169.254/1.0/user-data)
  2. Security of data when stored in the AMI user-data and encrypted (or decryptable) with easily obtainable key
    • low
    • easily-obtainable key (password) may include:
      • key hard-coded in a script inside an ABI (where the ABI can be obtained by an attacker)
      • key hard-coded in a script on the AMI itself, where the script is readable by any user who manages to log onto the AMI
      • any other easily obtainable information such as public keys, etc.
      • any private key (its public key may be readily obtainable)
    • given an easily-obtainable key (password), the same problems identified in point 1 apply, namely:
      • the decrypted data is accessible to any user who manages to log onto the AMI and has access to telnet, curl, wget, etc. (can access clear-text http://169.254.169.254/1.0/user-data)
      • you are vulnerable to proxy request attacks (e.g. attacker asks the Apache that may or may not be running on the AMI to get and forward the encrypted http://169.254.169.254/1.0/user-data, ulteriorly descrypted with the easily-obtainable key)
  3. Security of data when stored in the AMI user-data and encrypted with not easily obtainable key
    • average
    • the encrypted data is accessible to any user who manages to log onto the AMI and has access to telnet, curl, wget, etc. (can access encrypted http://169.254.169.254/1.0/user-data)
      • an attempt to decrypt the encrypted data can then be made using brute-force attacks
  4. Security of data when stored on the AMI, in a secured location (no added value for it to be encrypted)
    • higher
    • the data is only accessible to one user, the user who requires the data in order to operate
      • e.g. file owned by user:user with mask 0600 or 0400
    • attacker must be able to impersonate the particular user in order to gain access to the data
      • additional security layers, such as denying the user direct log-on (having to pass through root for interactive impersonation) improves security

So any method involving the AMI user-data is not the most secure, because gaining access to any user on the machine (weakest point) compromises the data.

This could be mitigated if the S3 credentials were only required for a limited period of time (i.e. during the deployment process only), if AWS allowed you to overwrite or remove the contents of user-data when done with it (but this does not appear to be the case.) An alternative would be the creation of temporary S3 credentials for the duration of the deployment process, if possible (compromising these credentials, from user-data, after the deployment process is completed and the credentials have been invalidated with AWS, no longer poses a security threat.)

If the above is not applicable (e.g. S3 credentials needed by deployed nodes indefinitely) or not possible (e.g. cannot issue temporary S3 credentials for deployment only) then the best method remains to bite the bullet and scp the credentials to the various nodes, possibly in parallel, with the correct ownership and permissions.

like image 173
vladr Avatar answered Sep 26 '22 20:09

vladr


I wrote an article examining various methods of passing secrets to an EC2 instance securely and the pros & cons of each.

http://www.shlomoswidler.com/2009/08/how-to-keep-your-aws-credentials-on-ec2/

like image 33
Shlomo Swidler Avatar answered Sep 26 '22 20:09

Shlomo Swidler