ec2-signature 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ec2-signature.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,31 @@
1
+ Code borrowed from fog (https://github.com/geemus/fog).
2
+
3
+ This gem will simply generate the AWS EC2 signature so that you can use your own HTTP methods and libraries to POST to your choice of AWS/EC2 compatible API servers such as Eucalyptus, OpenNebula, OpenAuth. Apparently, most of the AWS/EC2 API gems out there are not compatible with the aforementioned cloud frameworks, due to running on a custom port they do not account for that when generating the signature. Fog had implemented a compatible signature method, however, I did not want to use the EXCON http library implemented in it so I extracted the signature method only for use with my own choice of HTTP library.
4
+
5
+ ##############################
6
+ #### OpenStack Example 1 ####
7
+ ##############################
8
+
9
+ require 'ec2-signature'
10
+ # pass a hash containing your aws auth params to new obj
11
+ mysig = EC2Signature.new( {
12
+ :access_id => ,
13
+ :secret_key => ,
14
+ :ec2_url => 'http://myec2server:8773/services/Cloud'
15
+ } )
16
+ # generate the signature provided the query action you want to issue to your ec2 provider
17
+ signature = mysig.sign( {'Action' => 'DescribeImages'} )
18
+ # use the example net/http post method to post your signature to the ec2_url specified above
19
+ mysig.post signature
20
+
21
+
22
+ ##############################
23
+ #### OpenStack Example 2 ####
24
+ ##############################
25
+ mysig.path = '/services/Admin'
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
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ec2-signature/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ec2-signature"
7
+ s.version = Ec2::Signature::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["John Tran"]
10
+ s.email = ["jtran@attinteractive.com"]
11
+ s.homepage = "http://rubygems.org/gems/ec2-signature"
12
+ s.summary = %q{generate a signature to be posted to any EC2 compatible API}
13
+ s.description = %q{AWS EC2 API generates signatures to authenticate. This will generate one that is compatible even with Eucalyptus, OpenNebula & OpenStack.}
14
+
15
+ s.rubyforge_project = "ec2-signature"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,58 @@
1
+ require 'uri'
2
+ require 'openssl'
3
+ require 'base64'
4
+ require 'cgi'
5
+
6
+ class Ec2Signature
7
+
8
+ attr_accessor :accessid, :secretkey, :ec2url, :host, :port, :path, :scheme
9
+
10
+ def initialize creds
11
+ 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]
14
+ end
15
+ self.accessid = creds[:accessid]
16
+ self.secretkey = creds[:secretkey]
17
+ self.ec2url = creds[:ec2url]
18
+ uri = URI.parse creds[:ec2url]
19
+ self.host = uri.host
20
+ self.scheme = uri.scheme
21
+ self.path = uri.path
22
+ self.port = uri.port
23
+ end
24
+
25
+ def sign actionparams={'Action'=>'DescribeInstances'}
26
+ raise "hash of AWS EC2 web params action required" unless actionparams.kind_of? Hash
27
+ raise "hash missing 'Action' key/value" unless actionparams['Action']
28
+
29
+ actionparams.merge!({
30
+ 'AWSAccessKeyId' => accessid,
31
+ 'SignatureMethod' => 'HmacSHA256',
32
+ 'SignatureVersion' => '2',
33
+ 'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
34
+ 'Version' => '2010-08-31'
35
+ })
36
+
37
+ body = ''
38
+ for key in actionparams.keys.sort
39
+ unless (value = actionparams[key]).nil?
40
+ body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&"
41
+ end
42
+ end
43
+ string_to_sign = "POST\n#{host}:#{port}\n#{path}\n" << body.chop
44
+ digest = OpenSSL::Digest::Digest.new('sha256')
45
+ signed_string = OpenSSL::HMAC.digest(digest, secretkey, string_to_sign)
46
+ body << "Signature=#{CGI.escape(Base64.encode64(signed_string).chomp!).gsub(/\+/, '%20')}"
47
+
48
+ body
49
+ end
50
+
51
+ def post signature=sign
52
+ require 'net/http'
53
+ http = Net::HTTP.new host, port
54
+ resp = http.post path, signature
55
+ resp.body
56
+ end
57
+
58
+ end
@@ -0,0 +1,5 @@
1
+ module Ec2
2
+ module Signature
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2-signature
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - John Tran
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-10 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: AWS EC2 API generates signatures to authenticate. This will generate one that is compatible even with Eucalyptus, OpenNebula & OpenStack.
22
+ email:
23
+ - jtran@attinteractive.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README
34
+ - Rakefile
35
+ - ec2-signature.gemspec
36
+ - lib/ec2-signature.rb
37
+ - lib/ec2-signature/version.rb
38
+ has_rdoc: true
39
+ homepage: http://rubygems.org/gems/ec2-signature
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project: ec2-signature
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: generate a signature to be posted to any EC2 compatible API
68
+ test_files: []
69
+