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

BI-4968: use a default clickhouse user with an empty username field #164

Merged
merged 2 commits into from
Dec 12, 2023
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

from marshmallow import fields as ma_fields

from dl_api_connector.api_schema.connection_base import ConnectionMetaMixin
from dl_api_connector.api_schema.connection_base_fields import secret_string_field
from dl_api_connector.api_schema.connection_mixins import (
DataExportForbiddenMixin,
RawSQLLevelMixin,
Expand All @@ -18,6 +21,14 @@ class ClickHouseConnectionSchema(
TARGET_CLS = ConnectionClickhouse
ALLOW_MULTI_HOST = True

username = ma_fields.String(attribute="data.username", allow_none=True, bi_extra=FieldExtra(editable=True))
password = secret_string_field(
attribute="data.password",
required=False,
allow_none=True,
bi_extra=FieldExtra(editable=True),
)

secure = core_ma_fields.OnOffField(attribute="data.secure", bi_extra=FieldExtra(editable=True))
ssl_ca = core_ma_fields.Base64StringField(
attribute="data.ssl_ca",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

SOURCE_TYPE_CH_TABLE = DataSourceType.declare("CH_TABLE")
SOURCE_TYPE_CH_SUBSELECT = DataSourceType.declare("CH_SUBSELECT")

DEFAULT_CLICKHOUSE_USER = "default"
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def make_clickhouse_saved_connection(
db_name: Optional[str],
host: str,
port: Optional[int],
username: Optional[str],
password: Optional[str],
username: Optional[str] = None,
password: Optional[str] = None,
raw_sql_level: RawSQLLevel = RawSQLLevel.off,
secure: bool = False,
ssl_ca: Optional[str] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from dl_i18n.localizer_base import Localizer

from dl_connector_clickhouse.core.clickhouse.constants import (
DEFAULT_CLICKHOUSE_USER,
SOURCE_TYPE_CH_SUBSELECT,
SOURCE_TYPE_CH_TABLE,
)
from dl_connector_clickhouse.core.clickhouse_base.dto import ClickHouseConnDTO
from dl_connector_clickhouse.core.clickhouse_base.us_connection import ConnectionClickhouseBase


Expand All @@ -27,6 +29,13 @@ class ConnectionClickhouse(ConnectionClickhouseBase):
def get_data_source_template_templates(self, localizer: Localizer) -> list[DataSourceTemplate]:
return self._make_subselect_templates(source_type=SOURCE_TYPE_CH_SUBSELECT, localizer=localizer)

def get_conn_dto(self) -> ClickHouseConnDTO:
base_dto = super().get_conn_dto()
return base_dto.clone(
username=base_dto.username or DEFAULT_CLICKHOUSE_USER,
password=base_dto.password or "",
)

@property
def allow_public_usage(self) -> bool:
return True
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def connection_params(self) -> dict:
)


class ClickHouseConnectionDefaultUserTestBase(ClickHouseConnectionTestBase):
@pytest.fixture(scope="class")
def connection_params(self) -> dict:
return dict(
db_name=CoreConnectionSettings.DB_NAME,
host=CoreConnectionSettings.HOST,
port=CoreConnectionSettings.PORT,
)


class ClickHouseDashSQLConnectionTest(ClickHouseConnectionTestBase):
raw_sql_level = RawSQLLevel.dashsql

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from dl_api_lib_testing.connector.connection_suite import DefaultConnectorConnectionTestSuite

from dl_connector_clickhouse_tests.db.api.base import ClickHouseConnectionTestBase
from dl_connector_clickhouse_tests.db.api.base import (
ClickHouseConnectionDefaultUserTestBase,
ClickHouseConnectionTestBase,
)


class TestClickHouseConnection(ClickHouseConnectionTestBase, DefaultConnectorConnectionTestSuite):
pass


class TestClickHouseDefaultUserConnection(ClickHouseConnectionDefaultUserTestBase, DefaultConnectorConnectionTestSuite):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ def saved_connection(
return conn


class BaseClickHouseDefaultUserTestClass(BaseClickHouseTestClass):
@pytest.fixture(scope="function")
def connection_creation_params(self) -> dict:
return dict(
db_name=test_config.CoreConnectionSettings.DB_NAME,
host=test_config.CoreConnectionSettings.HOST,
port=test_config.CoreConnectionSettings.PORT,
)


class BaseSslClickHouseTestClass(BaseClickHouseTestClass):
@pytest.fixture(autouse=True)
def clear_ssl_folder(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dl_core.us_connection_base import DataSourceTemplate
from dl_core_testing.testcases.connection import DefaultConnectionTestClass

from dl_connector_clickhouse.core.clickhouse.constants import DEFAULT_CLICKHOUSE_USER
from dl_connector_clickhouse.core.clickhouse.us_connection import ConnectionClickhouse
from dl_connector_clickhouse_tests.db.core.base import (
BaseClickHouseDefaultUserTestClass,
BaseClickHouseTestClass,
BaseSslClickHouseTestClass,
)
Expand Down Expand Up @@ -42,6 +44,15 @@ def check_data_source_templates(
assert "system" not in tmpl_db_names


class TestClickHouseDefaultUserConnection(BaseClickHouseDefaultUserTestClass, TestClickHouseConnection):
def check_saved_connection(self, conn: ConnectionClickhouse, params: dict) -> None:
assert conn.uuid is not None
assert conn.data.db_name == params["db_name"]
assert conn.data.username is None
assert conn.data.secure is False
assert conn.data.ssl_ca is None


@pytest.mark.skipif(os.environ.get("WE_ARE_IN_CI"), reason="can't use localhost")
class TestSslClickHouseConnection(
BaseSslClickHouseTestClass,
Expand Down
11 changes: 9 additions & 2 deletions lib/dl_core/dl_core/connection_models/dto_defs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from __future__ import annotations

from typing import (
Any,
ClassVar,
List,
Optional,
TypeVar,
)

import attr

from dl_constants.enums import ConnectionType


_CONN_DTO_TV = TypeVar("_CONN_DTO_TV")


@attr.s(frozen=True)
class ConnDTO:
conn_type: ClassVar[ConnectionType]
Expand All @@ -22,6 +26,9 @@ def conn_reporting_data(self) -> dict:
connection_id=self.conn_id,
)

def clone(self: _CONN_DTO_TV, **kwargs: Any) -> _CONN_DTO_TV:
return attr.evolve(self, **kwargs)


@attr.s(frozen=True)
class DefaultSQLDTO(ConnDTO): # noqa
Expand All @@ -32,7 +39,7 @@ class DefaultSQLDTO(ConnDTO): # noqa
username: str = attr.ib(kw_only=True)
password: str = attr.ib(repr=False, kw_only=True)

def get_all_hosts(self) -> List[str]:
def get_all_hosts(self) -> list[str]:
return list(self.multihosts) if self.multihosts else [self.host] if self.host else []

def conn_reporting_data(self) -> dict:
Expand Down
Loading