-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
120 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from datetime import datetime, timezone, timedelta | ||
from os import environ | ||
|
||
import jwt | ||
import pytest | ||
|
||
from clickhouse_connect.driver import create_client, ProgrammingError, create_async_client | ||
from tests.integration_tests.conftest import TestConfig | ||
|
||
|
||
def test_jwt_auth_sync_client(test_config: TestConfig): | ||
if not test_config.cloud: | ||
pytest.skip('Skipping JWT test in non-Cloud mode') | ||
|
||
access_token = make_access_token() | ||
client = create_client( | ||
host=test_config.host, | ||
port=test_config.port, | ||
access_token=access_token | ||
) | ||
result = client.query(query=CHECK_CLOUD_MODE_QUERY).result_set | ||
assert result == [(True,)] | ||
|
||
|
||
def test_jwt_auth_sync_client_config_errors(): | ||
with pytest.raises(ProgrammingError): | ||
create_client( | ||
username='bob', | ||
access_token='foobar' | ||
) | ||
with pytest.raises(ProgrammingError): | ||
create_client( | ||
username='bob', | ||
password='secret', | ||
access_token='foo' | ||
) | ||
with pytest.raises(ProgrammingError): | ||
create_client( | ||
password='secret', | ||
access_token='foo' | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_jwt_auth_async_client(test_config: TestConfig): | ||
if not test_config.cloud: | ||
pytest.skip('Skipping JWT test in non-Cloud mode') | ||
|
||
access_token = make_access_token() | ||
client = await create_async_client( | ||
host=test_config.host, | ||
port=test_config.port, | ||
access_token=access_token | ||
) | ||
result = (await client.query(query=CHECK_CLOUD_MODE_QUERY)).result_set | ||
assert result == [(True,)] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_jwt_auth_async_client_config_errors(): | ||
with pytest.raises(ProgrammingError): | ||
await create_async_client( | ||
username='bob', | ||
access_token='foobar' | ||
) | ||
with pytest.raises(ProgrammingError): | ||
await create_async_client( | ||
username='bob', | ||
password='secret', | ||
access_token='foo' | ||
) | ||
with pytest.raises(ProgrammingError): | ||
await create_async_client( | ||
password='secret', | ||
access_token='foo' | ||
) | ||
|
||
|
||
CHECK_CLOUD_MODE_QUERY = "SELECT value='1' FROM system.settings WHERE name='cloud_mode'" | ||
JWT_SECRET_ENV_KEY = 'CLICKHOUSE_CONNECT_TEST_JWT_SECRET' | ||
|
||
|
||
def make_access_token(): | ||
secret = environ.get(JWT_SECRET_ENV_KEY) | ||
if not secret: | ||
raise ValueError(f'{JWT_SECRET_ENV_KEY} environment variable is not set') | ||
payload = { | ||
'iss': 'ClickHouse', | ||
'sub': 'CI_Test', | ||
'aud': '1f7f78b8-da67-480b-8913-726fdd31d2fc', | ||
'clickhouse:roles': ['default'], | ||
'clickhouse:grants': [], | ||
'exp': datetime.now(tz=timezone.utc) + timedelta(minutes=15) | ||
} | ||
return jwt.encode(payload, secret, algorithm='RS256') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters