1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import pytest
from botocore.config import Config
from botocore.exceptions import EndpointResolutionError
from tests import ClientHTTPStubber, patch_load_service_model
from tests.functional.test_useragent import (
get_captured_ua_strings,
parse_registered_feature_ids,
)
FAKE_RULESET = {
"version": "1.0",
"parameters": {
"AccountId": {
"type": "String",
"builtIn": "AWS::Auth::AccountId",
"documentation": "The AWS account ID used for the request, eg. `123456789012`.",
},
"AccountIdEndpointMode": {
"type": "String",
"builtIn": "AWS::Auth::AccountIdEndpointMode",
"documentation": "The behavior for account ID based endpoint routing, eg. `preferred`.",
},
},
"rules": [
{
"documentation": "Template account ID into the URI when account ID is set and AccountIdEndpointMode is "
"set to preferred.",
"conditions": [
{"fn": "isSet", "argv": [{"ref": "AccountId"}]},
{"fn": "isSet", "argv": [{"ref": "AccountIdEndpointMode"}]},
{
"fn": "stringEquals",
"argv": [{"ref": "AccountIdEndpointMode"}, "preferred"],
},
],
"endpoint": {
"url": "https://{AccountId}.otherservice.us-west-2.amazonaws.com"
},
"type": "endpoint",
},
{
"documentation": "Do not template account ID into the URI when AccountIdEndpointMode is set to disabled.",
"conditions": [
{"fn": "isSet", "argv": [{"ref": "AccountId"}]},
{"fn": "isSet", "argv": [{"ref": "AccountIdEndpointMode"}]},
{
"fn": "stringEquals",
"argv": [{"ref": "AccountIdEndpointMode"}, "disabled"],
},
],
"endpoint": {
"url": "https://otherservice.us-west-2.amazonaws.com/"
},
"type": "endpoint",
},
{
"documentation": "Raise an error when account ID is unset but AccountIdEndpointMode is set to required.",
"conditions": [
{
"fn": "not",
"argv": [{"fn": "isSet", "argv": [{"ref": "AccountId"}]}],
},
{"fn": "isSet", "argv": [{"ref": "AccountIdEndpointMode"}]},
{
"fn": "stringEquals",
"argv": [{"ref": "AccountIdEndpointMode"}, "required"],
},
],
"error": "AccountIdEndpointMode is required but no AccountID was provided",
"type": "error",
},
{
"documentation": "Template account ID into the URI when account ID is set and AccountIdEndpointMode is "
"set to required.",
"conditions": [
{"fn": "isSet", "argv": [{"ref": "AccountId"}]},
{"fn": "isSet", "argv": [{"ref": "AccountIdEndpointMode"}]},
{
"fn": "stringEquals",
"argv": [{"ref": "AccountIdEndpointMode"}, "required"],
},
],
"endpoint": {
"url": "https://{AccountId}.otherservice.us-west-2.amazonaws.com"
},
"type": "endpoint",
},
{
"documentation": "Fallback when AccountID is unset but AccountIdEndpointMode is set to preferred.",
"conditions": [
{
"fn": "not",
"argv": [{"fn": "isSet", "argv": [{"ref": "AccountId"}]}],
},
{"fn": "isSet", "argv": [{"ref": "AccountIdEndpointMode"}]},
{
"fn": "stringEquals",
"argv": [{"ref": "AccountIdEndpointMode"}, "preferred"],
},
],
"endpoint": {
"url": "https://otherservice.us-west-2.amazonaws.com/"
},
"type": "endpoint",
},
],
}
FAKE_SERVICE_MODEL = {
"version": "2.0",
"documentation": "",
"metadata": {
"apiVersion": "2020-02-02",
"endpointPrefix": "otherservice",
"protocol": "rest-xml",
"serviceFullName": "Other Service",
"serviceId": "Other Service",
"signatureVersion": "v4",
"signingName": "otherservice",
"uid": "otherservice-2020-02-02",
},
"operations": {
"MockOperation": {
"name": "MockOperation",
"http": {"method": "GET", "requestUri": "/"},
"input": {"shape": "MockOperationRequest"},
"documentation": "",
},
},
"shapes": {
"MockOpParam": {
"type": "string",
},
"MockOperationRequest": {
"type": "structure",
"required": ["MockOpParam"],
"members": {
"MockOpParam": {
"shape": "MockOpParam",
"documentation": "",
"location": "uri",
"locationName": "param",
},
},
},
},
}
@pytest.mark.parametrize(
"account_id_endpoint_mode, expected_endpoint, expected_error, account_id_in_url",
[
# Test case for 'preferred' mode: Account ID is in the URL
(
'preferred',
'https://123456789012.otherservice.us-west-2.amazonaws.com/',
None,
True,
),
# Test case for 'disabled' mode: Account ID is not in the URL
(
'disabled',
'https://otherservice.us-west-2.amazonaws.com/',
None,
False,
),
# Test case for 'required' mode: Error is raised due to missing Account ID
(
'required',
None,
'AccountIdEndpointMode is required but no AccountID was provided',
False,
),
],
)
def test_account_id_endpoint_resolution(
account_id_endpoint_mode,
expected_endpoint,
expected_error,
account_id_in_url,
patched_session,
monkeypatch,
):
account_id = '123456789012'
patch_load_service_model(
patched_session, monkeypatch, FAKE_SERVICE_MODEL, FAKE_RULESET
)
if account_id_endpoint_mode != 'required':
monkeypatch.setenv('AWS_ACCOUNT_ID', account_id)
client = patched_session.create_client(
'otherservice',
region_name='us-west-2',
config=Config(account_id_endpoint_mode=account_id_endpoint_mode),
)
# If we expect an error, assert that the error is raised
if expected_error:
with pytest.raises(EndpointResolutionError) as exc_info:
with ClientHTTPStubber(client, strict=True) as http_stubber:
http_stubber.add_response()
client.mock_operation(MockOpParam='mock-op-param-value')
assert str(exc_info.value) == expected_error
else:
# If no error is expected, verify the endpoint resolution
with ClientHTTPStubber(client, strict=True) as http_stubber:
http_stubber.add_response(status=200, body=b'{}')
client.mock_operation(MockOpParam='mock-op-param-value')
request = http_stubber.requests[0]
if account_id_in_url:
assert account_id in request.url, (
f"Account ID should be in the URL, but it's not: {request.url}"
)
else:
assert account_id not in request.url, (
f"Account ID should not be in the URL, but it is: {request.url}"
)
assert request.url == expected_endpoint, (
f"Expected endpoint '{expected_endpoint}', but got: {request.url}"
)
@pytest.mark.parametrize(
"account_id, account_id_endpoint_mode, expected_feature_ids",
[
# Feature IDs:
# ACCOUNT_ID_MODE_PREFERRED -> P
# ACCOUNT_ID_MODE_DISABLED -> Q
# ACCOUNT_ID_MODE_REQUIRED -> R
# RESOLVED_ACCOUNT_ID -> T
('123456789012', 'preferred', ['P', 'T']),
('123456789012', 'disabled', ['Q', 'T']),
('123456789012', 'required', ['R', 'T']),
(None, 'preferred', ['P']),
('', 'preferred', ['P']),
],
)
def test_user_agent_has_account_id_endpoint_feature_ids(
patched_session,
monkeypatch,
account_id,
account_id_endpoint_mode,
expected_feature_ids,
):
if account_id is not None:
monkeypatch.setenv('AWS_ACCOUNT_ID', account_id)
else:
monkeypatch.delenv('AWS_ACCOUNT_ID', raising=False)
patch_load_service_model(
patched_session, monkeypatch, FAKE_SERVICE_MODEL, FAKE_RULESET
)
client = patched_session.create_client(
'otherservice',
region_name='us-west-2',
config=Config(account_id_endpoint_mode=account_id_endpoint_mode),
)
with ClientHTTPStubber(client, strict=True) as http_stubber:
http_stubber.add_response()
client.mock_operation(MockOpParam='mock-op-param-value')
ua_string = get_captured_ua_strings(http_stubber)[0]
feature_list = parse_registered_feature_ids(ua_string)
assert all(item in feature_list for item in expected_feature_ids)
|