Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
------------------

* Feature - Support `ENV['AWS_DISABLE_HOST_PREFIX_INJECTION']` and `disable_host_prefix_injection` shared config to disable host prefix injection for all services.

3.223.0 (2025-05-01)
------------------

Expand Down
72 changes: 40 additions & 32 deletions gems/aws-sdk-core/lib/aws-sdk-core/plugins/endpoint_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,70 @@ module Aws
module Plugins
# @api private
class EndpointPattern < Seahorse::Client::Plugin

option(:disable_host_prefix_injection,
option(
:disable_host_prefix_injection,
default: false,
doc_type: 'Boolean',
docstring: <<-DOCS
Set to true to disable SDK automatically adding host prefix
to default service endpoint when available.
DOCS
)
docstring: 'When `true`, the SDK will not prepend the modeled host prefix to the endpoint.'
) do |cfg|
resolve_disable_host_prefix_injection(cfg)
end

def add_handlers(handlers, config)
def add_handlers(handlers, _config)
handlers.add(Handler, priority: 10)
end

class Handler < Seahorse::Client::Handler
class << self
private

def resolve_disable_host_prefix_injection(cfg)
value = ENV['AWS_DISABLE_HOST_PREFIX_INJECTION'] ||
Aws.shared_config.disable_host_prefix_injection(profile: cfg.profile) ||
'false'
value = Aws::Util.str_2_bool(value)
unless [true, false].include?(value)
raise ArgumentError,
'Must provide either `true` or `false` for '\
'disable_host_prefix_injection profile option or for '\
'ENV[\'AWS_DISABLE_HOST_PREFIX_INJECTION\']'
end
value
end
end

# @api private
class Handler < Seahorse::Client::Handler
def call(context)
if !context.config.disable_host_prefix_injection
unless context.config.disable_host_prefix_injection
endpoint_trait = context.operation.endpoint_pattern
if endpoint_trait && !endpoint_trait.empty?
_apply_endpoint_trait(context, endpoint_trait)
end
apply_endpoint_trait(context, endpoint_trait) if endpoint_trait && !endpoint_trait.empty?
end
@handler.call(context)
end

private

def _apply_endpoint_trait(context, trait)
# currently only support host pattern
ori_host = context.http_request.endpoint.host
if pattern = trait['hostPrefix']
host_prefix = pattern.gsub(/\{.+?\}/) do |label|
label = label.delete("{}")
_replace_label_value(
ori_host, label, context.operation.input, context.params)
end
context.http_request.endpoint.host = host_prefix + context.http_request.endpoint.host
def apply_endpoint_trait(context, trait)
pattern = trait['hostPrefix']
return unless pattern

host_prefix = pattern.gsub(/\{.+?}/) do |label|
label = label.delete('{}')
replace_label_value(label, context.operation.input, context.params)
end
context.http_request.endpoint.host = host_prefix + context.http_request.endpoint.host
end

def _replace_label_value(ori, label, input_ref, params)
def replace_label_value(label, input_ref, params)
name = nil
input_ref.shape.members.each do |m_name, ref|
if ref['hostLabel'] && ref['hostLabelName'] == label
name = m_name
end
end
if name.nil? || params[name].nil?
raise Errors::MissingEndpointHostLabelValue.new(name)
name = m_name if ref['hostLabel'] && ref['hostLabelName'] == label
end
raise Errors::MissingEndpointHostLabelValue, name if name.nil? || params[name].nil?

params[name]
end

end

end
end
end
1 change: 1 addition & 0 deletions gems/aws-sdk-core/lib/aws-sdk-core/shared_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def self.config_reader(*attrs)
:ec2_metadata_service_endpoint,
:ec2_metadata_service_endpoint_mode,
:ec2_metadata_v1_disabled,
:disable_host_prefix_injection,
:max_attempts,
:retry_mode,
:adaptive_retry_wait_to_fill,
Expand Down
11 changes: 11 additions & 0 deletions gems/aws-sdk-core/spec/aws/shared_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,17 @@ module Aws
end
end

context 'disable_host_prefix_injection selection' do
it 'can resolve disable_host_prefix_injection from config file' do
config = SharedConfig.new(
config_path: mock_config_file,
config_enabled: true,
profile_name: 'disable_host_prefix_injection'
)
expect(config.disable_host_prefix_injection).to eq('true')
end
end

context 'retry_mode selection' do
it 'can resolve retry_mode from config file' do
config = SharedConfig.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ s3_disable_express_session_auth = true
[profile s3_do_not_use_express_zonal_auth]
s3_disable_express_session_auth = false

[profile disable_host_prefix_injection]
disable_host_prefix_injection = true

[profile retry_mode_legacy]
retry_mode = legacy

Expand Down