Skip to content

Commit a9d62d5

Browse files
authored
Support disable host prefixing in env and shared config (#3237)
1 parent 763f0fc commit a9d62d5

File tree

5 files changed

+57
-32
lines changed

5 files changed

+57
-32
lines changed

gems/aws-sdk-core/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Feature - Support `ENV['AWS_DISABLE_HOST_PREFIX_INJECTION']` and `disable_host_prefix_injection` shared config to disable host prefix injection for all services.
5+
46
3.223.0 (2025-05-01)
57
------------------
68

gems/aws-sdk-core/lib/aws-sdk-core/plugins/endpoint_pattern.rb

+40-32
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,70 @@ module Aws
44
module Plugins
55
# @api private
66
class EndpointPattern < Seahorse::Client::Plugin
7-
8-
option(:disable_host_prefix_injection,
7+
option(
8+
:disable_host_prefix_injection,
99
default: false,
1010
doc_type: 'Boolean',
11-
docstring: <<-DOCS
12-
Set to true to disable SDK automatically adding host prefix
13-
to default service endpoint when available.
14-
DOCS
15-
)
11+
docstring: 'When `true`, the SDK will not prepend the modeled host prefix to the endpoint.'
12+
) do |cfg|
13+
resolve_disable_host_prefix_injection(cfg)
14+
end
1615

17-
def add_handlers(handlers, config)
16+
def add_handlers(handlers, _config)
1817
handlers.add(Handler, priority: 10)
1918
end
2019

21-
class Handler < Seahorse::Client::Handler
20+
class << self
21+
private
22+
23+
def resolve_disable_host_prefix_injection(cfg)
24+
value = ENV['AWS_DISABLE_HOST_PREFIX_INJECTION'] ||
25+
Aws.shared_config.disable_host_prefix_injection(profile: cfg.profile) ||
26+
'false'
27+
value = Aws::Util.str_2_bool(value)
28+
unless [true, false].include?(value)
29+
raise ArgumentError,
30+
'Must provide either `true` or `false` for '\
31+
'disable_host_prefix_injection profile option or for '\
32+
'ENV[\'AWS_DISABLE_HOST_PREFIX_INJECTION\']'
33+
end
34+
value
35+
end
36+
end
2237

38+
# @api private
39+
class Handler < Seahorse::Client::Handler
2340
def call(context)
24-
if !context.config.disable_host_prefix_injection
41+
unless context.config.disable_host_prefix_injection
2542
endpoint_trait = context.operation.endpoint_pattern
26-
if endpoint_trait && !endpoint_trait.empty?
27-
_apply_endpoint_trait(context, endpoint_trait)
28-
end
43+
apply_endpoint_trait(context, endpoint_trait) if endpoint_trait && !endpoint_trait.empty?
2944
end
3045
@handler.call(context)
3146
end
3247

3348
private
3449

35-
def _apply_endpoint_trait(context, trait)
36-
# currently only support host pattern
37-
ori_host = context.http_request.endpoint.host
38-
if pattern = trait['hostPrefix']
39-
host_prefix = pattern.gsub(/\{.+?\}/) do |label|
40-
label = label.delete("{}")
41-
_replace_label_value(
42-
ori_host, label, context.operation.input, context.params)
43-
end
44-
context.http_request.endpoint.host = host_prefix + context.http_request.endpoint.host
50+
def apply_endpoint_trait(context, trait)
51+
pattern = trait['hostPrefix']
52+
return unless pattern
53+
54+
host_prefix = pattern.gsub(/\{.+?}/) do |label|
55+
label = label.delete('{}')
56+
replace_label_value(label, context.operation.input, context.params)
4557
end
58+
context.http_request.endpoint.host = host_prefix + context.http_request.endpoint.host
4659
end
4760

48-
def _replace_label_value(ori, label, input_ref, params)
61+
def replace_label_value(label, input_ref, params)
4962
name = nil
5063
input_ref.shape.members.each do |m_name, ref|
51-
if ref['hostLabel'] && ref['hostLabelName'] == label
52-
name = m_name
53-
end
54-
end
55-
if name.nil? || params[name].nil?
56-
raise Errors::MissingEndpointHostLabelValue.new(name)
64+
name = m_name if ref['hostLabel'] && ref['hostLabelName'] == label
5765
end
66+
raise Errors::MissingEndpointHostLabelValue, name if name.nil? || params[name].nil?
67+
5868
params[name]
5969
end
60-
6170
end
62-
6371
end
6472
end
6573
end

gems/aws-sdk-core/lib/aws-sdk-core/shared_config.rb

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def self.config_reader(*attrs)
212212
:ec2_metadata_service_endpoint,
213213
:ec2_metadata_service_endpoint_mode,
214214
:ec2_metadata_v1_disabled,
215+
:disable_host_prefix_injection,
215216
:max_attempts,
216217
:retry_mode,
217218
:adaptive_retry_wait_to_fill,

gems/aws-sdk-core/spec/aws/shared_config_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ module Aws
258258
end
259259
end
260260

261+
context 'disable_host_prefix_injection selection' do
262+
it 'can resolve disable_host_prefix_injection from config file' do
263+
config = SharedConfig.new(
264+
config_path: mock_config_file,
265+
config_enabled: true,
266+
profile_name: 'disable_host_prefix_injection'
267+
)
268+
expect(config.disable_host_prefix_injection).to eq('true')
269+
end
270+
end
271+
261272
context 'retry_mode selection' do
262273
it 'can resolve retry_mode from config file' do
263274
config = SharedConfig.new(

gems/aws-sdk-core/spec/fixtures/credentials/mock_shared_config

+3
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ s3_disable_express_session_auth = true
239239
[profile s3_do_not_use_express_zonal_auth]
240240
s3_disable_express_session_auth = false
241241

242+
[profile disable_host_prefix_injection]
243+
disable_host_prefix_injection = true
244+
242245
[profile retry_mode_legacy]
243246
retry_mode = legacy
244247

0 commit comments

Comments
 (0)