ec2-signature 0.0.2 → 0.0.4
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/README +9 -11
- data/ec2-signature.gemspec +1 -1
- data/lib/ec2-signature.rb +18 -16
- data/lib/ec2-signature/version.rb +2 -2
- metadata +3 -3
data/README
CHANGED
@@ -9,23 +9,21 @@ This gem will simply generate the AWS EC2 signature so that you can use your own
|
|
9
9
|
require 'ec2-signature'
|
10
10
|
# pass a hash containing your aws auth params to new obj
|
11
11
|
mysig = EC2Signature.new( {
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
12
|
+
:awsaccessid => 'abcde12345fiow13jlaf1',
|
13
|
+
:awssecretkey => '1380adj13j43jklj32a',
|
14
|
+
:ec2url => 'http://myec2server:8773/services/Cloud'
|
15
15
|
} )
|
16
16
|
# generate the signature provided the query action you want to issue to your ec2 provider
|
17
|
-
|
17
|
+
mysig.sign 'DescribeImages'
|
18
|
+
signature = mysig.signature
|
18
19
|
# use the example net/http post method to post your signature to the ec2_url specified above
|
19
|
-
mysig.
|
20
|
+
mysig.submit
|
20
21
|
|
21
22
|
|
22
23
|
##############################
|
23
24
|
#### OpenStack Example 2 ####
|
24
25
|
##############################
|
25
|
-
mysig.path = '/services/Admin'
|
26
26
|
# opennebula's aws/ec2 api implementation has a diff path for admin cmds
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
} )
|
31
|
-
mysig.post signature
|
27
|
+
mysig.path = '/services/Admin'
|
28
|
+
signature = mysig.sign 'DescribeUser', {'Name' => 'jsmith', }
|
29
|
+
mysig.submit
|
data/ec2-signature.gemspec
CHANGED
@@ -4,7 +4,7 @@ require "ec2-signature/version"
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "ec2-signature"
|
7
|
-
s.version =
|
7
|
+
s.version = EC2::Signature::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["John Tran"]
|
10
10
|
s.email = ["jtran@attinteractive.com"]
|
data/lib/ec2-signature.rb
CHANGED
@@ -3,18 +3,19 @@ require 'openssl'
|
|
3
3
|
require 'base64'
|
4
4
|
require 'cgi'
|
5
5
|
|
6
|
-
class
|
6
|
+
class EC2Signature
|
7
7
|
|
8
|
-
attr_accessor :
|
8
|
+
attr_accessor :awsaccessid, :awssecretkey, :ec2url, :host, :port, :path, :scheme, :method, :project
|
9
|
+
attr_accessor :signature
|
9
10
|
|
10
11
|
def initialize creds, method='POST'
|
11
12
|
raise "Need a hash of AWS/EC2 credential info" unless creds.kind_of? Hash
|
12
|
-
[:
|
13
|
-
raise "Credential hash requires :
|
13
|
+
[:awsaccessid, :awssecretkey, :ec2url].each do |a|
|
14
|
+
raise "Credential hash requires :awsaccessid, :awssecretkey & :ec2url" unless creds[a]
|
14
15
|
end
|
15
16
|
raise "Method can only be 'GET' or 'POST'" unless ['GET','POST'].include? method
|
16
|
-
self.
|
17
|
-
self.
|
17
|
+
self.awsaccessid = creds[:awsaccessid]
|
18
|
+
self.awssecretkey = creds[:awssecretkey]
|
18
19
|
self.ec2url = creds[:ec2url]
|
19
20
|
uri = URI.parse creds[:ec2url]
|
20
21
|
self.host = uri.host
|
@@ -24,12 +25,13 @@ class Ec2Signature
|
|
24
25
|
self.method = method
|
25
26
|
end
|
26
27
|
|
27
|
-
def sign
|
28
|
-
raise
|
29
|
-
|
30
|
-
|
28
|
+
def sign action='DescribeInstances', actionparams={}
|
29
|
+
raise 'actionparams needs to be a Hash' unless actionparams.kind_of?(Hash)
|
30
|
+
# openstack requires project names added to end of awssecretkey to change project context
|
31
|
+
newaccessid = ( project ? awsaccessid+':'+project : awsaccessid )
|
31
32
|
actionparams.merge!({
|
32
|
-
'
|
33
|
+
'Action' => action,
|
34
|
+
'AWSAccessKeyId' => newaccessid,
|
33
35
|
'SignatureMethod' => 'HmacSHA256',
|
34
36
|
'SignatureVersion' => '2',
|
35
37
|
'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
@@ -44,17 +46,17 @@ class Ec2Signature
|
|
44
46
|
end
|
45
47
|
string_to_sign = "#{method}\n#{host}:#{port}\n#{path}\n" << body.chop
|
46
48
|
digest = OpenSSL::Digest::Digest.new('sha256')
|
47
|
-
signed_string = OpenSSL::HMAC.digest(digest,
|
49
|
+
signed_string = OpenSSL::HMAC.digest(digest, awssecretkey, string_to_sign)
|
48
50
|
body << "Signature=#{CGI.escape(Base64.encode64(signed_string).chomp!).gsub(/\+/, '%20')}"
|
49
|
-
|
50
|
-
|
51
|
+
self.signature = body
|
52
|
+
self
|
51
53
|
end
|
52
54
|
|
53
|
-
def submit signature=
|
55
|
+
def submit signature=signature
|
54
56
|
require 'net/http'
|
55
57
|
http = Net::HTTP.new host, port
|
56
58
|
resp = case method
|
57
|
-
when 'GET' then http.get path
|
59
|
+
when 'GET' then http.get path+'?'+signature
|
58
60
|
when 'POST' then http.post path, signature
|
59
61
|
end
|
60
62
|
resp.body
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- John Tran
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-02-
|
17
|
+
date: 2011-02-11 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|