ec2-signature 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ec2-signature.rb CHANGED
@@ -5,13 +5,14 @@ require 'cgi'
5
5
 
6
6
  class Ec2Signature
7
7
 
8
- attr_accessor :accessid, :secretkey, :ec2url, :host, :port, :path, :scheme
8
+ attr_accessor :accessid, :secretkey, :ec2url, :host, :port, :path, :scheme, :method
9
9
 
10
- def initialize creds
10
+ def initialize creds, method='POST'
11
11
  raise "Need a hash of AWS/EC2 credential info" unless creds.kind_of? Hash
12
12
  [:accessid, :secretkey, :ec2url].each do |a|
13
13
  raise "Credential hash requires :accessid, :secretkey & :ec2url" unless creds[a]
14
14
  end
15
+ raise "Method can only be 'GET' or 'POST'" unless ['GET','POST'].include? method
15
16
  self.accessid = creds[:accessid]
16
17
  self.secretkey = creds[:secretkey]
17
18
  self.ec2url = creds[:ec2url]
@@ -20,6 +21,7 @@ class Ec2Signature
20
21
  self.scheme = uri.scheme
21
22
  self.path = uri.path
22
23
  self.port = uri.port
24
+ self.method = method
23
25
  end
24
26
 
25
27
  def sign actionparams={'Action'=>'DescribeInstances'}
@@ -40,7 +42,7 @@ class Ec2Signature
40
42
  body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&"
41
43
  end
42
44
  end
43
- string_to_sign = "POST\n#{host}:#{port}\n#{path}\n" << body.chop
45
+ string_to_sign = "#{method}\n#{host}:#{port}\n#{path}\n" << body.chop
44
46
  digest = OpenSSL::Digest::Digest.new('sha256')
45
47
  signed_string = OpenSSL::HMAC.digest(digest, secretkey, string_to_sign)
46
48
  body << "Signature=#{CGI.escape(Base64.encode64(signed_string).chomp!).gsub(/\+/, '%20')}"
@@ -48,10 +50,13 @@ class Ec2Signature
48
50
  body
49
51
  end
50
52
 
51
- def post signature=sign
53
+ def submit signature=sign
52
54
  require 'net/http'
53
55
  http = Net::HTTP.new host, port
54
- resp = http.post path, signature
56
+ resp = case method
57
+ when 'GET' then http.get path.concat('?'+signature)
58
+ when 'POST' then http.post path, signature
59
+ end
55
60
  resp.body
56
61
  end
57
62
 
@@ -1,5 +1,5 @@
1
1
  module Ec2
2
2
  module Signature
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Tran