Monday, September 27, 2010

Using Policies in AWS Identity and Access Management

For a project I'm working on here at work, I wanted to create a AWS user that could only launch instances, but could not write to S3, query SDB

{
 "Statement":[ {
    "Effect":"Allow",
    "Action":["ec2:RunInstances","ec2:RebootInstances",
              "ec2:GetConsoleOutput", "ec2:DescribeInstances" ],
    "Resource":"*"
  }, {
    "Effect":"Allow",
    "Action":["ec2:StopInstances","ec2:StartInstances",
              "ec2:TerminateInstances"],
    "Resource":"*"
  }, {
    "Effect":"Allow",
    "Action":["ec2:DescribeImages"],
    "Resource":"*"
  }, {
    "Effect":"Deny",
    "NotAction":["ec2:*"],
    "Resource":"*"
  } ]
}


I had hoped that I could allow this user to create his own security groups and keypairs (for launching instances with 'ec2-run-instances --key', and that I could also allow him to modify or delete those items as well. Unfortunately, I was not able to figure out how to do this. What I had hoped I could do was something like was something like:
{
  "Effect":"Allow",
  "Action":["ec2:*SecurityGroup*"],
  "Condition" :  {
     "StringLike": {
        "ec2:groupName":"foouser*"
     }
  },
  "Resource":"*"
},
{
  "Effect":"Allow",
  "Action":["ec2:*KeyPair*"],
  "Condition" :  {
     "StringLike": {
        "ec2:keyName":"foouser*"
     }
  },
  "Resource":"*"
}

The 'keyName' is an atribute of the [Add,Delete,Describe]KeyPair API calls, and 'groupName' is an attribute of the [Add,Delete]SecurityGroups and AuthorizeSecurityGroupIngress API calls as described in the EC2 API

My goal was to limit the user ('foouser') to manipulating SecurityGroups or KeyPairs that begain with 'foouser'. This would be a clear indication to other users of the account when they came across them.

However, the 'Condition' syntax isn't as "open" as that (couldn't think of a better term for than 'open'). I can think of reasons as to why it would be difficult or undesireable to make Conditions function like I wanted, but it would have been nice.

The IAM EC2 documentation indicates that EC2 only supports the following Condition types: aws:CurrentTime aws:EpochTime aws:SecureTransport aws:SourceIp and aws:UserAgent.

It seems to me that SecurityGroups and keypairs are an essential piece of Using EC2, but it seems like these are stuck at the account level, with no ability to limit them at the user level.

Another thing that I would like to do is give the user the ability to launch / stop / start / terminate her own instances, but not other users of the account. If I truly try to use IAM to split up my account usage, say with 'Development' and 'Production' users or groups, this is essential. When I use the 'Development' user I want to be protected from an accidental reboot or terminate of a 'Production' instance.

For example, I test our Official Ubuntu images. The testing scripts launch several instances. While they're running, I'll often be doing development, and also have an instance running. I would like my 'development' work to not accidentally terminate (or otherwise affect) my test runs. As it is right now, a 'euca-describe-instances' will show me all instances in either account, just waiting for me to copy and paste wrong and terminate one.

It is quite possible that I've missed something, if so, please let me know.

Wednesday, September 22, 2010

Playing with AWS Access Identity Management

Today I finally found some time to play with AWS Identity and Access Management. If you hadn't seen the announcement, or aren't familiar, the IAM tools basically allow you to create, manage, and limit multiple AWS accounts under a single account.


There are 2 reasons that immediately spring to mind for when you should use this:

  • If you're sharing an single AWS account between multiple people, then using this is almost required.
  • you want to use some AWS facility from inside an EC2 instance. Here, it just seems scary to put the entire keys to your account onto a remote machine.
To get started, I walked through the Getting Started guide. I downloaded the IAM Tools, and set them up as described. On Ubuntu, that consisted of:



