-
Notifications
You must be signed in to change notification settings - Fork 12
Remove deprecated ProxyListener for starting local aws-replicator proxy server #38
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4211e34
remove deprecated ProxyListener for starting local aws-replicator pro…
whummer 2b36f77
fix response formats
whummer 2805420
update test setup
whummer fd57b8a
add tmp CI debugging
whummer b72de8b
add bind_host to proxy config
whummer 0549b13
add --host parameter to proxy CLI command
whummer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,17 +19,20 @@ | |
from localstack.aws.spec import load_service | ||
from localstack.config import get_edge_url | ||
from localstack.constants import AWS_REGION_US_EAST_1, DOCKER_IMAGE_NAME_PRO | ||
from localstack.services.generic_proxy import ProxyListener, start_proxy_server | ||
from localstack.http import Request | ||
from localstack.utils.aws.aws_responses import requests_response | ||
from localstack.utils.bootstrap import setup_logging | ||
from localstack.utils.collections import select_attributes | ||
from localstack.utils.container_utils.container_client import PortMappings | ||
from localstack.utils.docker_utils import DOCKER_CLIENT, reserve_available_container_port | ||
from localstack.utils.files import new_tmp_file, save_file | ||
from localstack.utils.functions import run_safe | ||
from localstack.utils.net import get_free_tcp_port | ||
from localstack.utils.server.http2_server import run_server | ||
from localstack.utils.serving import Server | ||
from localstack.utils.strings import short_uid, to_str, truncate | ||
from localstack_ext.bootstrap.licensing import ENV_LOCALSTACK_API_KEY | ||
from requests import Response | ||
|
||
from aws_replicator.client.utils import truncate_content | ||
from aws_replicator.config import HANDLER_PATH_PROXIES | ||
|
@@ -47,6 +50,9 @@ | |
CONTAINER_CONFIG_FILE = "/tmp/ls.aws.proxy.yml" | ||
CONTAINER_LOG_FILE = "/tmp/ls-aws-proxy.log" | ||
|
||
# default bind host if `bind_host` is not specified for the proxy | ||
DEFAULT_BIND_HOST = "127.0.0.1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe this extension may benefit from also using |
||
|
||
|
||
class AuthProxyAWS(Server): | ||
def __init__(self, config: ProxyConfig, port: int = None): | ||
|
@@ -55,34 +61,30 @@ def __init__(self, config: ProxyConfig, port: int = None): | |
super().__init__(port=port) | ||
|
||
def do_run(self): | ||
class Handler(ProxyListener): | ||
def forward_request(_self, method, path, data, headers): | ||
return self.proxy_request(method, path, data, headers) | ||
|
||
self.register_in_instance() | ||
# TODO: change to using Gateway! | ||
proxy = start_proxy_server(self.port, update_listener=Handler()) | ||
bind_host = self.config.get("bind_host") or DEFAULT_BIND_HOST | ||
proxy = run_server(port=self.port, bind_addresses=[bind_host], handler=self.proxy_request) | ||
proxy.join() | ||
|
||
def proxy_request(self, method, path, data, headers): | ||
parsed = self._extract_region_and_service(headers) | ||
def proxy_request(self, request: Request, data: bytes) -> Response: | ||
parsed = self._extract_region_and_service(request.headers) | ||
if not parsed: | ||
return 400 | ||
return requests_response("", status_code=400) | ||
region_name, service_name = parsed | ||
|
||
LOG.debug( | ||
"Proxying request to %s (%s): %s %s", | ||
service_name, | ||
region_name, | ||
method, | ||
path, | ||
request.method, | ||
request.path, | ||
) | ||
|
||
path, _, query_string = path.partition("?") | ||
path, _, query_string = request.path.partition("?") | ||
request = HttpRequest( | ||
body=data, | ||
method=method, | ||
headers=headers, | ||
method=request.method, | ||
headers=request.headers, | ||
path=path, | ||
query_string=query_string, | ||
) | ||
|
@@ -104,7 +106,7 @@ def proxy_request(self, method, path, data, headers): | |
LOG.debug( | ||
"Sending request for service %s to AWS: %s %s - %s - %s", | ||
service_name, | ||
method, | ||
request.method, | ||
aws_request.url, | ||
truncate_content(request_dict.get("body"), max_length=500), | ||
headers_truncated, | ||
|
@@ -113,11 +115,12 @@ def proxy_request(self, method, path, data, headers): | |
# send request to upstream AWS | ||
result = client._endpoint.make_request(operation_model, request_dict) | ||
|
||
# create response object | ||
response = requests.Response() | ||
response.status_code = result[0].status_code | ||
response._content = result[0].content | ||
response.headers = dict(result[0].headers) | ||
# create response object - TODO: to be replaced with localstack.http.Response over time | ||
response = requests_response( | ||
result[0].content, | ||
status_code=result[0].status_code, | ||
headers=dict(result[0].headers), | ||
) | ||
|
||
LOG.debug( | ||
"Received response for service %s from AWS: %s - %s", | ||
|
@@ -129,7 +132,7 @@ def proxy_request(self, method, path, data, headers): | |
except Exception as e: | ||
if LOG.isEnabledFor(logging.DEBUG): | ||
LOG.exception("Error when making request to AWS service %s: %s", service_name, e) | ||
return 400 | ||
return requests_response("", status_code=400) | ||
|
||
def register_in_instance(self): | ||
port = getattr(self, "port", None) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be missing
enable
here