aws-sdk-core 3.165.0 → 3.201.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +462 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-defaults/default_configuration.rb +4 -4
  5. data/lib/aws-sdk-core/binary/decode_handler.rb +3 -9
  6. data/lib/aws-sdk-core/binary/encode_handler.rb +1 -1
  7. data/lib/aws-sdk-core/binary/event_builder.rb +34 -37
  8. data/lib/aws-sdk-core/binary/event_stream_decoder.rb +1 -0
  9. data/lib/aws-sdk-core/binary/event_stream_encoder.rb +4 -3
  10. data/lib/aws-sdk-core/cbor/cbor_engine.rb +19 -0
  11. data/lib/aws-sdk-core/cbor/decoder.rb +310 -0
  12. data/lib/aws-sdk-core/cbor/encoder.rb +243 -0
  13. data/lib/aws-sdk-core/cbor.rb +106 -0
  14. data/lib/aws-sdk-core/client_stubs.rb +18 -14
  15. data/lib/aws-sdk-core/credential_provider.rb +4 -1
  16. data/lib/aws-sdk-core/credential_provider_chain.rb +8 -5
  17. data/lib/aws-sdk-core/ec2_metadata.rb +1 -1
  18. data/lib/aws-sdk-core/ecs_credentials.rb +178 -53
  19. data/lib/aws-sdk-core/endpoints/condition.rb +5 -0
  20. data/lib/aws-sdk-core/endpoints/endpoint_rule.rb +5 -1
  21. data/lib/aws-sdk-core/endpoints/error_rule.rb +5 -0
  22. data/lib/aws-sdk-core/endpoints/function.rb +5 -0
  23. data/lib/aws-sdk-core/endpoints/matchers.rb +18 -10
  24. data/lib/aws-sdk-core/endpoints/reference.rb +5 -0
  25. data/lib/aws-sdk-core/endpoints/rule.rb +5 -0
  26. data/lib/aws-sdk-core/endpoints/rule_set.rb +5 -0
  27. data/lib/aws-sdk-core/endpoints/rules_provider.rb +5 -0
  28. data/lib/aws-sdk-core/endpoints/templater.rb +6 -0
  29. data/lib/aws-sdk-core/endpoints/tree_rule.rb +5 -0
  30. data/lib/aws-sdk-core/endpoints/url.rb +1 -0
  31. data/lib/aws-sdk-core/endpoints.rb +69 -19
  32. data/lib/aws-sdk-core/error_handler.rb +41 -0
  33. data/lib/aws-sdk-core/errors.rb +12 -3
  34. data/lib/aws-sdk-core/event_emitter.rb +0 -16
  35. data/lib/aws-sdk-core/ini_parser.rb +7 -0
  36. data/lib/aws-sdk-core/instance_profile_credentials.rb +55 -32
  37. data/lib/aws-sdk-core/json/builder.rb +8 -1
  38. data/lib/aws-sdk-core/json/error_handler.rb +29 -14
  39. data/lib/aws-sdk-core/json/handler.rb +12 -6
  40. data/lib/aws-sdk-core/json/json_engine.rb +3 -1
  41. data/lib/aws-sdk-core/json/oj_engine.rb +7 -1
  42. data/lib/aws-sdk-core/json/parser.rb +33 -3
  43. data/lib/aws-sdk-core/json.rb +43 -14
  44. data/lib/aws-sdk-core/log/formatter.rb +6 -0
  45. data/lib/aws-sdk-core/lru_cache.rb +75 -0
  46. data/lib/aws-sdk-core/pageable_response.rb +3 -1
  47. data/lib/aws-sdk-core/param_validator.rb +9 -4
  48. data/lib/aws-sdk-core/plugins/bearer_authorization.rb +2 -0
  49. data/lib/aws-sdk-core/plugins/checksum_algorithm.rb +7 -4
  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 +2 -0
  53. data/lib/aws-sdk-core/plugins/global_configuration.rb +8 -9
  54. data/lib/aws-sdk-core/plugins/http_checksum.rb +2 -1
  55. data/lib/aws-sdk-core/plugins/invocation_id.rb +1 -11
  56. data/lib/aws-sdk-core/plugins/logging.rb +2 -0
  57. data/lib/aws-sdk-core/plugins/protocols/api_gateway.rb +3 -1
  58. data/lib/aws-sdk-core/plugins/protocols/ec2.rb +2 -24
  59. data/lib/aws-sdk-core/plugins/protocols/json_rpc.rb +6 -8
  60. data/lib/aws-sdk-core/plugins/protocols/query.rb +4 -2
  61. data/lib/aws-sdk-core/plugins/protocols/rest_json.rb +3 -15
  62. data/lib/aws-sdk-core/plugins/protocols/rest_xml.rb +3 -0
  63. data/lib/aws-sdk-core/plugins/protocols/rpc_v2.rb +17 -0
  64. data/lib/aws-sdk-core/plugins/regional_endpoint.rb +140 -35
  65. data/lib/aws-sdk-core/plugins/request_compression.rb +226 -0
  66. data/lib/aws-sdk-core/plugins/retries/error_inspector.rb +2 -1
  67. data/lib/aws-sdk-core/plugins/retry_errors.rb +12 -3
  68. data/lib/aws-sdk-core/plugins/sign.rb +43 -19
  69. data/lib/aws-sdk-core/plugins/signature_v2.rb +2 -1
  70. data/lib/aws-sdk-core/plugins/signature_v4.rb +2 -1
  71. data/lib/aws-sdk-core/plugins/stub_responses.rb +1 -0
  72. data/lib/aws-sdk-core/plugins/transfer_encoding.rb +16 -9
  73. data/lib/aws-sdk-core/plugins/user_agent.rb +152 -14
  74. data/lib/aws-sdk-core/process_credentials.rb +45 -27
  75. data/lib/aws-sdk-core/query/ec2_handler.rb +27 -0
  76. data/lib/aws-sdk-core/query/ec2_param_builder.rb +5 -7
  77. data/lib/aws-sdk-core/query/handler.rb +4 -4
  78. data/lib/aws-sdk-core/query/param_builder.rb +2 -2
  79. data/lib/aws-sdk-core/query.rb +2 -1
  80. data/lib/aws-sdk-core/refreshing_credentials.rb +12 -12
  81. data/lib/aws-sdk-core/rest/content_type_handler.rb +60 -0
  82. data/lib/aws-sdk-core/rest/handler.rb +3 -4
  83. data/lib/aws-sdk-core/rest/request/body.rb +32 -5
  84. data/lib/aws-sdk-core/rest/request/endpoint.rb +24 -4
  85. data/lib/aws-sdk-core/rest/request/headers.rb +15 -7
  86. data/lib/aws-sdk-core/rest/request/querystring_builder.rb +62 -36
  87. data/lib/aws-sdk-core/rest/response/body.rb +15 -1
  88. data/lib/aws-sdk-core/rest/response/header_list_parser.rb +79 -0
  89. data/lib/aws-sdk-core/rest/response/headers.rb +8 -3
  90. data/lib/aws-sdk-core/rest.rb +1 -0
  91. data/lib/aws-sdk-core/rpc_v2/builder.rb +62 -0
  92. data/lib/aws-sdk-core/rpc_v2/content_type_handler.rb +45 -0
  93. data/lib/aws-sdk-core/rpc_v2/error_handler.rb +84 -0
  94. data/lib/aws-sdk-core/rpc_v2/handler.rb +74 -0
  95. data/lib/aws-sdk-core/rpc_v2/parser.rb +90 -0
  96. data/lib/aws-sdk-core/rpc_v2.rb +6 -0
  97. data/lib/aws-sdk-core/shared_config.rb +77 -18
  98. data/lib/aws-sdk-core/sso_credentials.rb +80 -45
  99. data/lib/aws-sdk-core/sso_token_provider.rb +3 -2
  100. data/lib/aws-sdk-core/stubbing/protocols/rpc_v2.rb +41 -0
  101. data/lib/aws-sdk-core/stubbing/stub_data.rb +11 -0
  102. data/lib/aws-sdk-core/util.rb +39 -0
  103. data/lib/aws-sdk-core/waiters/poller.rb +4 -2
  104. data/lib/aws-sdk-core/xml/builder.rb +17 -9
  105. data/lib/aws-sdk-core/xml/error_handler.rb +32 -42
  106. data/lib/aws-sdk-core/xml/parser/frame.rb +4 -20
  107. data/lib/aws-sdk-core/xml/parser/{engines/oga.rb → oga_engine.rb} +2 -0
  108. data/lib/aws-sdk-core/xml/parser/stack.rb +2 -0
  109. data/lib/aws-sdk-core/xml/parser.rb +2 -6
  110. data/lib/aws-sdk-core.rb +8 -2
  111. data/lib/aws-sdk-sso/client.rb +103 -46
  112. data/lib/aws-sdk-sso/client_api.rb +6 -0
  113. data/lib/aws-sdk-sso/endpoint_provider.rb +41 -96
  114. data/lib/aws-sdk-sso/endpoints.rb +1 -0
  115. data/lib/aws-sdk-sso/plugins/endpoints.rb +4 -2
  116. data/lib/aws-sdk-sso/types.rb +0 -35
  117. data/lib/aws-sdk-sso.rb +1 -1
  118. data/lib/aws-sdk-ssooidc/client.rb +488 -74
  119. data/lib/aws-sdk-ssooidc/client_api.rb +82 -1
  120. data/lib/aws-sdk-ssooidc/endpoint_provider.rb +41 -95
  121. data/lib/aws-sdk-ssooidc/endpoints.rb +15 -0
  122. data/lib/aws-sdk-ssooidc/errors.rb +52 -0
  123. data/lib/aws-sdk-ssooidc/plugins/endpoints.rb +6 -2
  124. data/lib/aws-sdk-ssooidc/types.rb +372 -83
  125. data/lib/aws-sdk-ssooidc.rb +1 -1
  126. data/lib/aws-sdk-sts/client.rb +361 -288
  127. data/lib/aws-sdk-sts/client_api.rb +24 -11
  128. data/lib/aws-sdk-sts/endpoint_provider.rb +96 -213
  129. data/lib/aws-sdk-sts/endpoints.rb +1 -0
  130. data/lib/aws-sdk-sts/plugins/endpoints.rb +4 -2
  131. data/lib/aws-sdk-sts/presigner.rb +1 -1
  132. data/lib/aws-sdk-sts/types.rb +128 -197
  133. data/lib/aws-sdk-sts.rb +1 -1
  134. data/lib/seahorse/client/async_base.rb +1 -1
  135. data/lib/seahorse/client/async_response.rb +19 -0
  136. data/lib/seahorse/client/base.rb +18 -7
  137. data/lib/seahorse/client/configuration.rb +0 -4
  138. data/lib/seahorse/client/h2/connection.rb +12 -11
  139. data/lib/seahorse/client/h2/handler.rb +1 -0
  140. data/lib/seahorse/client/handler.rb +1 -1
  141. data/lib/seahorse/client/net_http/connection_pool.rb +3 -9
  142. data/lib/seahorse/client/net_http/patches.rb +1 -4
  143. data/lib/seahorse/client/plugin.rb +9 -0
  144. data/lib/seahorse/client/plugins/endpoint.rb +0 -1
  145. data/lib/seahorse/client/plugins/h2.rb +3 -3
  146. data/lib/seahorse/client/plugins/net_http.rb +48 -16
  147. data/lib/seahorse/client/plugins/request_callback.rb +31 -0
  148. data/lib/seahorse/client/response.rb +6 -0
  149. data/lib/seahorse/model/operation.rb +3 -0
  150. data/lib/seahorse/model/shapes.rb +2 -2
  151. data/sig/aws-sdk-core/client_stubs.rbs +10 -0
  152. data/sig/aws-sdk-core/errors.rbs +22 -0
  153. data/sig/aws-sdk-core/resources/collection.rbs +21 -0
  154. data/sig/aws-sdk-core/structure.rbs +4 -0
  155. data/sig/aws-sdk-core/waiters/errors.rbs +20 -0
  156. data/sig/aws-sdk-core.rbs +7 -0
  157. data/sig/seahorse/client/base.rbs +25 -0
  158. data/sig/seahorse/client/handler_builder.rbs +16 -0
  159. data/sig/seahorse/client/response.rbs +61 -0
  160. metadata +44 -17
  161. /data/lib/aws-sdk-core/xml/parser/{engines/libxml.rb → libxml_engine.rb} +0 -0
  162. /data/lib/aws-sdk-core/xml/parser/{engines/nokogiri.rb → nokogiri_engine.rb} +0 -0
  163. /data/lib/aws-sdk-core/xml/parser/{engines/ox.rb → ox_engine.rb} +0 -0
  164. /data/lib/aws-sdk-core/xml/parser/{engines/rexml.rb → rexml_engine.rb} +0 -0
@@ -24,15 +24,16 @@ module Aws
24
24
  end
25
25
 
26
26
  # When a client is stubbed allow the user to access the requests made
27
- @api_requests = []
28
-
29
- requests = @api_requests
27
+ requests = @api_requests = []
28
+ requests_mutex = @requests_mutex = Mutex.new
30
29
  self.handle do |context|
31
- requests << {
32
- operation_name: context.operation_name,
33
- params: context.params,
34
- context: context
35
- }
30
+ requests_mutex.synchronize do
31
+ requests << {
32
+ operation_name: context.operation_name,
33
+ params: context.params,
34
+ context: context
35
+ }
36
+ end
36
37
  @handler.call(context)
37
38
  end
38
39
  end
@@ -194,10 +195,12 @@ module Aws
194
195
  # is not stubbed.
195
196
  def api_requests(options = {})
196
197
  if config.stub_responses
197
- if options[:exclude_presign]
198
- @api_requests.reject {|req| req[:context][:presigned_url] }
199
- else
200
- @api_requests
198
+ @requests_mutex.synchronize do
199
+ if options[:exclude_presign]
200
+ @api_requests.reject {|req| req[:context][:presigned_url] }
201
+ else
202
+ @api_requests
203
+ end
201
204
  end
202
205
  else
203
206
  msg = 'This method is only implemented for stubbed clients, and is '\
@@ -305,10 +308,11 @@ module Aws
305
308
  def protocol_helper
306
309
  case config.api.metadata['protocol']
307
310
  when 'json' then Stubbing::Protocols::Json
308
- when 'query' then Stubbing::Protocols::Query
309
- when 'ec2' then Stubbing::Protocols::EC2
310
311
  when 'rest-json' then Stubbing::Protocols::RestJson
311
312
  when 'rest-xml' then Stubbing::Protocols::RestXml
313
+ when 'query' then Stubbing::Protocols::Query
314
+ when 'ec2' then Stubbing::Protocols::EC2
315
+ when 'smithy-rpc-v2-cbor' then Stubbing::Protocols::RpcV2
312
316
  when 'api-gateway' then Stubbing::Protocols::ApiGateway
313
317
  else raise "unsupported protocol"
314
318
  end.new
@@ -6,9 +6,12 @@ module Aws
6
6
  # @return [Credentials]
7
7
  attr_reader :credentials
8
8
 
9
+ # @return [Time]
10
+ attr_reader :expiration
11
+
9
12
  # @return [Boolean]
10
13
  def set?
11
- !!credentials && credentials.set?
14
+ !!@credentials && @credentials.set?
12
15
  end