$ wget http://awsiammedia.s3.amazonaws.com/public/tools/cli/latest/IAMCli.zip
$ unzip IAMCli.zip
$ vi my-account-creds.txt
$ cat my-account-creds.txt
AWSAccessKeyId=ABCDEFGHIJKLMNOPQRST
AWSSecretKey=zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponm
$ export AWS_CREDENTIAL_FILE=my-account-creds.txt
$ export AWS_IAM_HOME=$PWD/IAMCli
$ export PATH=$AWS_IAM_HOME:$PATH JAVA_HOME=/usr


Then, I created a user and admin group as described in the guide:


$ iam-groupcreate -g admins
$ cat AdminGroupPolicy.txt
{
   "Statement":[{
      "Effect":"Allow",
      "Action":"*",
      "Resource":"*"
      }
   ]
}

$ iam-groupuploadpolicy -g admins -p AdminGroupPolicy -f AdminGroupPolicy.txt
$ iam-usercreate -u smoser -g Admins -k -v
TSRQPONMLKJIHGFEDCBA
mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
arn:aws:iam::950047163771:user/smoser
AHDAIABBGZ3Q31XMUE4AN


The first line of the iam-createuser output is the AWSAccessKeyId, and the second is the AWSSecretKey. I quickly added those to a file my-user-creds.txt as shown above, and set AWS_CREDENTIAL_FILE=my-user-creds.txt .

That's all it took. Now I have a set of credentials that I can use, and if they're lost or stolen, I can revoke them with the (now safely locked up) account credentials.

At this point, I could use the euca2ools with a config file like:

$ cat my-user-eucarc
AWSAccessKeyId=ABCDEFGHIJKLMNOPQRST
AWSSecretKey=mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
EC2_SECRET_KEY=${AWSSecretKey}
EC2_ACCESS_KEY=${AWSAccessKeyId}
EC2_USER_ID=950047163771
EC2_URL=https://ec2.amazonaws.com
S3_URL=https://s3.amazonaws.com:443
EC2_CERT=/etc/ec2/cert-ec2.pem
$ euca-describe-instances --config my-user-eucarc


Additionally, the above will also suffice as an AWS_CREDENTIAL_FILE for the iam-tools.

Thats great, but for one reason or another, I end up using the ec2-api-tools for a large amount of my work. Those tools require a private key and certificate. So, I had to go about creating one for my new user. Thanks to Nate@AWS in an EC2 Forum Post, that was easy also.


$ openssl version
OpenSSL 0.9.8o 01 Jun 2010
$ openssl genrsa 1024 > pk.pem
$ openssl req -new -x509 -nodes -sha1 -days 730 -key pk.pem -out cert.pem
# follow prompts here
$ iam-useraddcert -u smoser -f cert.pem
$ export EC2_PRIVATE_KEY=$PWD/pk.pem EC2_CERT=$PWD/cert.pem
$ ec2-describe-instances ...


Now my ec2-api-tools are functional. I have to admit to not completely understanding the implications of self signing a certificate here and uploading it. However, as I was authenticated to do the upload (via https and the given credentials) and only my user will use that signing key, I don't know what harm there could be.

Now I have the following TODOs:
  • Post about creating an IAM Policy
  • package the IAM tools for Ubuntu multiverse

Updates:

  • 2010-10-12: update case in AWS_IAM_HOME string ('IamCli' -> 'IAMCli')

Thursday, September 9, 2010

running Ubuntu on an Amazon "micro" instance

Amazon announced today a new instance type. The "micro" instance type (t1.micro) has 613 MB of memory and can be used with AMIs of either x86 or x86_64. The cost for either is only $0.02 per hour.

That means that you can try out Ubuntu on EC2 for 2 measly cents. The official images of Ubuntu 10.04 LTS (Lucid Lynx) are perfect for this. If you're more adventurous and want to try out 10.10, please try out a daily build.

I think this smaller instance size really makes EC2 available to a lot more people. The minimum price for running an instance for a month goes from something like $70 (31 * 24 * 0.095) to less than $15. If you add to that the fact that you can shut down the instance and not be charged for unused CPU time, then start it back up when you need it, it can be really cheap. If you happen to need more CPU power, you can even shut it down, and start it back up with more resources!

