Skip to content

Commit

Permalink
Add CLICKHOUSE_SECURE env var
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayak-mehta committed Dec 6, 2024
1 parent fd68529 commit 166bcfa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
25 changes: 20 additions & 5 deletions src/houseplant/clickhouse_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,35 @@ def __init__(self, database):


class ClickHouseClient:
def __init__(self, host=None, database=None, port=None):
def __init__(
self, host=None, port=None, database=None, user=None, password=None, secure=None
):
self.host = host or os.getenv("CLICKHOUSE_HOST", "localhost")
# Parse port from host:port string if present, otherwise use port parameter or default
if ":" in self.host:
self.host, self.port = self.host.split(":")
self.host, port_str = self.host.split(":")
self.port = int(port_str)
else:
self.port = int(port or os.getenv("CLICKHOUSE_PORT", 9000))

self.port = port or os.getenv("CLICKHOUSE_PORT", 9000)
self.database = database or os.getenv("CLICKHOUSE_DB", "development")

self.user = user or os.getenv("CLICKHOUSE_USER", "default")
self.password = password or os.getenv("CLICKHOUSE_PASSWORD", "")

# Use SSL port by default if secure
self.secure = secure or os.getenv("CLICKHOUSE_SECURE", "n").lower()
self.secure = self.secure in ("true", "t", "yes", "y", "1")
self.port = 9440 if self.secure else self.port

self.client = Client(
host=self.host,
port=self.port,
database=self.database,
user=os.getenv("CLICKHOUSE_USER", "default"),
password=os.getenv("CLICKHOUSE_PASSWORD", ""),
user=self.user,
password=self.password,
secure=self.secure,
verify=False,
)

self._cluster = None
Expand Down
53 changes: 48 additions & 5 deletions tests/test_clickhouse_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import pytest
from clickhouse_driver.errors import NetworkError, ServerException

from houseplant.clickhouse_client import (
ClickHouseAuthenticationError,
ClickHouseConnectionError,
ClickHouseDatabaseNotFoundError,
)
from houseplant.clickhouse_client import (ClickHouseAuthenticationError,
ClickHouseConnectionError,
ClickHouseDatabaseNotFoundError)


@pytest.fixture
Expand All @@ -15,6 +13,51 @@ def migrations_table(ch_client):
return ch_client


def test_port_parsing_from_host():
"""Test port parsing from host string."""
from houseplant.clickhouse_client import ClickHouseClient

client = ClickHouseClient(host="localhost:1234")
assert client.host == "localhost"
assert client.port == 1234


def test_default_port(monkeypatch):
"""Test default port selection."""
from houseplant.clickhouse_client import ClickHouseClient

# Test default non-secure port
client = ClickHouseClient()
assert client.port == 9000

# Test default secure port
monkeypatch.setenv("CLICKHOUSE_SECURE", "true")
client = ClickHouseClient()
assert client.port == 9440


def test_port_precedence(monkeypatch):
"""Test port parameter precedence."""
from houseplant.clickhouse_client import ClickHouseClient

# Host:port should override port parameter and env var
monkeypatch.setenv("CLICKHOUSE_PORT", "8000")
client = ClickHouseClient(host="localhost:7000", port=6000)
assert client.port == 7000

# Host:port should override env var
client = ClickHouseClient(host="localhost:7000")
assert client.port == 7000

# Port parameter should override env var
client = ClickHouseClient(host="localhost", port=7000)
assert client.port == 7000

# Env var should be used if no other port specified
client = ClickHouseClient(host="localhost")
assert client.port == 8000


def test_connection_error(monkeypatch):
"""Test connection error handling."""
monkeypatch.setenv("CLICKHOUSE_HOST", "invalid_host")
Expand Down

0 comments on commit 166bcfa

Please sign in to comment.