aws-sdk-core 3.117.0 → 3.197.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +836 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-defaults/default_configuration.rb +153 -0
  5. data/lib/aws-defaults/defaults_mode_config_resolver.rb +107 -0
  6. data/lib/aws-defaults.rb +3 -0
  7. data/lib/aws-sdk-core/arn.rb +13 -0
  8. data/lib/aws-sdk-core/assume_role_credentials.rb +21 -7
  9. data/lib/aws-sdk-core/assume_role_web_identity_credentials.rb +14 -10
  10. data/lib/aws-sdk-core/binary/decode_handler.rb +0 -5
  11. data/lib/aws-sdk-core/binary/encode_handler.rb +12 -1
  12. data/lib/aws-sdk-core/binary/event_builder.rb +34 -37
  13. data/lib/aws-sdk-core/client_stubs.rb +20 -13
  14. data/lib/aws-sdk-core/credential_provider.rb +4 -1
  15. data/lib/aws-sdk-core/credential_provider_chain.rb +8 -5
  16. data/lib/aws-sdk-core/ec2_metadata.rb +4 -3
  17. data/lib/aws-sdk-core/ecs_credentials.rb +187 -52
  18. data/lib/aws-sdk-core/endpoints/condition.rb +41 -0
  19. data/lib/aws-sdk-core/endpoints/endpoint.rb +17 -0
  20. data/lib/aws-sdk-core/endpoints/endpoint_rule.rb +75 -0
  21. data/lib/aws-sdk-core/endpoints/error_rule.rb +42 -0
  22. data/lib/aws-sdk-core/endpoints/function.rb +80 -0
  23. data/lib/aws-sdk-core/endpoints/matchers.rb +135 -0
  24. data/lib/aws-sdk-core/endpoints/reference.rb +31 -0
  25. data/lib/aws-sdk-core/endpoints/rule.rb +25 -0
  26. data/lib/aws-sdk-core/endpoints/rule_set.rb +52 -0
  27. data/lib/aws-sdk-core/endpoints/rules_provider.rb +37 -0
  28. data/lib/aws-sdk-core/endpoints/templater.rb +58 -0
  29. data/lib/aws-sdk-core/endpoints/tree_rule.rb +45 -0
  30. data/lib/aws-sdk-core/endpoints/url.rb +60 -0
  31. data/lib/aws-sdk-core/endpoints.rb +78 -0
  32. data/lib/aws-sdk-core/errors.rb +17 -4
  33. data/lib/aws-sdk-core/event_emitter.rb +0 -16
  34. data/lib/aws-sdk-core/ini_parser.rb +7 -0
  35. data/lib/aws-sdk-core/instance_profile_credentials.rb +127 -33
  36. data/lib/aws-sdk-core/json/builder.rb +8 -1
  37. data/lib/aws-sdk-core/json/error_handler.rb +30 -4
  38. data/lib/aws-sdk-core/json/handler.rb +8 -1
  39. data/lib/aws-sdk-core/json/json_engine.rb +10 -8
  40. data/lib/aws-sdk-core/json/oj_engine.rb +33 -6
  41. data/lib/aws-sdk-core/json/parser.rb +38 -2
  42. data/lib/aws-sdk-core/json.rb +8 -26
  43. data/lib/aws-sdk-core/log/formatter.rb +6 -0
  44. data/lib/aws-sdk-core/log/param_filter.rb +9 -1
  45. data/lib/aws-sdk-core/lru_cache.rb +75 -0
  46. data/lib/aws-sdk-core/pageable_response.rb +81 -32
  47. data/lib/aws-sdk-core/param_validator.rb +36 -2
  48. data/lib/aws-sdk-core/plugins/bearer_authorization.rb +67 -0
  49. data/lib/aws-sdk-core/plugins/checksum_algorithm.rb +342 -0
  50. data/lib/aws-sdk-core/plugins/client_metrics_plugin.rb +1 -0
  51. data/lib/aws-sdk-core/plugins/client_metrics_send_plugin.rb +14 -2
  52. data/lib/aws-sdk-core/plugins/credentials_configuration.rb +29 -1
  53. data/lib/aws-sdk-core/plugins/defaults_mode.rb +40 -0
  54. data/lib/aws-sdk-core/plugins/endpoint_discovery.rb +6 -2
  55. data/lib/aws-sdk-core/plugins/http_checksum.rb +9 -1
  56. data/lib/aws-sdk-core/plugins/invocation_id.rb +1 -11
  57. data/lib/aws-sdk-core/plugins/jsonvalue_converter.rb +34 -6
  58. data/lib/aws-sdk-core/plugins/logging.rb +2 -0
  59. data/lib/aws-sdk-core/plugins/protocols/api_gateway.rb +17 -0
  60. data/lib/aws-sdk-core/plugins/protocols/rest_json.rb +4 -2
  61. data/lib/aws-sdk-core/plugins/protocols/rest_xml.rb +1 -2
  62. data/lib/aws-sdk-core/plugins/recursion_detection.rb +38 -0
  63. data/lib/aws-sdk-core/plugins/regional_endpoint.rb +144 -17
  64. data/lib/aws-sdk-core/plugins/request_compression.rb +226 -0
  65. data/lib/aws-sdk-core/plugins/response_paging.rb +1 -1
  66. data/lib/aws-sdk-core/plugins/retries/error_inspector.rb +7 -4
  67. data/lib/aws-sdk-core/plugins/retry_errors.rb +33 -7
  68. data/lib/aws-sdk-core/plugins/sign.rb +211 -0
  69. data/lib/aws-sdk-core/plugins/signature_v2.rb +1 -0
  70. data/lib/aws-sdk-core/plugins/signature_v4.rb +28 -31
  71. data/lib/aws-sdk-core/plugins/stub_responses.rb +6 -1
  72. data/lib/aws-sdk-core/plugins/user_agent.rb +152 -14
  73. data/lib/aws-sdk-core/process_credentials.rb +50 -34
  74. data/lib/aws-sdk-core/query/ec2_param_builder.rb +5 -7
  75. data/lib/aws-sdk-core/query/param_builder.rb +2 -2
  76. data/lib/aws-sdk-core/refreshing_credentials.rb +49 -18
  77. data/lib/aws-sdk-core/refreshing_token.rb +71 -0
  78. data/lib/aws-sdk-core/rest/handler.rb +1 -1
  79. data/lib/aws-sdk-core/rest/request/body.rb +49 -4
  80. data/lib/aws-sdk-core/rest/request/content_type.rb +60 -0
  81. data/lib/aws-sdk-core/rest/request/endpoint.rb +24 -4
  82. data/lib/aws-sdk-core/rest/request/headers.rb +23 -7
  83. data/lib/aws-sdk-core/rest/request/querystring_builder.rb +62 -36
  84. data/lib/aws-sdk-core/rest/response/body.rb +15 -1
  85. data/lib/aws-sdk-core/rest/response/header_list_parser.rb +79 -0
  86. data/lib/aws-sdk-core/rest/response/headers.rb +10 -3
  87. data/lib/aws-sdk-core/rest.rb +1 -0
  88. data/lib/aws-sdk-core/shared_config.rb +112 -12
  89. data/lib/aws-sdk-core/sso_credentials.rb +92 -51
  90. data/lib/aws-sdk-core/sso_token_provider.rb +135 -0
  91. data/lib/aws-sdk-core/static_token_provider.rb +14 -0
  92. data/lib/aws-sdk-core/structure.rb +16 -5
  93. data/lib/aws-sdk-core/stubbing/stub_data.rb +11 -0
  94. data/lib/aws-sdk-core/token.rb +31 -0
  95. data/lib/aws-sdk-core/token_provider.rb +15 -0
  96. data/lib/aws-sdk-core/token_provider_chain.rb +51 -0
  97. data/lib/aws-sdk-core/util.rb +39 -0
  98. data/lib/aws-sdk-core/waiters/poller.rb +4 -2
  99. data/lib/aws-sdk-core/xml/builder.rb +17 -9
  100. data/lib/aws-sdk-core/xml/error_handler.rb +31 -8
  101. data/lib/aws-sdk-core/xml/parser/engines/oga.rb +2 -0
  102. data/lib/aws-sdk-core/xml/parser/engines/ox.rb +1 -1
  103. data/lib/aws-sdk-core/xml/parser/engines/rexml.rb +0 -8
  104. data/lib/aws-sdk-core/xml/parser/frame.rb +27 -20
  105. data/lib/aws-sdk-core/xml/parser/stack.rb +2 -0
  106. data/lib/aws-sdk-core.rb +21 -0
  107. data/lib/aws-sdk-sso/client.rb +157 -50
  108. data/lib/aws-sdk-sso/endpoint_parameters.rb +66 -0
  109. data/lib/aws-sdk-sso/endpoint_provider.rb +57 -0
  110. data/lib/aws-sdk-sso/endpoints.rb +72 -0
  111. data/lib/aws-sdk-sso/plugins/endpoints.rb +78 -0
  112. data/lib/aws-sdk-sso/types.rb +8 -43
  113. data/lib/aws-sdk-sso.rb +5 -1
  114. data/lib/aws-sdk-ssooidc/client.rb +1008 -0
  115. data/lib/aws-sdk-ssooidc/client_api.rb +293 -0
  116. data/lib/aws-sdk-ssooidc/customizations.rb +1 -0
  117. data/lib/aws-sdk-ssooidc/endpoint_parameters.rb +66 -0
  118. data/lib/aws-sdk-ssooidc/endpoint_provider.rb +57 -0
  119. data/lib/aws-sdk-ssooidc/endpoints.rb +72 -0
  120. data/lib/aws-sdk-ssooidc/errors.rb +342 -0
  121. data/lib/aws-sdk-ssooidc/plugins/endpoints.rb +78 -0
  122. data/lib/aws-sdk-ssooidc/resource.rb +26 -0
  123. data/lib/aws-sdk-ssooidc/types.rb +823 -0
  124. data/lib/aws-sdk-ssooidc.rb +59 -0
  125. data/lib/aws-sdk-sts/client.rb +472 -398
  126. data/lib/aws-sdk-sts/client_api.rb +20 -9
  127. data/lib/aws-sdk-sts/endpoint_parameters.rb +78 -0
  128. data/lib/aws-sdk-sts/endpoint_provider.rb +112 -0
  129. data/lib/aws-sdk-sts/endpoints.rb +136 -0
  130. data/lib/aws-sdk-sts/plugins/endpoints.rb +86 -0
  131. data/lib/aws-sdk-sts/plugins/sts_regional_endpoints.rb +5 -1
  132. data/lib/aws-sdk-sts/presigner.rb +14 -10
  133. data/lib/aws-sdk-sts/types.rb +168 -227
  134. data/lib/aws-sdk-sts.rb +5 -1
  135. data/lib/seahorse/client/async_base.rb +1 -2
  136. data/lib/seahorse/client/async_response.rb +19 -0
  137. data/lib/seahorse/client/base.rb +1 -0
  138. data/lib/seahorse/client/configuration.rb +5 -5
  139. data/lib/seahorse/client/h2/connection.rb +15 -16
  140. data/lib/seahorse/client/h2/handler.rb +5 -5
  141. data/lib/seahorse/client/net_http/connection_pool.rb +10 -9
  142. data/lib/seahorse/client/net_http/handler.rb +15 -7
  143. data/lib/seahorse/client/net_http/patches.rb +12 -86
  144. data/lib/seahorse/client/plugin.rb +9 -0
  145. data/lib/seahorse/client/plugins/content_length.rb +11 -5
  146. data/lib/seahorse/client/plugins/h2.rb +3 -3
  147. data/lib/seahorse/client/plugins/net_http.rb +73 -10
  148. data/lib/seahorse/client/plugins/request_callback.rb +40 -9
  149. data/lib/seahorse/client/response.rb +6 -0
  150. data/lib/seahorse/model/operation.rb +6 -0
  151. data/lib/seahorse/model/shapes.rb +27 -2
  152. data/lib/seahorse/util.rb +4 -0
  153. data/sig/aws-sdk-core/client_stubs.rbs +10 -0
  154. data/sig/aws-sdk-core/errors.rbs +22 -0
  155. data/sig/aws-sdk-core/resources/collection.rbs +21 -0
  156. data/sig/aws-sdk-core/structure.rbs +4 -0
  157. data/sig/aws-sdk-core/waiters/errors.rbs +20 -0
  158. data/sig/aws-sdk-core.rbs +7 -0
  159. data/sig/seahorse/client/base.rbs +25 -0
  160. data/sig/seahorse/client/handler_builder.rbs +16 -0
  161. data/sig/seahorse/client/response.rbs +61 -0
  162. metadata +82 -17
  163. data/lib/aws-sdk-sso/plugins/content_type.rb +0 -25
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'strscan'
4
+
5
+ module Aws
6
+ module Rest
7
+ module Response
8
+ # @api private
9
+ module HeaderListParser
10
+
11
+ class << self
12
+ # parse a list of possibly quoted and escaped string values
13
+ # Follows:
14
+ # # [RFC-7230's specification of header values](https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6).
15
+ def parse_string_list(value)
16
+ buffer = StringScanner.new(value)
17
+ parsed = []
18
+
19
+ parsed << read_value(buffer) until buffer.eos?
20
+
21
+ parsed
22
+ end
23
+
24
+ def parse_timestamp_list(value, ref)
25
+ # timestamp lists use an http-date by default and are unescaped
26
+ # eg: Mon, 16 Dec 2019 23:48:18 GMT, Mon, 16 Dec 2019 23:48:18 GMT
27
+ case ref['timestampFormat'] || ref.shape['timestampFormat']
28
+ when 'unixTimestamp'
29
+ value.split(', ').map { |v| Time.at(v.to_f) }
30
+ when 'iso8601' then value.split(', ').map { |v| Time.parse(v) }
31
+ else
32
+ # header default to rfc822/http-date, which has a comma after day
33
+ value.split(',').each_slice(2).map { |v| Time.parse(v[0] + v[1])}
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def read_value(buffer)
40
+ until buffer.eos?
41
+ case buffer.peek(1)
42
+ when ' ', "\t"
43
+ # drop leading whitespace
44
+ buffer.getch
45
+ next
46
+ when '"'
47
+ buffer.getch # drop the quote and advance
48
+ return read_quoted_value(buffer)
49
+ else
50
+ return read_unquoted_value(buffer)
51
+ end
52
+ end
53
+ # buffer is only whitespace
54
+ nil
55
+ end
56
+
57
+ def read_unquoted_value(buffer)
58
+ # there cannot be any escaped values
59
+ value = buffer.scan_until(/,|$/)
60
+ # drop the comma if we matched it
61
+ buffer.matched == ',' ? value.chop : value
62
+ end
63
+
64
+ def read_quoted_value(buffer)
65
+ # scan until we have an unescaped double quote
66
+ value = buffer.scan_until(/[^\\]"/)
67
+ raise ArgumentError, 'Invalid String list: No closing quote found' unless value
68
+
69
+ # drop any remaining whitespace/commas
70
+ buffer.scan_until(/[\s,]*/)
71
+ # the last character will always be the closing quote.
72
+ # Add a starting quote and then unescape (undump)
73
+ "\"#{value}".undump
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'time'
4
4
  require 'base64'
5
+ require_relative 'header_list_parser'
5
6
 
6
7
  module Aws
7
8
  module Rest
@@ -36,12 +37,18 @@ module Aws
36
37
  def cast_value(ref, value)
37
38
  value = extract_json_trait(value) if ref['jsonvalue']
38
39
  case ref.shape
39
- when StringShape then value
40
+ when StringShape then value.to_s
40
41
  when IntegerShape then value.to_i
41
- when FloatShape then value.to_f
42
+ when FloatShape then Util.deserialize_number(value)
42
43
  when BooleanShape then value == 'true'
44
+ when ListShape then
45
+ case ref.shape.member.shape
46
+ when StringShape then HeaderListParser.parse_string_list(value)
47
+ when TimestampShape then HeaderListParser.parse_timestamp_list(value, ref.shape.member)
48
+ else value.split(', ').map { |v| cast_value(ref.shape.member, v) }
49
+ end
43
50
  when TimestampShape
44
- if value =~ /\d+(\.\d*)/
51
+ if value =~ /^\d+(\.\d*)/
45
52
  Time.at(value.to_f)
46
53
  elsif value =~ /^\d+$/
47
54
  Time.at(value.to_i)
@@ -6,6 +6,7 @@ require_relative 'rest/request/builder'
6
6
  require_relative 'rest/request/endpoint'
7
7
  require_relative 'rest/request/headers'
8
8
  require_relative 'rest/request/querystring_builder'
9
+ require_relative 'rest/request/content_type'
9
10
  require_relative 'rest/response/body'
10
11
  require_relative 'rest/response/headers'
11
12
  require_relative 'rest/response/parser'
@@ -3,7 +3,11 @@
3
3
  module Aws
4
4
  # @api private
5
5
  class SharedConfig
6
- SSO_PROFILE_KEYS = %w[sso_start_url sso_region sso_account_id sso_role_name].freeze
6
+ SSO_CREDENTIAL_PROFILE_KEYS = %w[sso_account_id sso_role_name].freeze
7
+ SSO_PROFILE_KEYS = %w[sso_session sso_start_url sso_region sso_account_id sso_role_name].freeze
8
+ SSO_TOKEN_PROFILE_KEYS = %w[sso_session].freeze
9
+ SSO_SESSION_KEYS = %w[sso_region sso_start_url].freeze
10
+
7
11
 
8
12
  # @return [String]
9
13
  attr_reader :credentials_path
@@ -51,10 +55,12 @@ module Aws
51
55
  @config_enabled = options[:config_enabled]
52
56
  @credentials_path = options[:credentials_path] ||
53
57
  determine_credentials_path
58
+ @credentials_path = File.expand_path(@credentials_path) if @credentials_path
54
59
  @parsed_credentials = {}
55
60
  load_credentials_file if loadable?(@credentials_path)
56
61
  if @config_enabled
57
62
  @config_path = options[:config_path] || determine_config_path
63
+ @config_path = File.expand_path(@config_path) if @config_path
58
64
  load_config_file if loadable?(@config_path)
59
65
  end
60
66
  end
@@ -100,7 +106,7 @@ module Aws
100
106
  # or `nil` if no valid credentials were found.
101
107
  def credentials(opts = {})
102
108
  p = opts[:profile] || @profile_name
103
- validate_profile_exists(p) if credentials_present?
109
+ validate_profile_exists(p)
104
110
  if (credentials = credentials_from_shared(p, opts))
105
111
  credentials
106
112
  elsif (credentials = credentials_from_config(p, opts))
@@ -149,6 +155,38 @@ module Aws
149
155
  credentials
150
156
  end
151
157
 
158
+ # Attempts to load from shared config or shared credentials file.
159
+ # Will always attempt first to load from the shared credentials
160
+ # file, if present.
161
+ def sso_token_from_config(opts = {})
162
+ p = opts[:profile] || @profile_name
163
+ token = sso_token_from_profile(@parsed_credentials, p)
164
+ if @parsed_config
165
+ token ||= sso_token_from_profile(@parsed_config, p)
166
+ end
167
+ token
168
+ end
169
+
170
+ # Source a custom configured endpoint from the shared configuration file
171
+ #
172
+ # @param [Hash] opts
173
+ # @option opts [String] :profile
174
+ # @option opts [String] :service_id
175
+ def configured_endpoint(opts = {})
176
+ # services section is only allowed in the shared config file (not credentials)
177
+ profile = opts[:profile] || @profile_name
178
+ service_id = opts[:service_id]&.gsub(" ", "_")&.downcase
179
+ if @parsed_config && (prof_config = @parsed_config[profile])
180
+ services_section_name = prof_config['services']
181
+ if (services_config = @parsed_config["services #{services_section_name}"]) &&
182
+ (service_config = services_config[service_id])
183
+ return service_config['endpoint_url'] if service_config['endpoint_url']
184
+ end
185
+ return prof_config['endpoint_url']
186
+ end
187
+ nil
188
+ end
189
+
152
190
  # Add an accessor method (similar to attr_reader) to return a configuration value
153
191
  # Uses the get_config_value below to control where
154
192
  # values are loaded from
@@ -163,8 +201,11 @@ module Aws
163
201
  :ca_bundle,
164
202
  :credential_process,
165
203
  :endpoint_discovery_enabled,
204
+ :use_dualstack_endpoint,
205
+ :use_fips_endpoint,
166
206
  :ec2_metadata_service_endpoint,
167
207
  :ec2_metadata_service_endpoint_mode,
208
+ :ec2_metadata_v1_disabled,
168
209
  :max_attempts,
169
210
  :retry_mode,
170
211
  :adaptive_retry_wait_to_fill,
@@ -175,7 +216,14 @@ module Aws
175
216
  :csm_port,
176
217
  :sts_regional_endpoints,
177
218
  :s3_use_arn_region,
178
- :s3_us_east_1_regional_endpoint
219
+ :s3_us_east_1_regional_endpoint,
220
+ :s3_disable_multiregion_access_points,
221
+ :s3_disable_express_session_auth,
222
+ :defaults_mode,
223
+ :sdk_ua_app_id,
224
+ :disable_request_compression,
225
+ :request_min_compression_size_bytes,
226
+ :ignore_configured_endpoint_urls
179
227
  )