So what are you waiting for!

$ MY_LAUNCHPAD_ID="smoser"
$ printf "#cloud-config\n%s\n" "ssh_import_id: [${MY_LAUNCHPAD_ID}]" > user-data.txt
$ ec2-run-instances --region us-east-1 --user-data-file=user-data.txt ami-1234de7b


A few things to note:
  • Bug 634102 has to be worked around or you will not be able to reboot your instance. There are cloud-init debs available in my personal PPA with fixes. 10.10 won't have the issue, and I'll work on getting the fix backported to 10.04.  You can easily fix that by running:

    arch=$(uname -m)
    [ "$arch" = "x86_64" ] && ephd=/dev/sdb || ephd=/dev/sda2
    sudo sed -i.dist "\,${ephd},s,^,#," /etc/fstab

  • One interesting feature of this instance type is that it will run images that are x86 or x86_64 arch. Previously each instance type only ran a single arch.
  • Above, I launched the instance with cloud-config syntax that will pull in my keys from launchpad rather than using public ssh keys stored in EC2. Thats why I didn't need to pass '--key <mykey>'

edits

  • I fixed the ami id above, I had listed a instance-store image.  Also, a thing to note is that with t1.micro instances you *have* to use EBS images.
  • I fixed the 'sed' command above, and fixed the price for m1.small (0.095, not 0.95)

Wednesday, July 28, 2010

Verify SSH Keys on EC2 Instances

Like every server, every EC2 instance should have a unique ssh fingerprint. On "real servers" this fingerprint is generated at first installation of the openssh-server package. On EC2, instead, it is done on first boot of an instance. This is because each instance is a byte for byte copy of a registered image.

What this means to you, is that when you launch an instance and then connect with ssh, you'll see something like:


$ ssh -F /tmp/smoser/foo ec2-67-202-47-56.compute-1.amazonaws.com
The authenticity of host 'ec2-67-202-47-56.compute-1.amazonaws.com (67.202.47.56)' can't be established.
RSA key fingerprint is f1:40:a7:4e:0f:28:8d:12:21:59:f1:ff:03:5f:63:54.
Are you sure you want to continue connecting (yes/no)?


The ssh client is informing you that you are connecting to a host that you do not have ssh keys stored for. In short, it cannot confirm the identity of 'ec2-67-202-47-56'. There could be a "Man in the Middle" who is attempting to trick you. Just as with "real servers", you should identify that remote system via an out of band method. To do this outside of EC2, you might call a hosting provider up and ask them to verify the fingerprint that you see. On EC2, the only out of band transport is the ec2 console.

In order to provide you with the fingerprint that you need, the ssh fingerprint is written to the console when it is booted. You can see this with ec2-get-console-output.

As seen with the results of Eric's poll on alestic.com, this is a very little known or used piece of information. Over 50% of alestic.com voters have "never verified the fingerprint".


$ euca-get-console-output i-72bf1518 | grep ^ec2:
ec2:
ec2: #############################################################
ec2: -----BEGIN SSH HOST KEY FINGERPRINTS-----
ec2: 2048 f1:40:a7:4e:0f:28:8d:12:21:59:f1:ff:03:5f:63:54 /etc/ssh/ssh_host_rsa_key.pub (RSA)
ec2: 1024 28:f3:ef:a6:86:05:50:33:76:16:24:32:56:14:06:13 /etc/ssh/ssh_host_dsa_key.pub (DSA)
ec2: -----END SSH HOST KEY FINGERPRINTS-----
ec2: #############################################################


Note that the ssh fingerprint reported on the console matches the one that ssh client asked me to confirm above. So, I now know that the host I've connected to is the one that I just started.

Putting this all together, lets say you have booted a new EC2 instance, with instance-id i-72bf1518 and hostname ec2-67-202-47-56.compute-1.amazonaws.com.

First we will use ssh-keyscan to get the fingerprint that is being reported by the remote host, and store that in a shell variable 'fp'


