Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-review): mssql functions and procedures restrictions (#12095)
Azusain6d06b5d311d5
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex e219e72c7d..eda9aa46ef 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -609,6 +609,12 @@ const (// MSSQLTableDisallowDML is an advisor type for MSSQL disallow DML for specific tables.MSSQLTableDisallowDML = "bb.plugin.advisor.mssql.table.disallow-dml"++ // MSSQLFuctionDisallowCreate restricts the creation of functions.+ MSSQLFunctionDisallowCreateOrAlter Type = "bb.plugin.advisor.mssql.function.disallow-create-or-alter"++ // MSSQLProcedureDisallowCreateOrAlter restricts the creation of procedures.+ MSSQLProcedureDisallowCreateOrAlter Type = "bb.plugin.advisor.mssql.procedure.disallow-create-or-alter")// Advice is the result of an advisor.diff --git a/backend/plugin/advisor/mssql/advisor_function_disallow_create_or_alter.go b/backend/plugin/advisor/mssql/advisor_function_disallow_create_or_alter.gonew file mode 100644index 0000000000..0216ffd8bb--- /dev/null+++ b/backend/plugin/advisor/mssql/advisor_function_disallow_create_or_alter.go@@ -0,0 +1,64 @@+package mssql++import (+ "github.com/antlr4-go/antlr/v4"+ parser "github.com/bytebase/tsql-parser"+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/proto/generated-go/store"+)++func init() {+ advisor.Register(store.Engine_MSSQL, advisor.MSSQLFunctionDisallowCreateOrAlter, &FunctionDisallowCreateOrAlterAdvisor{})+}++type FunctionDisallowCreateOrAlterAdvisor struct{}++type FunctionDisallowCreateOrAlterChecker struct {+ *parser.BaseTSqlParserListener++ level advisor.Status+ title string+ adviceList []advisor.Advice+}++// Check implements advisor.Advisor.+func (*FunctionDisallowCreateOrAlterAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ tree, ok := ctx.AST.(antlr.Tree)+ if !ok {+ return nil, errors.Errorf("failed to convert to Tree")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }++ checker := &FunctionDisallowCreateOrAlterChecker{+ level: level,+ title: ctx.Rule.Type,+ }++ antlr.ParseTreeWalkerDefault.Walk(checker, 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+}++func (checker *FunctionDisallowCreateOrAlterChecker) EnterCreate_or_alter_function(ctx *parser.Create_or_alter_functionContext) {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.DisallowCreateFunction,+ Title: checker.title,+ Content: "Creating or altering functions is prohibited",+ Line: ctx.GetStart().GetLine(),+ })+}diff --git a/backend/plugin/advisor/mssql/advisor_procedure_disallow_create_or_alter.go b/backend/plugin/advisor/mssql/advisor_procedure_disallow_create_or_alter.gonew file mode 100644index 0000000000..001aebc83b--- /dev/null+++ b/backend/plugin/advisor/mssql/advisor_procedure_disallow_create_or_alter.go@@ -0,0 +1,63 @@+package mssql++import (+ "github.com/antlr4-go/antlr/v4"+ parser "github.com/bytebase/tsql-parser"+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/proto/generated-go/store"+)++func init() {+ advisor.Register(store.Engine_MSSQL, advisor.MSSQLProcedureDisallowCreateOrAlter, &FunctionDisallowCreateOrAlterAdvisor{})+}++type ProcedureDisallowCreateOrAlterAdvisor struct{}++type ProcedureDisallowCreateOrAlterChecker struct {+ *parser.BaseTSqlParserListener++ level advisor.Status+ title string+ adviceList []advisor.Advice+}++func (*ProcedureDisallowCreateOrAlterAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ tree, ok := ctx.AST.(antlr.Tree)+ if !ok {+ return nil, errors.Errorf("failed to convert to Tree")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }++ checker := &ProcedureDisallowCreateOrAlterChecker{+ level: level,+ title: ctx.Rule.Type,+ }++ antlr.ParseTreeWalkerDefault.Walk(checker, 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+}++func (checker *FunctionDisallowCreateOrAlterChecker) EnterCreate_or_alter_procedure(ctx *parser.Create_or_alter_procedureContext) {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.DisallowCreateFunction,+ Title: checker.title,+ Content: "Creating or altering procedures is prohibited",+ Line: ctx.GetStart().GetLine(),+ })+}diff --git a/backend/plugin/advisor/mssql/mssql_rules_test.go b/backend/plugin/advisor/mssql/mssql_rules_test.goindex a758a78bf7..6ce85412d9 100644--- a/backend/plugin/advisor/mssql/mssql_rules_test.go+++ b/backend/plugin/advisor/mssql/mssql_rules_test.go@@ -9,7 +9,7 @@ import ()func TestMSSQLRules(t *testing.T) {- snowflakeRules := []advisor.SQLReviewRuleType{+ mssqlRules := []advisor.SQLReviewRuleType{advisor.SchemaRuleStatementNoSelectAll,advisor.SchemaRuleTableNaming,advisor.SchemaRuleTableNameNoKeyword,@@ -25,9 +25,11 @@ func TestMSSQLRules(t *testing.T) {advisor.SchemaRuleSchemaBackwardCompatibility,advisor.SchemaRuleRequiredColumn,advisor.SchemaRuleColumnTypeDisallowList,+ advisor.SchemaRuleFunctionDisallowCreate,+ advisor.SchemaRuleProcedureDisallowCreate,}- for _, rule := range snowflakeRules {+ for _, rule := range mssqlRules {advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_MSSQL, nil, false /* record */)}}diff --git a/backend/plugin/advisor/mssql/test/system_function_disallow_create.yaml b/backend/plugin/advisor/mssql/test/system_function_disallow_create.yamlnew file mode 100644index 0000000000..7569e4e185--- /dev/null+++ b/backend/plugin/advisor/mssql/test/system_function_disallow_create.yaml@@ -0,0 +1,23 @@+- statement: |-+ CREATE FUNCTION dbo.ufnGetInventoryStock(@ProductID int)+ RETURNS int+ AS+ -- Returns the stock level for the product.+ BEGIN+ DECLARE @ret int;+ SELECT @ret = SUM(p.Quantity)+ FROM Production.ProductInventory p+ WHERE p.ProductID = @ProductID+ AND p.LocationID = '6';+ IF (@ret IS NULL)+ SET @ret = 0;+ RETURN @ret;+ END;+ want:+ - status: WARN+ code: 1701+ title: system.function.disallow-create+ content: |-+ Creating or altering functions is prohibited+ line: 1+ details: ""diff --git a/backend/plugin/advisor/mssql/test/system_procedure_disallow_create.yaml b/backend/plugin/advisor/mssql/test/system_procedure_disallow_create.yamlnew file mode 100644index 0000000000..5a9bf82970--- /dev/null+++ b/backend/plugin/advisor/mssql/test/system_procedure_disallow_create.yaml@@ -0,0 +1,27 @@+- statement: |-+ CREATE PROCEDURE SalesLT.uspGetCustomerCompany+ (+ -- Add the parameters for the stored procedure here+ @LastName nvarchar(50) = NULL,+ @FirstName nvarchar(50) = NULL+ )+ AS+ BEGIN+ -- SET NOCOUNT ON added to prevent extra result sets from+ -- interfering with SELECT statements.+ SET NOCOUNT ON++ -- Insert statements for procedure here+ SELECT FirstName, LastName, CompanyName+ FROM SalesLT.Customer+ WHERE FirstName = @FirstName AND LastName = @LastName;+ END+ GO+ want:+ - status: WARN+ code: 1701+ title: system.procedure.disallow-create+ content: |-+ Creating or altering procedures is prohibited+ line: 1+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 7ac757a581..e88849be90 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1676,6 +1676,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Tyif engine == storepb.Engine_MYSQL {return MySQLProcedureDisallowCreate, nil}+ if engine == storepb.Engine_MSSQL {+ return MSSQLProcedureDisallowCreateOrAlter, nil+ }case SchemaRuleEventDisallowCreate:if engine == storepb.Engine_MYSQL {return MySQLEventDisallowCreate, nil@@ -1688,6 +1691,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Tyif engine == storepb.Engine_MYSQL {return MySQLFunctionDisallowCreate, nil}+ if engine == storepb.Engine_MSSQL {+ return MSSQLFunctionDisallowCreateOrAlter, nil+ }case SchemaRuleFunctionDisallowList:if engine == storepb.Engine_MYSQL {return MySQLFunctionDisallowedList, nildiff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yamlindex 790bba09cc..83191b27f9 100644--- a/frontend/src/types/sql-review-schema.yaml+++ b/frontend/src/types/sql-review-schema.yaml@@ -852,6 +852,7 @@ ruleList:category: SYSTEMengineList:- MYSQL+ - MSSQL- type: system.event.disallow-createcategory: SYSTEMengineList:@@ -864,6 +865,7 @@ ruleList:category: SYSTEMengineList:- MYSQL+ - MSSQL- type: system.function.disallowed-listcategory: SYSTEMengineList: