ec2-signature 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ec2-signature.rb +10 -5
- data/lib/ec2-signature/version.rb +1 -1
- metadata +2 -2
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 = "
|
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
|
53
|
+
def submit signature=sign
|
52
54
|
require 'net/http'
|
53
55
|
http = Net::HTTP.new host, port
|
54
|
-
resp =
|
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
|
|