$ iid=i-72bf1518
$ ihost=ec2-67-202-47-56.compute-1.amazonaws.com

$ ssh-keyscan ${ihost} 2>/dev/null > ${iid}.keys
$ ssh-keygen -lf ${iid}.keys > ${iid}.fprint
$ read length fp hostname id < ${iid}.fprint
$ echo $fp
f1:40:a7:4e:0f:28:8d:12:21:59:f1:ff:03:5f:63:54



This fingerprint should also appear on the console output of the instance. If it doesn't, then something is wrong. So, we'll get the console output, and grep through it looking for the fingerprint:


$ euca-get-console-output ${iid} > ${iid}.console
$ grep "ec2: ${length} ${fp}" ${iid}.console
ec2: 2048 f1:40:a7:4e:0f:28:8d:12:21:59:f1:ff:03:5f:63:54 /etc/ssh/ssh_host_rsa_key.pub (RSA)


We've now verified that the host we're connecting to is the host we just launched, so we can connect safely. Now you can clean out any old occurences of that host in known_hosts and tell the ssh client that this is a "known_host"


# remove existing entries in ~/.ssh/known_hosts for this host
$ ssh-keygen -R "${ihost}"

# hash the output of the known hosts file. This prevents someone
# from reading known_hosts as simple list of remote hosts you have
# access to in the event that one of your keys was compromised.

$ ssh-keygen -H -f ${iid}.keys

# Add the key to your known_hosts
$ cat ${iid}.keys >> ~/.ssh/known_hosts

# remove the temporary files we created
$ shred -u "${iid}."*


There, we've now verified that the remote host is the instance we started and told the ssh client about it.

Unfortunately, console output on ec2 is only updated approximately every 4 minutes. So, you can't run through this process until you have console output to check.

Updates
  • [2010-09-22]: fix mismatched use of 'iid' and 'ihost'

Thursday, May 20, 2010

Easily test or demo Ubuntu Enterprise Cloud (UEC) in EC2 instance

Consider the following items:
  • recent efforts by Robert Collins, Dustin Kirkland and others have enabled running all components of UEC on a single system.
  • while performance suffers greatly, Eucalyptus can easily be made to use qemu rather than kvm for virtualization without hardware virtualization extensions
  • Ubuntu server images are quickly launchable on EC2

Now, re-read those, but do so while thinking about wanting to provide people with an easy way to test UEC.

Thats right, if you have $0.40 per hour, and an EC2 account, you can play with UEC. No real hardware required.

This nested virtualization is obviously not going to provide you with the worlds fastest performing cloud, but it will provide a functional system for test or demo purposes.

I've put some copy-and-paste shell code in commands.txt in a bzr branch uec-on-ec2.

  • check out the bzr branch: lp:~smoser/+junk/uec-on-ec2

    bzr branch lp:~smoser/+junk/uec-on-ec2

  • follow 'commands.txt', copy and pasting its content bit by bit in a root shell (run 'sudo -s').
  • Publish and run an instance (do this as 'ubuntu' user):

    $ uec-publish-tarball fastboot-amd64-0.11.tar.gz fastboot-amd64-0.11 amd64
    # the above creates an emi, see its output
    $ ( umask 066 ; euca-add-keypair mykey > mykey.pem )
    $ euca-run-instances --addressing private --key mykey emi-4D6C12BF
    # soon this will enter 'running' state and have an IP address associated with it.
    $ ssh -i mykey ubuntu@${IPADDR}


Your instance should now be functional.

There are a couple things that could be cleaned up on this, patches are very welcome:
* ideally all of the setup could be done from a '#!' user data script, I've just not worked out all the timing yet.
* separate the node out, allowing for multiple nodes
* fix the requirement of private addressing for functional nodes
* fix the issue with the NC being eventually discovered 3 times (with each of its 3 IP addresses)

That said, you should be able to test this and try out UEC on EC2 in a m1.large for less than $0.40 per hour.

Sunday, May 9, 2010

UDS Maverick: Call for Participation

