Skip to content

Commit d6ef3e2

Browse files
Adapt Charmed read / dml roles
1 parent 98a3de8 commit d6ef3e2

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

lib/charms/mysql/v0/mysql.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,11 +1239,9 @@ def configure_mysql_system_roles(self) -> None:
12391239
role_to_queries = {
12401240
ROLE_READ: [
12411241
f"CREATE ROLE {ROLE_READ}",
1242-
f"GRANT SELECT ON mysql.* TO {ROLE_READ}",
12431242
],
12441243
ROLE_DML: [
12451244
f"CREATE ROLE {ROLE_DML}",
1246-
f"GRANT INSERT, DELETE, UPDATE ON mysql.* TO {ROLE_DML}",
12471245
],
12481246
ROLE_STATS: [
12491247
f"CREATE ROLE {ROLE_STATS}",
@@ -1258,13 +1256,11 @@ def configure_mysql_system_roles(self) -> None:
12581256
],
12591257
ROLE_DDL: [
12601258
f"CREATE ROLE {ROLE_DDL}",
1261-
f"GRANT charmed_read TO {ROLE_DDL}",
12621259
f"GRANT charmed_dml TO {ROLE_DDL}",
12631260
f"GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TABLESPACE, CREATE VIEW, SHOW_ROUTINE, SHOW VIEW, INDEX, REFERENCES, TRIGGER, LOCK TABLES ON *.* TO {ROLE_DDL}",
12641261
],
12651262
ROLE_DBA: [
12661263
f"CREATE ROLE {ROLE_DBA}",
1267-
f"GRANT charmed_read TO {ROLE_DBA}",
12681264
f"GRANT charmed_dml TO {ROLE_DBA}",
12691265
f"GRANT charmed_stats TO {ROLE_DBA}",
12701266
f"GRANT charmed_backup TO {ROLE_DBA}",
@@ -1515,7 +1511,7 @@ def create_database(self, database: str) -> None:
15151511
"shell.connect_to_primary()",
15161512
f'session.run_sql("CREATE DATABASE IF NOT EXISTS `{database}`;")',
15171513
f'session.run_sql("GRANT SELECT ON `{database}`.* TO {ROLE_READ};")',
1518-
f'session.run_sql("GRANT INSERT, DELETE, UPDATE ON `{database}`.* TO {ROLE_DML};")',
1514+
f'session.run_sql("GRANT SELECT, INSERT, DELETE, UPDATE ON `{database}`.* TO {ROLE_DML};")',
15191515
)
15201516

15211517
try:

tests/integration/test_predefined_roles.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def test_charmed_read_role(ops_test: OpsTest):
9292
data_integrator_unit = ops_test.model.applications[f"{INTEGRATOR_APP_NAME}1"].units[0]
9393
results = await juju_.run_action(data_integrator_unit, "get-credentials")
9494

95-
logger.info("Checking that the charmed_read role can read from the database")
95+
logger.info("Checking that the charmed_read role can read from an existing table")
9696
rows = await execute_queries_on_unit(
9797
primary_unit_address,
9898
results["mysql"]["username"],
@@ -107,26 +107,26 @@ async def test_charmed_read_role(ops_test: OpsTest):
107107
"test_data_2",
108108
]), "Unexpected data in charmed_read_database with charmed_read role"
109109

110-
logger.info("Checking that the charmed_read role cannot create a new table")
110+
logger.info("Checking that the charmed_read role cannot write into an existing table")
111111
with pytest.raises(ProgrammingError):
112112
await execute_queries_on_unit(
113113
primary_unit_address,
114114
results["mysql"]["username"],
115115
results["mysql"]["password"],
116116
[
117-
"CREATE TABLE charmed_read_database.new_table (`id` SERIAL PRIMARY KEY, `data` TEXT)",
117+
"INSERT INTO charmed_read_database.test_table (`data`) VALUES ('test_data_3')",
118118
],
119119
commit=True,
120120
)
121121

