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: skip Postgres system schema for data masking (#7570)
rebeliceec2af0c69e9c
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex d25b1f59db..3b778241e0 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -35,6 +35,7 @@ import ("github.com/bytebase/bytebase/backend/plugin/advisor/catalog"advisorDB "github.com/bytebase/bytebase/backend/plugin/advisor/db""github.com/bytebase/bytebase/backend/plugin/db"+ "github.com/bytebase/bytebase/backend/plugin/db/pg"parser "github.com/bytebase/bytebase/backend/plugin/parser/sql""github.com/bytebase/bytebase/backend/plugin/parser/sql/ast""github.com/bytebase/bytebase/backend/plugin/parser/sql/transform"@@ -1108,11 +1109,20 @@ func (s *SQLService) preQuery(ctx context.Context, request *v1pb.QueryRequest) (if err != nil {return nil, nil, advisor.Success, nil, nil, nil, false, status.Errorf(codes.Internal, "Failed to get sensitive schema info for statement: %s, error: %v", request.Statement, err.Error())}- case db.Postgres, db.Redshift, db.RisingWave:+ case db.Redshift, db.RisingWave:sensitiveSchemaInfo, err = s.getSensitiveSchemaInfo(ctx, instance, []string{request.ConnectionDatabase}, request.ConnectionDatabase)if err != nil {return nil, nil, advisor.Success, nil, nil, nil, false, status.Errorf(codes.Internal, "Failed to get sensitive schema info for statement: %s, error: %v", request.Statement, err.Error())}+ case db.Postgres:+ if allPostgresSystemObjects(request.Statement) {+ sensitiveSchemaInfo = nil+ } else {+ sensitiveSchemaInfo, err = s.getSensitiveSchemaInfo(ctx, instance, []string{request.ConnectionDatabase}, request.ConnectionDatabase)+ if err != nil {+ return nil, nil, advisor.Success, nil, nil, nil, false, status.Errorf(codes.Internal, "Failed to get sensitive schema info for statement: %s, error: %v", request.Statement, err.Error())+ }+ }case db.Oracle, db.DM:if instance.Options == nil || !instance.Options.SchemaTenantMode {sensitiveSchemaInfo, err = s.getSensitiveSchemaInfo(ctx, instance, []string{request.ConnectionDatabase}, request.ConnectionDatabase)@@ -1192,6 +1202,35 @@ func (s *SQLService) preQuery(ctx context.Context, request *v1pb.QueryRequest) (return instance, database, adviceStatus, adviceList, sensitiveSchemaInfo, activity, allowExport, nil}+func allPostgresSystemObjects(statement string) bool {+ // We need to distinguish between specified public schema and by default.+ resources, err := parser.ExtractResourceList(parser.Postgres, "", "", statement)+ if err != nil {+ log.Debug("Failed to extract resource list from statement", zap.String("statement", statement), zap.Error(err))+ return false+ }+ systemSchemas := make(map[string]bool)+ for _, schema := range pg.SystemSchemaList {+ systemSchemas[schema] = true+ }+ systemTables := make(map[string]bool)+ for _, table := range pg.SystemTableList {+ systemTables[table] = true+ }+ for _, resource := range resources {+ if systemSchemas[resource.Schema] {+ continue+ }+ // If schema is not specified, user can access the pg_catalog schema if the table is pg_catalog's system table.+ // So we need to check this case.+ if resource.Schema == "" && systemTables[resource.Table] {+ continue+ }+ return false+ }+ return true+}+func (s *SQLService) createQueryActivity(ctx context.Context, user *store.UserMessage, level api.ActivityLevel, containerID int, payload api.ActivitySQLEditorQueryPayload) (*store.ActivityMessage, error) {// TODO: use v1 activity API instead ofactivityBytes, err := json.Marshal(payload)diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.goindex 286ff5fec5..6bbc44e535 100644--- a/backend/plugin/db/pg/sync.go+++ b/backend/plugin/db/pg/sync.go@@ -35,6 +35,95 @@ var SystemSchemaList = []string{"timescaledb_experimental",}+// SystemTableList is the list of system tables that we will exclude from the schema sync.+var SystemTableList = []string{+ "pg_aggregate",+ "pg_am",+ "pg_amop",+ "pg_amproc",+ "pg_attrdef",+ "pg_attribute",+ "pg_authid",+ "pg_auth_members",+ "pg_cast",+ "pg_class",+ "pg_collation",+ "pg_constraint",+ "pg_conversion",+ "pg_database",+ "pg_db_role_setting",+ "pg_default_acl",+ "pg_depend",+ "pg_description",+ "pg_enum",+ "pg_event_trigger",+ "pg_extension",+ "pg_foreign_data_wrapper",+ "pg_foreign_server",+ "pg_foreign_table",+ "pg_index",+ "pg_inherits",+ "pg_init_privs",+ "pg_language",+ "pg_largeobject",+ "pg_largeobject_metadata",+ "pg_namespace",+ "pg_opclass",+ "pg_operator",+ "pg_opfamily",+ "pg_parameter_acl",+ "pg_partitioned_table",+ "pg_policy",+ "pg_proc",+ "pg_publication",+ "pg_publication_namespace",+ "pg_publication_rel",+ "pg_range",+ "pg_replication_origin",+ "pg_rewrite",+ "pg_seclabel",+ "pg_sequence",+ "pg_shdepend",+ "pg_shdescription",+ "pg_shseclabel",+ "pg_statistic",+ "pg_statistic_ext",+ "pg_statistic_ext_data",+ "pg_subscription",+ "pg_subscription_rel",+ "pg_tablespace",+ "pg_transform",+ "pg_trigger",+ "pg_ts_config",+ "pg_ts_config_map",+ "pg_ts_dict",+ "pg_ts_parser",+ "pg_ts_template",+ "pg_type",+ "pg_user_mapping",+ "pg_stat_activity",+ "pg_stat_replication",+ "pg_stat_replication_slots",+ "pg_stat_wal_receiver",+ "pg_stat_recovery_prefetch",+ "pg_stat_subscription",+ "pg_stat_subscription_stats",+ "pg_stat_ssl",+ "pg_stat_gssapi",+ "pg_stat_archiver",+ "pg_stat_bgwriter",+ "pg_stat_wal",+ "pg_stat_database",+ "pg_stat_database_conflicts",+ "pg_stat_all_tables",+ "pg_stat_all_indexes",+ "pg_statio_all_tables",+ "pg_statio_all_indexes",+ "pg_statio_all_sequences",+ "pg_stat_user_functions",+ "pg_stat_slru",+}+const systemSchemas = "'information_schema', 'pg_catalog', 'pg_toast', '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_internal', '_timescaledb_config', 'timescaledb_information', 'timescaledb_experimental'"// SyncInstance syncs the instance.diff --git a/backend/plugin/parser/sql/utils.go b/backend/plugin/parser/sql/utils.goindex ed7594a508..c41986043b 100644--- a/backend/plugin/parser/sql/utils.go+++ b/backend/plugin/parser/sql/utils.go@@ -90,7 +90,7 @@ func ExtractResourceList(engineType EngineType, currentDatabase string, currentSreturn extractOracleResourceList(currentDatabase, currentSchema, sql)case Postgres, RisingWave:// The resource list for Postgres may contains table, view and temporary table.- return extractPostgresResourceList(currentDatabase, "public", sql)+ return extractPostgresResourceList(currentDatabase, currentSchema, sql)case Snowflake:return extractSnowflakeNormalizeResourceListFromSelectStatement(currentDatabase, "PUBLIC", sql)case MSSQL: