aws-sdk-core 3.168.4 → 3.224.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +719 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-defaults/default_configuration.rb +5 -6
  5. data/lib/aws-defaults.rb +4 -1
  6. data/lib/aws-sdk-core/arn.rb +1 -3
  7. data/lib/aws-sdk-core/assume_role_credentials.rb +13 -5
  8. data/lib/aws-sdk-core/assume_role_web_identity_credentials.rb +14 -7
  9. data/lib/aws-sdk-core/binary/decode_handler.rb +3 -9
  10. data/lib/aws-sdk-core/binary/encode_handler.rb +1 -1
  11. data/lib/aws-sdk-core/binary/event_builder.rb +34 -37
  12. data/lib/aws-sdk-core/binary/event_stream_decoder.rb +1 -0
  13. data/lib/aws-sdk-core/binary/event_stream_encoder.rb +4 -3
  14. data/lib/aws-sdk-core/cbor/decoder.rb +308 -0
  15. data/lib/aws-sdk-core/cbor/encoder.rb +243 -0
  16. data/lib/aws-sdk-core/cbor.rb +53 -0
  17. data/lib/aws-sdk-core/client_side_monitoring.rb +9 -0
  18. data/lib/aws-sdk-core/client_stubs.rb +33 -55
  19. data/lib/aws-sdk-core/credential_provider.rb +8 -1
  20. data/lib/aws-sdk-core/credential_provider_chain.rb +39 -11
  21. data/lib/aws-sdk-core/credentials.rb +19 -6
  22. data/lib/aws-sdk-core/ec2_metadata.rb +1 -1
  23. data/lib/aws-sdk-core/ecs_credentials.rb +179 -53
  24. data/lib/aws-sdk-core/endpoints/condition.rb +5 -0
  25. data/lib/aws-sdk-core/endpoints/endpoint.rb +3 -1
  26. data/lib/aws-sdk-core/endpoints/endpoint_rule.rb +5 -1
  27. data/lib/aws-sdk-core/endpoints/error_rule.rb +5 -0
  28. data/lib/aws-sdk-core/endpoints/function.rb +5 -0
  29. data/lib/aws-sdk-core/endpoints/matchers.rb +19 -18
  30. data/lib/aws-sdk-core/endpoints/reference.rb +5 -0
  31. data/lib/aws-sdk-core/endpoints/rule.rb +5 -0
  32. data/lib/aws-sdk-core/endpoints/rule_set.rb +5 -0
  33. data/lib/aws-sdk-core/endpoints/rules_provider.rb +5 -0
  34. data/lib/aws-sdk-core/endpoints/templater.rb +6 -0
  35. data/lib/aws-sdk-core/endpoints/tree_rule.rb +5 -0
  36. data/lib/aws-sdk-core/endpoints/url.rb +1 -0
  37. data/lib/aws-sdk-core/endpoints.rb +79 -19
  38. data/lib/aws-sdk-core/error_handler.rb +41 -0
  39. data/lib/aws-sdk-core/errors.rb +14 -5
  40. data/lib/aws-sdk-core/event_emitter.rb +0 -16
  41. data/lib/aws-sdk-core/ini_parser.rb +7 -0
  42. data/lib/aws-sdk-core/instance_profile_credentials.rb +56 -32
  43. data/lib/aws-sdk-core/json/builder.rb +8 -1
  44. data/lib/aws-sdk-core/json/error_handler.rb +30 -14
  45. data/lib/aws-sdk-core/json/handler.rb +13 -6
  46. data/lib/aws-sdk-core/json/json_engine.rb +3 -1
  47. data/lib/aws-sdk-core/json/oj_engine.rb +7 -1
  48. data/lib/aws-sdk-core/json/parser.rb +33 -3
  49. data/lib/aws-sdk-core/json.rb +43 -14
  50. data/lib/aws-sdk-core/log/formatter.rb +6 -0
  51. data/lib/aws-sdk-core/log/param_filter.rb +2 -2
  52. data/lib/aws-sdk-core/log/param_formatter.rb +7 -3
  53. data/lib/aws-sdk-core/log.rb +10 -0
  54. data/lib/aws-sdk-core/lru_cache.rb +75 -0
  55. data/lib/aws-sdk-core/pageable_response.rb +3 -1
  56. data/lib/aws-sdk-core/param_validator.rb +9 -4
  57. data/lib/aws-sdk-core/plugins/bearer_authorization.rb +2 -0
  58. data/lib/aws-sdk-core/plugins/checksum_algorithm.rb +333 -168
  59. data/lib/aws-sdk-core/plugins/client_metrics_plugin.rb +1 -1
  60. data/lib/aws-sdk-core/plugins/client_metrics_send_plugin.rb +14 -2
  61. data/lib/aws-sdk-core/plugins/credentials_configuration.rb +9 -3
  62. data/lib/aws-sdk-core/plugins/endpoint_pattern.rb +40 -32
  63. data/lib/aws-sdk-core/plugins/global_configuration.rb +8 -9
  64. data/lib/aws-sdk-core/plugins/http_checksum.rb +3 -8
  65. data/lib/aws-sdk-core/plugins/invocation_id.rb +1 -11
  66. data/lib/aws-sdk-core/plugins/logging.rb +2 -0
  67. data/lib/aws-sdk-core/plugins/protocols/api_gateway.rb +3 -1
  68. data/lib/aws-sdk-core/plugins/protocols/ec2.rb +2 -24
  69. data/lib/aws-sdk-core/plugins/protocols/json_rpc.rb +6 -8
  70. data/lib/aws-sdk-core/plugins/protocols/query.rb +4 -2
  71. data/lib/aws-sdk-core/plugins/protocols/rest_json.rb +3 -15
  72. data/lib/aws-sdk-core/plugins/protocols/rest_xml.rb +3 -0
  73. data/lib/aws-sdk-core/plugins/protocols/rpc_v2.rb +17 -0
  74. data/lib/aws-sdk-core/plugins/regional_endpoint.rb +162 -37
  75. data/lib/aws-sdk-core/plugins/request_compression.rb +226 -0
  76. data/lib/aws-sdk-core/plugins/retry_errors.rb +12 -3
  77. data/lib/aws-sdk-core/plugins/sign.rb +44 -17
  78. data/lib/aws-sdk-core/plugins/signature_v2.rb +2 -1
  79. data/lib/aws-sdk-core/plugins/signature_v4.rb +2 -1
  80. data/lib/aws-sdk-core/plugins/stub_responses.rb +53 -9
  81. data/lib/aws-sdk-core/plugins/telemetry.rb +75 -0
  82. data/lib/aws-sdk-core/plugins/transfer_encoding.rb +16 -9
  83. data/lib/aws-sdk-core/plugins/user_agent.rb +191 -14
  84. data/lib/aws-sdk-core/plugins.rb +39 -0
  85. data/lib/aws-sdk-core/process_credentials.rb +48 -29
  86. data/lib/aws-sdk-core/query/ec2_handler.rb +27 -0
  87. data/lib/aws-sdk-core/query/ec2_param_builder.rb +5 -7
  88. data/lib/aws-sdk-core/query/handler.rb +4 -4
  89. data/lib/aws-sdk-core/query/param_builder.rb +2 -2
  90. data/lib/aws-sdk-core/query.rb +2 -1
  91. data/lib/aws-sdk-core/refreshing_credentials.rb +12 -12
  92. data/lib/aws-sdk-core/resources.rb +8 -0
  93. data/lib/aws-sdk-core/rest/content_type_handler.rb +60 -0
  94. data/lib/aws-sdk-core/rest/handler.rb +3 -4
  95. data/lib/aws-sdk-core/rest/request/body.rb +32 -5
  96. data/lib/aws-sdk-core/rest/request/endpoint.rb +24 -4
  97. data/lib/aws-sdk-core/rest/request/headers.rb +15 -7
  98. data/lib/aws-sdk-core/rest/request/querystring_builder.rb +62 -36
  99. data/lib/aws-sdk-core/rest/response/body.rb +15 -1
  100. data/lib/aws-sdk-core/rest/response/header_list_parser.rb +79 -0
  101. data/lib/aws-sdk-core/rest/response/headers.rb +8 -3
  102. data/lib/aws-sdk-core/rest.rb +1 -0
  103. data/lib/aws-sdk-core/rpc_v2/builder.rb +62 -0
  104. data/lib/aws-sdk-core/rpc_v2/cbor_engine.rb +18 -0
  105. data/lib/aws-sdk-core/rpc_v2/content_type_handler.rb +47 -0
  106. data/lib/aws-sdk-core/rpc_v2/error_handler.rb +85 -0
  107. data/lib/aws-sdk-core/rpc_v2/handler.rb +79 -0
  108. data/lib/aws-sdk-core/rpc_v2/parser.rb +90 -0
  109. data/lib/aws-sdk-core/rpc_v2.rb +69 -0
  110. data/lib/aws-sdk-core/shared_config.rb +125 -39
  111. data/lib/aws-sdk-core/shared_credentials.rb +1 -7
  112. data/lib/aws-sdk-core/sso_credentials.rb +5 -2
  113. data/lib/aws-sdk-core/stubbing/protocols/ec2.rb +12 -11
  114. data/lib/aws-sdk-core/stubbing/protocols/json.rb +11 -10
  115. data/lib/aws-sdk-core/stubbing/protocols/query.rb +7 -6
  116. data/lib/aws-sdk-core/stubbing/protocols/rest.rb +2 -1
  117. data/lib/aws-sdk-core/stubbing/protocols/rest_json.rb +9 -8
  118. data/lib/aws-sdk-core/stubbing/protocols/rest_xml.rb +6 -5
  119. data/lib/aws-sdk-core/stubbing/protocols/rpc_v2.rb +39 -0
  120. data/lib/aws-sdk-core/stubbing/stub_data.rb +11 -0
  121. data/lib/aws-sdk-core/stubbing.rb +22 -0
  122. data/lib/aws-sdk-core/telemetry/base.rb +177 -0
  123. data/lib/aws-sdk-core/telemetry/no_op.rb +70 -0
  124. data/lib/aws-sdk-core/telemetry/otel.rb +235 -0
  125. data/lib/aws-sdk-core/telemetry/span_kind.rb +22 -0
  126. data/lib/aws-sdk-core/telemetry/span_status.rb +59 -0
  127. data/lib/aws-sdk-core/telemetry.rb +78 -0
  128. data/lib/aws-sdk-core/util.rb +39 -0
  129. data/lib/aws-sdk-core/waiters/poller.rb +12 -5
  130. data/lib/aws-sdk-core/xml/builder.rb +17 -9
  131. data/lib/aws-sdk-core/xml/error_handler.rb +32 -42
  132. data/lib/aws-sdk-core/xml/parser/frame.rb +4 -20
  133. data/lib/aws-sdk-core/xml/parser/{engines/oga.rb → oga_engine.rb} +2 -0
  134. data/lib/aws-sdk-core/xml/parser/stack.rb +2 -0
  135. data/lib/aws-sdk-core/xml/parser.rb +2 -6
  136. data/lib/aws-sdk-core.rb +82 -107
  137. data/lib/aws-sdk-sso/client.rb +185 -79
  138. data/lib/aws-sdk-sso/client_api.rb +7 -0
  139. data/lib/aws-sdk-sso/endpoint_parameters.rb +9 -6
  140. data/lib/aws-sdk-sso/endpoint_provider.rb +37 -96
  141. data/lib/aws-sdk-sso/endpoints.rb +3 -54
  142. data/lib/aws-sdk-sso/plugins/endpoints.rb +23 -22
  143. data/lib/aws-sdk-sso/types.rb +1 -0
  144. data/lib/aws-sdk-sso.rb +15 -11
  145. data/lib/aws-sdk-ssooidc/client.rb +592 -112
  146. data/lib/aws-sdk-ssooidc/client_api.rb +89 -1
  147. data/lib/aws-sdk-ssooidc/endpoint_parameters.rb +9 -6
  148. data/lib/aws-sdk-ssooidc/endpoint_provider.rb +37 -95
  149. data/lib/aws-sdk-ssooidc/endpoints.rb +3 -40
  150. data/lib/aws-sdk-ssooidc/errors.rb +52 -0
  151. data/lib/aws-sdk-ssooidc/plugins/endpoints.rb +23 -20
  152. data/lib/aws-sdk-ssooidc/types.rb +407 -53
  153. data/lib/aws-sdk-ssooidc.rb +15 -11
  154. data/lib/aws-sdk-sts/client.rb +516 -238
  155. data/lib/aws-sdk-sts/client_api.rb +48 -11
  156. data/lib/aws-sdk-sts/customizations.rb +5 -1
  157. data/lib/aws-sdk-sts/endpoint_parameters.rb +10 -9
  158. data/lib/aws-sdk-sts/endpoint_provider.rb +91 -213
  159. data/lib/aws-sdk-sts/endpoints.rb +3 -118
  160. data/lib/aws-sdk-sts/errors.rb +16 -0
  161. data/lib/aws-sdk-sts/plugins/endpoints.rb +23 -30
  162. data/lib/aws-sdk-sts/presigner.rb +1 -1
  163. data/lib/aws-sdk-sts/types.rb +217 -36
  164. data/lib/aws-sdk-sts.rb +15 -11
  165. data/lib/seahorse/client/async_base.rb +4 -5
  166. data/lib/seahorse/client/async_response.rb +19 -0
  167. data/lib/seahorse/client/base.rb +18 -21
  168. data/lib/seahorse/client/configuration.rb +0 -4
  169. data/lib/seahorse/client/h2/connection.rb +25 -31
  170. data/lib/seahorse/client/h2/handler.rb +14 -3
  171. data/lib/seahorse/client/handler.rb +1 -1
  172. data/lib/seahorse/client/http/response.rb +1 -1
  173. data/lib/seahorse/client/net_http/connection_pool.rb +13 -11
  174. data/lib/seahorse/client/net_http/handler.rb +21 -9
  175. data/lib/seahorse/client/net_http/patches.rb +1 -4
  176. data/lib/seahorse/client/networking_error.rb +1 -1
  177. data/lib/seahorse/client/plugin.rb +9 -0
  178. data/lib/seahorse/client/plugins/endpoint.rb +0 -1
  179. data/lib/seahorse/client/plugins/h2.rb +4 -4
  180. data/lib/seahorse/client/plugins/net_http.rb +57 -16
  181. data/lib/seahorse/client/plugins/request_callback.rb +31 -0
  182. data/lib/seahorse/client/request_context.rb +8 -1
  183. data/lib/seahorse/client/response.rb +8 -0
  184. data/lib/seahorse/model/operation.rb +3 -0
  185. data/lib/seahorse/model/shapes.rb +2 -2
  186. data/sig/aws-sdk-core/async_client_stubs.rbs +21 -0
  187. data/sig/aws-sdk-core/client_stubs.rbs +10 -0
  188. data/sig/aws-sdk-core/errors.rbs +22 -0
  189. data/sig/aws-sdk-core/resources/collection.rbs +21 -0
  190. data/sig/aws-sdk-core/structure.rbs +4 -0
  191. data/sig/aws-sdk-core/telemetry/base.rbs +46 -0
  192. data/sig/aws-sdk-core/telemetry/otel.rbs +22 -0
  193. data/sig/aws-sdk-core/telemetry/span_kind.rbs +15 -0
  194. data/sig/aws-sdk-core/telemetry/span_status.rbs +24 -0
  195. data/sig/aws-sdk-core/waiters/errors.rbs +20 -0
  196. data/sig/aws-sdk-core.rbs +7 -0
  197. data/sig/seahorse/client/async_base.rbs +18 -0
  198. data/sig/seahorse/client/base.rbs +25 -0
  199. data/sig/seahorse/client/handler_builder.rbs +16 -0
  200. data/sig/seahorse/client/response.rbs +61 -0
  201. metadata +92 -23
  202. /data/lib/aws-sdk-core/xml/parser/{engines/libxml.rb → libxml_engine.rb} +0 -0
  203. /data/lib/aws-sdk-core/xml/parser/{engines/nokogiri.rb → nokogiri_engine.rb} +0 -0
  204. /data/lib/aws-sdk-core/xml/parser/{engines/ox.rb → ox_engine.rb} +0 -0
  205. /data/lib/aws-sdk-core/xml/parser/{engines/rexml.rb → rexml_engine.rb} +0 -0
@@ -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,22 +64,20 @@ 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]
69
78
  @backoff = backoff(options[:backoff])
70
79
  @async_refresh = false
80
+ @metrics = ['CREDENTIALS_HTTP']
71
81
  super
72
82
  end
73
83
 
@@ -77,11 +87,95 @@ module Aws
77
87
 
78
88
  private
79
89
 
90
+ def initialize_uri(options, credential_path, endpoint)
91
+ if credential_path
92
+ initialize_relative_uri(options, credential_path)
93
+ # Use FULL_URI/endpoint only if RELATIVE_URI/path is not set
94
+ elsif endpoint
95
+ initialize_full_uri(endpoint)
96
+ else
97
+ raise ArgumentError,
98
+ 'Cannot instantiate an ECS Credential Provider '\
99
+ 'without a credential path or endpoint.'
100
+ end
101
+ end
102
+
103
+ def initialize_relative_uri(options, path)
104
+ @host = options[:ip_address] || '169.254.170.2'
105
+ @port = options[:port] || 80
106
+ @scheme = 'http'
107
+ @credential_path = path
108
+ end
109
+
110
+ def initialize_full_uri(endpoint)
111
+ uri = URI.parse(endpoint)
112
+ validate_full_uri_scheme!(uri)
113
+ validate_full_uri!(uri)
114
+ @host = uri.hostname
115
+ @port = uri.port
116
+ @scheme = uri.scheme
117
+ @credential_path = uri.request_uri
118
+ end
119
+
120
+ def validate_full_uri_scheme!(full_uri)
121
+ return if full_uri.is_a?(URI::HTTP) || full_uri.is_a?(URI::HTTPS)
122
+
123
+ raise ArgumentError, "'#{full_uri}' must be a valid HTTP or HTTPS URI"
124
+ end
125
+
126
+ # Validate that the full URI is using a loopback address if scheme is http.
127
+ def validate_full_uri!(full_uri)
128
+ return unless full_uri.scheme == 'http'
129
+
130
+ begin
131
+ return if valid_ip_address?(IPAddr.new(full_uri.host))
132
+ rescue IPAddr::InvalidAddressError
133
+ addresses = Resolv.getaddresses(full_uri.host)
134
+ return if addresses.all? { |addr| valid_ip_address?(IPAddr.new(addr)) }
135
+ end
136
+
137
+ raise ArgumentError,
138
+ 'AWS_CONTAINER_CREDENTIALS_FULL_URI must use a local loopback '\
139
+ 'or an ECS or EKS link-local address when using the http scheme.'
140
+ end
141
+
142
+ def valid_ip_address?(ip_address)
143
+ ip_loopback?(ip_address) || ecs_or_eks_ip?(ip_address)
144
+ end
145
+
146
+ # loopback? method is available in Ruby 2.5+
147
+ # Replicate the logic here.
148
+ # loopback (IPv4 127.0.0.0/8, IPv6 ::1/128)
149
+ def ip_loopback?(ip_address)
150
+ case ip_address.family
151
+ when Socket::AF_INET
152
+ ip_address & 0xff000000 == 0x7f000000
153
+ when Socket::AF_INET6
154
+ ip_address == 1
155
+ else
156
+ false
157
+ end
158
+ end
159
+
160
+ # Verify that the IP address is a link-local address from ECS or EKS.
161
+ # ECS container host (IPv4 `169.254.170.2`)
162
+ # EKS container host (IPv4 `169.254.170.23`, IPv6 `fd00:ec2::23`)
163
+ def ecs_or_eks_ip?(ip_address)
164
+ case ip_address.family
165
+ when Socket::AF_INET
166
+ [0xa9feaa02, 0xa9feaa17].include?(ip_address)
167
+ when Socket::AF_INET6
168
+ ip_address == 0xfd00_0ec2_0000_0000_0000_0000_0000_0023
169
+ else
170
+ false
171
+ end
172
+ end
173
+
80
174
  def backoff(backoff)
