8000 Remove deprecated networking config helpers (#9600) · codeperl/localstack@6adf6da · GitHub
[go: up one dir, main page]

Skip to content

Commit 6adf6da

Browse files
authored
Remove deprecated networking config helpers (localstack#9600)
1 parent 93809ab commit 6adf6da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1157
-134
lines changed

localstack/aws/handlers/partition_rewriter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __call__(self, chain: HandlerChain, context: RequestContext, response: Respo
101101
# forward to the handler chain again
102102
result_response = forward(
103103
request=request,
104-
forward_base_url=config.get_edge_url(),
104+
forward_base_url=config.internal_service_url(),
105105
forward_path=get_raw_path(request),
106106
headers=request.headers,
107107
)

localstack/cli/localstack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def cmd_status_services(format_: str) -> None:
376376
"""
377377
import requests
378378

379-
url = config.get_edge_url()
379+
url = config.external_service_url()
380380

381381
try:
382382
health = requests.get(f"{url}/_localstack/health", timeout=2)

localstack/config.py

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import tempfile
77
import time
8+
import warnings
89
from typing import Any, Dict, List, Mapping, Optional, Tuple, TypeVar, Union
910

1011
from localstack import constants
@@ -416,8 +417,7 @@ def in_docker():
416417
# whether to make debugpy wait for a debbuger client
417418
WAIT_FOR_DEBUGGER = is_env_true("WAIT_FOR_DEBUGGER")
418419

419-
# whether to use SSL encryption for the services
420-
# TODO: this is deprecated and should be removed (edge port supports HTTP/HTTPS multiplexing)
420+
# whether to assume http or https for `get_protocol`
421421
USE_SSL = is_env_true("USE_SSL")
422422

423423
# whether the S3 legacy V2/ASF provider is enabled
@@ -1260,19 +1260,14 @@ def populate_config_env_var_names():
12601260
populate_config_env_var_names()
12611261

12621262

1263-
def service_port(service_key: str, external: bool = False) -> int:
1264-
"""@deprecated: Use `localstack_host().port` for external and `GATEWAY_LISTEN[0].port` for internal use."""
1265-
if external:
1266-
return LOCALSTACK_HOST.port
1267-
return GATEWAY_LISTEN[0].port
1268-
1269-
1270-
def get_protocol():
1263+
# helpers to build urls
1264+
def get_protocol() -> str:
12711265
return "https" if USE_SSL else "http"
12721266

12731267

1274-
# TODO: refactor internal codebase to use external_service_url and internal_service_url
1275-
def external_service_url(host=None, port=None, protocol=None) -> str:
1268+
def external_service_url(
1269+
host: Optional[str] = None, port: Optional[int] = None, protocol: Optional[str] = None
1270+
) -> str:
12761271
"""Returns a service URL to an external client used outside where LocalStack runs.
12771272
The configurations LOCALSTACK_HOST and USE_SSL can customize these returned URLs.
12781273
`host` can be used to overwrite the default for subdomains.
@@ -1283,46 +1278,72 @@ def external_service_url(host=None, port=None, protocol=None) -> str:
12831278
return f"{protocol}://{host}:{port}"
12841279

12851280

1286-
def internal_service_url(host=None, port=None, protocol=None) -> str:
1287-
"""Returns a service URL for internal use within where LocalStack runs.
1288-
Cannot be customized through LOCALSTACK_HOST because we assume LocalStack runs on the same host (i.e., localhost).
1281+
def internal_service_url(
1282+
host: Optional[str] = None, port: Optional[int] = None, protocol: Optional[str] = None
1283+
) -> str:
1284+
"""Returns a service URL for internal use within where LocalStack runs. Cannot be customized
1285+
through LOCALSTACK_HOST because we assume LocalStack runs on the same host (i.e., localhost).
12891286
"""
12901287
protocol = protocol or get_protocol()
12911288
host = host or LOCALHOST
12921289
port = port or GATEWAY_LISTEN[0].port
12931290
return f"{protocol}://{host}:{port}"
12941291

12951292

1296-
# TODO: Go over all usages and decide whether it's an internal or external usage
1293+
# DEPRECATED: old helpers for building URLs
1294+
1295+
12971296
def service_url(service_key, host=None, port=None):
1298-
"""@deprecated: Use `internal_service_url()` instead.
1299-
We assume that most usages are internal but really need to check and update each usage accordingly.
1297+
"""@deprecated: Use `internal_service_url()` instead. We assume that most usages are internal
1298+
but really need to check and update each usage accordingly.
13001299
"""
1300+
warnings.warn(
1301+
"""@deprecated: Use `internal_service_url()` instead. We assume that most usages are
1302+
internal but really need to check and update each usage accordingly.""",
1303+
DeprecationWarning,
1304+
)
13011305
return internal_service_url(host=host, port=port)
13021306

13031307

1304-
# TODO: go over all usages and replace depending on internal or external usage
1308+
def service_port(service_key: str, external: bool = False) -> int:
1309+
"""@deprecated: Use `localstack_host().port` for external and `GATEWAY_LISTEN[0].port` for
1310+
internal use."""
1311+
warnings.warn(
1312+
"Deprecated: use `localstack_host().port` for external and `GATEWAY_LISTEN[0].port` for "
1313+
"internal use.",
1314+
DeprecationWarning,
1315+
)
1316+
if external:
1317+
return LOCALSTACK_HOST.port
1318+
return GATEWAY_LISTEN[0].port
1319+
1320+
13051321
def get_edge_port_http():
1306-
"""@deprecated: Use `localstack_host().port` for external and `GATEWAY_LISTEN[0].port` for internal use.
1307-
This function is also not needed anymore because we don't separate between HTTP and HTTP ports anymore since
1308-
LocalStack listens to both."""
1322+
"""@deprecated: Use `localstack_host().port` for external and `GATEWAY_LISTEN[0].port` for
1323+
internal use. This function is also not needed anymore because we don't separate between HTTP
1324+
and HTTP ports anymore since LocalStack listens to both."""
1325+
warnings.warn(
1326+
"""@deprecated: Use `localstack_host().port` for external and `GATEWAY_LISTEN[0].port`
1327+
for internal use. This function is also not needed anymore because we don't separate
1328+
between HTTP and HTTP ports anymore since LocalStack listens to both.""",
1329+
DeprecationWarning,
1330+
)
13091331
return GATEWAY_LISTEN[0].port
13101332

13111333

1312-
# TODO: Go over all usages and decide whether it's an internal or external usage
13131334
def get_edge_url(localstack_hostname=None, protocol=None):
13141335
"""@deprecated: Use `internal_service_url()` instead.
13151336
We assume that most usages are internal but really need to check and update each usage accordingly.
13161337
"""
1338+
warnings.warn(
1339+
"""@deprecated: Use `internal_service_url()` instead.
1340+
We assume that most usages are internal but really need to check and update each usage accordingly.
1341+
""",
1342+
DeprecationWarning,
1343+
)
13171344
return internal_service_url(host=localstack_hostname, protocol=protocol)
13181345

13191346

1320-
def gateway_listen_ports_info():
1321-
"""Example: http port [4566,443]"""
1322-
gateway_listen_ports = [gw_listen.port for gw_listen in GATEWAY_LISTEN]
1323-
return f"{get_protocol()} port {gateway_listen_ports}"
1324-
1325-
13261347
class ServiceProviderConfig(Mapping[str, str]):
13271348
_provider_config: Dict[str, str]
13281349
default_value: str

localstack/services/apigateway/helpers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def get_stage_variables(context: ApiInvocationContext) -> Optional[Dict[str, str
602602
def path_based_url(api_id: str, stage_name: str, path: str) -> str:
603603
"""Return URL for inbound API gateway for given API ID, stage name, and path"""
604604
pattern = "%s/restapis/{api_id}/{stage_name}/%s{path}" % (
605-
config.service_url("apigateway"),
605+
config.external_service_url(),
606606
PATH_USER_REQUEST,
607607
)
608608
return pattern.format(api_id=api_id, stage_name=stage_name, path=path)
@@ -611,14 +611,15 @@ def path_based_url(api_id: str, stage_name: str, path: str) -> str:
611611
def host_based_url(rest_api_id: str, path: str, stage_name: str = None):
612612
"""Return URL for inbound API gateway for given API ID, stage name, and path with custom dns
613613
format"""
614-
pattern = "http://{endpoint}{stage}{path}"
614+
pattern = "{endpoint}{stage}{path}"
615615
stage = stage_name and f"/{stage_name}" or ""
616616
return pattern.format(endpoint=get_execute_api_endpoint(rest_api_id), stage=stage, path=path)
617617

618618

619-
def get_execute_api_endpoint(api_id: str, protocol: str = "") -> str:
619+
def get_execute_api_endpoint(api_id: str, protocol: str | None = None) -> str:
620620
host = localstack_host()
621-
return f"{protocol}{api_id}.execute-api.{host.host_and_port()}"
621+
protocol = protocol or config.get_protocol()
622+
return f"{protocol}://{api_id}.execute-api.{host.host_and_port()}"
622623

623624

624625
def tokenize_path(path):

localstack/services/apigateway/integration.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def invoke(self, invocation_context: ApiInvocationContext):
487487
headers["X-Amz-Target"] = target
488488

489489
result = common.make_http_request(
490-
url=config.service_url("kinesis"), data=payload, headers=headers, method="POST"
490+
url=config.internal_service_url(), data=payload, headers=headers, method="POST"
491491
)
492492

493493
# apply response template
@@ -686,10 +686,10 @@ def invoke(self, invocation_context: ApiInvocationContext):
686686
new_request = f"{payload}&QueueName={queue}"
687687
else:
688688
payload = self.request_templates.render(invocation_context)
689-
queue_url = f"{config.get_edge_url()}/{account_id}/{queue}"
689+
queue_url = f"{config.internal_service_url()}/{account_id}/{queue}"
690690
new_request = f"{payload}&QueueUrl={queue_url}"
691691

692-
url = urljoin(config.service_url("sqs"), f"{get_aws_account_id()}/{queue}")
692+
url = urljoin(config.internal_service_url(), f"{get_aws_account_id()}/{queue}")
693693
response = common.make_http_request(url, method="POST", headers=headers, data=new_request)
694694

695695
# apply response template
@@ -716,7 +716,7 @@ def invoke(self, invocation_context: ApiInvocationContext) -> Response:
716716
service="sns", aws_access_key_id=invocation_context.account_id, region_name=region_name
717717
)
718718
result = make_http_request(
719-
config.service_url("sns"), method="POST", headers=headers, data=payload
719+
config.internal_service_url(), method="POST", headers=headers, data=payload
720720
)
721721
return self.apply_response_parameters(invocation_context, result)
722722

@@ -926,7 +926,7 @@ def invoke(self, invocation_context: ApiInvocationContext):
926926
)
927927
headers.update({"X-Amz-Target": invocation_context.headers.get("X-Amz-Target")})
928928
response = make_http_request(
929-
config.service_url("events"), method="POST", headers=headers, data=payload
929+
config.internal_service_url(), method="POST", headers=headers, data=payload
930930
)
931931

932932
invocation_context.response = response

localstack/services/cloudformation/api_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def convert_s3_to_local_url(url: str) -> str:
9797

9898
# note: make sure to normalize the bucket name here!
9999
bucket_name = normalize_bucket_name(bucket_name)
100-
local_url = f"{config.service_url('s3')}/{bucket_name}/{key_name}"
100+
local_url = f"{config.internal_service_url()}/{bucket_name}/{key_name}"
101101
return local_url
102102

103103

localstack/services/cloudformation/engine/template_deployer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
ACTION_CREATE = "create"
4646
ACTION_DELETE = "delete"
47-
AWS_URL_SUFFIX = "localhost.localstack.cloud" # value is "amazonaws.com" in real AWS
47+
AWS_URL_SUFFIX = localstack_host().host # value is "amazonaws.com" in real AWS
4848

4949
REGEX_OUTPUT_APIGATEWAY = re.compile(
5050
rf"^(https?://.+\.execute-api\.)(?:[^-]+-){{2,3}}\d\.(amazonaws\.com|{AWS_URL_SUFFIX})/?(.*)$"

localstack/services/dynamodb/provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def get_forward_url(self) -> str:
446446
return self.server.url
447447

448448
def handle_shell_ui_redirect(self, request: werkzeug.Request) -> Response:
449-
headers = {"Refresh": f"0; url={config.service_url('dynamodb')}/shell/index.html"}
449+
headers = {"Refresh": f"0; url={config.external_service_url()}/shell/index.html"}
450450
return Response("", headers=headers)
451451

452452
def handle_shell_ui_request(self, request: werkzeug.Request, req_path: str) -> Response:

localstack/services/infra.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,14 @@ def cleanup_resources():
174174
)
175175

176176

177+
def gateway_listen_ports_info() -> str:
178+
"""Example: http port [4566,443]"""
179+
gateway_listen_ports = [gw_listen.port for gw_listen in config.GATEWAY_LISTEN]
180+
return f"{config.get_protocol()} port {gateway_listen_ports}"
181+
182+
177183
def log_startup_message(service):
178-
LOG.info("Starting mock %s service on %s ...", service, config.gateway_listen_ports_info())
184+
LOG.info("Starting mock %s service on %s ...", service, gateway_listen_ports_info())
179185

180186

181187
def check_aws_credentials():

localstack/services/sqs/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,14 @@ def arn(self) -> str:
267267
return f"arn:aws:sqs:{self.region}:{self.account_id}:{self.name}"
268268

269269
def url(self, context: RequestContext) -> str:
270-
"""Return queue URL using either SQS_PORT_EXTERNAL (if configured), the SQS_ENDPOINT_STRATEGY (if configured)
271-
or based on the 'Host' request header"""
270+
"""Return queue URL which depending on the endpoint strategy returns e.g.:
271+
* (standard) http://sqs.eu-west-1.localhost.localstack.cloud:4566/000000000000/myqueue
272+
* (domain) http://eu-west-1.queue.localhost.localstack.cloud:4566/000000000000/myqueue
273+
* (path) http://localhost.localstack.cloud:4566/queue/eu-central-1/000000000000/myqueue
274+
* otherwise: http://localhost.localstack.cloud:4566/000000000000/myqueue
275+
"""
272276

273-
scheme = context.request.scheme
277+
scheme = config.get_protocol()
274278
host_definition = localstack_host()
275279

276280
if config.SQS_ENDPOINT_STRATEGY == "standard":

localstack/services/stepfunctions/asl/component/state/state_execution/state_task/service/state_task_service_api_gateway.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _path_based_url_of(api_endpoint: ApiEndpoint) -> ApiEndpoint:
168168
return api_endpoint
169169
path_parts = url_path.split(".")
170170
api_id = path_parts[0]
171-
path_based_api_endpoint = f"{config.service_url('apigateway')}/restapis/{api_id}"
171+
path_based_api_endpoint = f"{config.internal_service_url()}/restapis/{api_id}"
172172
return path_based_api_endpoint
173173

174174
@staticmethod

localstack/testing/aws/eventbus_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import requests
44

5-
from localstack.config import get_edge_url
5+
from localstack import config
66
from localstack.testing.aws.util import is_aws_cloud
77
from localstack.utils.aws.client_types import TypedServiceClientFactory
88

@@ -18,7 +18,7 @@ def trigger_scheduled_rule(rule_arn: str):
1818
if is_aws_cloud():
1919
return
2020

21-
url = get_edge_url() + f"/_aws/events/rules/{rule_arn}/trigger"
21+
url = config.internal_service_url() + f"/_aws/events/rules/{rule_arn}/trigger"
2222
response = requests.get(url)
2323
if not response.ok:
2424
raise ValueError(

localstack/testing/aws/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def create_client_with_keys(
9393
aws_secret_access_key=keys["SecretAccessKey"],
9494
aws_session_token=keys.get("SessionToken"),
9595
config=client_config,
96-
endpoint_url=config.get_edge_url()
96+
endpoint_url=config.internal_service_url()
9797
if os.environ.get("TEST_TARGET") != "AWS_CLOUD"
9898
else None,
9999
)

localstack/testing/pytest/container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def container_factory() -> Generator[Co 10000 ntainerFactory, None, None]:
165165
factory.remove_all_containers()
166166

167167

168-
@pytest.fixture
168+
@pytest.fixture(scope="session")
169169
def wait_for_localstack_ready():
170170
def _wait_for(container: RunningContainer, timeout: Optional[float] = None):
171171
container.wait_until_ready(timeout)

localstack/testing/pytest/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def factory(
9898
resolver: EndpointResolver = aws_session._session.get_component("endpoint_resolver")
9999
endpoint_url = "https://" + resolver.construct_endpoint(service, region)["hostname"]
100100
else:
101-
endpoint_url = config.get_edge_url()
101+
endpoint_url = config.internal_service_url()
102102

103103
return SigningHttpClient(signer_factory(creds, service, region), endpoint_url=endpoint_url)
104104

@@ -1900,7 +1900,7 @@ def factory(**kwargs):
19001900
def assert_host_customisation(monkeypatch):
19011901
localstack_host = "foo.bar"
19021902
monkeypatch.setattr(
1903-
config, "LOCALSTACK_HOST", config.HostAndPort(host=localstack_host, port=config.EDGE_PORT)
1903+
config, "LOCALSTACK_HOST", config.HostAndPort(host=localstack_host, port=8888)
19041904
)
19051905

19061906
def asserter(

localstack/utils/aws/aws_stack.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,10 @@ def get_boto3_region() -> str:
164164

165165
def get_local_service_url(service_name_or_port: Union[str, int]) -> str:
166166
"""Return the local service URL for the given service name or port."""
167+
# TODO(srw): we don't need to differentiate on service name any more, so remove the argument
167168
if isinstance(service_name_or_port, int):
168169
return f"{config.get_protocol()}://{LOCALHOST}:{service_name_or_port}"
169-
service_name = service_name_or_port
170-
if service_name == "s3api":
171-
service_name = "s3"
172-
elif service_name == "runtime.sagemaker":
173-
service_name = "sagemaker-runtime"
174-
return config.service_url(service_name)
170+
return config.internal_service_url()
175171

176172

177173
def get_s3_hostname():

localstack/utils/kinesis/kinesis_connector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def get_stream_info(
305305
if aws_stack.is_local_env(env):
306306
stream_info["conn_kwargs"] = {
307307
"host": LOCALHOST,
308-
"port": config.service_port("kinesis"),
308+
"port": config.GATEWAY_LISTEN[0].port,
309309
"is_secure": bool(config.USE_SSL),
310310
}
311311
if endpoint_url:
@@ -375,8 +375,8 @@ def start_kcl_client_process(
375375
kwargs = {"metricsLevel": "NONE", "initialPositionInStream": "LATEST"}
376376
# set parameters for local connection
377377
if aws_stack.is_local_env(env):
378-
kwargs["kinesisEndpoint"] = config.get_edge_url(protocol="http")
379-
kwargs["dynamoDBEndpoint"] = config.get_edge_url(protocol="http")
378+
kwargs["kinesisEndpoint"] = config.internal_service_url(protocol="http")
379+
kwargs["dynamoDBEndpoint"] = config.internal_service_url(protocol="http")
380380
kwargs["disableCertChecking"] = "true"
381381
kwargs.update(configs)
382382
# create config file

localstack/utils/testutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def send_dynamodb_request(path, action, request_body):
500500
"dynamodb", aws_access_key_id=TEST_AWS_ACCESS_KEY_ID, region_name=TEST_AWS_REGION_NAME
501501
)["Authorization"],
502502
}
503-
url = f"{config.service_url('dynamodb')}/{path}"
503+
url = f"{config.internal_service_url()}/{path}"
504504
return requests.put(url, data=request_body, headers=headers, verify=False)
505505

506506

0 commit comments

Comments
 (0)
0