public
Created

AWS request signing (not working)

  • Download Gist
sign.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#!/bin/bash
 
EC2_ACCESS_KEY="ACCESSKEY"
EC2_SECRET_KEY="SECRETKEY"
EC2_URL="ec2.eu-west-1.amazonaws.com"
arch="x86_64"
snapshot_id="snap-1234abcd"
aki="aki-71665e05"
name="debian-squeeze-amd64-20121112"
timestamp=`date +%Y-%m-%dT%H%%3A%M%%3A%SZ`
 
params=()
 
params+=("AWSAccessKeyId=$EC2_ACCESS_KEY")
params+=("Action=RegisterImage")
params+=("Architecture=$arch")
params+=("BlockDeviceMapping.1.DeviceName=%2Fdev%2Fsda1")
params+=("BlockDeviceMapping.1.Ebs.DeleteOnTermination=true")
params+=("BlockDeviceMapping.1.Ebs.SnapshotId=$snapshot_id")
params+=("BlockDeviceMapping.2.DeviceName=%2Fdev%2Fsdd")
params+=("BlockDeviceMapping.2.VirtualName=ephemeral4")
params+=("BlockDeviceMapping.3.DeviceName=%2Fdev%2Fsde")
params+=("BlockDeviceMapping.3.VirtualName=ephemeral5")
params+=("BlockDeviceMapping.4.DeviceName=%2Fdev%2Fsdb")
params+=("BlockDeviceMapping.4.VirtualName=ephemeral2")
params+=("BlockDeviceMapping.5.DeviceName=%2Fdev%2Fsdc")
params+=("BlockDeviceMapping.5.VirtualName=ephemeral3")
params+=("BlockDeviceMapping.6.DeviceName=%2Fdev%2Fsda2")
params+=("BlockDeviceMapping.6.VirtualName=ephemeral0")
params+=("BlockDeviceMapping.7.DeviceName=%2Fdev%2Fsda3")
params+=("BlockDeviceMapping.7.VirtualName=ephemeral1")
params+=("KernelId=$aki")
params+=("Name=$name")
params+=("RootDeviceName=%2Fdev%2Fsda1")
params+=("SignatureMethod=HmacSHA256")
params+=("SignatureVersion=2")
params+=("Timestamp=$timestamp")
params+=("Version=2009-11-30")
 
SAVE_IFS=$IFS
IFS="&"
query_string="${params[*]}"
IFS=$SAVE_IFS
 
binary_key=`printf "$EC2_SECRET_KEY" | base64 -d`
 
digest=`printf "POST\n$EC2_URL\n/\n$query_string" | openssl dgst -binary -sha256 -hmac "$EC2_SECRET_KEY" | base64`
printf "Digest result: %s\n" $digest

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.