Skip to content

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

feat(sql-review): implement object owner check rule (#14157)

rebelice8867221818ee
diff --git a/backend/plugin/advisor/builtin_rules.go b/backend/plugin/advisor/builtin_rules.go
index 510685e1af..610bf6693e 100644
--- a/backend/plugin/advisor/builtin_rules.go
+++ b/backend/plugin/advisor/builtin_rules.go
@@ -4,11 +4,12 @@ import storepb "github.com/bytebase/bytebase/proto/generated-go/store"
const (
BuiltinRulePriorBackupCheck SQLReviewRuleType = "builtin.prior-backup-check"
+ BuiltinRuleObjectOwnerCheck SQLReviewRuleType = "builtin.object-owner-check"
)
func GetBuiltinRules(engine storepb.Engine) []*storepb.SQLReviewRule {
switch engine {
- case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_POSTGRES, storepb.Engine_MSSQL, storepb.Engine_ORACLE:
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MSSQL, storepb.Engine_ORACLE:
return []*storepb.SQLReviewRule{
{
Type: string(BuiltinRulePriorBackupCheck),
@@ -18,6 +19,23 @@ func GetBuiltinRules(engine storepb.Engine) []*storepb.SQLReviewRule {
Comment: "",
},
}
+ case storepb.Engine_POSTGRES:
+ return []*storepb.SQLReviewRule{
+ {
+ Type: string(BuiltinRulePriorBackupCheck),
+ Level: storepb.SQLReviewRuleLevel_ERROR,
+ Payload: "",
+ Engine: engine,
+ Comment: "",
+ },
+ {
+ Type: string(BuiltinRuleObjectOwnerCheck),
+ Level: storepb.SQLReviewRuleLevel_WARNING,
+ Payload: "",
+ Engine: engine,
+ Comment: "",
+ },
+ }
default:
return nil
}
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index ed71e7f987..9a28ece621 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -202,6 +202,7 @@ const (
// 2001 ~ 2099 builtin error code.
BuiltinPriorBackupCheck Code = 2001
+ BuiltinObjectOwnerCheck Code = 2002
)
// Int returns the int type of code.
@@ -648,6 +649,9 @@ const (
// PostgreSQLBuiltinPriorBackupCheck is an advisor type for PostgreSQL do prior backup check.
PostgreSQLBuiltinPriorBackupCheck Type = "bb.plugin.advisor.postgresql.builtin.prior-backup-check"
+ // PostgreSQLBuiltinObjectOwnerCheck is an advisor type for PostgreSQL do object owner check.
+ PostgreSQLBuiltinObjectOwnerCheck Type = "bb.plugin.advisor.postgresql.builtin.object-owner-check"
+
// PostgreSQLStatementMaximumLimitValue is an advisor type for PostgreSQL statement maximum limit value.
PostgreSQLStatementMaximumLimitValue Type = "bb.plugin.advisor.postgresql.statement.maximum-limit-value"
diff --git a/backend/plugin/advisor/pg/advisor_builtin_object_owner_check.go b/backend/plugin/advisor/pg/advisor_builtin_object_owner_check.go
new file mode 100644
index 0000000000..d1ec6e08ab
--- /dev/null
+++ b/backend/plugin/advisor/pg/advisor_builtin_object_owner_check.go
@@ -0,0 +1,608 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
+ "github.com/bytebase/bytebase/backend/store/model"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*BuiltinObjectOwnerCheckAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLBuiltinObjectOwnerCheck, &BuiltinObjectOwnerCheckAdvisor{})
+}
+
+const (
+ pgDatabaseOwner = "pg_database_owner"
+)
+
+type BuiltinObjectOwnerCheckAdvisor struct {
+}
+
+func (*BuiltinObjectOwnerCheckAdvisor) Check(ctx advisor.Context, _ string) ([]*storepb.Advice, error) {
+ var adviceList []*storepb.Advice
+ stmtList, ok := ctx.AST.([]ast.Node)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to Node")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ title := string(ctx.Rule.Type)
+
+ dbMetadata := model.NewDatabaseMetadata(ctx.DBSchema)
+ currentRole := ctx.DBSchema.Owner
+ defaultSchema := "public"
+
+ for _, stmt := range stmtList {
+ switch n := stmt.(type) {
+ case *ast.VariableSetStmt:
+ if n.Name == "role" {
+ currentRole = n.GetRoleName()
+ }
+ case *ast.AlterSequenceStmt:
+ // todo: use sequence owner instead of schema owner
+ if n.Name == nil {
+ continue
+ }
+ schemaName := n.Name.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Sequence \"%s\" is owned by \"%s\", but the current role is \"%s\".", n.Name.Name, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.AlterTableStmt:
+ if n.Table == nil {
+ continue
+ }
+ schemaName := n.Table.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ tableMeta := schemaMeta.GetTable(n.Table.Name)
+ if tableMeta == nil {
+ continue
+ }
+ owner := tableMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Table \"%s\" is owned by \"%s\", but the current role is \"%s\".", n.Table.Name, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.AlterTypeStmt:
+ // todo: use type owner instead of schema owner
+ if n.Type == nil {
+ continue
+ }
+ schemaName := n.Type.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Type \"%s\" is owned by \"%s\", but the current role is \"%s\".", n.Type.Name, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateExtensionStmt:
+ schemaName := n.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateFunctionStmt:
+ if n.Function == nil {
+ continue
+ }
+ schemaName := n.Function.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateIndexStmt:
+ if n.Index == nil || n.Index.Table == nil {
+ continue
+ }
+ schemaName := n.Index.Table.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateSchemaStmt:
+ owner := dbMetadata.GetOwner()
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Database \"%s\" is owned by \"%s\", but the current role is \"%s\".", dbMetadata.GetName(), owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateSequenceStmt:
+ if n.SequenceDef.SequenceName == nil {
+ continue
+ }
+ schemaName := n.SequenceDef.SequenceName.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateTableStmt:
+ if n.Name == nil {
+ continue
+ }
+ schemaName := n.Name.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateTriggerStmt:
+ if n.Trigger == nil || n.Trigger.Table == nil {
+ continue
+ }
+ schemaName := n.Trigger.Table.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateTypeStmt:
+ if n.Type == nil || n.Type.TypeName() == nil {
+ continue
+ }
+ schemaName := n.Type.TypeName().Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.CreateViewStmt:
+ if n.Name == nil {
+ continue
+ }
+ schemaName := n.Name.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.DropFunctionStmt:
+ for _, funcName := range n.FunctionList {
+ if funcName == nil {
+ continue
+ }
+ schemaName := funcName.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
+ case *ast.DropIndexStmt:
+ for _, indexDef := range n.IndexList {
+ if indexDef == nil {
+ continue
+ }
+ schemaName := defaultSchema
+ if indexDef.Table != nil && indexDef.Table.Schema != "" {
+ schemaName = indexDef.Table.Schema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
+ case *ast.DropSchemaStmt:
+ for _, schemaName := range n.SchemaList {
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
+ case *ast.DropSequenceStmt:
+ for _, seqName := range n.SequenceNameList {
+ if seqName == nil {
+ continue
+ }
+ schemaName := seqName.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
+ case *ast.DropTableStmt:
+ for _, table := range n.TableList {
+ if table == nil {
+ continue
+ }
+ schemaName := table.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ tableMeta := schemaMeta.GetTable(table.Name)
+ owner := tableMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Table \"%s\" is owned by \"%s\", but the current role is \"%s\".", table.Name, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
+ case *ast.DropTriggerStmt:
+ if n.Trigger == nil || n.Trigger.Table == nil {
+ continue
+ }
+ schemaName := n.Trigger.Table.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.DropTypeStmt:
+ for _, typeName := range n.TypeNameList {
+ if typeName == nil {
+ continue
+ }
+ schemaName := typeName.Schema
+ if schemaName == "" {
+ schemaName = defaultSchema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
+ case *ast.RenameIndexStmt:
+ schemaName := defaultSchema
+ if n.Table != nil && n.Table.Schema != "" {
+ schemaName = n.Table.Schema
+ }
+ schemaMeta := dbMetadata.GetSchema(schemaName)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", schemaName, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ case *ast.RenameSchemaStmt:
+ schemaMeta := dbMetadata.GetSchema(n.Schema)
+ if schemaMeta == nil {
+ continue
+ }
+ owner := schemaMeta.GetOwner()
+ if owner == pgDatabaseOwner {
+ owner = dbMetadata.GetOwner()
+ }
+ if owner != currentRole {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Schema \"%s\" is owned by \"%s\", but the current role is \"%s\".", n.Schema, owner, currentRole),
+ Code: advisor.BuiltinObjectOwnerCheck.Int32(),
+ StartPosition: &storepb.Position{
+ Line: int32(stmt.LastLine()),
+ },
+ })
+ }
+ }
+ }
+
+ return adviceList, nil
+}
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 7248c1e90e..3abcf774f5 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1602,6 +1602,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
case storepb.Engine_ORACLE:
return OracleBuiltinPriorBackupCheck, nil
}
+ case BuiltinRuleObjectOwnerCheck:
+ if engine == storepb.Engine_POSTGRES {
+ return PostgreSQLBuiltinObjectOwnerCheck, nil
+ }
}
return "", errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)
}
diff --git a/backend/plugin/parser/sql/ast/variable_set_stmt.go b/backend/plugin/parser/sql/ast/variable_set_stmt.go
index c95e8172aa..1908743705 100644
--- a/backend/plugin/parser/sql/ast/variable_set_stmt.go
+++ b/backend/plugin/parser/sql/ast/variable_set_stmt.go
@@ -7,3 +7,15 @@ type VariableSetStmt struct {
Args []ExpressionNode
IsLocal bool
}
+
+func (s *VariableSetStmt) GetRoleName() string {
+ if len(s.Args) != 1 {
+ return ""
+ }
+
+ if v, ok := s.Args[0].(*StringDef); ok {
+ return v.Value
+ }
+
+ return ""
+}
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 9b0c17179e..b3e52a029e 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -301,6 +301,7 @@ func (t *TableConfig) IsEmpty() bool {
// DatabaseMetadata is the metadata for a database.
type DatabaseMetadata struct {
name string
+ owner string
internal map[string]*SchemaMetadata
linkedDatabase map[string]*LinkedDatabaseMetadata
}
@@ -309,6 +310,7 @@ type DatabaseMetadata struct {
func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *DatabaseMetadata {
databaseMetadata := &DatabaseMetadata{
name: metadata.Name,
+ owner: metadata.Owner,
internal: make(map[string]*SchemaMetadata),
linkedDatabase: make(map[string]*LinkedDatabaseMetadata),
}
@@ -408,6 +410,10 @@ func (d *DatabaseMetadata) GetLinkedDatabase(name string) *LinkedDatabaseMetadat
return d.linkedDatabase[name]
}
+func (d *DatabaseMetadata) GetOwner() string {
+ return d.owner
+}
+
// LinkedDatabaseMetadata is the metadata for a linked database.
type LinkedDatabaseMetadata struct {
name string
@@ -442,6 +448,10 @@ type SchemaMetadata struct {
proto *storepb.SchemaMetadata
}
+func (s *SchemaMetadata) GetOwner() string {
+ return s.proto.Owner
+}
+
// GetTable gets the schema by name.
func (s *SchemaMetadata) GetTable(name string) *TableMetadata {
return s.internalTables[name]
@@ -664,6 +674,10 @@ type TableMetadata struct {
proto *storepb.TableMetadata
}
+func (t *TableMetadata) GetOwner() string {
+ return t.proto.Owner
+}
+
func (t *TableMetadata) GetTableComment() string {
return t.proto.Comment
}
diff --git a/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue b/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue
index 74390d6f87..b2992ad7de 100644
--- a/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue
+++ b/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue
@@ -362,6 +362,17 @@ const isBuiltinRule = (type: string) => {
return type.startsWith("builtin.");
};
+const builtinRuleLevel = (type: string): SQLReviewRuleLevel => {
+ switch (type) {
+ case "builtin.prior-backup-check":
+ return SQLReviewRuleLevel.ERROR;
+ case "builtin.object-owner-check":
+ return SQLReviewRuleLevel.WARNING
+ default:
+ return SQLReviewRuleLevel.ERROR;
+ }
+};
+
const categoryAndTitle = (
checkResult: PlanCheckRun_Result
): [string, string] => {
@@ -478,7 +489,7 @@ const getActiveRule = (type: string): RuleTemplateV2 | undefined => {
type,
category: "BUILTIN",
engine,
- level: SQLReviewRuleLevel.ERROR,
+ level: builtinRuleLevel(type),
componentList: [],
};
}
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index eba012ce32..73bd5d095b 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -711,6 +711,10 @@
"builtin-prior-backup-check": {
"title": "Prior backup feasibility check",
"description": "This rule checks whether a backup can be created before data changes. It verifies the existence of the backup target database and disallows non-supported scenarios. It's builtin and cannot skip."
+ },
+ "builtin-object-owner-check": {
+ "title": "Object owner check",
+ "description": "This rule checks whether the object owner for DDL is the same as the current user. It's builtin and cannot skip."
}
},
"level": {
diff --git a/frontend/src/locales/sql-review/es-ES.json b/frontend/src/locales/sql-review/es-ES.json
index 9907dc3730..138bb08107 100644
--- a/frontend/src/locales/sql-review/es-ES.json
+++ b/frontend/src/locales/sql-review/es-ES.json
@@ -711,6 +711,10 @@
"builtin-prior-backup-check": {
"title": "Comprobación previa de viabilidad de la copia de seguridad",
"description": "Esta regla verifica si se puede crear una copia de seguridad antes de que se modifiquen los datos. Verifica la existencia de la base de datos de destino de la copia de seguridad y rechaza los escenarios no admitidos. Está incorporada y no se puede omitir."
+ },
+ "builtin-object-owner-check": {
+ "title": "Verificación del propietario del objeto",
+ "description": "Esta regla verifica si el propietario del objeto para DDL es el mismo que el usuario actual. Está incorporada y no se puede omitir."
}
},
"level": {
diff --git a/frontend/src/locales/sql-review/ja-JP.json b/frontend/src/locales/sql-review/ja-JP.json
index 48f6910b67..efa6be1fed 100644
--- a/frontend/src/locales/sql-review/ja-JP.json
+++ b/frontend/src/locales/sql-review/ja-JP.json
@@ -711,6 +711,10 @@
"builtin-prior-backup-check": {
"title": "事前のバックアップ実行可能性チェック",
"description": "このルールは、データが変更される前にバックアップを作成できるかどうかを確認します。バックアップ ターゲット データベースの存在を確認し、サポートされていないシナリオを禁止します。これは組み込みであり、スキップできません。"
+ },
+ "builtin-object-owner-check": {
+ "title": "オブジェクト所有者のチェック",
+ "description": "このルールは、DDLのオブジェクト所有者が現在のユーザーと同じであるかどうかを確認します。これは組み込みであり、スキップできません。"
}
},
"level": {
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index 2469a3037b..ca1c7808dc 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -711,6 +711,10 @@
"builtin-prior-backup-check": {
"title": "事先备份可行性检查",
"description": "此规则检查是否可以在数据更改之前创建备份。它会验证备份目标数据库是否存在,并禁止不支持的方案。它是内置的,无法跳过。"
+ },
+ "builtin-object-owner-check": {
+ "title": "对象所有者检查",
+ "description": "此规则检查 DDL 的对象所有者是否与当前用户相同。内置规则,无法跳过。"
}
},
"level": {