81
175
  case backoff
82
176
  when Proc then backoff
83
- when Numeric then lambda { |_| sleep(backoff) }
84
- else lambda { |num_failures| Kernel.sleep(1.2 ** num_failures) }
177
+ when Numeric then ->(_) { sleep(backoff) }
178
+ else ->(num_failures) { Kernel.sleep(1.2**num_failures) }
85
179
  end
86
180
  end
87
181
 
@@ -89,68 +183,100 @@ module Aws
89
183
  # Retry loading credentials up to 3 times is the instance metadata
90
184
  # service is responding but is returning invalid JSON documents
91
185
  # 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
186
+
187
+ retry_errors([Aws::Json::ParseError, StandardError], max_retries: 3) do
188
+ c = Aws::Json.load(get_credentials.to_s)
189
+ @credentials = Credentials.new(
190
+ c['AccessKeyId'],
191
+ c['SecretAccessKey'],
192
+ c['Token']
193
+ )
194
+ @expiration = c['Expiration'] ? Time.iso8601(c['Expiration']) : nil
104
195
  end
196
+ rescue Aws::Json::ParseError
197
+ raise Aws::Errors::MetadataParserError
105
198
  end
106
199
 
107
200
  def get_credentials
108
201
  # Retry loading credentials a configurable number of times if
