ace-client 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -42,5 +42,10 @@ module AceClient
42
42
  @last_response_time = Time.now - start_time
43
43
  @last_response
44
44
  end
45
+
46
+ def endpoint_url
47
+ protocol = use_ssl ? 'https' : 'http'
48
+ protocol + '://' + endpoint
49
+ end
45
50
  end
46
51
  end
@@ -97,11 +97,6 @@ module AceClient
97
97
  @sampler[:output].puts Nokogiri::XML(response.body).to_xml(:indent => 4)
98
98
  end
99
99
 
100
- def endpoint_url
101
- protocol = use_ssl ? 'https' : 'http'
102
- protocol + '://' + endpoint
103
- end
104
-
105
100
  def create_signature
106
101
  digest = OpenSSL::Digest::Digest.new(@signature_method.downcase.gsub(/hmac/, ''))
107
102
  Base64.encode64(OpenSSL::HMAC.digest(digest, secret_access_key, string_to_sign)).strip
@@ -1,5 +1,187 @@
1
1
  module AceClient
2
2
  class Query4 < Base
3
- # not implemented yet
3
+ attr_accessor :headers
4
+ attr_accessor :region
5
+ attr_accessor :body
6
+ attr_accessor :service
7
+ attr_accessor :datetime
8
+ attr_accessor :query
9
+
10
+ format :xml
11
+ #debug_output $stdout
12
+
13
+ def initialize(options)
14
+ super(options)
15
+ @service = options[:service]
16
+ @region = options[:region]
17
+ end
18
+
19
+ def action(action, params={})
20
+ params.update('Action' => action)
21
+ execute(params)
22
+ end
23
+
24
+ def execute(params)
25
+ @datetime = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
26
+ if http_method == :get
27
+ execute_get(params)
28
+ else http_method == :post
29
+ execute_post(params)
30
+ end
31
+ end
32
+
33
+ def execute_get(params)
34
+ @headers = {}
35
+ @headers['host'] = @endpoint
36
+
37
+ @params = params
38
+ @params['Version'] = @version if @version
39
+ @params.update(
40
+ 'X-Amz-Algorithm' => 'AWS4-HMAC-SHA256',
41
+ 'X-Amz-Credential' => access_key_id + '/' + credential_string(datetime),
42
+ 'X-Amz-Date' => datetime,
43
+ 'X-Amz-SignedHeaders' => signed_headers
44
+ )
45
+ options = self.class.default_options.dup
46
+ options[:query] = @params
47
+ request = HTTParty::Request.new(Net::HTTP::Get, endpoint_url + @path, options)
48
+ @query = request.send(:normalize_query, options[:query])
49
+
50
+ @params.update('X-Amz-Signature' => signature(datetime))
51
+
52
+ @body = ''
53
+
54
+ request = HTTParty::Request.new(Net::HTTP::Get, endpoint_url + @path, options)
55
+ request.perform
56
+ end
57
+
58
+ def execute_post(params)
59
+ @params = params
60
+ options = {}
61
+ options = self.class.default_options.dup
62
+ options[:body] = @params
63
+ request = HTTParty::Request.new(Net::HTTP::Post, endpoint_url + @path, options)
64
+ @body = request.send(:normalize_query, options[:body])
65
+
66
+ @headers = {}
67
+ add_authorization!
68
+ options[:headers] = headers
69
+
70
+ request = HTTParty::Request.new(Net::HTTP::Post, endpoint_url + @path, options)
71
+ request.perform
72
+ end
73
+
74
+ def querystring
75
+ if http_method == :post
76
+ ''
77
+ elsif http_method == :get
78
+ @params.sort.collect { |param|
79
+ "#{CGI::escape(param[0])}=#{CGI::escape(param[1])}"
80
+ }.join("&").gsub('+', '%20').gsub('%7E', '~')
81
+ end
82
+ end
83
+
84
+ def add_authorization!
85
+ headers['content-type'] ||= 'application/x-www-form-urlencoded'
86
+ headers['host'] = endpoint
87
+ headers['x-amz-date'] = datetime
88
+ #headers['x-amz-security-token'] = credentials.session_token if credentials.session_token
89
+ headers['x-amz-content-sha256'] ||= hexdigest(body || '')
90
+ headers['authorization'] = authorization(datetime)
91
+ end
92
+
93
+ protected
94
+
95
+ def authorization datetime
96
+ parts = []
97
+ parts << "AWS4-HMAC-SHA256 Credential=#{access_key_id}/#{credential_string(datetime)}"
98
+ parts << "SignedHeaders=#{signed_headers}"
99
+ parts << "Signature=#{signature(datetime)}"
100
+ parts.join(', ')
101
+ end
102
+
103
+ def signature datetime
104
+ k_secret = secret_access_key
105
+ k_date = hmac("AWS4" + k_secret, datetime[0,8])
106
+ k_region = hmac(k_date, region)
107
+ k_service = hmac(k_region, service)
108
+ k_credentials = hmac(k_service, 'aws4_request')
109
+ hexhmac(k_credentials, string_to_sign(datetime))
110
+ end
111
+
112
+ def string_to_sign datetime
113
+ parts = []
114
+ parts << 'AWS4-HMAC-SHA256'
115
+ parts << datetime
116
+ parts << credential_string(datetime)
117
+ parts << hexdigest(canonical_request)
118
+ parts.join("\n")
119
+ end
120
+
121
+ def credential_string datetime
122
+ parts = []
123
+ parts << datetime[0,8]
124
+ parts << region
125
+ parts << service
126
+ parts << 'aws4_request'
127
+ parts.join("/")
128
+ end
129
+
130
+ def canonical_request
131
+ parts = []
132
+ parts << http_method.to_s.upcase
133
+ parts << @path
134
+ parts << querystring
135
+ parts << canonical_headers + "\n"
136
+ parts << signed_headers
137
+ if http_method == :post
138
+ parts << headers['x-amz-content-sha256']
139
+ else
140
+ parts << hexdigest('')
141
+ end
142
+ parts.join("\n")
143
+ end
144
+
145
+ def signed_headers
146
+ to_sign = headers.keys.map{|k| k.to_s.downcase }
147
+ to_sign.delete('authorization')
148
+ to_sign.sort.join(";")
149
+ end
150
+
151
+ def canonical_headers
152
+ headers = []
153
+ self.headers.each_pair do |k,v|
154
+ headers << [k,v] unless k == 'authorization'
155
+ end
156
+ headers = headers.sort_by(&:first)
157
+ headers.map{|k,v| "#{k}:#{canonical_header_values(v)}" }.join("\n")
158
+ end
159
+
160
+ def canonical_header_values values
161
+ values = [values] unless values.is_a?(Array)
162
+ values.map(&:to_s).join(',').gsub(/\s+/, ' ').strip
163
+ end
164
+
165
+ def hexdigest value
166
+ digest = Digest::SHA256.new
167
+ if value.respond_to?(:read)
168
+ chunk = nil
169
+ chunk_size = 1024 * 1024 # 1 megabyte
170
+ digest.update(chunk) while chunk = value.read(chunk_size)
171
+ value.rewind
172
+ else
173
+ digest.update(value)
174
+ end
175
+ digest.hexdigest
176
+ end
177
+
178
+ def hmac key, value
179
+ OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), key, value)
180
+ end
181
+
182
+ def hexhmac key, value
183
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), key, value)
184
+ end
4
185
  end
5
186
  end
187
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ace-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-25 00:00:00.000000000Z
12
+ date: 2013-11-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &46499740 !ruby/object:Gem::Requirement
16
+ requirement: &176407020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *46499740
24
+ version_requirements: *176407020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httparty
27
- requirement: &46498620 !ruby/object:Gem::Requirement
27
+ requirement: &176405860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.10.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *46498620
35
+ version_requirements: *176405860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: thor
38
- requirement: &46497540 !ruby/object:Gem::Requirement
38
+ requirement: &176404700 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *46497540
46
+ version_requirements: *176404700
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &46496500 !ruby/object:Gem::Requirement
49
+ requirement: &176403740 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.8.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *46496500
57
+ version_requirements: *176403740
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rdoc
60
- requirement: &46495600 !ruby/object:Gem::Requirement
60
+ requirement: &176402780 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '3.12'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *46495600
68
+ version_requirements: *176402780
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
- requirement: &46494460 !ruby/object:Gem::Requirement
71
+ requirement: &176401640 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '1.0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *46494460
79
+ version_requirements: *176401640
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: jeweler
82
- requirement: &46493700 !ruby/object:Gem::Requirement
82
+ requirement: &176398580 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: 1.8.7
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *46493700
90
+ version_requirements: *176398580
91
91
  description: Simple ACE(Amazon Compatible Environment) Client
92
92
  email: tily05@gmail.com
93
93
  executables:
@@ -125,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  segments:
127
127
  - 0
128
- hash: 2035499270960156883
128
+ hash: -1538802367424369011
129
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  none: false
131
131
  requirements: