Skip to content

Commit fe3ed3e

Browse files
pjsk-stripehsbt
authored andcommitted
[rubygems/rubygems] Update tests to respect token for where v2 and v1 are invoked
rubygems/rubygems@261315e399
1 parent 374f7db commit fe3ed3e

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

test/rubygems/test_gem_remote_fetcher_s3.rb

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def set_response(response)
2424

2525
class FakeS3URISigner < Gem::S3URISigner
2626
class << self
27-
attr_accessor :should_fail, :instance_profile
27+
attr_accessor :return_token, :instance_profile
2828
end
2929

3030
# Convenience method to output the recent aws iam queries made in tests
@@ -55,12 +55,12 @@ def ec2_iam_request(uri, verb)
5555

5656
case uri.to_s
5757
when "http://169.254.169.254/latest/api/token"
58-
if FakeS3URISigner.should_fail
58+
if FakeS3URISigner.return_token.nil?
5959
res = Gem::Net::HTTPUnauthorized.new nil, 401, nil
6060
def res.body = "you got a 401! panic!"
6161
else
6262
res = Gem::Net::HTTPOK.new nil, 200, nil
63-
def res.body = "mysecrettoken"
63+
def res.body = FakeS3URISigner.return_token
6464
end
6565
when "http://169.254.169.254/latest/meta-data/iam/info"
6666
res = Gem::Net::HTTPOK.new nil, 200, nil
@@ -112,7 +112,7 @@ def setup
112112
@a1.loaded_from = File.join(@gemhome, "specifications", @a1.full_name)
113113
end
114114

115-
def assert_fetched_s3_with_imds_v2
115+
def assert_fetched_s3_with_imds_v2(expected_token)
116116
# Three API requests:
117117
# 1. Get the token
118118
# 2. Lookup profile details
@@ -121,9 +121,9 @@ def assert_fetched_s3_with_imds_v2
121121
PUT http://169.254.169.254/latest/api/token
122122
x-aws-ec2-metadata-token-ttl-seconds=60
123123
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}
125125
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}
127127
TEXT
128128
recent_aws_query_logs = @fetcher.last_s3_uri_signer.recent_aws_query_logs
129129
assert_equal(expected.strip, recent_aws_query_logs.strip)
@@ -153,10 +153,11 @@ def with_imds_v2_failure
153153

154154
def assert_fetch_s3(url:, signature:, token: nil, region: "us-east-1", instance_profile_json: nil, fetcher: nil, method: "GET")
155155
FakeS3URISigner.instance_profile = instance_profile_json
156-
156+
FakeS3URISigner.return_token = token
157+
157158
@fetcher = fetcher || FakeGemFetcher.new(nil)
158159
res = @fetcher.fetch_s3 Gem::URI.parse(url), nil, (method == "HEAD")
159-
160+
160161
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
161162
if method == "HEAD"
162163
assert_equal 200, res.code
@@ -165,6 +166,7 @@ def assert_fetch_s3(url:, signature:, token: nil, region: "us-east-1", instance_
165166
end
166167
ensure
167168
FakeS3URISigner.instance_profile = nil
169+
FakeS3URISigner.return_token = nil
168170
end
169171

170172
def test_fetch_s3_config_creds
@@ -175,7 +177,7 @@ def test_fetch_s3_config_creds
175177
Time.stub :now, Time.at(1_561_353_581) do
176178
assert_fetch_s3(
177179
url: url,
178-
signature: "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c",
180+
signature: "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c",
179181
)
180182
end
181183
ensure
@@ -195,9 +197,9 @@ def test_fetch_s3_head_request
195197

196198
assert_fetch_s3(
197199
url: url,
198-
signature: "a3c6cf9a2db62e85f4e57f8fc8ac8b5ff5c1fdd4aeef55935d05e05174d9c885",
199-
token: token,
200-
region: region,
200+
signature: "a3c6cf9a2db62e85f4e57f8fc8ac8b5ff5c1fdd4aeef55935d05e05174d9c885",
201+
token: token,
202+
region: region,
201203
instance_profile_json: instance_profile_json,
202204
method: method
203205
)
@@ -317,11 +319,12 @@ def test_fetch_s3_instance_profile_creds
317319
Time.stub :now, Time.at(1_561_353_581) do
318320
assert_fetch_s3(
319321
url: url,
320-
signature: "b5cb80c1301f7b1c50c4af54f1f6c034f80b56d32f000a855f0a903dc5a8413c",
322+
signature: "da82e098bdaed0d3087047670efc98eaadc20559a473b5eac8d70190d2a9e8fd",
321323
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"}'
323326
)
324-
assert_fetched_s3_with_imds_v2
327+
assert_fetched_s3_with_imds_v2("mysecrettoken")
325328
end
326329
ensure
327330
Gem.configuration[:s3_source] = nil
@@ -336,11 +339,12 @@ def test_fetch_s3_instance_profile_creds_with_region
336339
Time.stub :now, Time.at(1_561_353_581) do
337340
assert_fetch_s3(
338341
url: url,
339-
signature: "ef07487bfd8e3ca594f8fc29775b70c0a0636f51318f95d4f12b2e6e1fd8c716",
342+
signature: "532960594dbfe31d1bbfc0e8e7a666c3cbdd8b00a143774da51b7f920704afd2",
340343
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"}'
342346
)
343-
assert_fetched_s3_with_imds_v2
347+
assert_fetched_s3_with_imds_v2("mysecrettoken")
344348
end
345349
ensure
346350
Gem.configuration[:s3_source] = nil
@@ -360,7 +364,7 @@ def test_fetch_s3_instance_profile_creds_with_token
360364
region: "us-east-1",
361365
instance_profile_json: '{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "testtoken"}'
362366
)
363-
assert_fetched_s3_with_imds_v2
367+
assert_fetched_s3_with_imds_v2("testtoken")
364368
end
365369
ensure
366370
Gem.configuration[:s3_source] = nil
@@ -373,16 +377,14 @@ def test_fetch_s3_instance_profile_creds_with_fallback
373377

374378
url = "s3://my-bucket/gems/specs.4.8.gz"
375379
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
386388
end
387389
ensure
388390
Gem.configuration[:s3_source] = nil

0 commit comments

Comments
 (0)