From e0c61bbf705e5b948217e5e684577eeb2c947538 Mon Sep 17 00:00:00 2001 From: Alex Woods Date: Wed, 25 Sep 2024 10:51:45 -0700 Subject: [PATCH] Add all core plugins to autoloads (#3116) --- .../views/service_module.rb | 12 ++++---- gems/aws-sdk-core/CHANGELOG.md | 2 ++ gems/aws-sdk-core/lib/aws-sdk-core.rb | 1 + .../aws-sdk-core/assume_role_credentials.rb | 17 +++++------ .../assume_role_web_identity_credentials.rb | 17 +++++------ gems/aws-sdk-core/lib/aws-sdk-core/plugins.rb | 30 ++++++++++++++++++- .../spec/aws/assume_role_credentials_spec.rb | 18 +++++++++++ ...sume_role_web_identity_credentials_spec.rb | 18 +++++++++++ 8 files changed, 90 insertions(+), 25 deletions(-) diff --git a/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/service_module.rb b/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/service_module.rb index 9dcfe8f31a4..f0f2d84021f 100644 --- a/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/service_module.rb +++ b/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/service_module.rb @@ -10,13 +10,12 @@ class ServiceModule < View # @option options [required, String] :prefix def initialize(options) @service = options.fetch(:service) - @service_identifier = @service.identifier @prefix = options.fetch(:prefix) @codegenerated_plugins = options.fetch(:codegenerated_plugins) || [] end # @return [String] - attr_reader :service_identifier + attr_reader :prefix # @return [String|nil] def generated_src_warning @@ -64,6 +63,11 @@ def require_core_guard? @service.included_in_core? end + # @return [String] + def service_identifier + @service.identifier + end + # @return [Array] list of autoload path hashes with :path, :class_name and # :is_plugin keys. def autoloads @@ -112,10 +116,6 @@ def auto_load(path, class_name, is_plugin = false) } end - def prefix - @prefix - end - def example_var_name underscore(name) end diff --git a/gems/aws-sdk-core/CHANGELOG.md b/gems/aws-sdk-core/CHANGELOG.md index 02c66a4a376..c997bf71d8e 100644 --- a/gems/aws-sdk-core/CHANGELOG.md +++ b/gems/aws-sdk-core/CHANGELOG.md @@ -1,6 +1,8 @@ Unreleased Changes ------------------ +* Issue - Add all core plugins to autoloads. + 3.209.0 (2024-09-24) ------------------ diff --git a/gems/aws-sdk-core/lib/aws-sdk-core.rb b/gems/aws-sdk-core/lib/aws-sdk-core.rb index e03196d5fec..e5c268e093e 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core.rb @@ -3,6 +3,7 @@ require 'aws-partitions' require 'seahorse' require 'jmespath' +require 'aws-sigv4' require_relative 'aws-sdk-core/deprecations' # defaults diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_credentials.rb b/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_credentials.rb index cdd2e19bc9d..ad9e6a4df6f 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_credentials.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_credentials.rb @@ -62,23 +62,22 @@ def initialize(options = {}) private def refresh - c = @client.assume_role(@assume_role_params) - creds = c.credentials - account_id = - begin - ARNParser.parse(c.assumed_role_user.arn).account_id - rescue Aws::Errors::InvalidARNError - nil - end + resp = @client.assume_role(@assume_role_params) + creds = resp.credentials @credentials = Credentials.new( creds.access_key_id, creds.secret_access_key, creds.session_token, - account_id: account_id + account_id: parse_account_id(resp) ) @expiration = creds.expiration end + def parse_account_id(resp) + arn = resp.assumed_role_user&.arn + ARNParser.parse(arn).account_id if ARNParser.arn?(arn) + end + class << self # @api private diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_web_identity_credentials.rb b/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_web_identity_credentials.rb index 3de6e70db33..c07828f32a1 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_web_identity_credentials.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/assume_role_web_identity_credentials.rb @@ -73,19 +73,13 @@ def refresh # read from token file everytime it refreshes @assume_role_web_identity_params[:web_identity_token] = _token_from_file(@token_file) - c = @client.assume_role_with_web_identity(@assume_role_web_identity_params) - creds = c.credentials - account_id = - begin - ARNParser.parse(c.assumed_role_user.arn).account_id - rescue Aws::Errors::InvalidARNError - nil - end + resp = @client.assume_role_with_web_identity(@assume_role_web_identity_params) + creds = resp.credentials @credentials = Credentials.new( creds.access_key_id, creds.secret_access_key, creds.session_token, - account_id: account_id + account_id: parse_account_id(resp) ) @expiration = creds.expiration end @@ -101,6 +95,11 @@ def _session_name Base64.strict_encode64(SecureRandom.uuid) end + def parse_account_id(resp) + arn = resp.assumed_role_user&.arn + ARNParser.parse(arn).account_id if ARNParser.arn?(arn) + end + class << self # @api private diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/plugins.rb b/gems/aws-sdk-core/lib/aws-sdk-core/plugins.rb index 8b65abc34b2..0c5ac696e8f 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/plugins.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/plugins.rb @@ -3,9 +3,37 @@ module Aws # setup autoloading for Plugins # Most plugins are required explicitly from service clients + # but users may reference them outside of client usage. module Plugins + autoload :ApiKey, 'aws-sdk-core/plugins/api_key' autoload :BearerAuthorization, 'aws-sdk-core/plugins/bearer_authorization' - autoload :SignatureV4, 'aws-sdk-core/plugins/signature_v4' + autoload :ChecksumAlgorithm, 'aws-sdk-core/plugins/checksum_algorithm' + autoload :ClientMetricsPlugin, 'aws-sdk-core/plugins/client_metrics_plugin' + autoload :ClientMetricsSendPlugin, 'aws-sdk-core/plugins/client_metrics_send_plugin' + autoload :CredentialsConfiguration, 'aws-sdk-core/plugins/credentials_configuration' + autoload :DefaultsMode, 'aws-sdk-core/plugins/defaults_mode' + autoload :EndpointDiscovery, 'aws-sdk-core/plugins/endpoint_discovery' + autoload :EndpointPattern, 'aws-sdk-core/plugins/endpoint_pattern' + autoload :EventStreamConfiguration, 'aws-sdk-core/plugins/event_stream_configuration' autoload :GlobalConfiguration, 'aws-sdk-core/plugins/global_configuration' + autoload :HelpfulSocketErrors, 'aws-sdk-core/plugins/helpful_socket_errors' + autoload :HttpChecksum, 'aws-sdk-core/plugins/http_checksum' + autoload :IdempotencyToken, 'aws-sdk-core/plugins/idempotency_token' + autoload :InvocationId, 'aws-sdk-core/plugins/invocation_id' + autoload :JsonvalueConverter, 'aws-sdk-core/plugins/jsonvalue_converter' + autoload :Logging, 'aws-sdk-core/plugins/logging' + autoload :ParamConverter, 'aws-sdk-core/plugins/param_converter' + autoload :ParamValidator, 'aws-sdk-core/plugins/param_validator' + autoload :RecursionDetection, 'aws-sdk-core/plugins/recursion_detection' + autoload :RegionalEndpoint, 'aws-sdk-core/plugins/regional_endpoint' + autoload :RequestCompression, 'aws-sdk-core/plugins/request_compression' + autoload :ResponsePaging, 'aws-sdk-core/plugins/response_paging' + autoload :RetryErrors, 'aws-sdk-core/plugins/retry_errors' + autoload :Sign, 'aws-sdk-core/plugins/sign' + autoload :SignatureV4, 'aws-sdk-core/plugins/signature_v4' + autoload :StubResponses, 'aws-sdk-core/plugins/stub_responses' + autoload :Telemetry, 'aws-sdk-core/plugins/telemetry' + autoload :TransferEncoding, 'aws-sdk-core/plugins/transfer_encoding' + autoload :UserAgent, 'aws-sdk-core/plugins/user_agent' end end \ No newline at end of file diff --git a/gems/aws-sdk-core/spec/aws/assume_role_credentials_spec.rb b/gems/aws-sdk-core/spec/aws/assume_role_credentials_spec.rb index bd24099289a..caadd47f486 100644 --- a/gems/aws-sdk-core/spec/aws/assume_role_credentials_spec.rb +++ b/gems/aws-sdk-core/spec/aws/assume_role_credentials_spec.rb @@ -118,6 +118,24 @@ module Aws expect(c.expiration).to eq(in_one_hour) end + context 'invalid assumed role arn' do + let(:assumed_role_user) do + double( + 'assumed_role_user', + arn: 'invalid_arn', + assumed_role_id: 'role id' + ) + end + + it 'does not set accountId' do + c = AssumeRoleCredentials.new( + role_arn: 'arn', + role_session_name: 'session' + ) + expect(c.credentials.account_id).to be_nil + end + end + it 'refreshes asynchronously' do # expiration 6 minutes out, within the async exp time window allow(credentials).to receive(:expiration).and_return(Time.now + (6*60)) diff --git a/gems/aws-sdk-core/spec/aws/assume_role_web_identity_credentials_spec.rb b/gems/aws-sdk-core/spec/aws/assume_role_web_identity_credentials_spec.rb index f4a1fc67a76..546097f2034 100644 --- a/gems/aws-sdk-core/spec/aws/assume_role_web_identity_credentials_spec.rb +++ b/gems/aws-sdk-core/spec/aws/assume_role_web_identity_credentials_spec.rb @@ -168,6 +168,24 @@ module Aws expect(c.expiration).to eq(in_one_hour) end + context 'invalid assumed role arn' do + let(:assumed_role_user) do + double( + 'assumed_role_user', + arn: 'invalid_arn', + assumed_role_id: 'role id' + ) + end + + it 'does not set accountId' do + c = AssumeRoleWebIdentityCredentials.new( + role_arn: 'arn', + web_identity_token_file: token_file_path, + ) + expect(c.credentials.account_id).to be_nil + end + end + it 'refreshes asynchronously' do # expiration 6 minutes out, within the async exp time window allow(credentials).to receive(:expiration).and_return(Time.now + (6*60))