122-
logger.info("Checking that the charmed_read role cannot write to an existing table")
122+
logger.info("Checking that the charmed_read role cannot create a new table")
123123
with pytest.raises(ProgrammingError):
124124
await execute_queries_on_unit(
125125
primary_unit_address,
126126
results["mysql"]["username"],
127127
results["mysql"]["password"],
128128
[
129-
"INSERT INTO charmed_read_database.test_table (`data`) VALUES ('test_data_3'), ('test_data_4')",
129+
"CREATE TABLE charmed_read_database.new_table (`id` SERIAL PRIMARY KEY, `data` TEXT)",
130130
],
131131
commit=True,
132132
)
@@ -191,6 +191,22 @@ async def test_charmed_dml_role(ops_test: OpsTest):
191191
data_integrator_2_unit = ops_test.model.applications[f"{INTEGRATOR_APP_NAME}2"].units[0]
192192
results = await juju_.run_action(data_integrator_2_unit, "get-credentials")
193193

194+
logger.info("Checking that the charmed_dml role can read from an existing table")
195+
rows = await execute_queries_on_unit(
196+
primary_unit_address,
197+
results["mysql"]["username"],
198+
results["mysql"]["password"],
199+
[
200+
"SELECT `data` FROM charmed_dml_database.test_table",
201+
],
202+
commit=True,
203+
)
204+
205+
assert sorted(rows) == sorted([
206+
"test_data_1",
207+
"test_data_2",
208+
]), "Unexpected data in charmed_read_database with charmed_read role"
209+
194210
logger.info("Checking that the charmed_dml role can write into an existing table")
195211
await execute_queries_on_unit(
196212
primary_unit_address,
@@ -202,14 +218,14 @@ async def test_charmed_dml_role(ops_test: OpsTest):
202218
commit=True,
203219
)
204220

205-
logger.info("Checking that the charmed_dml role cannot read from an existing table")
221+
logger.info("Checking that the charmed_dml role cannot create a new table")
206222
with pytest.raises(ProgrammingError):
207223
await execute_queries_on_unit(
208224
primary_unit_address,
209225
results["mysql"]["username"],
210226
results["mysql"]["password"],
211227
[
212-
"SELECT `data` FROM charmed_dml_database.test_table",
228+
"CREATE TABLE charmed_dml_database.new_table (`id` SERIAL PRIMARY KEY, `data` TEXT)",
213229
],
214230
commit=True,
215231
)

tests/unit/test_mysql.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ def test_configure_mysql_system_roles(self, _run_mysqlcli_script, _list_mysql_ro
180180
_expected_configure_roles_commands = [
181181
# Charmed read queries
182182
f"CREATE ROLE {ROLE_READ}",
183-
f"GRANT SELECT ON mysql.* TO {ROLE_READ}",
184183
# Charmed DML queries
185184
f"CREATE ROLE {ROLE_DML}",
186-
f"GRANT INSERT, DELETE, UPDATE ON mysql.* TO {ROLE_DML}",
187185
# Charmed stats queries
188186
f"CREATE ROLE {ROLE_STATS}",
189187
f"GRANT SELECT ON performance_schema.* TO {ROLE_STATS}",
@@ -195,7 +193,6 @@ def test_configure_mysql_system_roles(self, _run_mysqlcli_script, _list_mysql_ro
195193
f"GRANT BACKUP_ADMIN, CONNECTION_ADMIN ON *.* TO {ROLE_BACKUP}",
196194
# Charmed DDL queries
197195
f"CREATE ROLE {ROLE_DDL}",
198-
f"GRANT charmed_read TO {ROLE_DDL}",
199196
f"GRANT charmed_dml TO {ROLE_DDL}",
200197
f"GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TABLESPACE, CREATE VIEW, SHOW_ROUTINE, SHOW VIEW, INDEX, REFERENCES, TRIGGER, LOCK TABLES ON *.* TO {ROLE_DDL}",
201198
]
@@ -368,7 +365,7 @@ def test_create_application_database(self, _run_mysqlsh_script):
368365
"shell.connect_to_primary()",
369366
'session.run_sql("CREATE DATABASE IF NOT EXISTS `test-database`;")',
370367
'session.run_sql("GRANT SELECT ON `test-database`.* TO charmed_read;")',
371-
'session.run_sql("GRANT INSERT, DELETE, UPDATE ON `test-database`.* TO charmed_dml;")',
368+
'session.run_sql("GRANT SELECT, INSERT, DELETE, UPDATE ON `test-database`.* TO charmed_dml;")',
372369
))
373370

374371
self.mysql.create_database("test-database")

0 commit comments

Comments
 (0)