109
202
  # 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
203
+
204
+ retry_errors(NETWORK_ERRORS, max_retries: @retries) do
205
+ open_connection do |conn|
206
+ http_get(conn, @credential_path)
115
207
  end
116
- rescue
117
- '{}'
118
208
  end
209
+ rescue TokenFileReadError, InvalidTokenError
210
+ raise
211
+ rescue StandardError => e
212
+ warn("Error retrieving ECS Credentials: #{e.message}")
213
+ '{}'
214
+ end
215
+
216
+ def fetch_authorization_token
217
+ if (path = ENV['AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE'])
218
+ fetch_authorization_token_file(path)
219
+ elsif (token = ENV['AWS_CONTAINER_AUTHORIZATION_TOKEN'])
220
+ token
221
+ end
222
+ end
223
+
224
+ def fetch_authorization_token_file(path)
225
+ File.read(path).strip
226
+ rescue Errno::ENOENT
227
+ raise TokenFileReadError,
228
+ 'AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE is set '\
229
+ "but the file doesn't exist: #{path}"
230
+ end
231
+
232
+ def validate_authorization_token!(token)
233
+ return unless token.include?("\r\n")
234
+
235
+ raise InvalidTokenError,
236
+ 'Invalid Authorization token: token contains '\
237
+ 'a newline and carriage return character.'
119
238
  end
120
239
 
121
240
  def open_connection
122
- http = Net::HTTP.new(@ip_address, @port, nil)
241
+ http = Net::HTTP.new(@host, @port, nil)
123
242
  http.open_timeout = @http_open_timeout
124
243
  http.read_timeout = @http_read_timeout
125
244
  http.set_debug_output(@http_debug_output) if @http_debug_output
245
+ http.use_ssl = @scheme == 'https'
126
246
  http.start
127
247
  yield(http).tap { http.finish }
128
248
  end
129
249
 
130
250
  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
251
+ request = Net::HTTP::Get.new(path)
252
+ set_authorization_token(request)
253
+ response = connection.request(request)
254
+ raise Non200Response unless response.code.to_i == 200
255
+
256
+ response.body
257
+ end
258
+
259
+ def set_authorization_token(request)
260
+ if (authorization_token = fetch_authorization_token)
261
+ validate_authorization_token!(authorization_token)
262
+ request['Authorization'] = authorization_token
136
263
  end
137
264
  end
138
265
 
139
- def retry_errors(error_classes, options = {}, &block)
266
+ def retry_errors(error_classes, options = {})
140
267
  max_retries = options[:max_retries]
141
268
  retries = 0
142
269
  begin
143
270
  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
271
+ rescue TokenFileReadError, InvalidTokenError
272
+ raise
273
+ rescue *error_classes => _e
274
+ raise unless retries < max_retries
275
+
276
+ @backoff.call(retries)
277
+ retries += 1
278
+ retry
152
279
  end
