aws-sigv4 1.0.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: dc5dab371e96d81659b31a8f240fac3f0181e1b1
4
- data.tar.gz: fa9c9cce2b176379585923c0345890756f930e78
2
+ SHA256:
3
+ metadata.gz: 46dbb72f9e31ff1022703f91ae7e1d2cfe78c78629e682fe99468933704aff62
4
+ data.tar.gz: 963db4a8f39031b64398dea0d2ebc7167af20e79446e23bfd270c6cb85d4738f
5
5
  SHA512:
6
- metadata.gz: cdad04342ef2a0e2e8d1016af4d7e50daf13bc892981f7629c7fb444bf86c599ad6c9657e377176c66ebddaea8a73b211c13e05c63ae38da9cf7f4cec86d70c7
7
- data.tar.gz: af5088ab2930b07050ef3ab12c7de87671f54f3d8bcecc38a1fdde49e1df69f43cb11a80c402caf7257f5156b2eec935d35bcab427578f979ee8e6185d5bb1c0
6
+ metadata.gz: '0142942e58db9971d8ceaa2aeb7c97d1cd58bdba463366fd06831b1769a3c124f6e1f1b082c4d1b5917f794ab70556f6bd78dbb67824a35e74ccbfc5bdaafa25'
7
+ data.tar.gz: aca23ad7a8a98f24abdbbaa2afe9cde1430a7a10f627b63d7da56939665056ccc7a04226dc14e3793e3848032291aba990fc10576369f3974911593566ea9262
data/CHANGELOG.md ADDED
@@ -0,0 +1,88 @@
1
+ Unreleased Changes
2
+ ------------------
3
+
4
+ 1.4.0 (2021-09-02)
5
+ ------------------
6
+
7
+ * Feature - add `signing_algorithm` option with `sigv4` default.
8
+
9
+ 1.3.0 (2021-09-01)
10
+ ------------------
11
+
12
+ * Feature - AWS SDK for Ruby no longer supports Ruby runtime versions 1.9, 2.0, 2.1, and 2.2.
13
+
14
+ 1.2.4 (2021-07-08)
15
+ ------------------
16
+
17
+ * Issue - Fix usage of `:uri_escape_path` and `:apply_checksum_header` in `Signer`.
18
+
19
+ 1.2.3 (2021-03-04)
20
+ ------------------
21
+
22
+ * Issue - Include LICENSE, CHANGELOG, and VERSION files with this gem.
23
+
24
+ 1.2.2 (2020-08-13)
25
+ ------------------
26
+
27
+ * Issue - Sort query params with same names by value when signing. (#2376)
28
+
29
+ 1.2.1 (2020-06-24)
30
+ ------------------
31
+
32
+ * Issue - Don't overwrite `host` header in sigv4 signer if given.
33
+
34
+ 1.2.0 (2020-06-17)
35
+ ------------------
36
+
37
+ * Feature - Bump `aws-eventstream` dependency to `~> 1`.
38
+
39
+ 1.1.4 (2020-05-28)
40
+ ------------------
41
+
42
+ * Issue - Don't use `expect` header to compute Signature.
43
+
44
+ 1.1.3 (2020-04-27)
45
+ ------------------
46
+
47
+ * Issue - Don't rely on the set? method of credentials.
48
+
49
+ 1.1.2 (2020-04-17)
50
+ ------------------
51
+
52
+ * Issue - Raise errors when credentials are not set (nil or empty)
53
+
54
+ 1.1.1 (2020-02-26)
55
+ ------------------
56
+
57
+ * Issue - Handle signing for unknown protocols and default ports.
58
+
59
+ 1.1.0 (2019-03-13)
60
+ ------------------
61
+
62
+ * Feature - Support signature V4 signing per event.
63
+
64
+ 1.0.3 (2018-06-28)
65
+ ------------------
66
+
67
+ * Issue - Reduce memory allocation when generating signatures.
68
+
69
+ 1.0.2 (2018-02-21)
70
+ ------------------
71
+
72
+ * Issue - Fix Ruby warning: shadowed local variable "headers".
73
+
74
+ 1.0.2 (2017-08-31)
75
+ ------------------
76
+
77
+ * Issue - Update `aws-sigv4` gemspec metadata.
78
+
79
+ 1.0.1 (2017-07-12)
80
+ ------------------
81
+
82
+ * Issue - Make UTF-8 encoding explicit in spec test.
83
+
84
+
85
+ 1.0.0 (2016-11-08)
86
+ ------------------
87
+
88
+ * Feature - Initial release of the `aws-sigv4` gem.
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.4.0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module Sigv4
3
5
  # Users that wish to configure static credentials can use the
@@ -28,11 +30,14 @@ module Aws
28
30
  # @return [String, nil]
29
31
  attr_reader :session_token
30
32
 
31
- # @return [Boolean]
33
+ # @return [Boolean] Returns `true` if the access key id and secret
34
+ # access key are both set.
32
35
  def set?
33
- !!(access_key_id && secret_access_key)
36
+ !access_key_id.nil? &&
37
+ !access_key_id.empty? &&
38
+ !secret_access_key.nil? &&
39
+ !secret_access_key.empty?
34
40
  end
35
-
36
41
  end
37
42
 
38
43
  # Users that wish to configure static credentials can use the
@@ -53,6 +58,10 @@ module Aws
53
58
  # @return [Credentials]
54
59
  attr_reader :credentials
55
60
 
61
+ # @return [Boolean]
62
+ def set?
63
+ !!credentials && credentials.set?
64
+ end
56
65
  end
57
66
 
58
67
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module Sigv4
3
5
  module Errors
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
 
3
5
  module Aws
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module Sigv4
3
5
  class Signature
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'openssl'
2
4
  require 'tempfile'
3
5
  require 'time'
4
6
  require 'uri'
5
7
  require 'set'
6
8
  require 'cgi'
9
+ require 'aws-eventstream'
7
10
 
8
11
  module Aws
9
12
  module Sigv4
@@ -121,8 +124,16 @@ module Aws
121
124
  @credentials_provider = extract_credentials_provider(options)
122
125
  @unsigned_headers = Set.new((options.fetch(:unsigned_headers, [])).map(&:downcase))
123
126
  @unsigned_headers << 'authorization'
127
+ @unsigned_headers << 'x-amzn-trace-id'
128
+ @unsigned_headers << 'expect'
124
129
  [:uri_escape_path, :apply_checksum_header].each do |opt|
125
- instance_variable_set("@#{opt}", options.key?(opt) ? !!options[:opt] : true)
130
+ instance_variable_set("@#{opt}", options.key?(opt) ? !!options[opt] : true)
131
+ end
132
+
133
+ if options[:signing_algorithm] == :sigv4a
134
+ raise ArgumentError, 'You are attempting to sign a' \
135
+ ' request with sigv4a which requires aws-crt and version 1.4.0.crt or later of the aws-sigv4 gem.'\
136
+ ' Please install the gem or add it to your gemfile.'
126
137
  end
127
138
  end
128
139
 
@@ -165,11 +176,11 @@ module Aws
165
176
  # )
166
177
  #
167
178
  # # Apply the following hash of headers to your HTTP request
168
- # signature.headers['Host']
169
- # signature.headers['X-Amz-Date']
170
- # signature.headers['X-Amz-Security-Token']
171
- # signature.headers['X-Amz-Content-Sha256']
172
- # signature.headers['Authorization']
179
+ # signature.headers['host']
180
+ # signature.headers['x-amz-date']
181
+ # signature.headers['x-amz-security-token']
182
+ # signature.headers['x-amz-content-sha256']
183
+ # signature.headers['authorization']
173
184
  #
174
185
  # In addition to computing the signature headers, the canonicalized
175
186
  # request, string to sign and content sha256 checksum are also available.
@@ -191,7 +202,7 @@ module Aws
191
202
  # to sign. If the 'X-Amz-Content-Sha256' header is set, the `:body`
192
203
  # is optional and will not be read.
193
204
  #
194
- # @option request [otpional, String, IO] :body ('') The HTTP request body.
205
+ # @option request [optional, String, IO] :body ('') The HTTP request body.
195
206
  # A sha256 checksum is computed of the body unless the
196
207
  # 'X-Amz-Content-Sha256' header is set.
197
208
  #
@@ -200,7 +211,7 @@ module Aws
200
211
  #
201
212
  def sign_request(request)
202
213
 
203
- creds = get_credentials
214
+ creds = fetch_credentials
204
215
 
205
216
  http_method = extract_http_method(request)
206
217
  url = extract_url(request)
@@ -214,7 +225,7 @@ module Aws
214
225
  content_sha256 ||= sha256_hexdigest(request[:body] || '')
215
226
 
216
227
  sigv4_headers = {}
217
- sigv4_headers['host'] = host(url)
228
+ sigv4_headers['host'] = headers['host'] || host(url)
218
229
  sigv4_headers['x-amz-date'] = datetime
219
230
  sigv4_headers['x-amz-security-token'] = creds.session_token if creds.session_token
220
231
  sigv4_headers['x-amz-content-sha256'] ||= content_sha256 if @apply_checksum_header
@@ -242,6 +253,59 @@ module Aws
242
253
  )
