Class: Ec2Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2-signature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(creds, method = 'POST') ⇒ Ec2Signature

Returns a new instance of Ec2Signature.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ec2-signature.rb', line 10

def initialize creds, method='POST'
  raise "Need a hash of AWS/EC2 credential info" unless creds.kind_of? Hash
  [:accessid, :secretkey, :ec2url].each do |a| 
    raise "Credential hash requires :accessid, :secretkey & :ec2url" unless creds[a]
  end
  raise "Method can only be 'GET' or 'POST'" unless ['GET','POST'].include? method
  self.accessid = creds[:accessid]
  self.secretkey = creds[:secretkey]
  self.ec2url = creds[:ec2url]
  uri = URI.parse creds[:ec2url]
  self.host = uri.host
  self.scheme = uri.scheme
  self.path = uri.path
  self.port = uri.port
  self.method = method
end

Instance Attribute Details

#accessidObject

Returns the value of attribute accessid.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def accessid
  @accessid
end

#ec2urlObject

Returns the value of attribute ec2url.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def ec2url
  @ec2url
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def host
  @host
end

#methodObject

Returns the value of attribute method.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def method
  @method
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def path
  @path
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def scheme
  @scheme
end

#secretkeyObject

Returns the value of attribute secretkey.



8
9
10
# File 'lib/ec2-signature.rb', line 8

def secretkey
  @secretkey
end

Instance Method Details

#sign(actionparams = {'Action'=>'DescribeInstances'}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ec2-signature.rb', line 27

def sign actionparams={'Action'=>'DescribeInstances'}
  raise "hash of AWS EC2 web params action required" unless actionparams.kind_of? Hash
  raise "hash missing 'Action' key/value"  unless actionparams['Action']

  actionparams.merge!({
    'AWSAccessKeyId'    => accessid,
    'SignatureMethod'   => 'HmacSHA256',
    'SignatureVersion'  => '2',
    'Timestamp'         => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
    'Version'           => '2010-08-31'
  })

  body = ''
  for key in actionparams.keys.sort
    unless (value = actionparams[key]).nil?
      body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&"
    end
  end
  string_to_sign = "#{method}\n#{host}:#{port}\n#{path}\n" << body.chop
  digest = OpenSSL::Digest::Digest.new('sha256')
  signed_string = OpenSSL::HMAC.digest(digest, secretkey, string_to_sign)
  body << "Signature=#{CGI.escape(Base64.encode64(signed_string).chomp!).gsub(/\+/, '%20')}"

  body
end

#submit(signature = sign) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/ec2-signature.rb', line 53

def submit signature=sign
  require 'net/http'
  http = Net::HTTP.new host, port
  resp = case method
    when 'GET' then http.get path.concat('?'+signature)
    when 'POST' then http.post path, signature
  end
  resp.body
end