Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(sql-review): add disallow creating view rule for MySQL (#10865)
boojackdddb66cf4899
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex df0efae530..91e4d3430a 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -234,6 +234,8 @@ const (MySQLDisallowProcedure Type = "bb.plugin.advisor.mysql.disallow-procedure"// MySQLEventDisallowCreate is an advisor type for MySQL disallow create event.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"// PostgreSQL Advisor.diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.goindex 74cc477477..908ff3e40b 100644--- a/backend/plugin/advisor/code.go+++ b/backend/plugin/advisor/code.go@@ -149,6 +149,9 @@ const (// 1501 ~ 1599 event error code.DisallowEvent Code = 1501++ // 1601 ~ 1699 view error code.+ DisallowView Code = 1601)// 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.yamlindex 0d9effa22c..feb368a91e 100644--- a/backend/plugin/advisor/config/sql-review.dev.yaml+++ b/backend/plugin/advisor/config/sql-review.dev.yaml@@ -217,3 +217,5 @@ ruleList:level: ERROR- type: system.event.disallowlevel: WARNING+ - type: system.view.disallow+ level: WARNINGdiff --git a/backend/plugin/advisor/config/sql-review.prod.yaml b/backend/plugin/advisor/config/sql-review.prod.yamlindex 9f54542c1d..0b6fd28dc3 100644--- a/backend/plugin/advisor/config/sql-review.prod.yaml+++ b/backend/plugin/advisor/config/sql-review.prod.yaml@@ -217,3 +217,5 @@ ruleList:level: ERROR- type: system.event.disallowlevel: WARNING+ - type: system.view.disallow+ level: WARNINGdiff --git a/backend/plugin/advisor/mysql/advisor_view_disallow_create.go b/backend/plugin/advisor/mysql/advisor_view_disallow_create.gonew file mode 100644index 0000000000..dba5d3b85d--- /dev/null+++ b/backend/plugin/advisor/mysql/advisor_view_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 = (*ViewDisallowCreateAdvisor)(nil)+)++func init() {+ advisor.Register(storepb.Engine_MYSQL, advisor.MySQLViewDisallowCreate, &ViewDisallowCreateAdvisor{})+}++// ViewDisallowCreateAdvisor is the advisor checking for disallow creating view.+type ViewDisallowCreateAdvisor struct {+}++// Check checks for disallow creating view.+func (*ViewDisallowCreateAdvisor) 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 := &viewDisallowCreateChecker{+ 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 viewDisallowCreateChecker struct {+ *mysql.BaseMySQLParserListener++ baseLine int+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+}++func (checker *viewDisallowCreateChecker) EnterQuery(ctx *mysql.QueryContext) {+ checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)+}++func (checker *viewDisallowCreateChecker) EnterCreateView(ctx *mysql.CreateViewContext) {+ code := advisor.Ok+ if ctx.ViewName() != nil {+ code = advisor.DisallowView+ }++ if code != advisor.Ok {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: code,+ Title: checker.title,+ Content: fmt.Sprintf("View is forbidden, but \"%s\" creates", checker.text),+ Line: checker.baseLine + ctx.GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.goindex 85fdcc9ea9..23c623040d 100644--- a/backend/plugin/advisor/mysql/mysql_rules_test.go+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go@@ -132,6 +132,8 @@ func TestMySQLRules(t *testing.T) {advisor.SchemaRuleDisallowProcedure,// advisor.SchemaRuleDisallowEvent enforce the disallow event.advisor.SchemaRuleDisallowEvent,+ // advisor.SchemaRuleDisallowView enforce the disallow view.+ advisor.SchemaRuleDisallowView,}for _, rule := range mysqlRules {diff --git a/backend/plugin/advisor/mysql/test/system_view_disallow.yaml b/backend/plugin/advisor/mysql/test/system_view_disallow.yamlnew file mode 100644index 0000000000..2d69bf1b27--- /dev/null+++ b/backend/plugin/advisor/mysql/test/system_view_disallow.yaml@@ -0,0 +1,18 @@+- statement: CREATE TABLE t(a int);+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: |-+ CREATE VIEW get_one AS SELECT 1;+ want:+ - status: WARN+ code: 1601+ title: system.view.disallow+ content: |-+ View is forbidden, but "CREATE VIEW get_one AS SELECT 1;" creates+ line: 1+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex c5379df893..1361a756cf 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -183,6 +183,8 @@ const (SchemaRuleDisallowProcedure SQLReviewRuleType = "system.procedure.disallow"// SchemaRuleDisallowEvent disallow event.SchemaRuleDisallowEvent SQLReviewRuleType = "system.event.disallow"+ // SchemaRuleDisallowView disallow view.+ SchemaRuleDisallowView SQLReviewRuleType = "system.view.disallow"// TableNameTemplateToken is the token for table name.TableNameTemplateToken = "{{table}}"@@ -1472,6 +1474,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Tyif engine == storepb.Engine_MYSQL {return MySQLEventDisallowCreate, nil}+ case SchemaRuleDisallowView:+ if engine == storepb.Engine_MYSQL {+ return MySQLViewDisallowCreate, 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.goindex 723b75f857..535209ed52 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -357,6 +357,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.EngSchemaRuleTableNameNoKeyword,SchemaRuleDisallowProcedure,SchemaRuleDisallowEvent,+ SchemaRuleDisallowView,SchemaRuleStatementCreateSpecifySchema:case SchemaRuleTableDropNamingConvention:payload, err = json.Marshal(NamingRulePayload{diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.jsonindex 7208a7f82a..b1ff286cad 100644--- a/frontend/src/locales/sql-review/en-US.json+++ b/frontend/src/locales/sql-review/en-US.json@@ -463,6 +463,10 @@"system-event-disallow": {"title": "Disallow to create events","description": "This rule prohibits the creation of events within the database. System events often perform automated tasks that could affect the database environment. By disallowing their creation, it helps maintain control over database operations and prevents potential disruptions. Suggested error level: Warning"+ },+ "system-view-disallow": {+ "title": "Disallow to create views",+ "description": "This rule prohibits the creation of views within the database. Views provide a virtual representation of data that can simplify queries and enhance data security. By disallowing their creation, it helps maintain control over database schema and prevents potential security risks. Suggested error level: Warning"}},"level": {diff --git a/frontend/src/locales/sql-review/es-ES.json b/frontend/src/locales/sql-review/es-ES.jsonindex 0cb7608afc..b297d2c6ee 100644--- a/frontend/src/locales/sql-review/es-ES.json+++ b/frontend/src/locales/sql-review/es-ES.json@@ -463,6 +463,10 @@"system-event-disallow": {"title": "Prohibir la creación de eventos","description": "Esta regla prohíbe la creación de eventos dentro de la base de datos. Los eventos del sistema a menudo realizan tareas automatizadas que podrían afectar el entorno de la base de datos. Al prohibir su creación, ayuda a mantener el control sobre las operaciones de la base de datos y previene posibles interrupciones. Nivel de error sugerido: Advertencia"+ },+ "system-view-disallow": {+ "title": "Prohibir la creación de vistas",+ "description": "Esta regla prohíbe la creación de vistas dentro de la base de datos. Las vistas proporcionan una representación virtual de los datos que puede simplificar las consultas y mejorar la seguridad de los datos. Al prohibir su creación, ayuda a mantener el control sobre el esquema de la base de datos y previene posibles riesgos de seguridad. Nivel de error sugerido: Advertencia"}},"level": {diff --git a/frontend/src/locales/sql-review/ja-JP.json b/frontend/src/locales/sql-review/ja-JP.jsonindex bfb734c9e3..f115ec4a8a 100644--- a/frontend/src/locales/sql-review/ja-JP.json+++ b/frontend/src/locales/sql-review/ja-JP.json@@ -463,6 +463,10 @@"system-event-disallow": {"title": "Không cho phép tạo sự kiện","description": "Quy tắc này cấm tạo sự kiện trong cơ sở dữ liệu. Các sự kiện hệ thống thường thực hiện các tác vụ tự động có thể ảnh hưởng đến môi trường cơ sở dữ liệu. Bằng cách không cho phép tạo chúng, nó giúp duy trì kiểm soát về các hoạt động cơ sở dữ liệu và ngăn chặn các sự cố tiềm ẩn. Mức độ lỗi được đề xuất: Cảnh báo"+ },+ "system-view-disallow": {+ "title": "ビューの作成を禁止",+ "description": "このルールは、データベース内でのビューの作成を禁止します。ビューは、クエリを単純化し、データのセキュリティを向上させることができるデータの仮想表現を提供します。その作成を禁止することで、データベーススキーマの管理を維持し、潜在的なセキュリティリスクを防ぐのに役立ちます。推奨されるエラーレベル:警告"}},"level": {diff --git a/frontend/src/locales/sql-review/vi-VN.json b/frontend/src/locales/sql-review/vi-VN.jsonindex bbf8086e14..8db1b0e4be 100644--- a/frontend/src/locales/sql-review/vi-VN.json+++ b/frontend/src/locales/sql-review/vi-VN.json@@ -463,6 +463,10 @@"system-event-disallow": {"title": "Không cho phép tạo sự kiện","description": "Quy tắc này cấm tạo sự kiện trong cơ sở dữ liệu. Các sự kiện hệ thống thường thực hiện các tác vụ tự động có thể ảnh hưởng đến môi trường cơ sở dữ liệu. Bằng cách không cho phép tạo chúng, nó giúp duy trì kiểm soát về các hoạt động cơ sở dữ liệu và ngăn chặn các sự cố tiềm ẩn. Mức độ lỗi được đề xuất: Cảnh báo"+ },+ "system-view-disallow": {+ "title": "Không cho phép tạo view",+ "description": "Quy tắc này cấm tạo view trong cơ sở dữ liệu. View cung cấp một biểu diễn ảo của dữ liệu có thể đơn giản hóa các truy vấn và tăng cường bảo mật dữ liệu. Bằng cách không cho phép tạo chúng, nó giúp duy trì kiểm soát về cấu trúc cơ sở dữ liệu và ngăn chặn các rủi ro bảo mật tiềm ẩn. Mức độ lỗi được đề xuất: Cảnh báo"}},"level": {diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.jsonindex 92afe48345..dfba85212b 100644--- a/frontend/src/locales/sql-review/zh-CN.json+++ b/frontend/src/locales/sql-review/zh-CN.json@@ -463,6 +463,10 @@"system-event-disallow": {"title": "禁止创建事件","description": "该规则禁止在数据库中创建事件。系统事件通常执行可能影响数据库环境的自动化任务。通过禁止创建它们,有助于控制数据库操作并防止潜在的中断。建议的错误级别:警告"+ },+ "system-view-disallow": {+ "title": "禁止创建视图",+ "description": "该规则禁止在数据库中创建视图。视图提供了数据的虚拟表示,可以简化查询并增强数据安全性。通过禁止创建它们,有助于维护对数据库模式的控制,并防止潜在的安全风险。建议的错误级别:警告"}},"level": {diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yamlindex 5e9c1170c0..f946783d48 100644--- a/frontend/src/types/sql-review-schema.yaml+++ b/frontend/src/types/sql-review-schema.yaml@@ -751,3 +751,8 @@ ruleList:engineList:- MYSQLcomponentList: []+ - type: system.view.disallow+ category: SYSTEM+ engineList:+ - MYSQL+ componentList: []diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.tsindex f642832148..8f0e0996d3 100644--- a/frontend/src/types/sqlReview.ts+++ b/frontend/src/types/sqlReview.ts@@ -157,6 +157,7 @@ export type RuleType =| "system.comment.length"| "system.procedure.disallow"| "system.event.disallow"+ | "system.view.disallow"| "index.no-duplicate-column"| "index.type-no-blob"| "index.key-number-limit"