aws-sigv4 1.0.3 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +88 -0
- data/LICENSE.txt +202 -0
- data/VERSION +1 -0
- data/lib/aws-sigv4/credentials.rb +12 -3
- data/lib/aws-sigv4/errors.rb +2 -0
- data/lib/aws-sigv4/request.rb +2 -0
- data/lib/aws-sigv4/signature.rb +2 -0
- data/lib/aws-sigv4/signer.rb +142 -24
- data/lib/aws-sigv4.rb +2 -0
- metadata +32 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 46dbb72f9e31ff1022703f91ae7e1d2cfe78c78629e682fe99468933704aff62
|
4
|
+
data.tar.gz: 963db4a8f39031b64398dea0d2ebc7167af20e79446e23bfd270c6cb85d4738f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/lib/aws-sigv4/errors.rb
CHANGED
data/lib/aws-sigv4/request.rb
CHANGED
data/lib/aws-sigv4/signature.rb
CHANGED
data/lib/aws-sigv4/signer.rb
CHANGED
@@ -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
|
@@ -122,8 +125,15 @@ module Aws
|
|
122
125
|
@unsigned_headers = Set.new((options.fetch(:unsigned_headers, [])).map(&:downcase))
|
123
126
|
@unsigned_headers << 'authorization'
|
124
127
|
@unsigned_headers << 'x-amzn-trace-id'
|
128
|
+
@unsigned_headers << 'expect'
|
125
129
|
[:uri_escape_path, :apply_checksum_header].each do |opt|
|
126
|
-
instance_variable_set("@#{opt}", options.key?(opt) ? !!options[
|
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.'
|
127
137
|
end
|
128
138
|
end
|
129
139
|
|
@@ -201,7 +211,7 @@ module Aws
|
|
201
211
|
#
|
202
212
|
def sign_request(request)
|
203
213
|
|
204
|
-
creds =
|
214
|
+
creds = fetch_credentials
|
205
215
|
|
206
216
|
http_method = extract_http_method(request)
|
207
217
|
url = extract_url(request)
|
@@ -215,7 +225,7 @@ module Aws
|
|
215
225
|
content_sha256 ||= sha256_hexdigest(request[:body] || '')
|
216
226
|
|
217
227
|
sigv4_headers = {}
|
218
|
-
sigv4_headers['host'] = host(url)
|
228
|
+
sigv4_headers['host'] = headers['host'] || host(url)
|
219
229
|
sigv4_headers['x-amz-date'] = datetime
|
220
230
|
sigv4_headers['x-amz-security-token'] = creds.session_token if creds.session_token
|
221
231
|
sigv4_headers['x-amz-content-sha256'] ||= content_sha256 if @apply_checksum_header
|
@@ -243,6 +253,59 @@ module Aws
|
|
243
253
|
)
|
244
254
|
end
|
245
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
|
+
|
246
309
|
# Signs a URL with query authentication. Using query parameters
|
247
310
|
# to authenticate requests is useful when you want to express a
|
248
311
|
# request entirely in a URL. This method is also referred as
|
@@ -313,13 +376,13 @@ module Aws
|
|
313
376
|
#
|
314
377
|
def presign_url(options)
|
315
378
|
|
316
|
-
creds =
|
379
|
+
creds = fetch_credentials
|
317
380
|
|
318
381
|
http_method = extract_http_method(options)
|
319
382
|
url = extract_url(options)
|
320
383
|
|
321
384
|
headers = downcase_headers(options[:headers])
|
322
|
-
headers['host']
|
385
|
+
headers['host'] ||= host(url)
|
323
386
|
|
324
387
|
datetime = headers['x-amz-date']
|
325
388
|
datetime ||= (options[:time] || Time.now).utc.strftime("%Y%m%dT%H%M%SZ")
|
@@ -334,8 +397,8 @@ module Aws
|
|
334
397
|
params['X-Amz-Credential'] = credential(creds, date)
|
335
398
|
params['X-Amz-Date'] = datetime
|
336
399
|
params['X-Amz-Expires'] = extract_expires_in(options)
|
337
|
-
params['X-Amz-SignedHeaders'] = signed_headers(headers)
|
338
400
|
params['X-Amz-Security-Token'] = creds.session_token if creds.session_token
|
401
|
+
params['X-Amz-SignedHeaders'] = signed_headers(headers)
|
339
402
|
|
340
403
|
params = params.map do |key, value|
|
341
404
|
"#{uri_escape(key)}=#{uri_escape(value)}"
|
@@ -375,6 +438,29 @@ module Aws
|
|
375
438
|
].join("\n")
|
376
439
|
end
|
377
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
|
+
|
378
464
|
def credential_scope(date)
|
379
465
|
[
|
380
466
|
date,
|
@@ -396,6 +482,24 @@ module Aws
|
|
396
482
|
hexhmac(k_credentials, string_to_sign)
|
397
483
|
end
|
398
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
|
+
|
399
503
|
def path(url)
|
400
504
|
path = url.path
|
401
505
|
path = '/' if path == ''
|
@@ -409,18 +513,26 @@ module Aws
|
|
409
513
|
def normalized_querystring(querystring)
|
410
514
|
params = querystring.split('&')
|
411
515
|
params = params.map { |p| p.match(/=/) ? p : p + '=' }
|
412
|
-
#
|
413
|
-
#
|
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
|
414
521
|
# occasionally when <=> is 0 (considered still sorted), but this
|
415
522
|
# causes our normalized query string to not match the sent querystring.
|
416
|
-
# When names match, we then sort by their
|
417
|
-
|
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|
|
418
526
|
a, a_offset = a
|
419
|
-
a_name = a.split('=')[0]
|
420
527
|
b, b_offset = b
|
421
|
-
|
528
|
+
a_name, a_value = a.split('=')
|
529
|
+
b_name, b_value = b.split('=')
|
422
530
|
if a_name == b_name
|
423
|
-
|
531
|
+
if a_value == b_value
|
532
|
+
a_offset <=> b_offset
|
533
|
+
else
|
534
|
+
a_value <=> b_value
|
535
|
+
end
|
424
536
|
else
|
425
537
|
a_name <=> b_name
|
426
538
|
end
|
@@ -454,18 +566,14 @@ module Aws
|
|
454
566
|
end
|
455
567
|
|
456
568
|
def host(uri)
|
457
|
-
|
569
|
+
# Handles known and unknown URI schemes; default_port nil when unknown.
|
570
|
+
if uri.default_port == uri.port
|
458
571
|
uri.host
|
459
572
|
else
|
460
573
|
"#{uri.host}:#{uri.port}"
|
461
574
|
end
|
462
575
|
end
|
463
576
|
|
464
|
-
def standard_port?(uri)
|
465
|
-
(uri.scheme == 'http' && uri.port == 80) ||
|
466
|
-
(uri.scheme == 'https' && uri.port == 443)
|
467
|
-
end
|
468
|
-
|
469
577
|
# @param [File, Tempfile, IO#read, String] value
|
470
578
|
# @return [String<SHA256 Hexdigest>]
|
471
579
|
def sha256_hexdigest(value)
|
@@ -473,7 +581,9 @@ module Aws
|
|
473
581
|
OpenSSL::Digest::SHA256.file(value).hexdigest
|
474
582
|
elsif value.respond_to?(:read)
|
475
583
|
sha256 = OpenSSL::Digest::SHA256.new
|
476
|
-
|
584
|
+
loop do
|
585
|
+
chunk = value.read(1024 * 1024) # 1MB
|
586
|
+
break unless chunk
|
477
587
|
sha256.update(chunk)
|
478
588
|
end
|
479
589
|
value.rewind
|
@@ -561,18 +671,26 @@ module Aws
|
|
561
671
|
self.class.uri_escape_path(string)
|
562
672
|
end
|
563
673
|
|
564
|
-
|
674
|
+
|
675
|
+
def fetch_credentials
|
565
676
|
credentials = @credentials_provider.credentials
|
566
677
|
if credentials_set?(credentials)
|
567
678
|
credentials
|
568
679
|
else
|
569
|
-
|
570
|
-
|
680
|
+
raise Errors::MissingCredentialsError,
|
681
|
+
'unable to sign request without credentials set'
|
571
682
|
end
|
572
683
|
end
|
573
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)
|
574
689
|
def credentials_set?(credentials)
|
575
|
-
credentials.access_key_id &&
|
690
|
+
!credentials.access_key_id.nil? &&
|
691
|
+
!credentials.access_key_id.empty? &&
|
692
|
+
!credentials.secret_access_key.nil? &&
|
693
|
+
!credentials.secret_access_key.empty?
|
576
694
|
end
|
577
695
|
|
578
696
|
class << self
|
data/lib/aws-sigv4.rb
CHANGED
metadata
CHANGED
@@ -1,34 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aws-sigv4
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.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:
|
12
|
-
dependencies:
|
13
|
-
|
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:
|
49
|
+
homepage: https://github.com/aws/aws-sdk-ruby
|
27
50
|
licenses:
|
28
51
|
- Apache-2.0
|
29
52
|
metadata:
|
30
|
-
source_code_uri: https://github.com/aws/aws-sdk-ruby/tree/
|
31
|
-
changelog_uri: https://github.com/aws/aws-sdk-ruby/tree/
|
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
|
32
55
|
post_install_message:
|
33
56
|
rdoc_options: []
|
34
57
|
require_paths:
|
@@ -37,15 +60,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
60
|
requirements:
|
38
61
|
- - ">="
|
39
62
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
63
|
+
version: '2.3'
|
41
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
65
|
requirements:
|
43
66
|
- - ">="
|
44
67
|
- !ruby/object:Gem::Version
|
45
68
|
version: '0'
|
46
69
|
requirements: []
|
47
|
-
|
48
|
-
rubygems_version: 2.5.2.3
|
70
|
+
rubygems_version: 3.1.6
|
49
71
|
signing_key:
|
50
72
|
specification_version: 4
|
51
73
|
summary: AWS Signature Version 4 library.
|