aws-sdk-core 3.125.6 → 3.188.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +533 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-defaults/default_configuration.rb +4 -4
  5. data/lib/aws-sdk-core/arn.rb +13 -0
  6. data/lib/aws-sdk-core/assume_role_credentials.rb +21 -7
  7. data/lib/aws-sdk-core/assume_role_web_identity_credentials.rb +14 -10
  8. data/lib/aws-sdk-core/binary/encode_handler.rb +12 -1
  9. data/lib/aws-sdk-core/client_stubs.rb +5 -1
  10. data/lib/aws-sdk-core/credential_provider.rb +3 -0
  11. data/lib/aws-sdk-core/credential_provider_chain.rb +8 -5
  12. data/lib/aws-sdk-core/ec2_metadata.rb +3 -2
  13. data/lib/aws-sdk-core/ecs_credentials.rb +186 -52
  14. data/lib/aws-sdk-core/endpoints/condition.rb +41 -0
  15. data/lib/aws-sdk-core/endpoints/endpoint.rb +17 -0
  16. data/lib/aws-sdk-core/endpoints/endpoint_rule.rb +75 -0
  17. data/lib/aws-sdk-core/endpoints/error_rule.rb +42 -0
  18. data/lib/aws-sdk-core/endpoints/function.rb +80 -0
  19. data/lib/aws-sdk-core/endpoints/matchers.rb +131 -0
  20. data/lib/aws-sdk-core/endpoints/reference.rb +31 -0
  21. data/lib/aws-sdk-core/endpoints/rule.rb +25 -0
  22. data/lib/aws-sdk-core/endpoints/rule_set.rb +52 -0
  23. data/lib/aws-sdk-core/endpoints/rules_provider.rb +37 -0
  24. data/lib/aws-sdk-core/endpoints/templater.rb +58 -0
  25. data/lib/aws-sdk-core/endpoints/tree_rule.rb +45 -0
  26. data/lib/aws-sdk-core/endpoints/url.rb +60 -0
  27. data/lib/aws-sdk-core/endpoints.rb +78 -0
  28. data/lib/aws-sdk-core/errors.rb +14 -1
  29. data/lib/aws-sdk-core/ini_parser.rb +7 -0
  30. data/lib/aws-sdk-core/instance_profile_credentials.rb +124 -31
  31. data/lib/aws-sdk-core/json/error_handler.rb +20 -1
  32. data/lib/aws-sdk-core/json/handler.rb +8 -1
  33. data/lib/aws-sdk-core/json/parser.rb +27 -2
  34. data/lib/aws-sdk-core/log/formatter.rb +6 -0
  35. data/lib/aws-sdk-core/pageable_response.rb +81 -32
  36. data/lib/aws-sdk-core/param_validator.rb +2 -2
  37. data/lib/aws-sdk-core/plugins/bearer_authorization.rb +67 -0
  38. data/lib/aws-sdk-core/plugins/checksum_algorithm.rb +340 -0
  39. data/lib/aws-sdk-core/plugins/credentials_configuration.rb +24 -0
  40. data/lib/aws-sdk-core/plugins/endpoint_discovery.rb +6 -2
  41. data/lib/aws-sdk-core/plugins/http_checksum.rb +8 -1
  42. data/lib/aws-sdk-core/plugins/jsonvalue_converter.rb +34 -6
  43. data/lib/aws-sdk-core/plugins/recursion_detection.rb +38 -0
  44. data/lib/aws-sdk-core/plugins/regional_endpoint.rb +111 -30
  45. data/lib/aws-sdk-core/plugins/request_compression.rb +217 -0
  46. data/lib/aws-sdk-core/plugins/response_paging.rb +1 -1
  47. data/lib/aws-sdk-core/plugins/retries/error_inspector.rb +3 -2
  48. data/lib/aws-sdk-core/plugins/retry_errors.rb +12 -2
  49. data/lib/aws-sdk-core/plugins/sign.rb +201 -0
  50. data/lib/aws-sdk-core/plugins/signature_v2.rb +1 -0
  51. data/lib/aws-sdk-core/plugins/signature_v4.rb +15 -8
  52. data/lib/aws-sdk-core/plugins/stub_responses.rb +5 -1
  53. data/lib/aws-sdk-core/plugins/user_agent.rb +117 -14
  54. data/lib/aws-sdk-core/process_credentials.rb +9 -11
  55. data/lib/aws-sdk-core/refreshing_credentials.rb +41 -16
  56. data/lib/aws-sdk-core/refreshing_token.rb +71 -0
  57. data/lib/aws-sdk-core/rest/handler.rb +1 -1
  58. data/lib/aws-sdk-core/rest/request/headers.rb +2 -6
  59. data/lib/aws-sdk-core/rest/request/querystring_builder.rb +43 -29
  60. data/lib/aws-sdk-core/shared_config.rb +107 -12
  61. data/lib/aws-sdk-core/sso_credentials.rb +91 -50
  62. data/lib/aws-sdk-core/sso_token_provider.rb +135 -0
  63. data/lib/aws-sdk-core/static_token_provider.rb +14 -0
  64. data/lib/aws-sdk-core/structure.rb +6 -4
  65. data/lib/aws-sdk-core/stubbing/stub_data.rb +11 -0
  66. data/lib/aws-sdk-core/token.rb +31 -0
  67. data/lib/aws-sdk-core/token_provider.rb +15 -0
  68. data/lib/aws-sdk-core/token_provider_chain.rb +51 -0
  69. data/lib/aws-sdk-core/waiters/poller.rb +3 -1
  70. data/lib/aws-sdk-core/xml/error_handler.rb +7 -0
  71. data/lib/aws-sdk-core/xml/parser/engines/oga.rb +2 -0
  72. data/lib/aws-sdk-core.rb +14 -0
  73. data/lib/aws-sdk-sso/client.rb +75 -11
  74. data/lib/aws-sdk-sso/endpoint_parameters.rb +66 -0
  75. data/lib/aws-sdk-sso/endpoint_provider.rb +57 -0
  76. data/lib/aws-sdk-sso/endpoints.rb +72 -0
  77. data/lib/aws-sdk-sso/plugins/endpoints.rb +76 -0
  78. data/lib/aws-sdk-sso/types.rb +8 -43
  79. data/lib/aws-sdk-sso.rb +5 -1
  80. data/lib/aws-sdk-ssooidc/client.rb +935 -0
  81. data/lib/aws-sdk-ssooidc/client_api.rb +271 -0
  82. data/lib/aws-sdk-ssooidc/customizations.rb +1 -0
  83. data/lib/aws-sdk-ssooidc/endpoint_parameters.rb +66 -0
  84. data/lib/aws-sdk-ssooidc/endpoint_provider.rb +57 -0
  85. data/lib/aws-sdk-ssooidc/endpoints.rb +72 -0
  86. data/lib/aws-sdk-ssooidc/errors.rb +321 -0
  87. data/lib/aws-sdk-ssooidc/plugins/endpoints.rb +76 -0
  88. data/lib/aws-sdk-ssooidc/resource.rb +26 -0
  89. data/lib/aws-sdk-ssooidc/types.rb +755 -0
  90. data/lib/aws-sdk-ssooidc.rb +59 -0
  91. data/lib/aws-sdk-sts/client.rb +330 -262
  92. data/lib/aws-sdk-sts/client_api.rb +12 -1
  93. data/lib/aws-sdk-sts/endpoint_parameters.rb +78 -0
  94. data/lib/aws-sdk-sts/endpoint_provider.rb +112 -0
  95. data/lib/aws-sdk-sts/endpoints.rb +136 -0
  96. data/lib/aws-sdk-sts/plugins/endpoints.rb +84 -0
  97. data/lib/aws-sdk-sts/presigner.rb +14 -16
  98. data/lib/aws-sdk-sts/types.rb +135 -204
  99. data/lib/aws-sdk-sts.rb +5 -1
  100. data/lib/seahorse/client/async_base.rb +0 -1
  101. data/lib/seahorse/client/configuration.rb +1 -5
  102. data/lib/seahorse/client/h2/connection.rb +12 -11
  103. data/lib/seahorse/client/net_http/patches.rb +1 -4
  104. data/lib/seahorse/client/plugins/h2.rb +3 -3
  105. data/lib/seahorse/client/plugins/request_callback.rb +40 -9
  106. data/lib/seahorse/client/response.rb +6 -0
  107. data/lib/seahorse/model/operation.rb +6 -0
  108. data/lib/seahorse/util.rb +4 -0
  109. metadata +59 -9
@@ -5,9 +5,8 @@ require 'securerandom'
5
5
  require 'base64'
6
6
 
7
7
  module Aws
8
-
9
- # An auto-refreshing credential provider that works by assuming
10
- # a role via {Aws::STS::Client#assume_role_with_web_identity}.
8
+ # An auto-refreshing credential provider that assumes a role via
9
+ # {Aws::STS::Client#assume_role_with_web_identity}.
11
10
  #
12
11
  # role_credentials = Aws::AssumeRoleWebIdentityCredentials.new(
13
12
  # client: Aws::STS::Client.new(...),
@@ -16,12 +15,12 @@ module Aws
16
15
  # role_session_name: "session-name"
17
16
  # ...
18
17
  # )
19
- # For full list of parameters accepted
20
- # @see Aws::STS::Client#assume_role_with_web_identity
18
+ # ec2 = Aws::EC2::Client.new(credentials: role_credentials)
21
19
  #
20
+ # If you omit `:client` option, a new {Aws::STS::Client} object will be
21
+ # constructed with additional options that were provided.
22
22
  #
23
- # If you omit `:client` option, a new {STS::Client} object will be
24
- # constructed.
23
+ # @see Aws::STS::Client#assume_role_with_web_identity
25
24
  class AssumeRoleWebIdentityCredentials
