Skip to content

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

chore: add disallow create function rule (#10874)

boojackf00557db6d1b
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 91e4d3430a..cb6d811495 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -236,6 +236,8 @@ const (
MySQLEventDisallowCreate Type = "bb.plugin.advisor.mysql.event.disallow-create"
// MySQLViewDisallowCreate is an advisor type for MySQL disallow create view.
MySQLViewDisallowCreate Type = "bb.plugin.advisor.mysql.view.disallow-create"
+ // MySQLFunctionDisallowCreate is an advisor type for MySQL disallow create function.
+ MySQLFunctionDisallowCreate Type = "bb.plugin.advisor.mysql.function.disallow-create"
// PostgreSQL Advisor.
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 908ff3e40b..e9455ae2e5 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -145,13 +145,16 @@ const (
CommentTooLong Code = 1301
// 1401 ~ 1499 procedure error code.
- DisallowProcedure Code = 1401
+ DisallowCreateProcedure Code = 1401
// 1501 ~ 1599 event error code.
- DisallowEvent Code = 1501
+ DisallowCreateEvent Code = 1501
// 1601 ~ 1699 view error code.
- DisallowView Code = 1601
+ DisallowCreateView Code = 1601
+
+ // 1701 ~ 1799 function error code.
+ DisallowCreateFunction Code = 1701
)
// Int returns the int type of code.
diff --git a/backend/plugin/advisor/config/sql-review.dev.yaml b/backend/plugin/advisor/config/sql-review.dev.yaml
index feb368a91e..7544b511f5 100644
--- a/backend/plugin/advisor/config/sql-review.dev.yaml
+++ b/backend/plugin/advisor/config/sql-review.dev.yaml
@@ -219,3 +219,5 @@ ruleList:
level: WARNING
- type: system.view.disallow
level: WARNING
+ - type: system.function.disallow-create
+ level: WARNING
diff --git a/backend/plugin/advisor/config/sql-review.prod.yaml b/backend/plugin/advisor/config/sql-review.prod.yaml
index 0b6fd28dc3..9d09d94a0c 100644
--- a/backend/plugin/advisor/config/sql-review.prod.yaml
+++ b/backend/plugin/advisor/config/sql-review.prod.yaml
@@ -219,3 +219,5 @@ ruleList:
level: WARNING
- type: system.view.disallow
level: WARNING
+ - type: system.function.disallow-create
+ level: WARNING
diff --git a/backend/plugin/advisor/mysql/advisor_disallow_procedure.go b/backend/plugin/advisor/mysql/advisor_disallow_procedure.go
index eaa0f34a75..0cbadec4a2 100644
--- a/backend/plugin/advisor/mysql/advisor_disallow_procedure.go
+++ b/backend/plugin/advisor/mysql/advisor_disallow_procedure.go
@@ -75,7 +75,7 @@ func (checker *disallowProcedureChecker) EnterQuery(ctx *mysql.QueryContext) {
func (checker *disallowProcedureChecker) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {
code := advisor.Ok
if ctx.ProcedureName() != nil {
- code = advisor.DisallowProcedure
+ code = advisor.DisallowCreateProcedure
}
if code != advisor.Ok {
diff --git a/backend/plugin/advisor/mysql/advisor_event_disallow_create.go b/backend/plugin/advisor/mysql/advisor_event_disallow_create.go
index 47755140c4..1dc1e0e534 100644
--- a/backend/plugin/advisor/mysql/advisor_event_disallow_create.go
+++ b/backend/plugin/advisor/mysql/advisor_event_disallow_create.go
@@ -74,7 +74,7 @@ func (checker *eventDisallowCreateChecker) EnterQuery(ctx *mysql.QueryContext) {
func (checker *eventDisallowCreateChecker) EnterCreateEvent(ctx *mysql.CreateEventContext) {
code := advisor.Ok
if ctx.EventName() != nil {
- code = advisor.DisallowEvent
+ code = advisor.DisallowCreateEvent
}
if code != advisor.Ok {
diff --git a/backend/plugin/advisor/mysql/advisor_function_disallow_create.go b/backend/plugin/advisor/mysql/advisor_function_disallow_create.go
new file mode 100644
index 0000000000..3da8dd4116
--- /dev/null
+++ b/backend/plugin/advisor/mysql/advisor_function_disallow_create.go
@@ -0,0 +1,89 @@
+package mysql
+
+import (
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+ "github.com/pkg/errors"
+
+ mysql "github.com/bytebase/mysql-parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*FunctionDisallowCreateAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_MYSQL, advisor.MySQLFunctionDisallowCreate, &FunctionDisallowCreateAdvisor{})
+}
+
+// FunctionDisallowCreateAdvisor is the advisor checking for disallow creating function.
+type FunctionDisallowCreateAdvisor struct {
+}
+
+// Check checks for disallow creating function.
+func (*FunctionDisallowCreateAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to mysql parser result")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ checker := &functionDisallowCreateChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ for _, stmt := range stmtList {
+ checker.baseLine = stmt.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type functionDisallowCreateChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+}
+
+func (checker *functionDisallowCreateChecker) EnterQuery(ctx *mysql.QueryContext) {
+ checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+}
+
+func (checker *functionDisallowCreateChecker) EnterCreateFunction(ctx *mysql.CreateFunctionContext) {
+ code := advisor.Ok
+ if ctx.FunctionName() != nil {
+ code = advisor.DisallowCreateFunction
+ }
+
+ if code != advisor.Ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: code,
+ Title: checker.title,
+ Content: fmt.Sprintf("Function is forbidden, but \"%s\" creates", checker.text),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+}
diff --git a/backend/plugin/advisor/mysql/advisor_view_disallow_create.go b/backend/plugin/advisor/mysql/advisor_view_disallow_create.go
index dba5d3b85d..2c80d8cacd 100644
--- a/backend/plugin/advisor/mysql/advisor_view_disallow_create.go
+++ b/backend/plugin/advisor/mysql/advisor_view_disallow_create.go
@@ -74,7 +74,7 @@ func (checker *viewDisallowCreateChecker) EnterQuery(ctx *mysql.QueryContext) {
func (checker *viewDisallowCreateChecker) EnterCreateView(ctx *mysql.CreateViewContext) {
code := advisor.Ok
if ctx.ViewName() != nil {
- code = advisor.DisallowView
+ code = advisor.DisallowCreateView
}
if code != advisor.Ok {
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 23c623040d..8c5dfe0fa3 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -134,6 +134,8 @@ func TestMySQLRules(t *testing.T) {
advisor.SchemaRuleDisallowEvent,
// advisor.SchemaRuleDisallowView enforce the disallow view.
advisor.SchemaRuleDisallowView,
+ // advisor.SchemaRuleFunctionDisallowCreate enforce the disallow create function.
+ advisor.SchemaRuleFunctionDisallowCreate,
}
for _, rule := range mysqlRules {
diff --git a/backend/plugin/advisor/mysql/test/system_function_disallow_create.yaml b/backend/plugin/advisor/mysql/test/system_function_disallow_create.yaml
new file mode 100644
index 0000000000..f95f29926a
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/system_function_disallow_create.yaml
@@ -0,0 +1,30 @@
+- statement: CREATE TABLE t(a int);
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: |-
+ CREATE FUNCTION add_two_numbers(a INT, b INT)
+ RETURNS INT
+ BEGIN
+ DECLARE result INT;
+ SET result = a + b;
+ RETURN result;
+ END;
+ want:
+ - status: WARN
+ code: 1701
+ title: system.function.disallow-create
+ content: |-
+ Function is forbidden, but "CREATE FUNCTION add_two_numbers(a INT, b INT)
+ RETURNS INT
+ BEGIN
+ DECLARE result INT;
+ SET result = a + b;
+ RETURN result;
+ END;" creates
+ line: 1
+ details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 1361a756cf..5fa4a9bcb7 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -185,6 +185,8 @@ const (
SchemaRuleDisallowEvent SQLReviewRuleType = "system.event.disallow"
// SchemaRuleDisallowView disallow view.
SchemaRuleDisallowView SQLReviewRuleType = "system.view.disallow"
+ // SchemaRuleFunctionDisallowCreate disallow create function.
+ SchemaRuleFunctionDisallowCreate SQLReviewRuleType = "system.function.disallow-create"
// TableNameTemplateToken is the token for table name.
TableNameTemplateToken = "{{table}}"
@@ -1478,6 +1480,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
if engine == storepb.Engine_MYSQL {
return MySQLViewDisallowCreate, nil
}
+ case SchemaRuleFunctionDisallowCreate:
+ if engine == storepb.Engine_MYSQL {
+ return MySQLFunctionDisallowCreate, nil
+ }
}
return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)
}
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 535209ed52..dd248327ff 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -358,6 +358,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
SchemaRuleDisallowProcedure,
SchemaRuleDisallowEvent,
SchemaRuleDisallowView,
+ SchemaRuleFunctionDisallowCreate,
SchemaRuleStatementCreateSpecifySchema:
case SchemaRuleTableDropNamingConvention:
payload, err = json.Marshal(NamingRulePayload{