This is an automated email from the ASF dual-hosted git repository.
xddeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/master by this push:
new 823b3aa Reject 'connections add' CLI request if URI provided is
invalid (#12370)
823b3aa is described below
commit 823b3aace298ab13d2e19b8f0bf1c426ff20407c
Author: Xiaodong DENG <[email protected]>
AuthorDate: Sun Nov 15 11:47:57 2020 +0100
Reject 'connections add' CLI request if URI provided is invalid (#12370)
The validity is decided by availability of both 'scheme' and 'netloc' in
the parse result
---
airflow/cli/commands/connection_command.py | 17 ++++++++++++++---
tests/cli/commands/test_connection_command.py | 7 +++++++
2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/airflow/cli/commands/connection_command.py
b/airflow/cli/commands/connection_command.py
index 6708806..7ca9f1a 100644
--- a/airflow/cli/commands/connection_command.py
+++ b/airflow/cli/commands/connection_command.py
@@ -20,7 +20,7 @@ import json
import os
import sys
from typing import List
-from urllib.parse import urlunparse
+from urllib.parse import urlparse, urlunparse
import pygments
import yaml
@@ -136,6 +136,14 @@ def _is_stdout(fileio: io.TextIOWrapper) -> bool:
return False
+def _valid_uri(uri: str) -> bool:
+ """Check if a URI is valid, by checking if both scheme and netloc are
available"""
+ uri_parts = urlparse(uri)
+ if uri_parts.scheme == '' or uri_parts.netloc == '':
+ return False
+ return True
+
+
def connections_export(args):
"""Exports all connections to a file"""
allowed_formats = ['.yaml', '.json', '.env']
@@ -177,16 +185,19 @@ def connections_add(args):
missing_args = []
invalid_args = []
if args.conn_uri:
+ if not _valid_uri(args.conn_uri):
+ msg = f'The URI provided to --conn-uri is invalid: {args.conn_uri}'
+ raise SystemExit(msg)
for arg in alternative_conn_specs:
if getattr(args, arg) is not None:
invalid_args.append(arg)
elif not args.conn_type:
missing_args.append('conn-uri or conn-type')
if missing_args:
- msg = 'The following args are required to add a connection:' + f'
{missing_args!r}'
+ msg = f'The following args are required to add a connection:
{missing_args!r}'
raise SystemExit(msg)
if invalid_args:
- msg = 'The following args are not compatible with the ' + 'add flag
and --conn-uri flag: {invalid!r}'
+ msg = 'The following args are not compatible with the add flag and
--conn-uri flag: {invalid!r}'
msg = msg.format(invalid=invalid_args)
raise SystemExit(msg)
diff --git a/tests/cli/commands/test_connection_command.py
b/tests/cli/commands/test_connection_command.py
index d711226..779f4ac 100644
--- a/tests/cli/commands/test_connection_command.py
+++ b/tests/cli/commands/test_connection_command.py
@@ -646,6 +646,13 @@ class TestCliAddConnections(unittest.TestCase):
):
connection_command.connections_add(self.parser.parse_args(["connections",
"add", "new1"]))
+ def test_cli_connections_add_invalid_uri(self):
+ # Attempt to add with invalid uri
+ with self.assertRaisesRegex(SystemExit, r"The URI provided to
--conn-uri is invalid: nonsense_uri"):
+ connection_command.connections_add(
+ self.parser.parse_args(["connections", "add", "new1",
"--conn-uri=%s" % "nonsense_uri"])
+ )
+
class TestCliDeleteConnections(unittest.TestCase):
@classmethod