26
25
 
27
26
  include CredentialProvider
@@ -39,14 +38,20 @@ module Aws
39
38
  # encoded UUID is generated as the session name
40
39
  #
41
40
  # @option options [STS::Client] :client
41
+ #
42
+ # @option options [Callable] before_refresh Proc called before
43
+ # credentials are refreshed. `before_refresh` is called
44
+ # with an instance of this object when
45
+ # AWS credentials are required and need to be refreshed.
42
46
  def initialize(options = {})
43
47
  client_opts = {}
44
48
  @assume_role_web_identity_params = {}
45
49
  @token_file = options.delete(:web_identity_token_file)
50
+ @async_refresh = true
46
51
  options.each_pair do |key, value|
47
52
  if self.class.assume_role_web_identity_options.include?(key)
48
53
  @assume_role_web_identity_params[key] = value
49
- else
54
+ elsif !CLIENT_EXCLUDE_OPTIONS.include?(key)
50
55
  client_opts[key] = value
51
56
  end
52
57
  end
@@ -94,11 +99,10 @@ module Aws
94
99
  # @api private
95
100
  def assume_role_web_identity_options
96
101
  @arwio ||= begin
97
- input = STS::Client.api.operation(:assume_role_with_web_identity).input
102
+ input = Aws::STS::Client.api.operation(:assume_role_with_web_identity).input
98
103
  Set.new(input.shape.member_names)
99
104
  end
100
105
  end
101
-
102
106
  end
103
107
  end
104
108
  end
@@ -13,7 +13,7 @@ module Aws
13
13
  context.config.api.metadata['protocol'],
14
14
  eventstream_member,
15
15
  context.operation.input,
16
- context.config.sigv4_signer
16
+ signer_for(context)
17
17
  )
18
18
  context[:input_event_emitter] = input_es_handler.event_emitter
19
19
  end
@@ -22,6 +22,17 @@ module Aws
22
22
 
23
23
  private
24
24
 
25
+ def signer_for(context)
26
+ # New endpoint/signing logic, use the auth scheme to make a signer
27
+ if context[:auth_scheme]
28
+ Aws::Plugins::Sign.signer_for(context[:auth_scheme], context.config)
29
+ else
30
+ # Previous implementation always assumed sigv4_signer from config.
31
+ # Relies only on sigv4 signing (and plugin) for event stream services
32
+ context.config.sigv4_signer
33
+ end
34
+ end
35
+
25
36
  def eventstream_input?(ctx)
26
37
  ctx.operation.input.shape.members.each do |_, ref|
27
38
  return ref if ref.eventstream
@@ -262,13 +262,17 @@ module Aws
262
262
  end
263
263
 
264
264
  def convert_stub(operation_name, stub)
265
- case stub
265
+ stub = case stub
266
266
  when Proc then stub
267
267
  when Exception, Class then { error: stub }
268
268
  when String then service_error_stub(stub)
269
269
  when Hash then http_response_stub(operation_name, stub)
270
270
  else { data: stub }
271
271
  end
272
+ if Hash === stub
273
+ stub[:mutex] = Mutex.new
274
+ end
275
+ stub
272
276
  end
