@@ -1042,7 +1042,7 @@ resource "mysql_grant" "test_procedure" {
10421042 host = "%s"
10431043 privileges = ["EXECUTE"]
10441044 database = "PROCEDURE %s"
1045- table = "%s"
1045+ table = "%s"
10461046}
10471047` , dbName , dbName , dbName , dbName , hostName , dbName , procedureName )
10481048}
@@ -1159,7 +1159,7 @@ func TestAllowDuplicateUsersDifferentTables(t *testing.T) {
11591159 user = "${mysql_user.test.user}"
11601160 host = "${mysql_user.test.host}"
11611161 database = "${mysql_database.test.name}"
1162- table = "table1"
1162+ table = "table1"
11631163 privileges = ["UPDATE", "SELECT"]
11641164 }
11651165
@@ -1224,7 +1224,7 @@ func TestDisallowDuplicateUsersSameTable(t *testing.T) {
12241224 user = "${mysql_user.test.user}"
12251225 host = "${mysql_user.test.host}"
12261226 database = "${mysql_database.test.name}"
1227- table = "table1"
1227+ table = "table1"
12281228 privileges = ["UPDATE", "SELECT"]
12291229 }
12301230
@@ -1255,3 +1255,129 @@ func TestDisallowDuplicateUsersSameTable(t *testing.T) {
12551255 },
12561256 })
12571257}
1258+
1259+ // TestModifyPrivileges explicitly verifies the correct and incorrect ways of modifying privileges.
1260+ // It tests adding privileges by augmenting the existing grant (correct way).
1261+ // It also tests that dynamic privileges configured on the global (`*`) database can coexist with grants on specific databases.
1262+ func TestModifyPrivileges (t * testing.T ) {
1263+ dbName := fmt .Sprintf ("tf-test-modify-%d" , rand .Intn (100 ))
1264+ roleName := fmt .Sprintf ("TFRole-modify-%d" , rand .Intn (100 ))
1265+ userName := fmt .Sprintf ("jdoe-modify-%s" , dbName )
1266+
1267+ onePrivilegeConfig := getGrantsSampleWithPrivileges (roleName , dbName , userName , `"SELECT"` )
1268+ twoPrivilegesConfig := getGrantsSampleWithPrivileges (roleName , dbName , userName , `"SELECT", "UPDATE"` )
1269+ additionalStaticPrivilegeConfig := twoPrivilegesConfig + getAdditionalGrantSample (dbName , `"INSERT"` )
1270+ threePrivilegesConfig := getGrantsSampleWithPrivileges (roleName , dbName , userName , `"SELECT", "UPDATE", "INSERT"` )
1271+ // Configuring dynamic privilege on global (`*`) database alongside specific database grants
1272+ additionalDynamicPrivilegeConfigFlushTables := threePrivilegesConfig + getAdditionalGrantSample ("*" , `"FLUSH_TABLES"` )
1273+ additionalDynamicPrivilegeConfigShowRoutine := threePrivilegesConfig + getAdditionalGrantSample ("*" , `"SHOW_ROUTINE"` )
1274+ resource .Test (t , resource.TestCase {
1275+ PreCheck : func () {
1276+ testAccPreCheck (t )
1277+ testAccPreCheckSkipRds (t )
1278+ testAccPreCheckSkipMariaDB (t )
1279+ testAccPreCheckSkipNotMySQLVersionMin (t , "8.0.0" )
1280+ testAccPreCheckSkipTiDB (t )
1281+ },
1282+ ProviderFactories : testAccProviderFactories ,
1283+ CheckDestroy : testAccGrantCheckDestroy ,
1284+ Steps : []resource.TestStep {
1285+ {
1286+ Config : testAccGrantConfigNoGrant (dbName ),
1287+ },
1288+ {
1289+ Config : onePrivilegeConfig ,
1290+ Check : resource .ComposeTestCheckFunc (
1291+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1292+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , false , false ),
1293+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , false , false ),
1294+ ),
1295+ },
1296+ {
1297+ // Correct way: augment existing grant with additional privileges
1298+ Config : twoPrivilegesConfig ,
1299+ Check : resource .ComposeTestCheckFunc (
1300+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1301+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , true , false ),
1302+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , false , false ),
1303+ ),
1304+ },
1305+ {
1306+ // Incorrect way: create a new conflicting grant (expected to fail)
1307+ Config : additionalStaticPrivilegeConfig ,
1308+ ExpectError : regexp .MustCompile ("already has" ),
1309+ },
1310+ {
1311+ // Correct way: augment existing grant with additional privileges
1312+ Config : threePrivilegesConfig ,
1313+ Check : resource .ComposeTestCheckFunc (
1314+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1315+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , true , false ),
1316+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , true , false ),
1317+ ),
1318+ },
1319+
1320+ // Testing coexistence of dynamic privilege on global (`*`) database with specific database grants
1321+
1322+ {
1323+ Config : additionalDynamicPrivilegeConfigFlushTables ,
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" , true , false ),
1329+ testAccPrivilege ("mysql_grant.additional_grant" , "SHOW_ROUTINE" , false , false ),
1330+ ),
1331+ },
1332+ {
1333+ Config : additionalDynamicPrivilegeConfigShowRoutine ,
1334+ Check : resource .ComposeTestCheckFunc (
1335+ testAccPrivilege ("mysql_grant.grant" , "SELECT" , true , false ),
1336+ testAccPrivilege ("mysql_grant.grant" , "UPDATE" , true , false ),
1337+ testAccPrivilege ("mysql_grant.grant" , "INSERT" , true , false ),
1338+ testAccPrivilege ("mysql_grant.additional_grant" , "FLUSH_TABLES" , false , false ),
1339+ testAccPrivilege ("mysql_grant.additional_grant" , "SHOW_ROUTINE" , true , false ),
1340+ ),
1341+ },
1342+ },
1343+ })
1344+ }
1345+
1346+ func getGrantsSampleWithPrivileges (roleName string , dbName string , userName string , privileges string ) string {
1347+ return fmt .Sprintf (`
1348+
1349+ resource "mysql_role" "role" {
1350+ name = "%s"
1351+ }
1352+
1353+ resource "mysql_grant" "grant" {
1354+ role = "${mysql_role.role.name}"
1355+ database = "%s"
1356+ privileges = [%s]
1357+ }
1358+
1359+ resource "mysql_user" "user" {
1360+ user = "%s"
1361+ host = "%%"
1362+ }
1363+
1364+ resource "mysql_grant" "user_grant" {
1365+ user = "${mysql_user.user.user}"
1366+ host = "${mysql_user.user.host}"
1367+ database = "%s"
1368+ roles = ["${mysql_role.role.name}"]
1369+ }
1370+
1371+ ` , roleName , dbName , privileges , userName , dbName )
1372+ }
1373+
1374+ func getAdditionalGrantSample (dbName string , privileges string ) string {
1375+ return fmt .Sprintf (`
1376+
1377+ resource "mysql_grant" "additional_grant" {
1378+ role = "${mysql_role.role.name}"
1379+ database = "%s"
1380+ privileges = [%s]
1381+ }
1382+ ` , dbName , privileges )
1383+ }
0 commit comments