180
228
 
181
229
  private
@@ -191,11 +239,6 @@ module Aws
191
239
  value
192
240
  end
193
241
 
194
- def credentials_present?
195
- (@parsed_credentials && !@parsed_credentials.empty?) ||
196
- (@parsed_config && !@parsed_config.empty?)
197
- end
198
-
199
242
  def assume_role_from_profile(cfg, profile, opts, chain_config)
200
243
  if cfg && prof_cfg = cfg[profile]
201
244
  opts[:source_profile] ||= prof_cfg['source_profile']
@@ -315,13 +358,53 @@ module Aws
315
358
  def sso_credentials_from_profile(cfg, profile)
316
359
  if @parsed_config &&
317
360
  (prof_config = cfg[profile]) &&
318
- !(prof_config.keys & SSO_PROFILE_KEYS).empty?
361
+ !(prof_config.keys & SSO_CREDENTIAL_PROFILE_KEYS).empty?
362
+
363
+ if sso_session_name = prof_config['sso_session']
364
+ sso_session = sso_session(cfg, profile, sso_session_name)
365
+
366
+ sso_region = sso_session['sso_region']
367
+ sso_start_url = sso_session['sso_start_url']
368
+
369
+ # validate sso_region and sso_start_url don't conflict if set on profile and session
370
+ if prof_config['sso_region'] && prof_config['sso_region'] != sso_region
371
+ raise ArgumentError,
372
+ "sso-session #{sso_session_name}'s sso_region (#{sso_region}) " \
373
+ "does not match the profile #{profile}'s sso_region (#{prof_config['sso_region']}'"
374
+ end
375
+ if prof_config['sso_start_url'] && prof_config['sso_start_url'] != sso_start_url
376
+ raise ArgumentError,
377
+ "sso-session #{sso_session_name}'s sso_start_url (#{sso_start_url}) " \
378
+ "does not match the profile #{profile}'s sso_start_url (#{prof_config['sso_start_url']}'"
379
+ end
380
+ else
381
+ sso_region = prof_config['sso_region']
382
+ sso_start_url = prof_config['sso_start_url']
383
+ end
319
384
 