273
277
 
274
278
  def service_error_stub(error_code)
@@ -6,6 +6,9 @@ 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
14
  !!credentials && credentials.set?
@@ -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
@@ -136,8 +136,9 @@ module Aws
136
136
 
137
137
  def fetch_token
138
138
  open_connection do |conn|
139
+ created_time = Time.now
139
140
  token_value, token_ttl = http_put(conn, @token_ttl)
140
- @token = Token.new(value: token_value, ttl: token_ttl)
141
+ @token = Token.new(value: token_value, ttl: token_ttl, created_time: created_time)
141
142
  end
142
143
  end
143
144
 
@@ -222,7 +223,7 @@ module Aws
222
223
  def initialize(options = {})
223
224
  @ttl = options[:ttl]
224
225
  @value = options[:value]
225
- @created_time = Time.now
226
+ @created_time = options[:created_time] || Time.now
226
227
  end
227
228
 
228
229
  # [String] Returns the token value.
@@ -2,16 +2,27 @@
2
2
 
3
3
  require 'time'
4
4
  require 'net/http'
5
+ require 'resolv'
5
6
 
6
7
  module Aws
8
+ # An auto-refreshing credential provider that loads credentials from
9
+ # instances running in containers.
10
+ #
11
+ # ecs_credentials = Aws::ECSCredentials.new(retries: 3)
12
+ # ec2 = Aws::EC2::Client.new(credentials: ecs_credentials)
7
13
  class ECSCredentials
8
-
9
14
  include CredentialProvider
10
15
  include RefreshingCredentials
11
16
 
12
17
  # @api private
13
18
  class Non200Response < RuntimeError; end
14
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
+
15
26
  # These are the errors we trap when attempting to talk to the
16
27
  # instance metadata service. Any of these imply the service
17
28
  # is not present, no responding or some other non-recoverable
@@ -24,16 +35,22 @@ module Aws
24
35
  Errno::ENETUNREACH,
25
36
  SocketError,
26
37
  Timeout::Error,
27
- Non200Response,
28
- ]
38
+ Non200Response
39
+ ].freeze
29
40
 
30
41
  # @param [Hash] options
31
42
  # @option options [Integer] :retries (5) Number of times to retry
32
43
  # when retrieving credentials.
33
- # @option options [String] :ip_address ('169.254.170.2')
34
- # @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.
35
48
  # @option options [String] :credential_path By default, the value of the
36
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.
37
54
  # @option options [Float] :http_open_timeout (5)
38
55
  # @option options [Float] :http_read_timeout (5)
39
56
  # @option options [Numeric, Proc] :delay By default, failures are retried
@@ -43,21 +60,23 @@ module Aws
43
60
  # @option options [IO] :http_debug_output (nil) HTTP wire
44
61
  # traces are sent to this object. You can specify something
45
62
  # like $stdout.
46
- def initialize options = {}
63
+ # @option options [Callable] before_refresh Proc called before
64
+ # credentials are refreshed. `before_refresh` is called
65
+ # with an instance of this object when
66
+ # AWS credentials are required and need to be refreshed.
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
+
47
74
  @retries = options[:retries] || 5
48
- @ip_address = options[:ip_address] || '169.254.170.2'
49
- @port = options[:port] || 80
50
- @credential_path = options[:credential_path]
51
- @credential_path ||= ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI']
52
- unless @credential_path
53
- raise ArgumentError.new(
54
- "Cannot instantiate an ECS Credential Provider without a credential path."
55
- )
56
- end
57
75
  @http_open_timeout = options[:http_open_timeout] || 5
58
76
  @http_read_timeout = options[:http_read_timeout] || 5
59
77
  @http_debug_output = options[:http_debug_output]
60
78
  @backoff = backoff(options[:backoff])
79
+ @async_refresh = false
61
80
  super
62
81
  end
63
82
 
@@ -67,11 +86,95 @@ module Aws
67
86
 
68
87
  private
69
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
+
70
173
  def backoff(backoff)
71
174
  case backoff
72
175
  when Proc then backoff
73
- when Numeric then lambda { |_| sleep(backoff) }
74
- 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) }
75
178
  end
76
179
  end
77
180
 
@@ -79,68 +182,99 @@ module Aws
79
182
  # Retry loading credentials up to 3 times is the instance metadata
80
183
  # service is responding but is returning invalid JSON documents
81
184
  # in response to the GET profile credentials call.
82
- begin
83
- retry_errors([Aws::Json::ParseError, StandardError], max_retries: 3) do
84
- c = Aws::Json.load(get_credentials.to_s)
85
- @credentials = Credentials.new(
86
- c['AccessKeyId'],
87
- c['SecretAccessKey'],
88
- c['Token']
89
- )
90
- @expiration = c['Expiration'] ? Time.iso8601(c['Expiration']) : nil
91
- end
92
- rescue Aws::Json::ParseError
93
- 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
94
194
  end
195
+ rescue Aws::Json::ParseError
196
+ raise Aws::Errors::MetadataParserError
95
197
  end
96
198
 
97
199
  def get_credentials
98
200
  # Retry loading credentials a configurable number of times if
99
201
  # the instance metadata service is not responding.
100
- begin
101
- retry_errors(NETWORK_ERRORS, max_retries: @retries) do
102
- open_connection do |conn|
103
- http_get(conn, @credential_path)
104
- end
202
+
203
+ retry_errors(NETWORK_ERRORS, max_retries: @retries) do
204
+ open_connection do |conn|
205
+ http_get(conn, @credential_path)
105
206
  end
106
- rescue
107
- '{}'
108
207
  end
208
+ rescue TokenFileReadError, InvalidTokenError
209
+ raise
210
+ rescue StandardError
211
+ '{}'
212
+ end
213
+
214
+ def fetch_authorization_token
215
+ if (path = ENV['AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE'])
216
+ fetch_authorization_token_file(path)
217
+ elsif (token = ENV['AWS_CONTAINER_AUTHORIZATION_TOKEN'])
218
+ token
219
+ end
220
+ end
221
+
222
+ def fetch_authorization_token_file(path)
223
+ File.read(path).strip
224
+ rescue Errno::ENOENT
225
+ raise TokenFileReadError,
226
+ 'AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE is set '\
227
+ "but the file doesn't exist: #{path}"
228
+ end
229
+
230
+ def validate_authorization_token!(token)
231
+ return unless token.include?("\r\n")
232
+
233
+ raise InvalidTokenError,
234
+ 'Invalid Authorization token: token contains '\
235
+ 'a newline and carriage return character.'
109
236
  end
110
237
 
111
238
  def open_connection
112
- http = Net::HTTP.new(@ip_address, @port, nil)
239
+ http = Net::HTTP.new(@host, @port, nil)
113
240
  http.open_timeout = @http_open_timeout
114
241
  http.read_timeout = @http_read_timeout
115
242
  http.set_debug_output(@http_debug_output) if @http_debug_output
243
+ http.use_ssl = @scheme == 'https'
116
244
  http.start
117
245
  yield(http).tap { http.finish }
118
246
  end
119
247
 
120
248
  def http_get(connection, path)
121
- response = connection.request(Net::HTTP::Get.new(path))
122
- if response.code.to_i == 200
123
- response.body
124
- else
125
- raise Non200Response
249
+ request = Net::HTTP::Get.new(path)
250
+ set_authorization_token(request)
251
+ response = connection.request(request)
252
+ raise Non200Response unless response.code.to_i == 200
253
+
254
+ response.body
255
+ end
256
+
257
+ def set_authorization_token(request)
258
+ if (authorization_token = fetch_authorization_token)
259
+ validate_authorization_token!(authorization_token)
260
+ request['Authorization'] = authorization_token
126
261
  end
127
262
  end
128
263
 
129
- def retry_errors(error_classes, options = {}, &block)
264
+ def retry_errors(error_classes, options = {})
130
265
  max_retries = options[:max_retries]
131
266
  retries = 0
132
267
  begin
133
268
  yield
134
- rescue *error_classes => _error
135
- if retries < max_retries
136
- @backoff.call(retries)
137
- retries += 1
138
- retry
139
- else
140
- raise
141
- end
269
+ rescue TokenFileReadError, InvalidTokenError
270
+ raise
271
+ rescue *error_classes => _e
272
+ raise unless retries < max_retries
273
+
274
+ @backoff.call(retries)
275
+ retries += 1
276
+ retry
142
277
  end
143
278
  end
144
-
145
279
  end