243
254
  end
244
255
 
256
+ # Signs a event and returns signature headers and prior signature
257
+ # used for next event signing.
258
+ #
259
+ # Headers of a sigv4 signed event message only contains 2 headers
260
+ # * ':chunk-signature'
261
+ # * computed signature of the event, binary string, 'bytes' type
262
+ # * ':date'
263
+ # * millisecond since epoch, 'timestamp' type
264
+ #
265
+ # Payload of the sigv4 signed event message contains eventstream encoded message
266
+ # which is serialized based on input and protocol
267
+ #
268
+ # To sign events
269
+ #
270
+ # headers_0, signature_0 = signer.sign_event(
271
+ # prior_signature, # hex-encoded string
272
+ # payload_0, # binary string (eventstream encoded event 0)
273
+ # encoder, # Aws::EventStreamEncoder
274
+ # )
275
+ #
276
+ # headers_1, signature_1 = signer.sign_event(
277
+ # signature_0,
278
+ # payload_1, # binary string (eventstream encoded event 1)
279
+ # encoder
280
+ # )
281
+ #
282
+ # The initial prior_signature should be using the signature computed at initial request
283
+ #
284
+ # Note:
285
+ #
286
+ # Since ':chunk-signature' header value has bytes type, the signature value provided
287
+ # needs to be a binary string instead of a hex-encoded string (like original signature
288
+ # V4 algorithm). Thus, when returning signature value used for next event siging, the
289
+ # signature value (a binary string) used at ':chunk-signature' needs to converted to
290
+ # hex-encoded string using #unpack
291
+ def sign_event(prior_signature, payload, encoder)
292
+ creds = fetch_credentials
293
+ time = Time.now
294
+ headers = {}
295
+
296
+ datetime = time.utc.strftime("%Y%m%dT%H%M%SZ")
297
+ date = datetime[0,8]
298
+ headers[':date'] = Aws::EventStream::HeaderValue.new(value: time.to_i * 1000, type: 'timestamp')
299
+
300
+ sts = event_string_to_sign(datetime, headers, payload, prior_signature, encoder)
301
+ sig = event_signature(creds.secret_access_key, date, sts)
302
+
303
+ headers[':chunk-signature'] = Aws::EventStream::HeaderValue.new(value: sig, type: 'bytes')
304
+
305
+ # Returning signed headers and signature value in hex-encoded string
306
+ [headers, sig.unpack('H*').first]
307
+ end
308
+
245
309
  # Signs a URL with query authentication. Using query parameters
