Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: disallow set role statements for backup (#13364)

rebeliced47108d71cfa
diff --git a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
index 82ec69074f..bc318553d3 100644
--- a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
+++ b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
@@ -14,19 +14,19 @@ import (
)
var (
- _ advisor.Advisor = (*StatementPriorBackupCheckAdvisor)(nil)
+ _ advisor.Advisor = (*BuiltinPriorBackupCheckAdvisor)(nil)
)
func init() {
- advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLBuiltinPriorBackupCheck, &StatementPriorBackupCheckAdvisor{})
+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLBuiltinPriorBackupCheck, &BuiltinPriorBackupCheckAdvisor{})
}
-// StatementPriorBackupCheckAdvisor is the advisor checking for disallow mix DDL and DML.
-type StatementPriorBackupCheckAdvisor struct {
+// BuiltinPriorBackupCheckAdvisor is the advisor checking for disallow mix DDL and DML.
+type BuiltinPriorBackupCheckAdvisor struct {
}
// Check checks for disallow mix DDL and DML.
-func (*StatementPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([]*storepb.Advice, error) {
+func (*BuiltinPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([]*storepb.Advice, error) {
if ctx.PreUpdateBackupDetail == nil || ctx.ChangeType != storepb.PlanCheckRunConfig_DML {
return nil, nil
}
@@ -58,6 +58,19 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([
},
})
}
+ if n, ok := stmt.(*ast.VariableSetStmt); ok {
+ if n.Name == "role" {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: "Backup cannot deal with SET ROLE statement",
+ Code: int32(advisor.BuiltinPriorBackupCheck.Int32()),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
}
name := extractDatabaseName(ctx.PreUpdateBackupDetail.Database)
diff --git a/backend/plugin/advisor/pg/pg_rules_test.go b/backend/plugin/advisor/pg/pg_rules_test.go
index 676459993a..49f1773575 100644
--- a/backend/plugin/advisor/pg/pg_rules_test.go
+++ b/backend/plugin/advisor/pg/pg_rules_test.go
@@ -9,6 +9,7 @@ import (
func TestPostgreSQLRules(t *testing.T) {
pgRules := []advisor.SQLReviewRuleType{
+ advisor.BuiltinRulePriorBackupCheck,
advisor.SchemaRuleColumnNotNull,
advisor.SchemaRuleRequiredColumn,
advisor.SchemaRuleColumnTypeDisallowList,
@@ -66,4 +67,5 @@ func TestPostgreSQLRules(t *testing.T) {
// Add SQL review type here if you need metadata for test.
var advisorNeedMockData = map[advisor.SQLReviewRuleType]bool{
advisor.SchemaRuleFullyQualifiedObjectName: true,
+ advisor.BuiltinRulePriorBackupCheck: true,
}
diff --git a/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml b/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
new file mode 100644
index 0000000000..980b3f7a43
--- /dev/null
+++ b/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
@@ -0,0 +1,28 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 2
+- statement: CREATE TABLE t_test_backup(a int);DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+ want:
+ - status: 3
+ code: 2001
+ title: builtin.prior-backup-check
+ content: Data change can only run DML, "CREATE TABLE t_test_backup(a int);" is not DML
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: SET ROLE role1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+ want:
+ - status: 3
+ code: 2001
+ title: builtin.prior-backup-check
+ content: Backup cannot deal with SET ROLE statement
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pg/test/statement_prior_backup_check.yaml b/backend/plugin/advisor/pg/test/statement_prior_backup_check.yaml
deleted file mode 100644
index dcd8f3bf18..0000000000
--- a/backend/plugin/advisor/pg/test/statement_prior_backup_check.yaml
+++ /dev/null
@@ -1,27 +0,0 @@
-- statement: DELETE FROM tech_book WHERE a > 1;
- want:
- - status: 1
- code: 0
- title: OK
- content: ""
- line: 0
- column: 0
- details: ""
-- statement: UPDATE tech_book SET id = 1;
- want:
- - status: 1
- code: 0
- title: OK
- content: ""
- line: 0
- column: 0
- details: ""
-- statement: UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
- want:
- - status: 2
- code: 227
- title: statement.prior-backup-check
- content: Found 1 DELETE, 1 UPDATE on table ``.`tech_book`, disallow mixing different types of DML statements
- line: 0
- column: 0
- details: ""
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 52f967f12e..ed7a674f3f 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -117,6 +117,9 @@ var (
},
},
},
+ {
+ Name: "bbdataarchive",
+ },
},
}
MockMSSQLDatabase = &storepb.DatabaseSchemaMetadata{
@@ -217,6 +220,10 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
},
}
+ if isBuiltinRule(rule) {
+ ruleList = []*storepb.SQLReviewRule{}
+ }
+
ctx := SQLReviewCheckContext{
Charset: "",
Collation: "",
@@ -227,6 +234,9 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
CurrentDatabase: curDB,
DBSchema: schemaMetadata,
ChangeType: tc.ChangeType,
+ PreUpdateBackupDetail: &storepb.PreUpdateBackupDetail{
+ Database: "instances/instanceName/databases/bbdataarchive",
+ },
}
adviceList, err := SQLReviewCheck(sm, tc.Statement, ruleList, ctx)
@@ -256,6 +266,15 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
}
}
+func isBuiltinRule(rule SQLReviewRuleType) bool {
+ switch rule {
+ case BuiltinRulePriorBackupCheck:
+ return true
+ default:
+ return false
+ }
+}
+
// MockDriver is the driver for test only.
type MockDriver struct {
}
@@ -346,6 +365,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
var err error
switch ruleTp {
case SchemaRuleMySQLEngine,
+ BuiltinRulePriorBackupCheck,
SchemaRuleFullyQualifiedObjectName,
SchemaRuleStatementNoSelectAll,
SchemaRuleStatementRequireWhere,