Skip to content

Commit 374f7db

Browse files
pjsk-stripehsbt
authored andcommitted
[rubygems/rubygems] removed global variables
rubygems/rubygems@42c5947dbe
1 parent 01ae9e4 commit 374f7db

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

test/rubygems/test_gem_remote_fetcher_s3.rb

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def set_response(response)
2323
end
2424

2525
class FakeS3URISigner < Gem::S3URISigner
26+
class << self
27+
attr_accessor :should_fail, :instance_profile
28+
end
29+
2630
# Convenience method to output the recent aws iam queries made in tests
2731
# this outputs the verb, path, and any non-generic headers
2832
def recent_aws_query_logs
@@ -51,7 +55,7 @@ def ec2_iam_request(uri, verb)
5155

5256
case uri.to_s
5357
when "http://169.254.169.254/latest/api/token"
54-
if $imdsv2_token_failure
58+
if FakeS3URISigner.should_fail
5559
res = Gem::Net::HTTPUnauthorized.new nil, 401, nil
5660
def res.body = "you got a 401! panic!"
5761
else
@@ -73,7 +77,7 @@ def res.body
7377

7478
when "http://169.254.169.254/latest/meta-data/iam/security-credentials/TestRole"
7579
res = Gem::Net::HTTPOK.new nil, 200, nil
76-
def res.body = $instance_profile
80+
def res.body = FakeS3URISigner.instance_profile
7781
else
7882
raise "Unexpected request to #{uri}"
7983
end
@@ -141,46 +145,26 @@ def assert_fetched_s3_with_imds_v1
141145
end
142146

143147
def with_imds_v2_failure
144-
$imdsv2_token_failure = true
148+
FakeS3URISigner.should_fail = true
145149
yield(fetcher)
146150
ensure
147-
$imdsv2_token_failure = nil
151+
FakeS3URISigner.should_fail = false
148152
end
149153

150154
def assert_fetch_s3(url:, signature:, token: nil, region: "us-east-1", instance_profile_json: nil, fetcher: nil, method: "GET")
155+
FakeS3URISigner.instance_profile = instance_profile_json
156+
151157
@fetcher = fetcher || FakeGemFetcher.new(nil)
152-
$instance_profile = instance_profile_json
153158
res = @fetcher.fetch_s3 Gem::URI.parse(url), nil, (method == "HEAD")
154-
$imdsv2_token_failure ||= nil
155-
159+
156160
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
157161
if method == "HEAD"
158162
assert_equal 200, res.code
159163
else
160164
assert_equal "success", res
161165
end
162-
163-
# Validation for EC2 IAM signing
164-
if Gem.configuration[:s3_source]&.dig("my-bucket", :provider) == "instance_profile"
165-
# Three API requests:
166-
# 1. Get the token
167-
# 2. Lookup profile details
168-
# 3. Query the credentials
169-
expected = <<~TEXT
170-
PUT http://169.254.169.254/latest/api/token
171-
x-aws-ec2-metadata-token-ttl-seconds=60
172-
GET http://169.254.169.254/latest/meta-data/iam/info
173-
x-aws-ec2-metadata-token=mysecrettoken
174-
GET http://169.254.169.254/latest/meta-data/iam/security-credentials/TestRole
175-
x-aws-ec2-metadata-token=mysecrettoken
176-
TEXT
177-
recent_aws_query_logs = @fetcher.last_s3_uri_signer.recent_aws_query_logs
178-
assert_equal(expected.strip, recent_aws_query_logs.strip)
179-
else
180-
assert_equal("", @fetcher.last_s3_uri_signer.recent_aws_query_logs)
181-
end
182166
ensure
183-
$instance_profile = nil
167+
FakeS3URISigner.instance_profile = nil
184168
end
185169

186170
def test_fetch_s3_config_creds

0 commit comments

Comments
 (0)