246
310
  # to authenticate requests is useful when you want to express a
247
311
  # request entirely in a URL. This method is also referred as
@@ -252,7 +316,7 @@ module Aws
252
316
  # To generate a presigned URL, you must provide a HTTP URI and
253
317
  # the http method.
254
318
  #
255
- # url = signer.presigned_url(
319
+ # url = signer.presign_url(
256
320
  # http_method: 'GET',
257
321
  # url: 'https://my-bucket.s3-us-east-1.amazonaws.com/key',
258
322
  # expires_in: 60
@@ -261,7 +325,7 @@ module Aws
261
325
  # By default, signatures are valid for 15 minutes. You can specify
262
326
  # the number of seconds for the URL to expire in.
263
327
  #
264
- # url = signer.presigned_url(
328
+ # url = signer.presign_url(
265
329
  # http_method: 'GET',
266
330
  # url: 'https://my-bucket.s3-us-east-1.amazonaws.com/key',
267
331
  # expires_in: 3600 # one hour
@@ -272,7 +336,7 @@ module Aws
272
336
  # **must** be provided, or the signature is invalid. Other headers
273
337
  # are optional, but should be provided for security reasons.
274
338
  #
275
- # url = signer.presigned_url(
339
+ # url = signer.presign_url(
276
340
  # http_method: 'PUT',
