@@ -1033,7 +1033,7 @@ resource "mysql_grant" "test_procedure" {
10331033 host = "%s"
10341034 privileges = ["EXECUTE"]
10351035 database = "PROCEDURE %s"
1036- table = "%s"
1036+ table = "%s"
10371037}
10381038` , dbName , dbName , dbName , dbName , hostName , dbName , procedureName )
10391039}
@@ -1150,7 +1150,7 @@ func TestAllowDuplicateUsersDifferentTables(t *testing.T) {
11501150 user = "${mysql_user.test.user}"
11511151 host = "${mysql_user.test.host}"
11521152 database = "${mysql_database.test.name}"
1153- table = "table1"
1153+ table = "table1"
11541154 privileges = ["UPDATE", "SELECT"]
11551155 }
11561156
@@ -1215,7 +1215,7 @@ func TestDisallowDuplicateUsersSameTable(t *testing.T) {
12151215 user = "${mysql_user.test.user}"
12161216 host = "${mysql_user.test.host}"
12171217 database = "${mysql_database.test.name}"
1218- table = "table1"
1218+ table = "table1"
12191219 privileges = ["UPDATE", "SELECT"]
12201220 }
12211221
@@ -1246,3 +1246,115 @@ func TestDisallowDuplicateUsersSameTable(t *testing.T) {
12461246 },
12471247 })
12481248}
1249+
1250+ // TestModifyPrivileges explicitly verifies the correct and incorrect ways of modifying privileges.
1251+ // It tests adding privileges by augmenting the existing grant (correct way).
1252+ // It also tests that dynamic privileges configured on the global (`*`) database can coexist with grants on specific databases.
1253+ func TestModifyPrivileges (t * testing.T ) {
1254+ dbName := fmt .Sprintf ("tf-test-modify-%d" , rand .Intn (100 ))
1255+ roleName := fmt .Sprintf ("TFRole-modify-%d" , rand .Intn (100 ))
1256+
1257+ onePrivilegeConfig := getGrantsSampleWithPrivileges (roleName , dbName , `"SELECT"` )
1258+ twoPrivilegesConfig := getGrantsSampleWithPrivileges (roleName , dbName , `"SELECT", "UPDATE"` )
1259+ additionalStaticPrivilegeConfig := twoPrivilegesConfig + getAdditionalGrantSample (dbName , `"INSERT"` )
1260+ threePrivilegesConfig := getGrantsSampleWithPrivileges (roleName , dbName , `"SELECT", "UPDATE", "INSERT"` )
1261+ // Configuring dynamic privilege on global (`*`) database alongside specific database grants
1262+ additionalDynamicPrivilegeConfigFlushTables := threePrivilegesConfig + getAdditionalGrantSample ("*" , `"FLUSH_TABLES"` )
1263+ additionalDynamicPrivilegeConfigShowRoutine := threePrivilegesConfig + getAdditionalGrantSample ("*" , `"SHOW_ROUTINE"` )
1264+
1265+ resource .Test (t , resource.TestCase {
1266+ PreCheck : func () {
1267+ testAccPreCheck (t )
1268+ testAccPreCheckSkipMariaDB (t )
1269+ testAccPreCheckSkipNotMySQLVersionMin (t , "8.0.0" )
1270+ testAccPreCheckSkipTiDB (t )
1271+ },
1272+ ProviderFactories : testAccProviderFactories ,
1273+ CheckDestroy : testAccGrantCheckDestroy ,
1274+ Steps : []resource.TestStep {
1275+ {
1276+ Config : testAccGrantConfigNoGrant (dbName ),
1277+ },
1278+ {
1279+ Config : onePrivilegeConfig ,
1280+ Check : resource .ComposeTestCheckFunc (
1281+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1282+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , false , false ),
1283+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , false , false ),
1284+ ),
1285+ },
1286+ {
1287+ // Correct way: augment existing grant with additional privileges
1288+ Config : twoPrivilegesConfig ,
1289+ Check : resource .ComposeTestCheckFunc (
1290+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1291+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , true , false ),
1292+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , false , false ),
1293+ ),
1294+ },
1295+ {
1296+ // Incorrect way: create a new conflicting grant (expected to fail)
1297+ Config : additionalStaticPrivilegeConfig ,
1298+ ExpectError : regexp .MustCompile ("already has" ),
1299+ },
1300+ {
1301+ // Correct way: augment existing grant with additional privileges
1302+ Config : threePrivilegesConfig ,
1303+ Check : resource .ComposeTestCheckFunc (
1304+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1305+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , true , false ),
1306+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , true , false ),
1307+ ),
1308+ },
1309+
1310+ // Testing coexistence of dynamic privilege on global (`*`) database with specific database grants
1311+
1312+ {
1313+ Config : additionalDynamicPrivilegeConfigFlushTables ,
1314+ Check : resource .ComposeTestCheckFunc (
1315+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1316+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , true , false ),
1317+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , true , false ),
1318+ testAccPrivilege ("mysql_grant.additional_grant" , "FLUSH_TABLES" , true , false ),
1319+ testAccPrivilege ("mysql_grant.additional_grant" , "SHOW_ROUTINE" , false , false ),
1320+ ),
1321+ },
1322+ {
1323+ Config : additionalDynamicPrivilegeConfigShowRoutine ,
1324+ Check : resource .ComposeTestCheckFunc (
1325+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1326+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , true , false ),
1327+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , true , false ),
1328+ testAccPrivilege ("mysql_grant.additional_grant" , "FLUSH_TABLES" , false , false ),
1329+ testAccPrivilege ("mysql_grant.additional_grant" , "SHOW_ROUTINE" , true , false ),
1330+ ),
1331+ },
1332+ },
1333+ })
1334+ }
1335+
1336+ func getGrantsSampleWithPrivileges (roleName string , dbName string , privileges string ) string {
1337+ return fmt .Sprintf (`
1338+
1339+ resource "mysql_role" "role" {
1340+ name = "%s"
1341+ }
1342+
1343+ resource "mysql_grant" "grant" {
1344+ role = "${mysql_role.role.name}"
1345+ database = "%s"
1346+ privileges = [%s]
1347+ }
1348+ ` , roleName , dbName , privileges )
1349+ }
1350+
1351+ func getAdditionalGrantSample (dbName string , privileges string ) string {
1352+ return fmt .Sprintf (`
1353+
1354+ resource "mysql_grant" "additional_grant" {
1355+ role = "${mysql_role.role.name}"
1356+ database = "%s"
1357+ privileges = [%s]
1358+ }
1359+ ` , dbName , privileges )
1360+ }
0 commit comments