320
385
  SSOCredentials.new(
321
- sso_start_url: prof_config['sso_start_url'],
322
- sso_region: prof_config['sso_region'],
323
386
  sso_account_id: prof_config['sso_account_id'],
324
- sso_role_name: prof_config['sso_role_name']
387
+ sso_role_name: prof_config['sso_role_name'],
388
+ sso_session: prof_config['sso_session'],
389
+ sso_region: sso_region,
390
+ sso_start_url: sso_start_url
391
+ )
392
+ end
393
+ end
394
+
395
+ # If the required sso_ profile values are present, attempt to construct
396
+ # SSOTokenProvider
397
+ def sso_token_from_profile(cfg, profile)
398
+ if @parsed_config &&
399
+ (prof_config = cfg[profile]) &&
400
+ !(prof_config.keys & SSO_TOKEN_PROFILE_KEYS).empty?
401
+
402
+ sso_session_name = prof_config['sso_session']
403
+ sso_session = sso_session(cfg, profile, sso_session_name)
404
+
405
+ SSOTokenProvider.new(
406
+ sso_session: sso_session_name,
407
+ sso_region: sso_session['sso_region']
325
408
  )
326
409
  end
327
410
  end
@@ -375,5 +458,22 @@ module Aws
375
458
  ret ||= 'default'