277
341
  # url: 'https://my-bucket.s3-us-east-1.amazonaws.com/key',
278
342
  # headers: {
@@ -312,13 +376,13 @@ module Aws
312
376
  #
313
377
  def presign_url(options)
314
378
 
315
- creds = get_credentials
379
+ creds = fetch_credentials
316
380
 
317
381
  http_method = extract_http_method(options)
318
382
  url = extract_url(options)
319
383
 
320
384
  headers = downcase_headers(options[:headers])
321
- headers['host'] = host(url)
385
+ headers['host'] ||= host(url)
322
386
 
323
387
  datetime = headers['x-amz-date']
324
388
  datetime ||= (options[:time] || Time.now).utc.strftime("%Y%m%dT%H%M%SZ")
@@ -333,8 +397,8 @@ module Aws
333
397
  params['X-Amz-Credential'] = credential(creds, date)
334
398
  params['X-Amz-Date'] = datetime
335
399
  params['X-Amz-Expires'] = extract_expires_in(options)
336
- params['X-Amz-SignedHeaders'] = signed_headers(headers)
337
400
  params['X-Amz-Security-Token'] = creds.session_token if creds.session_token
401
+ params['X-Amz-SignedHeaders'] = signed_headers(headers)
338
402
 
339
403
  params = params.map do |key, value|
340
404
  "#{uri_escape(key)}=#{uri_escape(value)}"
@@ -374,6 +438,29 @@ module Aws
374
438
  ].join("\n")
375
439
  end
376
440
 
441
+ # Compared to original #string_to_sign at signature v4 algorithm
442
+ # there is no canonical_request concept for an eventstream event,
443
+ # instead, an event contains headers and payload two parts, and
444
+ # they will be used for computing digest in #event_string_to_sign
445
+ #
446
+ # Note:
447
+ # While headers need to be encoded under eventstream format,
448
+ # payload used is already eventstream encoded (event without signature),
449
+ # thus no extra encoding is needed.
450
+ def event_string_to_sign(datetime, headers, payload, prior_signature, encoder)
451
+ encoded_headers = encoder.encode_headers(
452
+ Aws::EventStream::Message.new(headers: headers, payload: payload)
453
+ )
454
+ [
455
+ "AWS4-HMAC-SHA256-PAYLOAD",
456
+ datetime,
457
+ credential_scope(datetime[0,8]),
458
+ prior_signature,
459
+ sha256_hexdigest(encoded_headers),
460
+ sha256_hexdigest(payload)
461
+ ].join("\n")
462
+ end
463
+
377
464
  def credential_scope(date)
