-
Notifications
You must be signed in to change notification settings - Fork 130
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
Updated endpoint parsing #618
Merged
berndverst
merged 13 commits into
dapr:master
from
elena-kolevska:updated-endpoint-parsing
Oct 31, 2023
+383
−244
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
af3c06c
wip
elena-kolevska 60b7b70
Updates endpoint parsing for new spec
elena-kolevska bc7c880
Adds support for unix URIs. Clean up.
elena-kolevska eb362ac
Completes support for all valid grpc endpoints
elena-kolevska 65a8955
Code cleanup
elena-kolevska 2d8d80b
Adds a warning about the http and https schemes being deprecated for …
elena-kolevska af1189a
Updates docs
elena-kolevska bc85dbc
Updates the docs for clarity and correctness
elena-kolevska 71d229b
Adds anoter test for vsock without a port
elena-kolevska d52c1a9
Adds more test cases and handles dns with ipv6
elena-kolevska 8874681
Code cleanup
elena-kolevska cb7fe97
Fixes linter
elena-kolevska 59b4e42
Fixes linter errors
elena-kolevska 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 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 |
---|---|---|
|
@@ -106,3 +106,6 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# OSX specific files | ||
.DS_Store |
This file contains 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 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 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 |
---|---|---|
@@ -1,56 +1,131 @@ | ||
from typing import Tuple | ||
|
||
|
||
def parse_endpoint(address: str) -> Tuple[str, str, int]: | ||
scheme = "http" | ||
fqdn = "localhost" | ||
port = 80 | ||
addr = address | ||
|
||
addr_list = address.split("://") | ||
|
||
if len(addr_list) == 2: | ||
# A scheme was explicitly specified | ||
scheme = addr_list[0] | ||
if scheme == "https": | ||
port = 443 | ||
addr = addr_list[1] | ||
|
||
addr_list = addr.split(":") | ||
if len(addr_list) == 2: | ||
# A port was explicitly specified | ||
if len(addr_list[0]) > 0: | ||
fqdn = addr_list[0] | ||
# Account for Endpoints of the type http://localhost:3500/v1.0/invoke | ||
addr_list = addr_list[1].split("/") | ||
port = addr_list[0] # type: ignore | ||
elif len(addr_list) == 1: | ||
# No port was specified | ||
# Account for Endpoints of the type :3500/v1.0/invoke | ||
addr_list = addr_list[0].split("/") | ||
fqdn = addr_list[0] | ||
else: | ||
# IPv6 address | ||
addr_list = addr.split("]:") | ||
if len(addr_list) == 2: | ||
# A port was explicitly specified | ||
fqdn = addr_list[0] | ||
fqdn = fqdn.replace("[", "") | ||
|
||
addr_list = addr_list[1].split("/") | ||
port = addr_list[0] # type: ignore | ||
elif len(addr_list) == 1: | ||
# No port was specified | ||
addr_list = addr_list[0].split("/") | ||
fqdn = addr_list[0] | ||
fqdn = fqdn.replace("[", "") | ||
fqdn = fqdn.replace("]", "") | ||
from urllib.parse import urlparse, parse_qs | ||
|
||
|
||
class URIParseConfig: | ||
DEFAULT_SCHEME = "dns" | ||
DEFAULT_HOSTNAME = "localhost" | ||
DEFAULT_PORT = 443 | ||
DEFAULT_TLS = False | ||
DEFAULT_AUTHORITY = "" | ||
ACCEPTED_SCHEMES = ["dns", "unix", "unix-abstract", "vsock", "http", "https", "grpc", "grpcs"] | ||
VALID_SCHEMES = ["dns", "unix", "unix-abstract", "vsock", "grpc", "grpcs"] | ||
|
||
|
||
class GrpcEndpoint: | ||
def __init__(self, url: str): | ||
self.authority = URIParseConfig.DEFAULT_AUTHORITY | ||
self.url = url | ||
|
||
url = self.preprocess_url(url) | ||
parsed_url = urlparse(url) | ||
validate_path_and_query(parsed_url.path, parsed_url.query, parsed_url.scheme) | ||
tls = extract_tls_from_query(parsed_url.query, parsed_url.scheme) | ||
|
||
self.scheme = parsed_url.scheme or URIParseConfig.DEFAULT_SCHEME | ||
self.hostname = parsed_url.hostname or URIParseConfig.DEFAULT_HOSTNAME | ||
self.port = parsed_url.port or URIParseConfig.DEFAULT_PORT | ||
self.tls = tls or URIParseConfig.DEFAULT_TLS | ||
|
||
def is_secure(self) -> bool: | ||
return self.tls | ||
|
||
def get_scheme(self) -> str: | ||
return self.scheme if self.scheme in URIParseConfig.VALID_SCHEMES \ | ||
else URIParseConfig.DEFAULT_SCHEME | ||
|
||
def get_port(self) -> str: | ||
port = self.get_port_as_int() | ||
if port == 0: | ||
return "" | ||
|
||
return str(port) | ||
|
||
def get_port_as_int(self) -> int: | ||
if self.scheme in ["unix", "unix-abstract"]: | ||
return 0 | ||
|
||
return self.port | ||
|
||
def get_hostname(self) -> str: | ||
hostname = self.hostname | ||
if self.hostname.count(":") == 7: | ||
# IPv6 address | ||
hostname = f"[{hostname}]" | ||
return hostname | ||
|
||
def get_endpoint(self) -> str: | ||
scheme = self.get_scheme() | ||
port = "" if len(self.get_port()) == 0 else f":{self.port}" | ||
|
||
if scheme == "unix": | ||
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. Is there a warning if the user uses the deprecated "https" or "http" schemas? 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. |
||
separator = "://" if self.url.startswith("unix://") else ":" | ||
return f"{scheme}{separator}{self.hostname}" | ||
|
||
if scheme == "vsock": | ||
port = "" if self.port == 0 else f":{self.port}" | ||
return f"{scheme}:{self.get_hostname()}{port}" | ||
|
||
if scheme == "unix-abstract": | ||
return f"{scheme}:{self.get_hostname()}{port}" | ||
|
||
if scheme == "dns": | ||
authority = f"//{self.authority}/" if self.authority else "" | ||
return f"{scheme}:{authority}{self.get_hostname()}{port}" | ||
|
||
return f"{scheme}:{self.get_hostname()}{port}" | ||
|
||
def preprocess_url(self, url: str) -> str: | ||
url_list = url.split(":") | ||
if len(url_list) == 3 and "://" not in url: | ||
# A URI like dns:mydomain:5000 or vsock:mycid:5000 was used | ||
url = url.replace(":", "://", 1) | ||
elif len(url_list) == 2 and "://" not in url and url_list[ | ||
0] in URIParseConfig.ACCEPTED_SCHEMES: | ||
# A URI like dns:mydomain was used | ||
url = url.replace(":", "://", 1) | ||
else: | ||
raise ValueError(f"Invalid address: {address}") | ||
url_list = url.split("://") | ||
if len(url_list) == 1: | ||
# If a scheme was not explicitly specified in the URL | ||
# we need to add a default scheme, | ||
# because of how urlparse works | ||
url = f'{URIParseConfig.DEFAULT_SCHEME}://{url}' | ||
else: | ||
# If a scheme was explicitly specified in the URL | ||
# we need to make sure it is a valid scheme | ||
scheme = url_list[0] | ||
if scheme not in URIParseConfig.ACCEPTED_SCHEMES: | ||
raise ValueError(f"Invalid scheme '{scheme}' in URL '{url}'") | ||
|
||
# We should do a special check if the scheme is dns, and it uses | ||
# an authority in the format of dns:[//authority/]host[:port] | ||
if scheme.lower() == "dns": | ||
# A URI like dns://authority/mydomain was used | ||
url_list = url.split("/") | ||
if len(url_list) < 4: | ||
raise ValueError(f"Invalid dns authority '{url_list[2]}' in URL '{url}'") | ||
self.authority = url_list[2] | ||
url = f'dns://{url_list[3]}' | ||
return url | ||
|
||
|
||
def validate_path_and_query(path: str, query: str, scheme: str) -> None: | ||
if path: | ||
raise ValueError(f"Paths are not supported for gRPC endpoints: '{path}'") | ||
if query: | ||
query_dict = parse_qs(query) | ||
if 'tls' in query_dict and scheme in ["http", "https"]: | ||
raise ValueError( | ||
f"The tls query parameter is not supported for http(s) endpoints: '{query}'") | ||
query_dict.pop('tls', None) | ||
if query_dict: | ||
raise ValueError(f"Query parameters are not supported for gRPC endpoints: '{query}'") | ||
|
||
try: | ||
port = int(port) | ||
except ValueError: | ||
raise ValueError(f"invalid port: {port}") | ||
|
||
return scheme, fqdn, port | ||
def extract_tls_from_query(query: str, scheme: str) -> bool: | ||
query_dict = parse_qs(query) | ||
tls_str = query_dict.get('tls', [""])[0] | ||
tls = tls_str.lower() == 'true' | ||
if scheme == "https": | ||
tls = True | ||
return tls |
This file contains 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
Oops, something went wrong.
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.
cc @elena-kolevska
something's off here, both
grpc
andgrpcs
are not present in the naming resolution doc and are also missing in the go-sdk: https://github.com/dapr/go-sdk/blob/main/client/internal/parse.go#L160There 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.
Fixed in this PR: #700