376
459
  ret
377
460
  end
461
+
462
+ def sso_session(cfg, profile, sso_session_name)
463
+ # aws sso-configure may add quotes around sso session names with whitespace
464
+ sso_session = cfg["sso-session #{sso_session_name}"] || cfg["sso-session '#{sso_session_name}'"]
465
+
466
+ unless sso_session
467
+ raise ArgumentError,
468
+ "sso-session #{sso_session_name} must be defined in the config file. " \
469
+ "Referenced by profile #{profile}"
470
+ end
471
+
472
+ unless sso_session['sso_region']
473
+ raise ArgumentError, "sso-session #{sso_session_name} missing required parameter: sso_region"
474
+ end
475
+
476
+ sso_session
477
+ end
378
478
  end
379
479
  end
@@ -1,45 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aws
4
- # An auto-refreshing credential provider that works by assuming a
5
- # role via {Aws::SSO::Client#get_role_credentials} using a cached access
6
- # token. This class does NOT implement the SSO login token flow - tokens
7
- # must generated and refreshed separately by running `aws login` from the
8
- # AWS CLI with the correct profile.
9
- #
10
- # For more background on AWS SSO see the official
11
- # {https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html what is SSO Userguide}
12
- #
13
- # ## Refreshing Credentials from SSO
14
- #
15
- # The `SSOCredentials` will auto-refresh the AWS credentials from SSO. In
16
- # addition to AWS credentials expiring after a given amount of time, the
17
- # access token generated and cached from `aws login` will also expire.
18
- # Once this token expires, it will not be usable to refresh AWS credentials,
19
- # and another token will be needed. The SDK does not manage refreshing of
20
- # the token value, but this can be done by running `aws login` with the
21
- # correct profile.
22
- #
4
+ # An auto-refreshing credential provider that assumes a role via
5
+ # {Aws::SSO::Client#get_role_credentials} using a cached access
6
+ # token. When `sso_session` is specified, token refresh logic from
7
+ # {Aws::SSOTokenProvider} will be used to refresh the token if possible.
8
+ # This class does NOT implement the SSO login token flow - tokens
9
+ # must generated separately by running `aws login` from the
10
+ # AWS CLI with the correct profile. The `SSOCredentials` will
11
+ # auto-refresh the AWS credentials from SSO.
23
12
  #
