@@ -69,8 +69,7 @@ def initialize(user = nil, credentials_cache: CredentialsCache.instance)
69
69
# Retrieves a valid set of credentials, if possible, or raises
70
70
# Auth::InvalidConfiguration.
71
71
#
72
- # @param [ Operation::Context | nil ] context Context of the operation
73
- # credentials are retrieved for.
72
+ # @param [ CsotTimeoutHolder | nil ] timeout_holder CSOT timeout, if any.
74
73
#
75
74
# @return [ Auth::Aws::Credentials ] A valid set of credentials.
76
75
#
@@ -80,14 +79,14 @@ def initialize(user = nil, credentials_cache: CredentialsCache.instance)
80
79
# retrieved from any source.
81
80
# @raise Error::TimeoutError if credentials cannot be retrieved within
82
81
# the timeout defined on the operation context.
83
- def credentials ( context = nil )
82
+ def credentials ( timeout_holder = nil )
84
83
credentials = credentials_from_user ( user )
85
84
return credentials unless credentials . nil?
86
85
87
86
credentials = credentials_from_environment
88
87
return credentials unless credentials . nil?
89
88
90
- credentials = @credentials_cache . fetch { obtain_credentials_from_endpoints ( context ) }
89
+ credentials = @credentials_cache . fetch { obtain_credentials_from_endpoints ( timeout_holder ) }
91
90
return credentials unless credentials . nil?
92
91
93
92
raise Auth ::Aws ::CredentialsNotFound
@@ -132,8 +131,7 @@ def credentials_from_environment
132
131
133
132
# Returns credentials from the AWS metadata endpoints.
134
133
#
135
- # @param [ Operation::Context | nil ] context Context of the operation
136
- # credentials are retrieved for.
134
+ # @param [ CsotTimeoutHolder ] timeout_holder CSOT timeout.
137
135
#
138
136
# @return [ Auth::Aws::Credentials | nil ] A set of credentials, or nil
139
137
# if retrieval failed or the obtained credentials are invalid.
@@ -142,48 +140,47 @@ def credentials_from_environment
142
140
# of credentials.
143
141
# @ raise Error::TimeoutError if credentials cannot be retrieved within
144
142
# the timeout defined on the operation context.
145
- def obtain_credentials_from_endpoints ( context = nil )
146
- if ( credentials = web_identity_credentials ( context ) ) && credentials_valid? ( credentials , 'Web identity token' )
143
+ def obtain_credentials_from_endpoints ( timeout_holder = nil )
144
+ if ( credentials = web_identity_credentials ( timeout_holder ) ) && credentials_valid? ( credentials , 'Web identity token' )
147
145
credentials
148
- elsif ( credentials = ecs_metadata_credentials ( context ) ) && credentials_valid? ( credentials , 'ECS task metadata' )
146
+ elsif ( credentials = ecs_metadata_credentials ( timeout_holder ) ) && credentials_valid? ( credentials , 'ECS task metadata' )
149
147
credentials
150
- elsif ( credentials = ec2_metadata_credentials ( context ) ) && credentials_valid? ( credentials , 'EC2 instance metadata' )
148
+ elsif ( credentials = ec2_metadata_credentials ( timeout_holder ) ) && credentials_valid? ( credentials , 'EC2 instance metadata' )
151
149
credentials
152
150
end
153
151
end
154
152
155
153
# Returns credentials from the EC2 metadata endpoint. The credentials
156
154
# could be empty, partial or invalid.
157
155
#
158
- # @param [ Operation::Context | nil ] context Context of the operation
159
- # credentials are retrieved for.
156
+ # @param [ CsotTimeoutHolder ] timeout_holder CSOT timeout.
160
157
#
161
158
# @return [ Auth::Aws::Credentials | nil ] A set of credentials, or nil
162
159
# if retrieval failed.
163
160
# @ raise Error::TimeoutError if credentials cannot be retrieved within
164
- # the timeout defined on the operation context .
165
- def ec2_metadata_credentials ( context = nil )
166
- context &.check_timeout!
161
+ # the timeout.
162
+ def ec2_metadata_credentials ( timeout_holder = nil )
163
+ timeout_holder &.check_timeout!
167
164
http = Net ::HTTP . new ( '169.254.169.254' )
168
165
req = Net ::HTTP ::Put . new ( '/latest/api/token' ,
169
166
# The TTL is required in order to obtain the metadata token.
170
167
{ 'x-aws-ec2-metadata-token-ttl-seconds' => '30' } )
171
- resp = with_timeout ( context ) do
168
+ resp = with_timeout ( timeout_holder ) do
172
169
http . request ( req )
173
170
end
174
171
if resp . code != '200'
175
172
return nil
176
173
end
177
174
metadata_token = resp . body
178
- resp = with_timeout ( context ) do
175
+ resp = with_timeout ( timeout_holder ) do
179
176
http_get ( http , '/latest/meta-data/iam/security-credentials' , metadata_token )
180
177
end
181
178
if resp . code != '200'
182
179
return nil
183
180
end
184
181
role_name = resp . body
185
182
escaped_role_name = CGI . escape ( role_name ) . gsub ( '+' , '%20' )
186
- resp = with_timeout ( context ) do
183
+ resp = with_timeout ( timeout_holder ) do
187
184
http_get ( http , "/latest/meta-data/iam/security-credentials/#{ escaped_role_name } " , metadata_token )
188
185
end
189
186
if resp . code != '200'
@@ -208,15 +205,14 @@ def ec2_metadata_credentials(context = nil)
208
205
# Returns credentials from the ECS metadata endpoint. The credentials
209
206
# could be empty, partial or invalid.
210
207
#
211
- # @param [ Operation::Context | nil ] context Context of the operation
212
- # credentials are retrieved for.
208
+ # @param [ CsotTimeoutHolder | nil ] timeout_holder CSOT timeout.
213
209
#
214
210
# @return [ Auth::Aws::Credentials | nil ] A set of credentials, or nil
215
211
# if retrieval failed.
216
212
# @ raise Error::TimeoutError if credentials cannot be retrieved within
217
213
# the timeout defined on the operation context.
218
- def ecs_metadata_credentials ( context = nil )
219
- context &.check_timeout!
214
+ def ecs_metadata_credentials ( timeout_holder = nil )
215
+ timeout_holder &.check_timeout!
220
216
relative_uri = ENV [ 'AWS_CONTAINER_CREDENTIALS_RELATIVE_URI' ]
221
217
if relative_uri . nil? || relative_uri . empty?
222
218
return nil
@@ -230,7 +226,7 @@ def ecs_metadata_credentials(context = nil)
230
226
# a leading slash must be added by the driver, but this is not
231
227
# in fact needed.
232
228
req = Net ::HTTP ::Get . new ( relative_uri )
233
- resp = with_timeout ( context ) do
229
+ resp = with_timeout ( timeout_holder ) do
234
230
http . request ( req )
235
231
end
236
232
if resp . code != '200'
@@ -252,16 +248,15 @@ def ecs_metadata_credentials(context = nil)
252
248
# inside EKS. See https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
253
249
# for further details.
254
250
#
255
- # @param [ Operation::Context | nil ] context Context of the operation
256
- # credentials are retrieved for.
251
+ # @param [ CsotTimeoutHolder | nil ] timeout_holder CSOT timeout.
257
252
#
258
253
# @return [ Auth::Aws::Credentials | nil ] A set of credentials, or nil
259
254
# if retrieval failed.
260
- def web_identity_credentials ( context = nil )
255
+ def web_identity_credentials ( timeout_holder = nil )
261
256
web_identity_token , role_arn , role_session_name = prepare_web_identity_inputs
262
257
return nil if web_identity_token . nil?
263
258
response = request_web_identity_credentials (
264
- web_identity_token , role_arn , role_session_name , context
259
+ web_identity_token , role_arn , role_session_name , timeout_holder
265
260
)
266
261
return if response . nil?
267
262
credentials_from_web_identity_response ( response )
@@ -296,16 +291,15 @@ def prepare_web_identity_inputs
296
291
# that the caller is assuming.
297
292
# @param [ String ] role_session_name An identifier for the assumed
298
293
# role session.
299
- # @param [ Operation::Context | nil ] context Context of the operation
300
- # credentials are retrieved for.
294
+ # @param [ CsotTimeoutHolder | nil ] timeout_holder CSOT timeout.
301
295
#
302
296
# @return [ Net::HTTPResponse | nil ] AWS API response if successful,
303
297
# otherwise nil.
304
298
#
305
299
# @ raise Error::TimeoutError if credentials cannot be retrieved within
306
300
# the timeout defined on the operation context.
307
- def request_web_identity_credentials ( token , role_arn , role_session_name , context )
308
- context &.check_timeout!
301
+ def request_web_identity_credentials ( token , role_arn , role_session_name , timeout_holder )
302
+ timeout_holder &.check_timeout!
309
303
uri = URI ( 'https://sts.amazonaws.com/' )
310
304
params = {
311
305
'Action' => 'AssumeRoleWithWebIdentity' ,
@@ -317,7 +311,7 @@ def request_web_identity_credentials(token, role_arn, role_session_name, context
317
311
uri . query = ::URI . encode_www_form ( params )
318
312
req = Net ::HTTP ::Post . new ( uri )
319
313
req [ 'Accept' ] = 'application/json'
320
- resp = with_timeout ( context ) do
314
+ resp = with_timeout ( timeout_holder ) do
321
315
Net ::HTTP . start ( uri . hostname , uri . port , use_ssl : true ) do |https |
322
316
https . request ( req )
323
317
end
@@ -396,13 +390,12 @@ def credentials_valid?(credentials, source)
396
390
# We use +Timeout.timeout+ here because there is no other acceptable easy
397
391
# way to time limit http requests.
398
392
#
399
- # @param [ Operation::Context | nil ] context Context of the operation
393
+ # @param [ CsotTimeoutHolder | nil ] timeout_holder CSOT timeout.
400
394
#
401
395
# @ raise Error::TimeoutError if deadline exceeded.
402
- def with_timeout ( context )
403
- context &.check_timeout!
404
- timeout = context &.remaining_timeout_sec || METADATA_TIMEOUT
405
- exception_class = if context &.csot?
396
+ def with_timeout ( timeout_holder )
397
+ timeout = timeout_holder &.remaining_timeout_sec! || METADATA_TIMEOUT
398
+ exception_class = if timeout_holder &.csot?
406
399
Error ::TimeoutError
407
400
else
408
401
nil
0 commit comments