Skip to content

Commit

Permalink
Add all core plugins to autoloads (#3116)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods authored Sep 25, 2024
1 parent 0219037 commit e0c61bb
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,6 +63,11 @@ def require_core_guard?
@service.included_in_core?
end

# @return [String]
def service_identifier
@service.identifier
end

# @return [Array<Hash>] list of autoload path hashes with :path, :class_name and
# :is_plugin keys.
def autoloads
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Add all core plugins to autoloads.

3.209.0 (2024-09-24)
------------------

Expand Down
1 change: 1 addition & 0 deletions gems/aws-sdk-core/lib/aws-sdk-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'aws-partitions'
require 'seahorse'
require 'jmespath'
require 'aws-sigv4'

require_relative 'aws-sdk-core/deprecations'
# defaults
Expand Down
17 changes: 8 additions & 9 deletions gems/aws-sdk-core/lib/aws-sdk-core/assume_role_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 29 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions gems/aws-sdk-core/spec/aws/assume_role_credentials_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit e0c61bb

Please sign in to comment.