378
465
  [
379
466
  date,
@@ -395,6 +482,24 @@ module Aws
395
482
  hexhmac(k_credentials, string_to_sign)
396
483
  end
397
484
 
485
+ # Comparing to original signature v4 algorithm,
486
+ # returned signature is a binary string instread of
487
+ # hex-encoded string. (Since ':chunk-signature' requires
488
+ # 'bytes' type)
489
+ #
490
+ # Note:
491
+ # converting signature from binary string to hex-encoded
492
+ # string is handled at #sign_event instead. (Will be used
493
+ # as next prior signature for event signing)
494
+ def event_signature(secret_access_key, date, string_to_sign)
495
+ k_date = hmac("AWS4" + secret_access_key, date)
496
+ k_region = hmac(k_date, @region)
497
+ k_service = hmac(k_region, @service)
498
+ k_credentials = hmac(k_service, 'aws4_request')
499
+ hmac(k_credentials, string_to_sign)
500
+ end
501
+
502
+
398
503
  def path(url)
399
504
  path = url.path
400
505
  path = '/' if path == ''
@@ -408,18 +513,26 @@ module Aws
408
513
  def normalized_querystring(querystring)
409
514
  params = querystring.split('&')
410
515
  params = params.map { |p| p.match(/=/) ? p : p + '=' }
411
- # We have to sort by param name and preserve order of params that
412
- # have the same name. Default sort <=> in JRuby will swap members
516
+ # From: https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
517
+ # Sort the parameter names by character code point in ascending order.
518
+ # Parameters with duplicate names should be sorted by value.
519
+ #
520
+ # Default sort <=> in JRuby will swap members
413
521
  # occasionally when <=> is 0 (considered still sorted), but this
414
522
  # causes our normalized query string to not match the sent querystring.
415
- # When names match, we then sort by their original order
416
- params = params.each.with_index.sort do |a, b|
523
+ # When names match, we then sort by their values. When values also
524
+ # match then we sort by their original order
525
+ params.each.with_index.sort do |a, b|
417
526
  a, a_offset = a
418
- a_name = a.split('=')[0]
419
527
  b, b_offset = b
420
- b_name = b.split('=')[0]
528
+ a_name, a_value = a.split('=')
529
+ b_name, b_value = b.split('=')
421
530
  if a_name == b_name
422
- a_offset <=> b_offset
531
+ if a_value == b_value
532
+ a_offset <=> b_offset
533
+ else
534
+ a_value <=> b_value
535
+ end
423
536
  else
424
537
  a_name <=> b_name
425
538
  end
@@ -437,11 +550,11 @@ module Aws
437
550
  end
438
551
 
439
552
  def canonical_headers(headers)
440
- headers = headers.inject([]) do |headers, (k,v)|
553
+ headers = headers.inject([]) do |hdrs, (k,v)|
441
554
  if @unsigned_headers.include?(k)
442
- headers
555
+ hdrs
443
556
  else
444
- headers << [k,v]
557
+ hdrs << [k,v]
445
558
  end
446
559
  end
447
560
  headers = headers.sort_by(&:first)
@@ -453,18 +566,14 @@ module Aws
453
566
  end
454
567
 
455
568
  def host(uri)
456
- if standard_port?(uri)
569
+ # Handles known and unknown URI schemes; default_port nil when unknown.
570
+ if uri.default_port == uri.port
457
571
  uri.host
458
572
  else
459
573
  "#{uri.host}:#{uri.port}"
460
574
  end
461
575
  end
462
576
 
463
- def standard_port?(uri)
464
- (uri.scheme == 'http' && uri.port == 80) ||
465
- (uri.scheme == 'https' && uri.port == 443)
466
- end
467
-
468
577
  # @param [File, Tempfile, IO#read, String] value
469
578
  # @return [String<SHA256 Hexdigest>]
470
579
  def sha256_hexdigest(value)
@@ -472,7 +581,9 @@ module Aws
472
581
  OpenSSL::Digest::SHA256.file(value).hexdigest
473
582
  elsif value.respond_to?(:read)
474
583
  sha256 = OpenSSL::Digest::SHA256.new
475
- while chunk = value.read(1024 * 1024) # 1MB
584
+ loop do
585
+ chunk = value.read(1024 * 1024) # 1MB
586
+ break unless chunk
476
587
  sha256.update(chunk)
477
588
  end
478
589
  value.rewind
@@ -560,18 +671,26 @@ module Aws
560
671
  self.class.uri_escape_path(string)
561
672
  end
562
673
 
563
- def get_credentials
674
+
675
+ def fetch_credentials
564
676
  credentials = @credentials_provider.credentials
565
677
  if credentials_set?(credentials)
566
678
  credentials
567
679
  else
568
- msg = 'unable to sign request without credentials set'
569
- raise Errors::MissingCredentialsError.new(msg)
680
+ raise Errors::MissingCredentialsError,
681
+ 'unable to sign request without credentials set'
570
682
  end
571
683
  end
572
684
 
685
+ # Returns true if credentials are set (not nil or empty)
686
+ # Credentials may not implement the Credentials interface
687
+ # and may just be credential like Client response objects
688
+ # (eg those returned by sts#assume_role)
573
689
  def credentials_set?(credentials)
574
- credentials.access_key_id && credentials.secret_access_key
690
+ !credentials.access_key_id.nil? &&
691
+ !credentials.access_key_id.empty? &&
692
+ !credentials.secret_access_key.nil? &&
693
+ !credentials.secret_access_key.empty?
575
694
  end
576
695
 
577
696
  class << self
data/lib/aws-sigv4.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'aws-sigv4/credentials'
2
4
  require_relative 'aws-sigv4/errors'
3
5
  require_relative 'aws-sigv4/signature'
metadata CHANGED
@@ -1,32 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-sigv4
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-08 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Amazon Web Services Signature Version 4 signing ligrary. Generates sigv4
11
+ date: 2021-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-eventstream
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.2
33
+ description: Amazon Web Services Signature Version 4 signing library. Generates sigv4
14
34
  signature for HTTP requests.
15
35
  email:
16
36
  executables: []
17
37
  extensions: []
18
38
  extra_rdoc_files: []
19
39
  files:
40
+ - CHANGELOG.md
41
+ - LICENSE.txt
42
+ - VERSION
20
43
  - lib/aws-sigv4.rb
21
44
  - lib/aws-sigv4/credentials.rb
22
45
  - lib/aws-sigv4/errors.rb
23
46
  - lib/aws-sigv4/request.rb
24
47
  - lib/aws-sigv4/signature.rb
25
48
  - lib/aws-sigv4/signer.rb
26
- homepage: http://github.com/aws/aws-sdk-ruby
49
+ homepage: https://github.com/aws/aws-sdk-ruby
27
50
  licenses:
28
51
  - Apache-2.0
29
- metadata: {}
52
+ metadata:
53
+ source_code_uri: https://github.com/aws/aws-sdk-ruby/tree/version-3/gems/aws-sigv4
54
+ changelog_uri: https://github.com/aws/aws-sdk-ruby/tree/version-3/gems/aws-sigv4/CHANGELOG.md
30
55
  post_install_message:
31
56
  rdoc_options: []
32
57
  require_paths:
@@ -35,15 +60,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
35
60
  requirements:
36
61
  - - ">="
37
62
  - !ruby/object:Gem::Version
38
- version: '0'
63
+ version: '2.3'
39
64
  required_rubygems_version: !ruby/object:Gem::Requirement
40
65
  requirements:
41
66
  - - ">="
42
67
  - !ruby/object:Gem::Version
43
68
  version: '0'
44
69
  requirements: []
45
- rubyforge_project:
46
- rubygems_version: 2.6.4
70
+ rubygems_version: 3.1.6
47
71
  signing_key:
48
72
  specification_version: 4
49
73
  summary: AWS Signature Version 4 library.