Skip to content

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

feat(pg/advisor): creates allowlist for encoding (#3039)

VictorAssunc2eb54bc949f4
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 482f3be6e9..62b7b6665a 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -236,6 +236,9 @@ const (
// PostgreSQLIndexKeyNumberLimit is an advisor type for postgresql index key number limit.
PostgreSQLIndexKeyNumberLimit Type = "bb.plugin.advisor.postgresql.index.key-number-limit"
+
+ // PostgreSQLEncodingAllowlist is an advisor type for PostgreSQL encoding allowlist.
+ PostgreSQLEncodingAllowlist Type = "bb.plugin.advisor.postgresql.charset.allowlist"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_encoding_allowlist.go b/plugin/advisor/pg/advisor_encoding_allowlist.go
new file mode 100644
index 0000000000..be9a2d4c10
--- /dev/null
+++ b/plugin/advisor/pg/advisor_encoding_allowlist.go
@@ -0,0 +1,112 @@
+package pg
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*EncodingAllowlistAdvisor)(nil)
+ _ ast.Visitor = (*encodingAllowlistChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLEncodingAllowlist, &EncodingAllowlistAdvisor{})
+}
+
+// EncodingAllowlistAdvisor is the advisor checking for encoding allowlist.
+type EncodingAllowlistAdvisor struct {
+}
+
+// Check checks for encoding allowlist.
+func (*EncodingAllowlistAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmtList, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ payload, err := advisor.UnmarshalAllowlistRulePayload(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &encodingAllowlistChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ allowlist: make(map[string]bool),
+ }
+
+ for _, encoding := range payload.Allowlist {
+ checker.allowlist[strings.ToLower(encoding)] = true
+ }
+
+ for _, stmt := range stmtList {
+ ast.Walk(checker, stmt)
+ }
+
+ 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 encodingAllowlistChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+ allowlist map[string]bool
+}
+
+// Visit implements the ast.Visitor interface.
+func (checker *encodingAllowlistChecker) Visit(in ast.Node) ast.Visitor {
+ code := advisor.Ok
+ var disabledEncoding string
+ line := checker.line
+ switch node := in.(type) {
+ case *ast.CreateDatabaseStmt:
+ encoding := getDatabaseEncoding(node.OptionList)
+ if _, exist := checker.allowlist[encoding]; encoding != "" && !exist {
+ code = advisor.DisabledCharset
+ disabledEncoding = encoding
+ }
+ default:
+ }
+
+ if code != advisor.Ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: code,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" used disabled encoding '%s'", checker.text, disabledEncoding),
+ Line: line,
+ })
+ }
+
+ return checker
+}
+
+func getDatabaseEncoding(optionList []*ast.DatabaseOptionDef) string {
+ for _, option := range optionList {
+ if option.Type == ast.DatabaseOptionEncoding {
+ return strings.ToLower(option.Value)
+ }
+ }
+
+ return ""
+}
diff --git a/plugin/advisor/pg/advisor_encoding_allowlist_test.go b/plugin/advisor/pg/advisor_encoding_allowlist_test.go
new file mode 100644
index 0000000000..48e77aee84
--- /dev/null
+++ b/plugin/advisor/pg/advisor_encoding_allowlist_test.go
@@ -0,0 +1,69 @@
+package pg
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestCharsetAllowlist(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: `CREATE DATABASE test WITH ENCODING 'UTF8'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `CREATE DATABASE test`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `CREATE DATABASE test WITH ENCODING 'LATIN1'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.DisabledCharset,
+ Title: "charset.allowlist",
+ Content: "\"\" used disabled encoding 'latin1'",
+ },
+ },
+ },
+ {
+ Statement: "/* this is a comment */",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ }
+
+ payload, err := json.Marshal(advisor.AllowlistRulePayload{
+ Allowlist: []string{"utf8"},
+ })
+ require.NoError(t, err)
+ advisor.RunSQLReviewRuleTests(t, tests, &EncodingAllowlistAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleCharsetAllowlist,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: string(payload),
+ }, advisor.MockPostgreSQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 000381ef01..2177cd4276 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -804,6 +804,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
switch engine {
case db.MySQL, db.TiDB:
return MySQLCharsetAllowlist, nil
+ case db.Postgres:
+ return PostgreSQLEncodingAllowlist, nil
}
case SchemaRuleCollationAllowlist:
switch engine {
diff --git a/plugin/parser/ast/database_option_def.go b/plugin/parser/ast/database_option_def.go
index 2bd2f22b96..87b830e511 100644
--- a/plugin/parser/ast/database_option_def.go
+++ b/plugin/parser/ast/database_option_def.go
@@ -5,7 +5,7 @@ type DatabaseOptionType int
const (
// DatabaseOptionEncoding is the type for the database encoding option.
- DatabaseOptionEncoding = iota
+ DatabaseOptionEncoding DatabaseOptionType = iota
)
// DatabaseOptionDef is the struct for database option.