13
16
 
14
17
  end
@@ -161,7 +161,8 @@ module Aws
161
161
 
162
162
  def instance_profile_credentials(options)
163
163
  profile_name = determine_profile_name(options)
164
- if ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI']
164
+ if ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] ||
165
+ ENV['AWS_CONTAINER_CREDENTIALS_FULL_URI']
165
166
  ECSCredentials.new(options)
166
167
  else
167
168
  InstanceProfileCredentials.new(options.merge(profile: profile_name))
@@ -169,12 +170,14 @@ module Aws
169
170
  end
170
171
 
171
172
  def assume_role_with_profile(options, profile_name)
172
- region = (options[:config] && options[:config].region)
173
- Aws.shared_config.assume_role_credentials_from_config(
173
+ assume_opts = {
174
174
  profile: profile_name,
175
- region: region,
176
175
  chain_config: @config
177
- )
176
+ }
177
+ if options[:config] && options[:config].region
178
+ assume_opts[:region] = options[:config].region
179
+ end
180
+ Aws.shared_config.assume_role_credentials_from_config(assume_opts)
178
181
  end
179
182
  end
180
183
  end
@@ -183,7 +183,7 @@ module Aws
183
183
 
184
184
  def open_connection
185
185
  uri = URI.parse(@endpoint)
186
- http = Net::HTTP.new(uri.hostname || @endpoint, @port || uri.port)
186
+ http = Net::HTTP.new(uri.hostname || @endpoint, uri.port || @port)
187
187
  http.open_timeout = @http_open_timeout
188
188
  http.read_timeout = @http_read_timeout
189
189
  http.set_debug_output(@http_debug_output) if @http_debug_output
@@ -2,21 +2,27 @@
2
2
 
3
3
  require 'time'
4
4
  require 'net/http'
5
+ require 'resolv'
5
6
 
6
7
  module Aws
7
8
  # An auto-refreshing credential provider that loads credentials from
8
- # instances running in ECS.
9
+ # instances running in containers.
9
10
  #
10
11
  # ecs_credentials = Aws::ECSCredentials.new(retries: 3)
11
12
  # ec2 = Aws::EC2::Client.new(credentials: ecs_credentials)
12
13
  class ECSCredentials
13
-
14
14
  include CredentialProvider
15
15
  include RefreshingCredentials
16
16
 
17
17
  # @api private
18
18
  class Non200Response < RuntimeError; end
19
19
 
20
+ # Raised when the token file cannot be read.
21
+ class TokenFileReadError < RuntimeError; end
22
+
23
+ # Raised when the token file is invalid.
24
+ class InvalidTokenError < RuntimeError; end
25
+
20
26
  # These are the errors we trap when attempting to talk to the
21
27
  # instance metadata service. Any of these imply the service
22
28
  # is not present, no responding or some other non-recoverable
@@ -29,16 +35,22 @@ module Aws
29
35
  Errno::ENETUNREACH,
30
36
  SocketError,
31
37
  Timeout::Error,
32
- Non200Response,
33
- ]
38
+ Non200Response
39
+ ].freeze
34
40
 
35
41
  # @param [Hash] options
36
42
  # @option options [Integer] :retries (5) Number of times to retry
37
43
  # when retrieving credentials.
38
- # @option options [String] :ip_address ('169.254.170.2')
39
- # @option options [Integer] :port (80)
44
+ # @option options [String] :ip_address ('169.254.170.2') This value is
45
+ # ignored if `endpoint` is set and `credential_path` is not set.
46
+ # @option options [Integer] :port (80) This value is ignored if `endpoint`
47
+ # is set and `credential_path` is not set.
40
48
  # @option options [String] :credential_path By default, the value of the
41
49
  # AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable.
50
+ # @option options [String] :endpoint The container credential endpoint.
51
+ # By default, this is the value of the AWS_CONTAINER_CREDENTIALS_FULL_URI
52
+ # environment variable. This value is ignored if `credential_path` or
53
+ # ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] is set.
42
54
  # @option options [Float] :http_open_timeout (5)
43
55
  # @option options [Float] :http_read_timeout (5)
44
56
  # @option options [Numeric, Proc] :delay By default, failures are retried
@@ -52,17 +64,14 @@ module Aws
52
64
  # credentials are refreshed. `before_refresh` is called
53
65
  # with an instance of this object when
54
66
  # AWS credentials are required and need to be refreshed.
55
- def initialize options = {}
67
+ def initialize(options = {})
68
+ credential_path = options[:credential_path] ||
69
+ ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI']
70
+ endpoint = options[:endpoint] ||
71
+ ENV['AWS_CONTAINER_CREDENTIALS_FULL_URI']
72
+ initialize_uri(options, credential_path, endpoint)
73
+
56
74
  @retries = options[:retries] || 5
57
- @ip_address = options[:ip_address] || '169.254.170.2'
58
- @port = options[:port] || 80
59
- @credential_path = options[:credential_path]
60
- @credential_path ||= ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI']
61
- unless @credential_path
62
- raise ArgumentError.new(
63
- "Cannot instantiate an ECS Credential Provider without a credential path."
64
- )
65
- end
66
75
  @http_open_timeout = options[:http_open_timeout] || 5
67
76
  @http_read_timeout = options[:http_read_timeout] || 5
68
77
  @http_debug_output = options[:http_debug_output]
@@ -77,11 +86,95 @@ module Aws
77
86
 
78
87
  private
79
88
 
89
+ def initialize_uri(options, credential_path, endpoint)
90
+ if credential_path
91
+ initialize_relative_uri(options, credential_path)
92
+ # Use FULL_URI/endpoint only if RELATIVE_URI/path is not set
93
+ elsif endpoint
94
+ initialize_full_uri(endpoint)
95
+ else
96
+ raise ArgumentError,
97
+ 'Cannot instantiate an ECS Credential Provider '\
98
+ 'without a credential path or endpoint.'
99
+ end
100
+ end
101
+
102
+ def initialize_relative_uri(options, path)
103
+ @host = options[:ip_address] || '169.254.170.2'
104
+ @port = options[:port] || 80
105
+ @scheme = 'http'
106
+ @credential_path = path
107
+ end
108
+
109
+ def initialize_full_uri(endpoint)
110
+ uri = URI.parse(endpoint)
111
+ validate_full_uri_scheme!(uri)
112
+ validate_full_uri!(uri)
113
+ @host = uri.hostname
114
+ @port = uri.port
115
+ @scheme = uri.scheme
116
+ @credential_path = uri.request_uri
117
+ end
118
+
119
+ def validate_full_uri_scheme!(full_uri)
120
+ return if full_uri.is_a?(URI::HTTP) || full_uri.is_a?(URI::HTTPS)
121
+
122
+ raise ArgumentError, "'#{full_uri}' must be a valid HTTP or HTTPS URI"
123
+ end
124
+
125
+ # Validate that the full URI is using a loopback address if scheme is http.
126
+ def validate_full_uri!(full_uri)
127
+ return unless full_uri.scheme == 'http'
128
+
129
+ begin
130
+ return if valid_ip_address?(IPAddr.new(full_uri.host))
131
+ rescue IPAddr::InvalidAddressError
132
+ addresses = Resolv.getaddresses(full_uri.host)
133
+ return if addresses.all? { |addr| valid_ip_address?(IPAddr.new(addr)) }
134
+ end
135
+
136
+ raise ArgumentError,
137
+ 'AWS_CONTAINER_CREDENTIALS_FULL_URI must use a local loopback '\
138
+ 'or an ECS or EKS link-local address when using the http scheme.'
139
+ end
140
+
141
+ def valid_ip_address?(ip_address)
142
+ ip_loopback?(ip_address) || ecs_or_eks_ip?(ip_address)
143
+ end
144
+
145
+ # loopback? method is available in Ruby 2.5+
146
+ # Replicate the logic here.
147
+ # loopback (IPv4 127.0.0.0/8, IPv6 ::1/128)
148
+ def ip_loopback?(ip_address)
149
+ case ip_address.family
150
+ when Socket::AF_INET
151
+ ip_address & 0xff000000 == 0x7f000000
152
+ when Socket::AF_INET6
153
+ ip_address == 1
154
+ else
155
+ false
156
+ end
157
+ end
158
+
159
+ # Verify that the IP address is a link-local address from ECS or EKS.
160
+ # ECS container host (IPv4 `169.254.170.2`)
161
+ # EKS container host (IPv4 `169.254.170.23`, IPv6 `fd00:ec2::23`)
162
+ def ecs_or_eks_ip?(ip_address)
163
+ case ip_address.family
164
+ when Socket::AF_INET
165
+ [0xa9feaa02, 0xa9feaa17].include?(ip_address)
166
+ when Socket::AF_INET6
167
+ ip_address == 0xfd00_0ec2_0000_0000_0000_0000_0000_0023
168
+ else
169
+ false
170
+ end
171
+ end
172
+
80
173
  def backoff(backoff)
81
174
  case backoff
82
175
  when Proc then backoff
83
- when Numeric then lambda { |_| sleep(backoff) }
84
- else lambda { |num_failures| Kernel.sleep(1.2 ** num_failures) }
176
+ when Numeric then ->(_) { sleep(backoff) }
177
+ else ->(num_failures) { Kernel.sleep(1.2**num_failures) }
85
178
  end
86
179
  end
87
180
 
@@ -89,68 +182,100 @@ module Aws
89
182
  # Retry loading credentials up to 3 times is the instance metadata
90
183
  # service is responding but is returning invalid JSON documents
91
184
  # in response to the GET profile credentials call.
92
- begin
93
- retry_errors([Aws::Json::ParseError, StandardError], max_retries: 3) do
94
- c = Aws::Json.load(get_credentials.to_s)
95
- @credentials = Credentials.new(
96
- c['AccessKeyId'],
97
- c['SecretAccessKey'],
98
- c['Token']
99
- )
100
- @expiration = c['Expiration'] ? Time.iso8601(c['Expiration']) : nil
101
- end
102
- rescue Aws::Json::ParseError
103
- raise Aws::Errors::MetadataParserError.new
185
+
186
+ retry_errors([Aws::Json::ParseError, StandardError], max_retries: 3) do
187
+ c = Aws::Json.load(get_credentials.to_s)
188
+ @credentials = Credentials.new(
189
+ c['AccessKeyId'],
190
+ c['SecretAccessKey'],
191
+ c['Token']
192
+ )
193
+ @expiration = c['Expiration'] ? Time.iso8601(c['Expiration']) : nil
104
194
  end
195
+ rescue Aws::Json::ParseError
196
+ raise Aws::Errors::MetadataParserError
105
197
  end
106
198
 
107
199
  def get_credentials
108
200
  # Retry loading credentials a configurable number of times if
109
201
  # the instance metadata service is not responding.
110
- begin
111
- retry_errors(NETWORK_ERRORS, max_retries: @retries) do
112
- open_connection do |conn|
113
- http_get(conn, @credential_path)
114
- end
202
+
203
+ retry_errors(NETWORK_ERRORS, max_retries: @retries) do
204
+ open_connection do |conn|
205
+ http_get(conn, @credential_path)
115
206
  end
116
- rescue
117
- '{}'
118
207
  end
208
+ rescue TokenFileReadError, InvalidTokenError
209
+ raise
210
+ rescue StandardError => e
211
+ warn("Error retrieving ECS Credentials: #{e.message}")
212
+ '{}'
213
+ end
214
+
215
+ def fetch_authorization_token
216
+ if (path = ENV['AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE'])
217
+ fetch_authorization_token_file(path)
218
+ elsif (token = ENV['AWS_CONTAINER_AUTHORIZATION_TOKEN'])
219
+ token
220
+ end
221
+ end
222
+
223
+ def fetch_authorization_token_file(path)
224
+ File.read(path).strip
225
+ rescue Errno::ENOENT
226
+ raise TokenFileReadError,
227
+ 'AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE is set '\
228
+ "but the file doesn't exist: #{path}"
229
+ end
230
+
231
+ def validate_authorization_token!(token)
232
+ return unless token.include?("\r\n")
233
+
234
+ raise InvalidTokenError,
235
+ 'Invalid Authorization token: token contains '\
236
+ 'a newline and carriage return character.'
119
237
  end
120
238
 
121
239
  def open_connection
122
- http = Net::HTTP.new(@ip_address, @port, nil)
240
+ http = Net::HTTP.new(@host, @port, nil)
123
241
  http.open_timeout = @http_open_timeout
124
242
  http.read_timeout = @http_read_timeout
125
243
  http.set_debug_output(@http_debug_output) if @http_debug_output
244
+ http.use_ssl = @scheme == 'https'
126
245
  http.start
127
246
  yield(http).tap { http.finish }
128
247
  end
129
248
 
130
249
  def http_get(connection, path)
131
- response = connection.request(Net::HTTP::Get.new(path))
132
- if response.code.to_i == 200
133
- response.body
134
- else
135
- raise Non200Response
250
+ request = Net::HTTP::Get.new(path)
251
+ set_authorization_token(request)
252
+ response = connection.request(request)
253
+ raise Non200Response unless response.code.to_i == 200
254
+
255
+ response.body
256
+ end
257
+
258
+ def set_authorization_token(request)
259
+ if (authorization_token = fetch_authorization_token)
260
+ validate_authorization_token!(authorization_token)
261
+ request['Authorization'] = authorization_token
136
262
  end
137
263
  end
138
264
 
139
- def retry_errors(error_classes, options = {}, &block)
265
+ def retry_errors(error_classes, options = {})
140
266
  max_retries = options[:max_retries]
141
267
  retries = 0
142
268
  begin
143
269
  yield
144
- rescue *error_classes => _error
145
- if retries < max_retries
146
- @backoff.call(retries)
147
- retries += 1
148
- retry
149
- else
150
- raise
151
- end
270
+ rescue TokenFileReadError, InvalidTokenError
271
+ raise
272
+ rescue *error_classes => _e
273
+ raise unless retries < max_retries
274
+
275
+ @backoff.call(retries)
276
+ retries += 1
277
+ retry
152
278
  end
153
279
  end
154
-
155
280
  end
156
281
  end
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version.
5
10
  # @api private
6
11
  class Condition
7
12
  def initialize(fn:, argv:, assign: nil)
@@ -2,7 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
- # @api private
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version. # @api private
6
10
  class EndpointRule < Rule
7
11
  def initialize(type: 'endpoint', conditions:, endpoint:,
8
12
  documentation: nil)
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version.
5
10
  # @api private
6
11
  class ErrorRule < Rule
7
12
  def initialize(type: 'error', conditions:, error: nil, documentation: nil)
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version.
5
10
  # @api private
6
11
  class Function
7
12
  def initialize(fn:, argv:)
@@ -28,7 +28,11 @@ module Aws
28
28
 
29
29
  val = if (index = parts.first[BRACKET_REGEX, 1])
30
30
  # remove brackets and index from part before indexing
31
- value[parts.first.gsub(BRACKET_REGEX, '')][index.to_i]
31
+ if (base = parts.first.gsub(BRACKET_REGEX, '')) && !base.empty?
32
+ value[base][index.to_i]
33
+ else
34
+ value[index.to_i]
35
+ end
32
36
  else
33
37
  value[parts.first]
34
38
  end
@@ -79,11 +83,11 @@ module Aws
79
83
  return false if value.empty?
80
84
 
81
85
  if allow_sub_domains
82
- labels = value.split('.')
86
+ labels = value.split('.', -1)
83
87
  return labels.all? { |l| valid_host_label?(l) }
84
88
  end
85
89
 
86
- value =~ /\A(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\z/
90
+ !!(value =~ /\A(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\z/)
87
91
  end
88
92
 
89
93
  # AWS
@@ -114,13 +118,17 @@ module Aws
114
118
 
115
119
  # aws.isVirtualHostableS3Bucket(value: string, allowSubDomains: bool) bool
116
120
  def self.aws_virtual_hostable_s3_bucket?(value, allow_sub_domains = false)
117
- !!(value.size < 64 &&
118
- # regular naming rules
119
- value =~ /^[a-z0-9][a-z0-9\-#{'.' if allow_sub_domains}]+[a-z0-9]$/ &&
120
- # not IP address
121
- value !~ /(\d+\.){3}\d+/ &&
122
- # no dash and hyphen together
123
- value !~ /[.-]{2}/)
121
+ return false if value.empty?
122
+
123
+ if allow_sub_domains
124
+ labels = value.split('.', -1)
125
+ return labels.all? { |l| aws_virtual_hostable_s3_bucket?(l) }
126
+ end
127
+
128
+ # must be between 3 and 63 characters long, no uppercase
129
+ value =~ /\A(?!-)[a-z0-9-]{3,63}(?<!-)\z/ &&
130
+ # not an IP address
131
+ value !~ /(\d+\.){3}\d+/
124
132
  end
125
133
  end
126
134
  end
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version.
5
10
  # @api private
6
11
  class Reference
7
12
  def initialize(ref:)
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version.
5
10
  # @api private
6
11
  class Rule
7
12
  # Resolves a value that is a function, reference, or template string.
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version.
5
10
  # @api private
6
11
  class RuleSet
7
12
  def initialize(version:, service_id:, parameters:, rules:)
@@ -1,5 +1,10 @@
1
1
  module Aws
2
2
  module Endpoints
3
+ # This class is deprecated. It is used by the Runtime endpoint
4
+ # resolution approach. It has been replaced by a code generated
5
+ # approach in each service gem. It can be removed in a new
6
+ # major version. It has to exist because
7
+ # old service gems can use a new core version.
3
8
  # @api private
4
9
  class RulesProvider
5
10
  def initialize(rule_set)
@@ -3,6 +3,12 @@
3
3
  module Aws
4
4
  module Endpoints
5
5
  # Does substitutions for templated endpoint strings
6
+
7
+ # This class is deprecated. It is used by the Runtime endpoint
8
+ # resolution approach. It has been replaced by a code generated
9
+ # approach in each service gem. It can be removed in a new
10
+ # major version. It has to exist because
11
+ # old service gems can use a new core version.
6
12
  # @api private
7
13
  module Templater
8
14
  class << self
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Aws
4
4
  module Endpoints
5
+ # This class is deprecated. It is used by the Runtime endpoint
6
+ # resolution approach. It has been replaced by a code generated
7
+ # approach in each service gem. It can be removed in a new
8
+ # major version. It has to exist because
9
+ # old service gems can use a new core version.
5
10
  # @api private
6
11
  class TreeRule
7
12
  def initialize(type: 'tree', conditions:, rules:, documentation: nil)
@@ -4,6 +4,7 @@ require 'ipaddr'
4
4
 
5
5
  module Aws
6
6
  module Endpoints
7
+
7
8
  # @api private
8
9
  class URL
9
10
  def initialize(url)