24
13
  # # You must first run aws sso login --profile your-sso-profile
25
14
  # sso_credentials = Aws::SSOCredentials.new(
26
15
  # sso_account_id: '123456789',
27
16
  # sso_role_name: "role_name",
28
17
  # sso_region: "us-east-1",
29
- # sso_start_url: 'https://your-start-url.awsapps.com/start'
18
+ # sso_session: 'my_sso_session'
30
19
  # )
31
- #
32
20
  # ec2 = Aws::EC2::Client.new(credentials: sso_credentials)
33
21
  #
34
- # If you omit `:client` option, a new {SSO::Client} object will be
35
- # constructed.
22
+ # If you omit `:client` option, a new {Aws::SSO::Client} object will be
23
+ # constructed with additional options that were provided.
24
+ #
25
+ # @see Aws::SSO::Client#get_role_credentials
26
+ # @see https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html
36
27
  class SSOCredentials
37
28
 
38
29
  include CredentialProvider
39
30
  include RefreshingCredentials
40
31
 
41
32
  # @api private
42
- SSO_REQUIRED_OPTS = [:sso_account_id, :sso_region, :sso_role_name, :sso_start_url].freeze
33
+ LEGACY_REQUIRED_OPTS = [:sso_start_url, :sso_account_id, :sso_region, :sso_role_name].freeze
34
+ TOKEN_PROVIDER_REQUIRED_OPTS = [:sso_session, :sso_account_id, :sso_region, :sso_role_name].freeze
43
35
 
