Skip to content

Commit 574cbd5

Browse files
[DPE-7627] Allow charmed_dba role members to connect to other databases (#1002)
* Allow charmed_dba role members to connect to other databases Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Test connection to another database Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Remove previously added statements Signed-off-by: Marcelo Henrique Neppel <[email protected]> --------- Signed-off-by: Marcelo Henrique Neppel <[email protected]>
1 parent fcc17af commit 574cbd5

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

templates/patroni.yml.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ postgresql:
164164
- local all backup peer map=operator
165165
- local all operator scram-sha-256
166166
- local all monitoring password
167+
- {{ 'hostssl' if enable_tls else 'host' }} all +charmed_dba 0.0.0.0/0 scram-sha-256
167168
{%- if not connectivity %}
168169
- {{ 'hostssl' if enable_tls else 'host' }} all all {{ self_ip }} md5
169170
- {{ 'hostssl' if enable_tls else 'host' }} all all 0.0.0.0/0 reject

tests/integration/test_predefined_dba_role.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
DATA_INTEGRATOR_APP_NAME,
1616
DATABASE_APP_NAME,
1717
check_connected_user,
18+
db_connect,
19+
get_password,
20+
get_primary,
21+
get_unit_address,
1822
)
1923
from .new_relations.helpers import build_connection_string
2024

@@ -56,6 +60,31 @@ async def test_charmed_dba_role(ops_test: OpsTest):
5660
apps=[DATA_INTEGRATOR_APP_NAME, DATABASE_APP_NAME], status="active"
5761
)
5862

63+
primary = await get_primary(ops_test, f"{DATABASE_APP_NAME}/0")
64+
primary_address = get_unit_address(ops_test, primary)
65+
operator_password = await get_password(ops_test, "operator")
66+
67+
# Create a test database to check the dblink functionality.
68+
connection = None
69+
cursor = None
70+
try:
71+
connection = db_connect(
72+
primary_address,
73+
operator_password,
74+
username="operator",
75+
database="charmed_dba_database",
76+
)
77+
connection.autocommit = True
78+
cursor = connection.cursor()
79+
cursor.execute("CREATE EXTENSION IF NOT EXISTS dblink;")
80+
cursor.execute("DROP DATABASE IF EXISTS test;")
81+
cursor.execute("CREATE DATABASE test;")
82+
finally:
83+
if cursor is not None:
84+
cursor.close()
85+
if connection is not None:
86+
connection.close()
87+
5988
action = await ops_test.model.units[f"{DATA_INTEGRATOR_APP_NAME}/0"].run_action(
6089
action_name="get-credentials"
6190
)
@@ -91,6 +120,33 @@ async def test_charmed_dba_role(ops_test: OpsTest):
91120
logger.info(f"Resetting the user to the {username} user in the {instance}")
92121
cursor.execute("SELECT reset_user();")
93122
check_connected_user(cursor, username, username, primary=read_write_endpoint)
123+
logger.info(
124+
f"Testing connection to another database through the same session in the {instance}"
125+
)
126+
other_database_connection_string = (
127+
await build_connection_string(
128+
ops_test,
129+
DATA_INTEGRATOR_APP_NAME,
130+
"postgresql",
131+
database="test",
132+
read_only_endpoint=(not read_write_endpoint),
133+
)
134+
).replace("'", "")
135+
cursor.execute(
136+
f"SELECT * FROM dblink('{other_database_connection_string}', 'SELECT current_database() AS database') AS t1(database TEXT);"
137+
)
138+
assert cursor.fetchone()[0] == "test"
94139
finally:
95140
if connection is not None:
96141
connection.close()
142+
143+
connection = psycopg2.connect(other_database_connection_string)
144+
try:
145+
with connection.cursor() as cursor:
146+
logger.info(
147+
f"Testing connection to another database through another session in the {instance}"
148+
)
149+
cursor.execute("SELECT current_database();")
150+
assert cursor.fetchone()[0] == "test"
151+
finally:
152+
connection.close()

0 commit comments

Comments
 (0)