ClickHouse/tests/integration/test_named_collections/test.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

455 lines
14 KiB
Python
Raw Normal View History

2022-11-16 17:27:13 +00:00
import logging
import pytest
import os
2022-11-17 14:33:06 +00:00
import time
2022-11-16 17:27:13 +00:00
from helpers.cluster import ClickHouseCluster
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
NAMED_COLLECTIONS_CONFIG = os.path.join(
SCRIPT_DIR, "./configs/config.d/named_collections.xml"
)
@pytest.fixture(scope="module")
def cluster():
try:
cluster = ClickHouseCluster(__file__)
cluster.add_instance(
"node",
main_configs=[
"configs/config.d/named_collections.xml",
],
2022-11-17 14:33:06 +00:00
user_configs=[
"configs/users.d/users.xml",
],
2022-11-16 17:27:13 +00:00
stay_alive=True,
)
2023-02-03 12:11:21 +00:00
cluster.add_instance(
"node_no_default_access",
main_configs=[
"configs/config.d/named_collections.xml",
],
user_configs=[
"configs/users.d/users_no_default_access.xml",
],
stay_alive=True,
)
cluster.add_instance(
"node_no_default_access_but_with_access_management",
main_configs=[
"configs/config.d/named_collections.xml",
],
user_configs=[
2023-02-07 12:01:15 +00:00
"configs/users.d/users_no_default_access_with_access_management.xml",
2023-02-03 12:11:21 +00:00
],
stay_alive=True,
)
2022-11-16 17:27:13 +00:00
logging.info("Starting cluster...")
cluster.start()
logging.info("Cluster started")
yield cluster
finally:
cluster.shutdown()
2023-02-03 12:11:21 +00:00
def replace_in_server_config(node, old, new):
2022-11-17 14:33:06 +00:00
node.replace_in_config(
"/etc/clickhouse-server/config.d/named_collections.xml",
old,
new,
)
2022-11-16 17:27:13 +00:00
2023-02-03 12:11:21 +00:00
def replace_in_users_config(node, old, new):
node.replace_in_config(
"/etc/clickhouse-server/users.d/users.xml",
old,
new,
)
2023-02-11 18:57:41 +00:00
def test_default_access(cluster):
2023-02-03 12:11:21 +00:00
node = cluster.instances["node_no_default_access"]
2023-02-11 18:57:41 +00:00
assert 0 == int(node.query("select count() from system.named_collections"))
2023-02-03 12:11:21 +00:00
node = cluster.instances["node_no_default_access_but_with_access_management"]
2023-02-11 18:57:41 +00:00
assert 0 == int(node.query("select count() from system.named_collections"))
2023-02-03 12:11:21 +00:00
node = cluster.instances["node"]
assert int(node.query("select count() from system.named_collections")) > 0
2023-02-11 18:57:41 +00:00
2023-02-03 12:11:21 +00:00
replace_in_users_config(
node, "show_named_collections>1", "show_named_collections>0"
)
assert "show_named_collections>0" in node.exec_in_container(
["bash", "-c", f"cat /etc/clickhouse-server/users.d/users.xml"]
)
node.restart_clickhouse()
2023-02-11 18:57:41 +00:00
assert 0 == int(node.query("select count() from system.named_collections"))
2023-02-03 12:11:21 +00:00
replace_in_users_config(
node, "show_named_collections>0", "show_named_collections>1"
)
assert "show_named_collections>1" in node.exec_in_container(
["bash", "-c", f"cat /etc/clickhouse-server/users.d/users.xml"]
)
node.restart_clickhouse()
2023-02-21 17:27:37 +00:00
assert (
node.query("select collection['key1'] from system.named_collections").strip()
== "value1"
)
replace_in_users_config(
node, "show_named_collections_secrets>1", "show_named_collections_secrets>0"
)
assert "show_named_collections_secrets>0" in node.exec_in_container(
["bash", "-c", f"cat /etc/clickhouse-server/users.d/users.xml"]
)
node.restart_clickhouse()
2023-02-21 17:27:37 +00:00
assert (
node.query("select collection['key1'] from system.named_collections").strip()
== "[HIDDEN]"
)
replace_in_users_config(
node, "show_named_collections_secrets>0", "show_named_collections_secrets>1"
)
assert "show_named_collections_secrets>1" in node.exec_in_container(
["bash", "-c", f"cat /etc/clickhouse-server/users.d/users.xml"]
)
node.restart_clickhouse()
2023-02-21 17:27:37 +00:00
assert (
node.query("select collection['key1'] from system.named_collections").strip()
== "value1"
)
2023-02-03 12:11:21 +00:00
2023-02-11 11:36:25 +00:00
def test_granular_access_show_query(cluster):
node = cluster.instances["node"]
assert 1 == int(node.query("SELECT count() FROM system.named_collections"))
assert (
"collection1" == node.query("SELECT name FROM system.named_collections").strip()
)
2023-02-11 18:57:41 +00:00
node.query("DROP USER IF EXISTS kek")
node.query("CREATE USER kek")
node.query("GRANT select ON *.* TO kek")
assert 0 == int(
node.query("SELECT count() FROM system.named_collections", user="kek")
)
node.query("GRANT show named collections ON collection1 TO kek")
assert 1 == int(
node.query("SELECT count() FROM system.named_collections", user="kek")
)
assert (
"collection1"
== node.query("SELECT name FROM system.named_collections", user="kek").strip()
)
node.query("CREATE NAMED COLLECTION collection2 AS key1=1, key2='value2'")
assert 2 == int(node.query("SELECT count() FROM system.named_collections"))
assert (
"collection1\ncollection2"
== node.query("select name from system.named_collections").strip()
)
assert 1 == int(
node.query("SELECT count() FROM system.named_collections", user="kek")
)
assert (
"collection1"
== node.query("select name from system.named_collections", user="kek").strip()
)
node.query("GRANT show named collections ON collection2 TO kek")
assert 2 == int(
node.query("SELECT count() FROM system.named_collections", user="kek")
)
assert (
"collection1\ncollection2"
== node.query("select name from system.named_collections", user="kek").strip()
)
node.restart_clickhouse()
assert (
"collection1\ncollection2"
== node.query("select name from system.named_collections", user="kek").strip()
)
2023-02-24 13:44:47 +00:00
# check:
# GRANT show named collections ON *
# REVOKE show named collections ON collection
2023-02-11 18:57:41 +00:00
node.query("DROP USER IF EXISTS koko")
node.query("CREATE USER koko")
node.query("GRANT select ON *.* TO koko")
assert 0 == int(
node.query("SELECT count() FROM system.named_collections", user="koko")
)
node.query("GRANT show named collections ON * TO koko")
assert (
"collection1\ncollection2"
== node.query("select name from system.named_collections", user="koko").strip()
)
node.restart_clickhouse()
assert (
"collection1\ncollection2"
== node.query("select name from system.named_collections", user="koko").strip()
)
2023-02-24 13:44:47 +00:00
node.query("REVOKE show named collections ON collection1 FROM koko;")
assert (
"collection2"
== node.query("select name from system.named_collections", user="koko").strip()
)
node.restart_clickhouse()
assert (
"collection2"
== node.query("select name from system.named_collections", user="koko").strip()
)
node.query("REVOKE show named collections ON collection2 FROM koko;")
assert (
"" == node.query("select * from system.named_collections", user="koko").strip()
)
# check:
# GRANT show named collections ON collection
# REVOKE show named collections ON *
node.query("GRANT show named collections ON collection2 TO koko")
assert (
"collection2"
== node.query("select name from system.named_collections", user="koko").strip()
)
node.query("REVOKE show named collections ON * FROM koko;")
assert (
"" == node.query("select * from system.named_collections", user="koko").strip()
)
node.query("DROP NAMED COLLECTION collection2")
2023-02-11 11:36:25 +00:00
def test_granular_access_create_alter_drop_query(cluster):
node = cluster.instances["node"]
2023-02-11 18:57:41 +00:00
node.query("DROP USER IF EXISTS kek")
2023-02-11 11:36:25 +00:00
node.query("CREATE USER kek")
node.query("GRANT select ON *.* TO kek")
assert 0 == int(
node.query("SELECT count() FROM system.named_collections", user="kek")
)
assert (
"DB::Exception: kek: Not enough privileges. To execute this query it's necessary to have grant CREATE NAMED COLLECTION"
in node.query_and_get_error(
"CREATE NAMED COLLECTION collection2 AS key1=1, key2='value2'", user="kek"
)
)
node.query("GRANT create named collection ON collection2 TO kek")
2023-02-11 18:57:41 +00:00
node.query(
2023-02-11 11:36:25 +00:00
"CREATE NAMED COLLECTION collection2 AS key1=1, key2='value2'", user="kek"
)
assert 0 == int(
node.query("select count() from system.named_collections", user="kek")
)
node.query("GRANT show named collections ON collection2 TO kek")
2023-02-11 18:57:41 +00:00
assert (
"collection2"
== node.query("select name from system.named_collections", user="kek").strip()
)
assert (
"1"
== node.query(
"select collection['key1'] from system.named_collections where name = 'collection2'"
).strip()
)
assert (
"DB::Exception: kek: Not enough privileges. To execute this query it's necessary to have grant ALTER NAMED COLLECTION"
in node.query_and_get_error(
"ALTER NAMED COLLECTION collection2 SET key1=2", user="kek"
)
)
node.query("GRANT alter named collection ON collection2 TO kek")
node.query("ALTER NAMED COLLECTION collection2 SET key1=2", user="kek")
assert (
"2"
== node.query(
"select collection['key1'] from system.named_collections where name = 'collection2'"
).strip()
)
2023-02-24 13:44:47 +00:00
node.query("REVOKE create named collection ON collection2 FROM kek")
assert (
"DB::Exception: kek: Not enough privileges. To execute this query it's necessary to have grant ALTER NAMED COLLECTION"
in node.query_and_get_error(
"ALTER NAMED COLLECTION collection2 SET key1=3", user="kek"
)
)
2023-02-11 18:57:41 +00:00
assert (
"DB::Exception: kek: Not enough privileges. To execute this query it's necessary to have grant DROP NAMED COLLECTION"
in node.query_and_get_error("DROP NAMED COLLECTION collection2", user="kek")
)
node.query("GRANT drop named collection ON collection2 TO kek")
node.query("DROP NAMED COLLECTION collection2", user="kek")
assert 0 == int(
node.query("select count() from system.named_collections", user="kek")
)
2023-02-11 11:36:25 +00:00
2022-11-16 17:27:13 +00:00
def test_config_reload(cluster):
node = cluster.instances["node"]
assert (
"collection1" == node.query("select name from system.named_collections").strip()
)
assert (
"['key1']"
== node.query(
"select mapKeys(collection) from system.named_collections where name = 'collection1'"
).strip()
)
assert (
"value1"
== node.query(
"select collection['key1'] from system.named_collections where name = 'collection1'"
).strip()
)
2023-02-03 12:11:21 +00:00
replace_in_server_config(node, "value1", "value2")
2022-11-16 17:27:13 +00:00
node.query("SYSTEM RELOAD CONFIG")
assert (
"['key1']"
== node.query(
"select mapKeys(collection) from system.named_collections where name = 'collection1'"
).strip()
)
assert (
"value2"
== node.query(
"select collection['key1'] from system.named_collections where name = 'collection1'"
).strip()
)
2022-11-17 14:33:06 +00:00
def test_sql_commands(cluster):
2022-11-16 17:27:13 +00:00
node = cluster.instances["node"]
assert "1" == node.query("select count() from system.named_collections").strip()
node.query("CREATE NAMED COLLECTION collection2 AS key1=1, key2='value2'")
def check_created():
assert (
"collection1\ncollection2"
== node.query("select name from system.named_collections").strip()
)
assert (
"['key1','key2']"
== node.query(
"select mapKeys(collection) from system.named_collections where name = 'collection2'"
).strip()
)
assert (
"1"
== node.query(
"select collection['key1'] from system.named_collections where name = 'collection2'"
).strip()
)
assert (
"value2"
== node.query(
"select collection['key2'] from system.named_collections where name = 'collection2'"
).strip()
)
check_created()
node.restart_clickhouse()
check_created()
2022-11-17 14:33:06 +00:00
node.query("ALTER NAMED COLLECTION collection2 SET key1=4, key3='value3'")
def check_altered():
assert (
"['key1','key2','key3']"
== node.query(
"select mapKeys(collection) from system.named_collections where name = 'collection2'"
).strip()
)
assert (
"4"
== node.query(
"select collection['key1'] from system.named_collections where name = 'collection2'"
).strip()
)
assert (
"value3"
== node.query(
"select collection['key3'] from system.named_collections where name = 'collection2'"
).strip()
)
2022-11-17 15:01:42 +00:00
check_altered()
2022-11-17 14:33:06 +00:00
node.restart_clickhouse()
2022-11-17 15:01:42 +00:00
check_altered()
2022-11-17 14:33:06 +00:00
node.query("ALTER NAMED COLLECTION collection2 DELETE key2")
def check_deleted():
assert (
"['key1','key3']"
== node.query(
"select mapKeys(collection) from system.named_collections where name = 'collection2'"
).strip()
)
2022-11-17 15:01:42 +00:00
check_deleted()
2022-11-17 14:33:06 +00:00
node.restart_clickhouse()
2022-11-17 15:01:42 +00:00
check_deleted()
2022-11-17 14:33:06 +00:00
2022-11-17 15:01:42 +00:00
node.query(
"ALTER NAMED COLLECTION collection2 SET key3=3, key4='value4' DELETE key1"
)
2022-11-17 14:33:06 +00:00
def check_altered_and_deleted():
assert (
"['key3','key4']"
== node.query(
"select mapKeys(collection) from system.named_collections where name = 'collection2'"
).strip()
)
assert (
"3"
== node.query(
"select collection['key3'] from system.named_collections where name = 'collection2'"
).strip()
)
2022-11-17 15:01:42 +00:00
2022-11-17 14:33:06 +00:00
assert (
"value4"
== node.query(
"select collection['key4'] from system.named_collections where name = 'collection2'"
).strip()
)
2022-11-17 15:01:42 +00:00
check_altered_and_deleted()
2022-11-17 14:33:06 +00:00
node.restart_clickhouse()
2022-11-17 15:01:42 +00:00
check_altered_and_deleted()
2022-11-17 14:33:06 +00:00
2022-11-16 17:27:13 +00:00
node.query("DROP NAMED COLLECTION collection2")
def check_dropped():
assert "1" == node.query("select count() from system.named_collections").strip()
assert (
"collection1"
== node.query("select name from system.named_collections").strip()
)
check_dropped()
node.restart_clickhouse()
check_dropped()