153
280
  end
154
-
155
281
  end
156
282
  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)
@@ -3,15 +3,17 @@
3
3
  module Aws
4
4
  module Endpoints
5
5
  class Endpoint
6
- def initialize(url:, properties: {}, headers: {})
6
+ def initialize(url:, properties: {}, headers: {}, metadata: {})
7
7
  @url = url
8
8
  @properties = properties
9
9
  @headers = headers
10
+ @metadata = metadata
10
11
  end
11
12
 
12
13
  attr_reader :url
13
14
  attr_reader :properties
14
15
  attr_reader :headers
16
+ attr_reader :metadata
15
17
  end
16
18
  end
17
19
  end
@@ -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,25 +83,18 @@ 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
90
94
 
91
95
  # aws.partition(value: string) Option<Partition>
92
96
  def self.aws_partition(value)
93
- partition =
94
- Aws::Partitions.find { |p| p.region?(value) } ||
95
- Aws::Partitions.find { |p| value.match(p.region_regex) } ||
96
- Aws::Partitions.find { |p| p.name == 'aws' }
97
-
98
- return nil unless partition
99
-
100
- partition.metadata
97
+ Aws::Partitions::Metadata.partition(value)
101
98
  end
102
99
 
103
100
  # aws.parseArn(value: string) Option<ARN>
