potiuk commented on code in PR #33531:
URL: https://github.com/apache/airflow/pull/33531#discussion_r1299355699
##########
airflow/models/connection.py:
##########
@@ -189,27 +211,63 @@ def _normalize_conn_type(conn_type):
return conn_type
def _parse_from_uri(self, uri: str):
- schemes_count_in_uri = uri.count("://")
- if schemes_count_in_uri > 2:
- raise AirflowException(f"Invalid connection string: {uri}.")
- host_with_protocol = schemes_count_in_uri == 2
- uri_parts = urlsplit(uri)
- conn_type = uri_parts.scheme
- self.conn_type = self._normalize_conn_type(conn_type)
- rest_of_the_url = uri.replace(f"{conn_type}://", ("" if
host_with_protocol else "//"))
- if host_with_protocol:
- uri_splits = rest_of_the_url.split("://", 1)
- if "@" in uri_splits[0] or ":" in uri_splits[0]:
- raise AirflowException(f"Invalid connection string: {uri}.")
- uri_parts = urlsplit(rest_of_the_url)
- protocol = uri_parts.scheme if host_with_protocol else None
- host = _parse_netloc_to_hostname(uri_parts)
- self.host = self._create_host(protocol, host)
- quoted_schema = uri_parts.path[1:]
- self.schema = unquote(quoted_schema) if quoted_schema else
quoted_schema
- self.login = unquote(uri_parts.username) if uri_parts.username else
uri_parts.username
- self.password = unquote(uri_parts.password) if uri_parts.password else
uri_parts.password
- self.port = uri_parts.port
+ if not uri:
+ raise AirflowException("Invalid (empty) - URI passed.")
+ special_treatment = False
+ if HOST_OR_HOST_PORT_ONLY_MATCHER.match(uri):
+ if not uri.startswith("//"):
+ uri = f"//{uri}"
+ special_treatment = True
+ first_colon_index = uri.find(":")
+ if (
+ not special_treatment
+ and first_colon_index != -1
+ and uri[first_colon_index : first_colon_index + 3] != "://"
+ ):
+ if not BASIC_URI_MATCHER.match(uri):
+ raise AirflowException(
+ f"Invalid URI: {uri}. Each URI must start with `<scheme>:`
where "
+ f"scheme consists of alphas, digits, +, -, . characters."
+ )
+ uri_parts = urlsplit(uri)
+ # Auth part is missing in case of "://"-less URI so no
login/password/port can be set
+ # we also pass the path directly to host
+ self.conn_type = self._normalize_conn_type(uri_parts.scheme)
+ self.schema = uri_parts.scheme
+ self.host = uri_parts.path
+ self.login = None
+ self.password = None
+ self.port = None
+ self.extract_query_part(uri_parts)
+ else:
+ schemes_count_in_uri = uri.count("://")
+ host_with_protocol = schemes_count_in_uri == 2
+ uri_parts = urlsplit(uri)
+ conn_type = uri_parts.scheme
+ self.conn_type = self._normalize_conn_type(conn_type)
+ rest_of_the_url = uri.replace(f"{conn_type}://", ("" if
host_with_protocol else "//"))
+ if host_with_protocol:
+ uri_splits = rest_of_the_url.split("://", 1)
+ if "@" in uri_splits[0] or ":" in uri_splits[0]:
+ raise AirflowException(
+ f"Invalid connection string: {uri}. "
+ f" The host cannot contain '@' or ':' :
{uri_splits[0]}"
+ )
+ uri_parts = urlsplit(rest_of_the_url)
+ protocol = uri_parts.scheme if host_with_protocol else None
+ host = _parse_netloc_to_hostname(uri_parts)
+ self.host = self._create_host(protocol, host)
+ quoted_schema = uri_parts.path[1:]
+ self.schema = unquote(quoted_schema) if quoted_schema else
quoted_schema
+ if not self.schema:
Review Comment:
This is what I have added here @hussein-awala -> I think it much better
reflects now what we want to do - especially it makes it possible to do `conn2
= Connection(uri=conn1.get_uri())` and have `conn2 == conn1` broadly speaking.
This whole "legacy" code is rather complex and only needed because we are
(for backwards compatibility reasons) extracting login/passwords from some urls
that do not really follow RFC3986 (i.e authority is retrieved from URIs like:
```
type://protocol://user:pass@host:123?param=value
```
Which makes no sense when you look at it from the RFC perspective.
But I think the way I have it now is kinda/sorta "all-in".
* it handles the RFC-compliant URLS
* it adds special treatment for "localhost" and "localhost:port"
* It adds special treatemant for
"type://protocol://user:pass@host:123?param=value" kind of URLs which are non
RFC-compliant (or ate least the user:pass:host:port are not part of the
authority and should not be treated as authority components.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]