44
36
  # @api private
45
37
  SSO_LOGIN_GUIDANCE = 'The SSO session associated with this profile has '\
@@ -49,38 +41,79 @@ module Aws
49
41
  # @option options [required, String] :sso_account_id The AWS account ID
50
42
  # that temporary AWS credentials will be resolved for
51
43
  #
52
- # @option options [required, String] :sso_region The AWS region where the
53
- # SSO directory for the given sso_start_url is hosted.
54
- #
55
44
  # @option options [required, String] :sso_role_name The corresponding
56
45
  # IAM role in the AWS account that temporary AWS credentials
57
46
  # will be resolved for.
58
47
  #
59
- # @option options [required, String] :sso_start_url The start URL is
60
- # provided by the SSO service via the console and is the URL used to
48
+ # @option options [required, String] :sso_region The AWS region where the
49
+ # SSO directory for the given sso_start_url is hosted.
50
+ #
51
+ # @option options [String] :sso_session The SSO Token used for fetching
52
+ # the token. If provided, refresh logic from the {Aws::SSOTokenProvider}
53
+ # will be used.
54
+ #
55
+ # @option options [String] :sso_start_url (legacy profiles) If provided,
56
+ # legacy token fetch behavior will be used, which does not support
57
+ # token refreshing. The start URL is provided by the SSO
58
+ # service via the console and is the URL used to
61
59
  # login to the SSO directory. This is also sometimes referred to as
62
- # the "User Portal URL"
60
+ # the "User Portal URL".
63
61
  #
64
62
  # @option options [SSO::Client] :client Optional `SSO::Client`. If not
65
63
  # provided, a client will be constructed.
64
+ #
65
+ # @option options [Callable] before_refresh Proc called before
66
+ # credentials are refreshed. `before_refresh` is called
67
+ # with an instance of this object when
68
+ # AWS credentials are required and need to be refreshed.
66
69
  def initialize(options = {})
