@@ -24,7 +24,7 @@ def set_response(response)
24
24
25
25
class FakeS3URISigner < Gem ::S3URISigner
26
26
class << self
27
- attr_accessor :should_fail , :instance_profile
27
+ attr_accessor :return_token , :instance_profile
28
28
end
29
29
30
30
# Convenience method to output the recent aws iam queries made in tests
@@ -55,12 +55,12 @@ def ec2_iam_request(uri, verb)
55
55
56
56
case uri . to_s
57
57
when "http://169.254.169.254/latest/api/token"
58
- if FakeS3URISigner . should_fail
58
+ if FakeS3URISigner . return_token . nil?
59
59
res = Gem ::Net ::HTTPUnauthorized . new nil , 401 , nil
60
60
def res . body = "you got a 401! panic!"
61
61
else
62
62
res = Gem ::Net ::HTTPOK . new nil , 200 , nil
63
- def res . body = "mysecrettoken"
63
+ def res . body = FakeS3URISigner . return_token
64
64
end
65
65
when "http://169.254.169.254/latest/meta-data/iam/info"
66
66
res = Gem ::Net ::HTTPOK . new nil , 200 , nil
@@ -112,7 +112,7 @@ def setup
112
112
@a1 . loaded_from = File . join ( @gemhome , "specifications" , @a1 . full_name )
113
113
end
114
114
115
- def assert_fetched_s3_with_imds_v2
115
+ def assert_fetched_s3_with_imds_v2 ( expected_token )
116
116
# Three API requests:
117
117
# 1. Get the token
118
118
# 2. Lookup profile details
@@ -121,9 +121,9 @@ def assert_fetched_s3_with_imds_v2
121
121
PUT http://169.254.169.254/latest/api/token
122
122
x-aws-ec2-metadata-token-ttl-seconds=60
123
123
GET http://169.254.169.254/latest/meta-data/iam/info
124
- x-aws-ec2-metadata-token=mysecrettoken
124
+ x-aws-ec2-metadata-token=#{ expected_token }
125
125
GET http://169.254.169.254/latest/meta-data/iam/security-credentials/TestRole
126
- x-aws-ec2-metadata-token=mysecrettoken
126
+ x-aws-ec2-metadata-token=#{ expected_token }
127
127
TEXT
128
128
recent_aws_query_logs = @fetcher . last_s3_uri_signer . recent_aws_query_logs
129
129
assert_equal ( expected . strip , recent_aws_query_logs . strip )
@@ -153,10 +153,11 @@ def with_imds_v2_failure
153
153
154
154
def assert_fetch_s3 ( url :, signature :, token : nil , region : "us-east-1" , instance_profile_json : nil , fetcher : nil , method : "GET" )
155
155
FakeS3URISigner . instance_profile = instance_profile_json
156
-
156
+ FakeS3URISigner . return_token = token
157
+
157
158
@fetcher = fetcher || FakeGemFetcher . new ( nil )
158
159
res = @fetcher . fetch_s3 Gem ::URI . parse ( url ) , nil , ( method == "HEAD" )
159
-
160
+
160
161
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
161
162
if method == "HEAD"
162
163
assert_equal 200 , res . code
@@ -165,6 +166,7 @@ def assert_fetch_s3(url:, signature:, token: nil, region: "us-east-1", instance_
165
166
end
166
167
ensure
167
168
FakeS3URISigner . instance_profile = nil
169
+ FakeS3URISigner . return_token = nil
168
170
end
169
171
170
172
def test_fetch_s3_config_creds
@@ -175,7 +177,7 @@ def test_fetch_s3_config_creds
175
177
Time . stub :now , Time . at ( 1_561_353_581 ) do
176
178
assert_fetch_s3 (
177
179
url : url ,
178
- signature : "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c" ,
180
+ signature : "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c" ,
179
181
)
180
182
end
181
183
ensure
@@ -195,9 +197,9 @@ def test_fetch_s3_head_request
195
197
196
198
assert_fetch_s3 (
197
199
url : url ,
198
- signature : "a3c6cf9a2db62e85f4e57f8fc8ac8b5ff5c1fdd4aeef55935d05e05174d9c885" ,
199
- token : token ,
200
- region : region ,
200
+ signature : "a3c6cf9a2db62e85f4e57f8fc8ac8b5ff5c1fdd4aeef55935d05e05174d9c885" ,
201
+ token : token ,
202
+ region : region ,
201
203
instance_profile_json : instance_profile_json ,
202
204
method : method
203
205
)
@@ -317,11 +319,12 @@ def test_fetch_s3_instance_profile_creds
317
319
Time . stub :now , Time . at ( 1_561_353_581 ) do
318
320
assert_fetch_s3 (
319
321
url : url ,
320
- signature : "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c " ,
322
+ signature : "da82e098bdaed0d3087047670efc98eaadc20559a473b5eac8d70190d2a9e8fd " ,
321
323
region : "us-east-1" ,
322
- instance_profile_json : '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass"}'
324
+ token : "mysecrettoken" ,
325
+ instance_profile_json : '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "mysecrettoken"}'
323
326
)
324
- assert_fetched_s3_with_imds_v2
327
+ assert_fetched_s3_with_imds_v2 ( "mysecrettoken" )
325
328
end
326
329
ensure
327
330
Gem . configuration [ :s3_source ] = nil
@@ -336,11 +339,12 @@ def test_fetch_s3_instance_profile_creds_with_region
336
339
Time . stub :now , Time . at ( 1_561_353_581 ) do
337
340
assert_fetch_s3 (
338
341
url : url ,
339
- signature : "ef07487bfd8e3ca594f8fc29775b70c0a0636f51318f95d4f12b2e6e1fd8c716 " ,
342
+ signature : "532960594dbfe31d1bbfc0e8e7a666c3cbdd8b00a143774da51b7f920704afd2 " ,
340
343
region : "us-west-2" ,
341
- instance_profile_json : '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass"}'
344
+ token : "mysecrettoken" ,
345
+ instance_profile_json : '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "mysecrettoken"}'
342
346
)
343
- assert_fetched_s3_with_imds_v2
347
+ assert_fetched_s3_with_imds_v2 ( "mysecrettoken" )
344
348
end
345
349
ensure
346
350
Gem . configuration [ :s3_source ] = nil
@@ -360,7 +364,7 @@ def test_fetch_s3_instance_profile_creds_with_token
360
364
region : "us-east-1" ,
361
365
instance_profile_json : '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "testtoken"}'
362
366
)
363
- assert_fetched_s3_with_imds_v2
367
+ assert_fetched_s3_with_imds_v2 ( "testtoken" )
364
368
end
365
369
ensure
366
370
Gem . configuration [ :s3_source ] = nil
@@ -373,16 +377,14 @@ def test_fetch_s3_instance_profile_creds_with_fallback
373
377
374
378
url = "s3://my-bucket/gems/specs.4.8.gz"
375
379
Time . stub :now , Time . at ( 1_561_353_581 ) do
376
- with_imds_v2_failure do
377
- assert_fetch_s3 (
378
- url : url ,
379
- signature : "e709338735f9077edf8f6b94b247171c266a9605975e08e4a519a123c3322625" ,
380
- token : "testtoken" ,
381
- region : "us-east-1" ,
382
- instance_profile_json : '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "testtoken"}'
383
- )
384
- assert_fetched_s3_with_imds_v1
385
- end
380
+ assert_fetch_s3 (
381
+ url : url ,
382
+ signature : "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c" ,
383
+ token : nil ,
384
+ region : "us-east-1" ,
385
+ instance_profile_json : '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass"}'
386
+ )
387
+ assert_fetched_s3_with_imds_v1
386
388
end
387
389
ensure
388
390
Gem . configuration [ :s3_source ] = nil
0 commit comments