@@ -114,13 +111,17 @@ module Aws
114
111
 
115
112
  # aws.isVirtualHostableS3Bucket(value: string, allowSubDomains: bool) bool
116
113
  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}/)
114
+ return false if value.empty?
115
+
116
+ if allow_sub_domains
117
+ labels = value.split('.', -1)
118
+ return labels.all? { |l| aws_virtual_hostable_s3_bucket?(l) }
119
+ end
120
+
121
+ # must be between 3 and 63 characters long, no uppercase
122
+ value =~ /\A(?!-)[a-z0-9-]{3,63}(?<!-)\z/ &&
123
+ # not an IP address
124
+ value !~ /(\d+\.){3}\d+/
124
125
  end
125
126
  end
126
127
  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)
@@ -14,9 +14,18 @@ require_relative 'endpoints/templater'
14
14
  require_relative 'endpoints/tree_rule'
15
15
  require_relative 'endpoints/url'
16
16
 
17
+ require 'aws-sigv4'
18
+
17
19
  module Aws
18
20
  # @api private
19
21
  module Endpoints
22
+ SUPPORTED_AUTH_TRAITS = %w[
23
+ aws.auth#sigv4
24
+ aws.auth#sigv4a
25
+ smithy.api#httpBearerAuth
26
+ smithy.api#noAuth
27
+ ].freeze
28
+
20
29
  class << self
21
30
  def resolve_auth_scheme(context, endpoint)
22
31
  if endpoint && (auth_schemes = endpoint.properties['authSchemes'])
@@ -33,13 +42,80 @@ module Aws
33
42
 
34
43
  private
35
44
 
