1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
# frozen_string_literal: true
require_relative "helper"
require "rubygems/remote_fetcher"
require "rubygems/package"
class TestGemRemoteFetcherS3 < Gem::TestCase
include Gem::DefaultUserInteraction
class FakeGemRequest < Gem::Request
attr_reader :last_request, :uri
# Override perform_request to stub things
def perform_request(request)
@last_request = request
@response
end
def set_response(response)
@response = response
end
end
class FakeS3URISigner < Gem::S3URISigner
class << self
attr_accessor :return_token, :instance_profile
end
# Convenience method to output the recent aws iam queries made in tests
# this outputs the verb, path, and any non-generic headers
def recent_aws_query_logs
sreqs = @aws_iam_calls.map do |c|
r = c.last_request
s = +"#{r.method} #{c.uri}\n"
r.each_header do |key, v|
# Only include headers that start with x-
next unless key.start_with?("x-")
s << " #{key}=#{v}\n"
end
s
end
sreqs.join("")
end
def initialize(uri, method)
@aws_iam_calls = []
super
end
def ec2_iam_request(uri, verb)
fake_s3_request = FakeGemRequest.new(uri, verb, nil, nil)
@aws_iam_calls << fake_s3_request
case uri.to_s
when "http://169.254.169.254/latest/api/token"
if FakeS3URISigner.return_token.nil?
res = Gem::Net::HTTPUnauthorized.new nil, 401, nil
def res.body = "you got a 401! panic!"
else
res = Gem::Net::HTTPOK.new nil, 200, nil
def res.body = FakeS3URISigner.return_token
end
when "http://169.254.169.254/latest/meta-data/iam/info"
res = Gem::Net::HTTPOK.new nil, 200, nil
def res.body
<<~JSON
{
"Code": "Success",
"LastUpdated": "2023-05-27:05:05",
"InstanceProfileArn": "arn:aws:iam::somesecretid:instance-profile/TestRole",
"InstanceProfileId": "SOMEPROFILEID"
}
JSON
end
when "http://169.254.169.254/latest/meta-data/iam/security-credentials/TestRole"
res = Gem::Net::HTTPOK.new nil, 200, nil
def res.body = FakeS3URISigner.instance_profile
else
raise "Unexpected request to #{uri}"
end
fake_s3_request.set_response(res)
fake_s3_request
end
end
class FakeGemFetcher < Gem::RemoteFetcher
attr_reader :fetched_uri, :last_s3_uri_signer
def request(uri, request_class, last_modified = nil)
@fetched_uri = uri
res = Gem::Net::HTTPOK.new nil, 200, nil
def res.body = "success"
res
end
def s3_uri_signer(uri, method)
@last_s3_uri_signer = FakeS3URISigner.new(uri, method)
end
end
def setup
super
@a1, @a1_gem = util_gem "a", "1" do |s|
s.executables << "a_bin"
end
@a1.loaded_from = File.join(@gemhome, "specifications", @a1.full_name)
end
def assert_fetched_s3_with_imds_v2(expected_token)
# Three API requests:
# 1. Get the token
# 2. Lookup profile details
# 3. Query the credentials
expected = <<~TEXT
PUT http://169.254.169.254/latest/api/token
x-aws-ec2-metadata-token-ttl-seconds=60
GET http://169.254.169.254/latest/meta-data/iam/info
x-aws-ec2-metadata-token=#{expected_token}
GET http://169.254.169.254/latest/meta-data/iam/security-credentials/TestRole
x-aws-ec2-metadata-token=#{expected_token}
TEXT
recent_aws_query_logs = @fetcher.last_s3_uri_signer.recent_aws_query_logs
assert_equal(expected.strip, recent_aws_query_logs.strip)
end
def assert_fetched_s3_with_imds_v1
# Three API requests:
# 1. Get the token (which fails)
# 2. Lookup profile details without token
# 3. Query the credentials without token
expected = <<~TEXT
PUT http://169.254.169.254/latest/api/token
x-aws-ec2-metadata-token-ttl-seconds=60
GET http://169.254.169.254/latest/meta-data/iam/info
GET http://169.254.169.254/latest/meta-data/iam/security-credentials/TestRole
TEXT
recent_aws_query_logs = @fetcher.last_s3_uri_signer.recent_aws_query_logs
assert_equal(expected.strip, recent_aws_query_logs.strip)
end
def with_imds_v2_failure
FakeS3URISigner.should_fail = true
yield(fetcher)
ensure
FakeS3URISigner.should_fail = false
end
def assert_fetch_s3(url:, signature:, token: nil, region: "us-east-1", instance_profile_json: nil, fetcher: nil, method: "GET")
FakeS3URISigner.instance_profile = instance_profile_json
FakeS3URISigner.return_token = token
@fetcher = fetcher || FakeGemFetcher.new(nil)
res = @fetcher.fetch_s3 Gem::URI.parse(url), nil, (method == "HEAD")
assert_equal "https://my-bucket.s3.#{region}.amazonaws.com/gems/specs.4.8.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=testuser%2F20190624%2F#{region}%2Fs3%2Faws4_request&X-Amz-Date=20190624T051941Z&X-Amz-Expires=86400#{token ? "&X-Amz-Security-Token=" + token : ""}&X-Amz-SignedHeaders=host&X-Amz-Signature=#{signature}", @fetcher.fetched_uri.to_s
if method == "HEAD"
assert_equal 200, res.code
else
assert_equal "success", res
end
ensure
FakeS3URISigner.instance_profile = nil
FakeS3URISigner.return_token = nil
end
def test_fetch_s3_config_creds
Gem.configuration[:s3_source] = {
"my-bucket" => { id: "testuser", secret: "testpass" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c",
)
end
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_head_request
Gem.configuration[:s3_source] = {
"my-bucket" => { id: "testuser", secret: "testpass" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
token = nil
region = "us-east-1"
instance_profile_json = nil
method = "HEAD"
assert_fetch_s3(
url: url,
signature: "a3c6cf9a2db62e85f4e57f8fc8ac8b5ff5c1fdd4aeef55935d05e05174d9c885",
token: token,
region: region,
instance_profile_json: instance_profile_json,
method: method
)
end
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_config_creds_with_region
Gem.configuration[:s3_source] = {
"my-bucket" => { id: "testuser", secret: "testpass", region: "us-west-2" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "ef07487bfd8e3ca594f8fc29775b70c0a0636f51318f95d4f12b2e6e1fd8c716",
region: "us-west-2"
)
end
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_config_creds_with_token
Gem.configuration[:s3_source] = {
"my-bucket" => { id: "testuser", secret: "testpass", security_token: "testtoken" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "e709338735f9077edf8f6b94b247171c266a9605975e08e4a519a123c3322625",
token: "testtoken"
)
end
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_env_creds
ENV["AWS_ACCESS_KEY_ID"] = "testuser"
ENV["AWS_SECRET_ACCESS_KEY"] = "testpass"
ENV["AWS_SESSION_TOKEN"] = nil
Gem.configuration[:s3_source] = {
"my-bucket" => { provider: "env" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c"
)
end
ensure
ENV.each_key {|key| ENV.delete(key) if key.start_with?("AWS") }
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_env_creds_with_region
ENV["AWS_ACCESS_KEY_ID"] = "testuser"
ENV["AWS_SECRET_ACCESS_KEY"] = "testpass"
ENV["AWS_SESSION_TOKEN"] = nil
Gem.configuration[:s3_source] = {
"my-bucket" => { provider: "env", region: "us-west-2" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "ef07487bfd8e3ca594f8fc29775b70c0a0636f51318f95d4f12b2e6e1fd8c716",
token: nil,
region: "us-west-2"
)
end
ensure
ENV.each_key {|key| ENV.delete(key) if key.start_with?("AWS") }
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_env_creds_with_token
ENV["AWS_ACCESS_KEY_ID"] = "testuser"
ENV["AWS_SECRET_ACCESS_KEY"] = "testpass"
ENV["AWS_SESSION_TOKEN"] = "testtoken"
Gem.configuration[:s3_source] = {
"my-bucket" => { provider: "env" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "e709338735f9077edf8f6b94b247171c266a9605975e08e4a519a123c3322625",
token: "testtoken"
)
end
ensure
ENV.each_key {|key| ENV.delete(key) if key.start_with?("AWS") }
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_url_creds
url = "s3://testuser:testpass@my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c"
)
end
end
def test_fetch_s3_instance_profile_creds
Gem.configuration[:s3_source] = {
"my-bucket" => { provider: "instance_profile" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "da82e098bdaed0d3087047670efc98eaadc20559a473b5eac8d70190d2a9e8fd",
region: "us-east-1",
token: "mysecrettoken",
instance_profile_json: '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "mysecrettoken"}'
)
assert_fetched_s3_with_imds_v2("mysecrettoken")
end
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_instance_profile_creds_with_region
Gem.configuration[:s3_source] = {
"my-bucket" => { provider: "instance_profile", region: "us-west-2" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "532960594dbfe31d1bbfc0e8e7a666c3cbdd8b00a143774da51b7f920704afd2",
region: "us-west-2",
token: "mysecrettoken",
instance_profile_json: '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "mysecrettoken"}'
)
assert_fetched_s3_with_imds_v2("mysecrettoken")
end
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_instance_profile_creds_with_token
Gem.configuration[:s3_source] = {
"my-bucket" => { provider: "instance_profile" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "e709338735f9077edf8f6b94b247171c266a9605975e08e4a519a123c3322625",
token: "testtoken",
region: "us-east-1",
instance_profile_json: '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "testtoken"}'
)
assert_fetched_s3_with_imds_v2("testtoken")
end
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_instance_profile_creds_with_fallback
Gem.configuration[:s3_source] = {
"my-bucket" => { provider: "instance_profile" },
}
url = "s3://my-bucket/gems/specs.4.8.gz"
Time.stub :now, Time.at(1_561_353_581) do
assert_fetch_s3(
url: url,
signature: "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c",
token: nil,
region: "us-east-1",
instance_profile_json: '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass"}'
)
assert_fetched_s3_with_imds_v1
end
ensure
Gem.configuration[:s3_source] = nil
end
def refute_fetch_s3(url:, expected_message:)
fetcher = Gem::RemoteFetcher.new nil
@fetcher = fetcher
e = assert_raise Gem::RemoteFetcher::FetchError do
fetcher.fetch_s3 Gem::URI.parse(url)
end
assert_match expected_message, e.message
end
def test_fetch_s3_no_source_key
url = "s3://my-bucket/gems/specs.4.8.gz"
refute_fetch_s3(url: url, expected_message: "no s3_source key exists in .gemrc")
end
def test_fetch_s3_no_host
Gem.configuration[:s3_source] = {
"my-bucket" => { id: "testuser", secret: "testpass" },
}
url = "s3://other-bucket/gems/specs.4.8.gz"
refute_fetch_s3(url: url, expected_message: "no key for host other-bucket in s3_source in .gemrc")
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_no_id
Gem.configuration[:s3_source] = { "my-bucket" => { secret: "testpass" } }
url = "s3://my-bucket/gems/specs.4.8.gz"
refute_fetch_s3(url: url, expected_message: "s3_source for my-bucket missing id or secret")
ensure
Gem.configuration[:s3_source] = nil
end
def test_fetch_s3_no_secret
Gem.configuration[:s3_source] = { "my-bucket" => { id: "testuser" } }
url = "s3://my-bucket/gems/specs.4.8.gz"
refute_fetch_s3(url: url, expected_message: "s3_source for my-bucket missing id or secret")
ensure
Gem.configuration[:s3_source] = nil
end
end
|