The Ubuntu Developer Summit (UDS) is the event in which the Ubuntu community discusses and plans the upcoming Ubuntu release. UDS Maverick begins Monday, May 10 (tomorrow) in Brussels.

If you've not yet made plans to attend physically, then its unlikely that you'll be present in the rooms. However, the Canonical IS does an outstanding job of making remote participation possible. For more information, on how you can participate remotely, read the Remote Participation document. In short, you join an IRC channel, listen to a live high quality audio stream from the room, and can see edits to a live gobby document.

The comprehensive list of all sessions is available through the summit schedule, or a filtered list of only the Ubuntu Server sessions.

Below is short, self centered, list that you might find interesting. The Ubuntu Community would love to have your participation.

  • Running cloud images outside UEC or EC2 (Tuesday 11:00 UTC+1): The UEC images that we produce to run in EC2 or UEC are ready-to-go filesystem image of Ubuntu Server. It seems that these images might also serve a more general purpose as a live demo of ubuntu server, or a very convenient starting point for customizing your own. Here we'll discuss other ways these images could be used, and what would need to be done to make that possible.
  • Improvements for cloud-init (Tuesday, 09:00 UTC+1) : This session will cover ways in which the cloud images can be made more user friendly. If you've ever booted one of the Ubuntu images, or re-bundled one, I'd like to know what we could do to the images to make that easier.
  • Handling kernel upgrades in EC2 and UEC (Wednesday 11:00 UTC+1): When a user starts a UEC/EC2 instance, they specify they have the option of specifying the kernel/ramdisk to use with it or use the default associated with the instance. Afterwards, the instance has no ability to modify that initial selection. We'll discuss ways that we could improve the user experience by making that limitation more clear, or possibly providing ways to overcome it.
  • Improve cloud libraries in ubunt (Thursday, 10:00 UTC+1): Throughout the 10.04 release, we packaged some popular libraries for interaction with AWS. We'd like to continue that work in Maverick. If you have suggestions on libraries you use that are not present in Ubuntu, please let us know
  • Utilties for easier interaction with UEC or EC2 (Tuesday, 10:00 UTC+1): If you have ideas on utilities that would make your life using Ubuntu on EC2 or UEC easier, please attend this session and let us know.
  • server-maverick-conffiles-and-puppetThis should be an interesting session discussing how Ubuntu can improve the management of conffiles. Soren Hansen has sent an email with more information
  • Discussion on plans for vmbuilder (17:10 UTC+1): Many people have used vmbuilder to build virtual machine images. In this session we'll discuss where vmbuilder is and where it is going.

If you have input for any session, but are not able to attend in real time, feel free to send me input at smoser at sign ubuntu dot com, and I'll try to make sure it gets brought up.

Oh, and if you're reading this on Sunday, May 9, don't forget Mother's Day.

Update 2010-05-10: Added times for sessions and used the titles rather than blueprint names so you can find them on the schedule

Wednesday, May 5, 2010

Ubuntu 10.04 LTS - Lucid Lynx available in all EC2 Regions

I'm a bit late to the party. Ubuntu 10.04 LTS (Lucid Lynx) was released almost a week ago at this point. Multitudes of others have blogged, reviewed, used, etcetera.

That said, I wanted to announce it here, and point out a few things. To get a list of AMI ids for Ubuntu 10.04 LTS Server, look at either:

Now, some miscellaneous items I wanted to mention:
  • 10.04 LTS was released on EC2 in all 4 regions (us-east-1, us-west-1, eu-west-1, and the new ap-southeast-1) at the same time as all other Ubuntu releases. The 10.04 LTS images were available on the ap-southeast-1 region less than 24 hours after it was officially announced.
  • The published EC2 images do not have a ramdisk associated with them. This is by design. The kernel has enough smarts built in to find the root filesystem and boot the system. The end result is that we only have to update and manage 2 pieces instead of 3, and the instances should boot faster.
  • In addition to 10.04 images, we also populated the ap-southeast-1 region with the latest released versions of 8.04 and 9.10 images.