Skip to content
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

Filter out invalid schemas in Hive hook #27647

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions airflow/providers/apache/hive/hooks/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ def run_cli(
"""
conn = self.conn
schema = schema or conn.schema
if "!" in schema or ";" in schema:
raise RuntimeError(f"The schema `{schema}` contains invalid characters (!;)")
if schema:
hql = f"USE {schema};\n{hql}"

Expand Down
15 changes: 15 additions & 0 deletions tests/providers/apache/hive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ def __init__(self, *args, **kwargs):
self.get_connection = MagicMock(return_value=MockDBConnection({}))


class InvalidHiveCliHook(HiveCliHook):
def __init__(self, *args, **kwargs):
super().__init__()
self.conn = MockConnectionCursor()
self.conn.schema = "default;"
self.conn.host = "localhost"
self.conn.port = 10000
self.conn.login = None
self.conn.password = None

self.conn.execute = MagicMock()
self.get_conn = MagicMock(return_value=self.conn)
self.get_connection = MagicMock(return_value=MockDBConnection({}))


class MockHiveServer2Hook(HiveServer2Hook):
def __init__(self, *args, **kwargs):
super().__init__()
Expand Down
6 changes: 6 additions & 0 deletions tests/providers/apache/hive/hooks/test_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from airflow.utils.operator_helpers import AIRFLOW_VAR_NAME_FORMAT_MAPPING
from tests.providers.apache.hive import (
BaseMockConnectionCursor,
InvalidHiveCliHook,
MockHiveCliHook,
MockHiveServer2Hook,
MockSubProcess,
Expand Down Expand Up @@ -131,6 +132,11 @@ def test_run_cli(self, mock_popen, mock_temp_dir):
close_fds=True,
)

def test_hive_cli_hook_invalid_schema(self):
with pytest.raises(RuntimeError):
hook = InvalidHiveCliHook()
hook.run_cli("SHOW DATABASES")

@mock.patch("subprocess.Popen")
def test_run_cli_with_hive_conf(self, mock_popen):
hql = (
Expand Down