Skip to content

Commit 3cb5ce0

Browse files
committed
An explicit test of correct and incorrect grant addition, and dynamic privilege on * to coexist with database-specific grants
1 parent 5be87ac commit 3cb5ce0

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

GNUmakefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ TEST?=$$(go list ./... |grep -v 'vendor')
22
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
33
WEBSITE_REPO=github.com/hashicorp/terraform-website
44
PKG_NAME=mysql
5-
TERRAFORM_VERSION=0.14.7
5+
TERRAFORM_VERSION=1.11.4
66
TERRAFORM_OS=$(shell uname -s | tr A-Z a-z)
77
TEST_USER=root
88
TEST_PASSWORD=my-secret-pw
@@ -22,7 +22,7 @@ bin/terraform:
2222
testacc: fmtcheck bin/terraform
2323
PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout=120s
2424

25-
acceptance: testversion5.6 testversion5.7 testversion8.0 testpercona5.7 testpercona8.0 testmariadb10.3 testmariadb10.8 testmariadb10.10 testtidb6.1.0 testtidb7.5.2
25+
acceptance: testversion5.6 testversion5.7 testversion8.0 testversion8.4.5 testpercona5.7 testpercona8.0 testmariadb10.3 testmariadb10.8 testmariadb10.10 testtidb6.1.0 testtidb7.5.2
2626

2727
testversion%:
2828
$(MAKE) MYSQL_VERSION=$* MYSQL_PORT=33$(shell echo "$*" | tr -d '.') testversion
@@ -76,6 +76,7 @@ testmariadb:
7676
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="$(TEST_PASSWORD)" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
7777
-docker rm -f test-mariadb$(MYSQL_VERSION)
7878

79+
7980
vet:
8081
@echo "go vet ."
8182
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \

mysql/resource_grant_test.go

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)