@@ -23,6 +23,10 @@ def set_response(response)
23
23
end
24
24
25
25
class FakeS3URISigner < Gem ::S3URISigner
26
+ class << self
27
+ attr_accessor :should_fail , :instance_profile
28
+ end
29
+
26
30
# Convenience method to output the recent aws iam queries made in tests
27
31
# this outputs the verb, path, and any non-generic headers
28
32
def recent_aws_query_logs
@@ -51,7 +55,7 @@ def ec2_iam_request(uri, verb)
51
55
52
56
case uri . to_s
53
57
when "http://169.254.169.254/latest/api/token"
54
- if $imdsv2_token_failure
58
+ if FakeS3URISigner . should_fail
55
59
res = Gem ::Net ::HTTPUnauthorized . new nil , 401 , nil
56
60
def res . body = "you got a 401! panic!"
57
61
else
@@ -73,7 +77,7 @@ def res.body
73
77
74
78
when "http://169.254.169.254/latest/meta-data/iam/security-credentials/TestRole"
75
79
res = Gem ::Net ::HTTPOK . new nil , 200 , nil
76
- def res . body = $ instance_profile
80
+ def res . body = FakeS3URISigner . instance_profile
77
81
else
78
82
raise "Unexpected request to #{ uri } "
79
83
end
@@ -141,46 +145,26 @@ def assert_fetched_s3_with_imds_v1
141
145
end
142
146
143
147
def with_imds_v2_failure
144
- $imdsv2_token_failure = true
148
+ FakeS3URISigner . should_fail = true
145
149
yield ( fetcher )
146
150
ensure
147
- $imdsv2_token_failure = nil
151
+ FakeS3URISigner . should_fail = false
148
152
end
149
153
150
154
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
+
151
157
@fetcher = fetcher || FakeGemFetcher . new ( nil )
152
- $instance_profile = instance_profile_json
153
158
res = @fetcher . fetch_s3 Gem ::URI . parse ( url ) , nil , ( method == "HEAD" )
154
- $imdsv2_token_failure ||= nil
155
-
159
+
156
160
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
157
161
if method == "HEAD"
158
162
assert_equal 200 , res . code
159
163
else
160
164
assert_equal "success" , res
161
165
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
182
166
ensure
183
- $ instance_profile = nil
167
+ FakeS3URISigner . instance_profile = nil
184
168
end
185
169
186
170
def test_fetch_s3_config_creds
0 commit comments