146
280
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
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.
10
+ # @api private
11
+ class Condition
12
+ def initialize(fn:, argv:, assign: nil)
13
+ @fn = Function.new(fn: fn, argv: argv)
14
+ @assign = assign
15
+ @assigned = {}
16
+ end
17
+
18
+ attr_reader :fn
19
+ attr_reader :argv
20
+ attr_reader :assign
21
+
22
+ attr_reader :assigned
23
+
24
+ def match?(parameters, assigns)
25
+ output = @fn.call(parameters, assigns)
26
+ @assigned = @assigned.merge({ @assign => output }) if @assign
27
+ output
28
+ end
29
+
30
+ def self.from_json(conditions_json)
31
+ conditions_json.each.with_object([]) do |condition, conditions|
32
+ conditions << new(
33
+ fn: condition['fn'],
34
+ argv: condition['argv'],
35
+ assign: condition['assign']
36
+ )
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Endpoints
5
+ class Endpoint
6
+ def initialize(url:, properties: {}, headers: {})
7
+ @url = url
8
+ @properties = properties
9
+ @headers = headers
10
+ end
11
+
12
+ attr_reader :url
13
+ attr_reader :properties
14
+ attr_reader :headers
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
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. # @api private
10
+ class EndpointRule < Rule
11
+ def initialize(type: 'endpoint', conditions:, endpoint:,
12
+ documentation: nil)
13
+ @type = type
14
+ @conditions = Condition.from_json(conditions)
15
+ @endpoint = endpoint
16
+ @documentation = documentation
17
+ end
18
+
19
+ attr_reader :type
20
+ attr_reader :conditions
21
+ attr_reader :endpoint
22
+ attr_reader :documentation
23
+
24
+ def match(parameters, assigned = {})
25
+ assigns = assigned.dup
26
+ matched = conditions.all? do |condition|
27
+ output = condition.match?(parameters, assigns)
28
+ assigns = assigns.merge(condition.assigned) if condition.assign
29
+ output
30
+ end
31
+ resolved_endpoint(parameters, assigns) if matched
32
+ end
33
+
34
+ def resolved_endpoint(parameters, assigns)
35
+ Endpoint.new(
36
+ url: resolve_value(@endpoint['url'], parameters, assigns),
37
+ properties: resolve_properties(
38
+ @endpoint['properties'] || {},
39
+ parameters,
40
+ assigns
41
+ ),
42
+ headers: resolve_headers(parameters, assigns)
43
+ )
44
+ end
45
+
46
+ private
47
+
48
+ def resolve_headers(parameters, assigns)
49
+ (@endpoint['headers'] || {}).each.with_object({}) do |(key, arr), headers|
50
+ headers[key] = []
51
+ arr.each do |value|
52
+ headers[key] << resolve_value(value, parameters, assigns)
53
+ end
54
+ end
55
+ end
56
+
57
+ def resolve_properties(obj, parameters, assigns)
58
+ case obj
59
+ when Hash
60
+ obj.each.with_object({}) do |(key, value), hash|
61
+ hash[key] = resolve_properties(value, parameters, assigns)
62
+ end
63
+ when Array
64
+ obj.collect { |value| resolve_properties(value, parameters, assigns) }
65
+ else
66
+ if obj.is_a?(String)
67
+ Templater.resolve(obj, parameters, assigns)
68
+ else
69
+ obj
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
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.
10
+ # @api private
11
+ class ErrorRule < Rule
12
+ def initialize(type: 'error', conditions:, error: nil, documentation: nil)
13
+ @type = type
14
+ @conditions = Condition.from_json(conditions)
15
+ @error = error
16
+ @documentation = documentation
17
+ end
18
+
19
+ attr_reader :type
20
+ attr_reader :conditions
21
+ attr_reader :error
22
+ attr_reader :documentation
23
+
24
+ def match(parameters, assigned = {})
25
+ assigns = assigned.dup
26
+ matched = conditions.all? do |condition|
27
+ output = condition.match?(parameters, assigns)
28
+ assigns = assigns.merge(condition.assigned) if condition.assign
29
+ output
30
+ end
31
+ resolved_error(parameters, assigns) if matched
32
+ end
33
+
34
+ private
35
+
36
+ def resolved_error(parameters, assigns)
37
+ error = resolve_value(@error, parameters, assigns)
38
+ ArgumentError.new(error)
39
+ end
40
+ end
41
+ end
42
+ end