70
+ options = options.select {|k, v| !v.nil? }
71
+ if (options[:sso_session])
72
+ missing_keys = TOKEN_PROVIDER_REQUIRED_OPTS.select { |k| options[k].nil? }
73
+ unless missing_keys.empty?
74
+ raise ArgumentError, "Missing required keys: #{missing_keys}"
75
+ end
76
+ @legacy = false
77
+ @sso_role_name = options.delete(:sso_role_name)
78
+ @sso_account_id = options.delete(:sso_account_id)
67
79
 
68
- missing_keys = SSO_REQUIRED_OPTS.select { |k| options[k].nil? }
69
- unless missing_keys.empty?
70
- raise ArgumentError, "Missing required keys: #{missing_keys}"
71
- end
80
+ # if client has been passed, don't pass through to SSOTokenProvider
81
+ @client = options.delete(:client)
82
+ options.delete(:sso_start_url)
83
+ @token_provider = Aws::SSOTokenProvider.new(options.dup)
84
+ @sso_session = options.delete(:sso_session)
85
+ @sso_region = options.delete(:sso_region)
86
+
87
+ unless @client
88
+ client_opts = {}
89
+ options.each_pair { |k,v| client_opts[k] = v unless CLIENT_EXCLUDE_OPTIONS.include?(k) }
90
+ client_opts[:region] = @sso_region
91
+ client_opts[:credentials] = nil
92
+ @client = Aws::SSO::Client.new(client_opts)
93
+ end
94
+ else # legacy behavior
95
+ missing_keys = LEGACY_REQUIRED_OPTS.select { |k| options[k].nil? }
96
+ unless missing_keys.empty?
97
+ raise ArgumentError, "Missing required keys: #{missing_keys}"
98
+ end
99
+ @legacy = true
100
+ @sso_start_url = options.delete(:sso_start_url)
101
+ @sso_region = options.delete(:sso_region)
102
+ @sso_role_name = options.delete(:sso_role_name)
103
+ @sso_account_id = options.delete(:sso_account_id)
72
104
 
73
- @sso_start_url = options.delete(:sso_start_url)
74
- @sso_region = options.delete(:sso_region)
75
- @sso_role_name = options.delete(:sso_role_name)
76
- @sso_account_id = options.delete(:sso_account_id)
105
+ # validate we can read the token file
106
+ read_cached_token
77
107
 
78
- # validate we can read the token file
79
- read_cached_token
108
+ client_opts = {}
109
+ options.each_pair { |k,v| client_opts[k] = v unless CLIENT_EXCLUDE_OPTIONS.include?(k) }
110
+ client_opts[:region] = @sso_region
111
+ client_opts[:credentials] = nil
112
+
113
+ @client = options[:client] || Aws::SSO::Client.new(client_opts)
114
+ end
80
115
 
81
- options[:region] = @sso_region
82
- options[:credentials] = nil
83
- @client = options[:client] || Aws::SSO::Client.new(options)
116
+ @async_refresh = true
84
117
  super
85
118
  end
86
119
 
@@ -100,24 +133,32 @@ module Aws
100
133
  raise ArgumentError, 'Cached SSO Token is expired.'
101
134
  end
102
135
  cached_token
103
- rescue Aws::Json::ParseError, ArgumentError
136
+ rescue Errno::ENOENT, Aws::Json::ParseError, ArgumentError
104
137
  raise Errors::InvalidSSOCredentials, SSO_LOGIN_GUIDANCE
105
138
  end
106
139
 
107
140
  def refresh
108
- cached_token = read_cached_token
109
- c = @client.get_role_credentials(
110
- account_id: @sso_account_id,
111
- role_name: @sso_role_name,
112
- access_token: cached_token['accessToken']
113
- ).role_credentials
141
+ c = if @legacy
142
+ cached_token = read_cached_token
143
+ @client.get_role_credentials(
144
+ account_id: @sso_account_id,
145
+ role_name: @sso_role_name,
146
+ access_token: cached_token['accessToken']
147
+ ).role_credentials
148
+ else
149
+ @client.get_role_credentials(
150
+ account_id: @sso_account_id,
151
+ role_name: @sso_role_name,
152
+ access_token: @token_provider.token.token
153
+ ).role_credentials
154
+ end
114
155
 
115
156
  @credentials = Credentials.new(
116
157
  c.access_key_id,
117
158
  c.secret_access_key,
118
159
  c.session_token
119
160
  )
120
- @expiration = c.expiration
161
+ @expiration = Time.at(c.expiration / 1000.0)
121
162
  end
122
163
 
