Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2] Add configuration to disable host prefix injection #9268

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changes/next-release/feature-configuration-42674.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "feature",
"category": "configuration",
"description": "Configure if the host prefix is prepended to the request URL in the shared configuration file or via an environment variable"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we document the specific environment variable and setting name here?

Suggested change
"description": "Configure if the host prefix is prepended to the request URL in the shared configuration file or via an environment variable"
"description": "Allow disabling the prepending of the host prefix to the request URL via ``disable_host_prefix_injection`` in the shared configuration file or the ``AWS_DISABLE_HOST_PREFIX_INJECTION`` environment variable"

}
21 changes: 20 additions & 1 deletion awscli/botocore/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def compute_client_args(self, service_model, client_config,
proxies_config=client_config.proxies_config,
retries=client_config.retries,
client_cert=client_config.client_cert,
inject_host_prefix=client_config.inject_host_prefix,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should leave this in here and when we compute the value, we can check if this is not None.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, not opposed to this. I went for self-containing all of the logic in the compute method.

request_min_compression_size_bytes=(
client_config.request_min_compression_size_bytes
),
Expand All @@ -236,6 +235,8 @@ def compute_client_args(self, service_model, client_config,
self._compute_user_agent_appid_config(config_kwargs)
self._compute_sigv4a_signing_region_set_config(config_kwargs)
self._compute_checksum_config(config_kwargs)
self._compute_inject_host_prefix(client_config, config_kwargs)

s3_config = self.compute_s3_config(client_config)

is_s3_service = self._is_s3_service(service_name)
Expand All @@ -256,6 +257,24 @@ def compute_client_args(self, service_model, client_config,
'socket_options': self._compute_socket_options(scoped_config)
}

def _compute_inject_host_prefix(self, client_config, config_kwargs):
if client_config is not None and client_config.inject_host_prefix is not None:
config_kwargs['inject_host_prefix'] = (
client_config.inject_host_prefix
)
Comment on lines +260 to +264
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here we can remove client_config as a parameter and check

if config_kwargs.get('inject_host_prefix') is not None:
    # User explicitly set a client config value. Respect it and return.
    return

else:
configured_disable_host_prefix_injection = (
self._config_store.get_config_variable(
'disable_host_prefix_injection'
)
)
if configured_disable_host_prefix_injection is not None:
config_kwargs[
'inject_host_prefix'
] = not configured_disable_host_prefix_injection
else:
config_kwargs['inject_host_prefix'] = True

def _compute_configured_endpoint_url(self, client_config, endpoint_url):
if endpoint_url is not None:
return endpoint_url
Expand Down
2 changes: 1 addition & 1 deletion awscli/botocore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class Config(object):
('s3', None),
('retries', None),
('client_cert', None),
('inject_host_prefix', True),
('inject_host_prefix', None),
('endpoint_discovery_enabled', None),
('use_dualstack_endpoint', None),
('use_fips_endpoint', None),
Expand Down
7 changes: 7 additions & 0 deletions awscli/botocore/configprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@
"when_supported",
None,
),
'disable_host_prefix_injection': (
'disable_host_prefix_injection',
'AWS_DISABLE_HOST_PREFIX_INJECTION',
None,
utils.ensure_boolean,
),
}

# A mapping for the s3 specific configuration vars. These are the configuration
# vars that typically go in the s3 section of the config file. This mapping
# follows the same schema as the previous session variable mapping.
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/botocore/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,32 @@ def test_response_checksum_validation_invalid_client_config(self):
with self.assertRaises(exceptions.InvalidChecksumConfigError):
self.call_get_client_args()

def test_disable_host_prefix_injection_default_client_config(self):
input_config = Config()
client_args = self.call_get_client_args(client_config=input_config)
config = client_args["client_config"]
self.assertEqual(config.inject_host_prefix, True)

def test_disable_host_prefix_injection_config_store(self):
self.config_store.set_config_variable(
"disable_host_prefix_injection",
True,
)
config = self.call_get_client_args()['client_config']
self.assertEqual(config.inject_host_prefix, False)

def test_disable_host_prefix_injection_client_config_overrides_config_store(
self,
):
self.config_store.set_config_variable(
"disable_host_prefix_injection",
True,
)
input_config = Config(inject_host_prefix=False)
client_args = self.call_get_client_args(client_config=input_config)
config = client_args['client_config']
self.assertEqual(config.inject_host_prefix, False)


class TestEndpointResolverBuiltins(unittest.TestCase):
def setUp(self):
Expand Down
Loading