ec2-signature 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
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
- :access_id => ,
13
- :secret_key => ,
14
- :ec2_url => 'http://myec2server:8773/services/Cloud'
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
- signature = mysig.sign( {'Action' => 'DescribeImages'} )
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.post signature
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
- signature = mysig.sign( {
28
- 'Action' => 'DescribeUser',
29
- 'Name' => 'jsmith',
30
- } )
31
- mysig.post signature
27
+ mysig.path = '/services/Admin'
28
+ signature = mysig.sign 'DescribeUser', {'Name' => 'jsmith', }
29
+ mysig.submit
@@ -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 = Ec2::Signature::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 Ec2Signature
6
+ class EC2Signature
7
7
 
8
- attr_accessor :accessid, :secretkey, :ec2url, :host, :port, :path, :scheme, :method
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
- [:accessid, :secretkey, :ec2url].each do |a|
13
- raise "Credential hash requires :accessid, :secretkey & :ec2url" unless creds[a]
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.accessid = creds[:accessid]
17
- self.secretkey = creds[:secretkey]
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 actionparams={'Action'=>'DescribeInstances'}
28
- raise "hash of AWS EC2 web params action required" unless actionparams.kind_of? Hash
29
- raise "hash missing 'Action' key/value" unless actionparams['Action']
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
- 'AWSAccessKeyId' => accessid,
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, secretkey, string_to_sign)
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
- body
51
+ self.signature = body
52
+ self
51
53
  end
52
54
 
53
- def submit signature=sign
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.concat('?'+signature)
59
+ when 'GET' then http.get path+'?'+signature
58
60
  when 'POST' then http.post path, signature
59
61
  end
60
62
  resp.body
@@ -1,5 +1,5 @@
1
- module Ec2
1
+ module EC2
2
2
  module Signature
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.4"
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
- - 2
9
- version: 0.0.2
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-10 00:00:00 -08:00
17
+ date: 2011-02-11 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20