123
164
  def sso_cache_file
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ class SSOTokenProvider
5
+
6
+ include TokenProvider
7
+ include RefreshingToken
8
+
9
+ # @api private
10
+ SSO_REQUIRED_OPTS = [:sso_region, :sso_session].freeze
11
+
12
+ # @api private
13
+ SSO_LOGIN_GUIDANCE = 'The SSO session associated with this profile has '\
14
+ 'expired or is otherwise invalid. To refresh this SSO session run '\
15
+ 'aws sso login with the corresponding profile.'.freeze
16
+
17
+ # @option options [required, String] :sso_region The AWS region where the
18
+ # SSO directory for the given sso_start_url is hosted.
19
+ #
20
+ # @option options [required, String] :sso_session The SSO Session used to
21
+ # for fetching this token.
22
+ #
23
+ # @option options [SSOOIDC::Client] :client Optional `SSOOIDC::Client`. If not
24
+ # provided, a client will be constructed.
25
+ #
26
+ # @option options [Callable] before_refresh Proc called before
27
+ # credentials are refreshed. `before_refresh` is called
28
+ # with an instance of this object when
29
+ # AWS credentials are required and need to be refreshed.
30
+ def initialize(options = {})
31
+
32
+ missing_keys = SSO_REQUIRED_OPTS.select { |k| options[k].nil? }
33
+ unless missing_keys.empty?
34
+ raise ArgumentError, "Missing required keys: #{missing_keys}"
35
+ end
36
+
37
+ @sso_session = options.delete(:sso_session)
38
+ @sso_region = options.delete(:sso_region)
39
+
40
+ options[:region] = @sso_region
41
+ options[:credentials] = nil
42
+ options[:token_provider] = nil
43
+ @client = options[:client] || Aws::SSOOIDC::Client.new(options)
44
+
45
+ super
46
+ end
47
+
48
+ # @return [SSOOIDC::Client]
49
+ attr_reader :client
50
+
51
+ private
52
+
53
+ def refresh
54
+ # token is valid and not in refresh window - do not refresh it.
55
+ return if @token && @token.expiration && !near_expiration?
56
+
57
+ # token may not exist or is out of the expiration window
58
+ # attempt to refresh from disk first (another process/application may have refreshed already)
59
+ token_json = read_cached_token
60
+ @token = Token.new(token_json['accessToken'], token_json['expiresAt'])
61
+ return if @token && @token.expiration && !near_expiration?
62
+
63
+ # The token is expired and needs to be refreshed
64
+ if can_refresh_token?(token_json)
65
+ begin
66
+ current_time = Time.now
67
+ resp = @client.create_token(
68
+ grant_type: 'refresh_token',
69
+ client_id: token_json['clientId'],
70
+ client_secret: token_json['clientSecret'],
71
+ refresh_token: token_json['refreshToken']
72
+ )
73
+ token_json['accessToken'] = resp.access_token
74
+ token_json['expiresAt'] = current_time + resp.expires_in
75
+ @token = Token.new(token_json['accessToken'], token_json['expiresAt'])
76
+
77
+ if resp.refresh_token
78
+ token_json['refreshToken'] = resp.refresh_token
79
+ else
80
+ token_json.delete('refreshToken')
81
+ end
82
+
83
+ update_token_cache(token_json)
84
+ rescue
85
+ # refresh has failed, continue attempting to use the token if its not hard expired
86
+ end
87
+ end
88
+
89
+ if !@token.expiration || @token.expiration < Time.now
90
+ # Token is hard expired, raise an exception
91
+ raise Errors::InvalidSSOToken, 'Token is invalid and failed to refresh.'
92
+ end
93
+ end
94
+
95
+ def read_cached_token
96
+ cached_token = Json.load(File.read(sso_cache_file))
97
+ # validation
98
+ unless cached_token['accessToken'] && cached_token['expiresAt']
99
+ raise ArgumentError, 'Missing required field(s)'
100
+ end
101
+ cached_token['expiresAt'] = Time.parse(cached_token['expiresAt'])
102
+ cached_token
103
+ rescue Errno::ENOENT, Aws::Json::ParseError, ArgumentError
104
+ raise Errors::InvalidSSOToken, SSO_LOGIN_GUIDANCE
105
+ end
106
+
107
+ def update_token_cache(token_json)
108
+ cached_token = token_json.dup
109
+ cached_token['expiresAt'] = cached_token['expiresAt'].iso8601
110
+ File.write(sso_cache_file, Json.dump(cached_token))
111
+ end
112
+
113
+ def sso_cache_file
114
+ sso_session_sha1 = OpenSSL::Digest::SHA1.hexdigest(@sso_session.encode('utf-8'))
115
+ File.join(Dir.home, '.aws', 'sso', 'cache', "#{sso_session_sha1}.json")
116
+ rescue ArgumentError
117
+ # Dir.home raises ArgumentError when ENV['home'] is not set
118
+ raise ArgumentError, "Unable to load sso_cache_file: ENV['HOME'] is not set."
119
+ end
120
+
121
+ # return true if all required fields are present
122
+ # return false if registrationExpiresAt exists and is later than now
123
+ def can_refresh_token?(token_json)
124
+ if token_json['clientId'] &&
125
+ token_json['clientSecret'] &&
126
+ token_json['refreshToken']
127
+
128
+ return !token_json['registrationExpiresAt'] ||
129
+ Time.parse(token_json['registrationExpiresAt']) > Time.now
130
+ else
131
+ false
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ class StaticTokenProvider
5
+
6
+ include TokenProvider
7
+
8
+ # @param [String] token
9
+ # @param [Time] expiration
10
+ def initialize(token, expiration=nil)
11
+ @token = Token.new(token, expiration)
12
+ end
13
+ end
14
+ end