45
+ def merge_signing_defaults(auth_scheme, config)
46
+ if %w[sigv4 sigv4a sigv4-s3express].include?(auth_scheme['name'])
47
+ auth_scheme['signingName'] ||= sigv4_name(config)
48
+
49
+ # back fill disableNormalizePath for S3 until it gets correctly set in the rules
50
+ if auth_scheme['signingName'] == 's3' &&
51
+ !auth_scheme.include?('disableNormalizePath') &&
52
+ auth_scheme.include?('disableDoubleEncoding')
53
+ auth_scheme['disableNormalizePath'] = auth_scheme['disableDoubleEncoding']
54
+ end
55
+ if auth_scheme['name'] == 'sigv4a'
56
+ # config option supersedes endpoint properties
57
+ auth_scheme['signingRegionSet'] =
58
+ config.sigv4a_signing_region_set || auth_scheme['signingRegionSet'] || [config.region]
59
+ else
60
+ auth_scheme['signingRegion'] ||= config.region
61
+ end
62
+ end
63
+ auth_scheme
64
+ end
65
+
66
+ def sigv4_name(config)
67
+ config.api.metadata['signingName'] ||
68
+ config.api.metadata['endpointPrefix']
69
+ end
70
+
36
71
  def default_auth_scheme(context)
37
- case default_api_authtype(context)
72
+ if (auth_list = default_api_auth(context))
73
+ auth = auth_list.find { |a| SUPPORTED_AUTH_TRAITS.include?(a) }
74
+ case auth
75
+ when 'aws.auth#sigv4', 'aws.auth#sigv4a'
76
+ auth_scheme = { 'name' => auth.split('#').last }
77
+ if s3_or_s3v4_signature_version?(context)
78
+ auth_scheme = auth_scheme.merge(
79
+ 'disableDoubleEncoding' => true,
80
+ 'disableNormalizePath' => true
81
+ )
82
+ end
83
+ merge_signing_defaults(auth_scheme, context.config)
84
+ when 'smithy.api#httpBearerAuth'
85
+ { 'name' => 'bearer' }
86
+ when 'smithy.api#noAuth'
87
+ { 'name' => 'none' }
88
+ else
89
+ raise 'No supported auth trait for this endpoint.'
90
+ end
91
+ else
92
+ legacy_default_auth_scheme(context)
93
+ end
94
+ end
95
+
96
+ def default_api_auth(context)
97
+ context.config.api.operation(context.operation_name)['auth'] ||
98
+ context.config.api.metadata['auth']
99
+ end
100
+
101
+ def s3_or_s3v4_signature_version?(context)
102
+ %w[s3 s3v4].include?(context.config.api.metadata['signatureVersion'])
103
+ end
104
+
105
+ # Legacy auth resolution - looks for deprecated signatureVersion
106
+ # and authType traits.
107
+
108
+ def legacy_default_auth_scheme(context)
109
+ case legacy_default_api_authtype(context)
38
110
  when 'v4', 'v4-unsigned-body'
39
111
  auth_scheme = { 'name' => 'sigv4' }
40
112
  merge_signing_defaults(auth_scheme, context.config)
41
113
  when 's3', 's3v4'
42
- auth_scheme = { 'name' => 'sigv4', 'disableDoubleEncoding' => true }
114
+ auth_scheme = {
115
+ 'name' => 'sigv4',
116
+ 'disableDoubleEncoding' => true,
117
+ 'disableNormalizePath' => true
118
+ }
43
119
  merge_signing_defaults(auth_scheme, context.config)
44
120
  when 'bearer'
45
121
  { 'name' => 'bearer' }
@@ -48,27 +124,11 @@ module Aws
48
124
  end
49
125
  end
50
126
 
51
- def merge_signing_defaults(auth_scheme, config)
52
- if %w[sigv4 sigv4a].include?(auth_scheme['name'])
53
- auth_scheme['signingName'] ||= sigv4_name(config)
54
- if auth_scheme['name'] == 'sigv4a'
55
- auth_scheme['signingRegionSet'] ||= ['*']
56
- else
57
- auth_scheme['signingRegion'] ||= config.region
58
- end
59
- end
60
- auth_scheme
61
- end
62
-
63
- def default_api_authtype(context)
127
+ def legacy_default_api_authtype(context)
64
128
  context.config.api.operation(context.operation_name)['authtype'] ||
65
129
  context.config.api.metadata['signatureVersion']
66
130
  end
67
131
 
68
- def sigv4_name(config)
69
- config.api.metadata['signingName'] ||
70
- config.api.metadata['endpointPrefix']
71
- end
72
132
  end
73
133
  end
74
134
  end