Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(pg): sync columns and foreign tables from pg_catalog so privileges cannot hide them (#20581)
rebelice106ed1653841
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.goindex 9204333985..674cb9a6e0 100644--- a/backend/plugin/db/pg/sync.go+++ b/backend/plugin/db/pg/sync.go@@ -75,7 +75,15 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetadif databaseMetadata == nil {return nil, common.Errorf(common.NotFound, "database %q not found", d.databaseName)}- isAtLeastPG10 := isAtLeastPG10(d.connectionCtx.EngineVersion)+ // Prefer the live server version over the engine version cached in the connection+ // context: the cached value can be empty or stale, and the version checks below+ // assume "new enough" on parse failure, which would break the sync on old servers.+ engineVersion := d.connectionCtx.EngineVersion+ if version, err := d.getVersion(ctx); err == nil {+ engineVersion = version+ }+ isAtLeastPG10 := isAtLeastPG10(engineVersion)+ isAtLeastPG12 := isAtLeastPG12(engineVersion)searchPath, err := d.GetSearchPath(ctx)if err != nil {return nil, common.Errorf(common.Internal, "failed to get search path for database %q: %v", d.databaseName, err)@@ -102,7 +110,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetadif err != nil {return nil, errors.Wrapf(err, "failed to get schemas from database %q", d.databaseName)}- columnMap, err := getTableColumns(txn)+ columnMap, err := getTableColumns(txn, isAtLeastPG12)if err != nil {return nil, errors.Wrapf(err, "failed to get columns from database %q", d.databaseName)}@@ -519,13 +527,25 @@ func getSchemas(txn *sql.Tx, extensionDepend map[int]bool) ([]string, []string,return schemaNames, schemaOwners, schemaComments, skipDump, nil}+// getListForeignTableQuery returns the foreign table query against pg_catalog directly.+// information_schema.foreign_tables hides foreign tables the current user has no privilege+// on (pg_has_role(c.relowner, 'USAGE') OR has_table_privilege(...) OR+// has_any_column_privilege(...)), silently omitting them from the synced metadata. The+// expressions are copied from the information_schema._pg_foreign_tables view definition+// minus the privilege predicate (and minus its pg_authid join, which only feeds a column+// we do not output and is not readable by non-superusers anyway).func getListForeignTableQuery() string {return `SELECT- foreign_table.foreign_table_schema,- foreign_table.foreign_table_name,- foreign_table.foreign_server_catalog,- foreign_table.foreign_server_name- FROM information_schema.foreign_tables AS foreign_table;`+ n.nspname AS foreign_table_schema,+ c.relname AS foreign_table_name,+ current_database() AS foreign_server_catalog,+ s.srvname AS foreign_server_name+ FROM pg_catalog.pg_foreign_table t+ JOIN pg_catalog.pg_class c ON c.oid = t.ftrelid+ JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace+ JOIN pg_catalog.pg_foreign_server s ON s.oid = t.ftserver+ WHERE c.relkind = 'f'+ ORDER BY n.nspname, c.relname;`}func getListTableQuery(isAtLeastPG10 bool) string {relisPartition := ""@@ -774,36 +794,76 @@ func setTxSearchPath(txn *sql.Tx, searchPath string) error {return nil}-var listColumnQuery = `+// getListColumnQuery returns the column query against pg_catalog directly.+// information_schema.columns hides columns of tables the current user has no privilege+// on (its WHERE clause checks pg_has_role(c.relowner, 'USAGE') OR has_column_privilege(...)),+// while indexes and constraints are synced from pg_catalog without such filtering. A sync+// user lacking table privileges would thus get table metadata with constraints but zero+// columns. Querying pg_catalog directly keeps column metadata complete regardless of+// privileges. The expressions are copied verbatim from the information_schema.columns+// view definition (minus the privilege predicate) so all produced values stay identical.+func getListColumnQuery(isAtLeastPG12 bool) string {+ // pg_attribute.attgenerated only exists on PostgreSQL >= 12. On older versions+ // information_schema.columns.column_default has no generated-column exclusion either,+ // so this matches the per-version information_schema behavior.+ columnDefault := "pg_catalog.pg_get_expr(ad.adbin, ad.adrelid)"+ if isAtLeastPG12 {+ columnDefault = "CASE WHEN a.attgenerated = '' THEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) END"+ }+ return fmt.Sprintf(`SELECT- cols.table_schema,- cols.table_name,- cols.column_name,- cols.data_type,- cols.character_maximum_length,- cols.numeric_precision,- cols.numeric_scale,- cols.datetime_precision,- cols.ordinal_position,- cols.column_default,- cols.is_nullable,- cols.collation_name,- cols.udt_schema,- cols.udt_name,- cols.identity_generation,- pg_catalog.col_description(c.oid, cols.ordinal_position::int) as column_comment-FROM INFORMATION_SCHEMA.COLUMNS AS cols-LEFT JOIN pg_namespace n ON n.nspname = cols.table_schema-LEFT JOIN pg_class c ON c.relname = cols.table_name AND c.relnamespace = n.oid AND c.relkind IN ('r', 'v', 'm', 'p', 'f')` + fmt.Sprintf(`-WHERE cols.table_schema NOT IN (%s)- AND cols.table_schema NOT LIKE 'pg_temp%%'- AND cols.table_schema NOT LIKE 'pg_toast%%'-ORDER BY cols.table_schema, cols.table_name, cols.ordinal_position;`, pgparser.SystemSchemaWhereClause)+ nc.nspname AS table_schema,+ c.relname AS table_name,+ a.attname AS column_name,+ CASE+ WHEN t.typtype = 'd' THEN+ CASE+ WHEN bt.typelem <> 0 AND bt.typlen = -1 THEN 'ARRAY'+ WHEN nbt.nspname = 'pg_catalog' THEN pg_catalog.format_type(t.typbasetype, NULL)+ ELSE 'USER-DEFINED'+ END+ ELSE+ CASE+ WHEN t.typelem <> 0 AND t.typlen = -1 THEN 'ARRAY'+ WHEN nt.nspname = 'pg_catalog' THEN pg_catalog.format_type(a.atttypid, NULL)+ ELSE 'USER-DEFINED'+ END+ END AS data_type,+ information_schema._pg_char_max_length(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)) AS character_maximum_length,+ information_schema._pg_numeric_precision(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)) AS numeric_precision,+ information_schema._pg_numeric_scale(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)) AS numeric_scale,+ information_schema._pg_datetime_precision(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)) AS datetime_precision,+ a.attnum AS ordinal_position,+ %s AS column_default,+ CASE WHEN a.attnotnull OR (t.typtype = 'd' AND t.typnotnull) THEN 'NO' ELSE 'YES' END AS is_nullable,+ co.collname AS collation_name,+ COALESCE(nbt.nspname, nt.nspname) AS udt_schema,+ COALESCE(bt.typname, t.typname) AS udt_name,+ CASE a.attidentity WHEN 'a' THEN 'ALWAYS' WHEN 'd' THEN 'BY DEFAULT' END AS identity_generation,+ pg_catalog.col_description(c.oid, a.attnum) AS column_comment+FROM pg_catalog.pg_attribute a+ LEFT JOIN pg_catalog.pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum+ JOIN pg_catalog.pg_class c ON a.attrelid = c.oid+ JOIN pg_catalog.pg_namespace nc ON c.relnamespace = nc.oid+ JOIN pg_catalog.pg_type t ON a.atttypid = t.oid+ JOIN pg_catalog.pg_namespace nt ON t.typnamespace = nt.oid+ LEFT JOIN (pg_catalog.pg_type bt JOIN pg_catalog.pg_namespace nbt ON bt.typnamespace = nbt.oid)+ ON t.typtype = 'd' AND t.typbasetype = bt.oid+ LEFT JOIN (pg_catalog.pg_collation co JOIN pg_catalog.pg_namespace nco ON co.collnamespace = nco.oid)+ ON a.attcollation = co.oid AND (nco.nspname <> 'pg_catalog' OR co.collname <> 'default')+WHERE a.attnum > 0+ AND NOT a.attisdropped+ AND c.relkind IN ('r', 'v', 'f', 'p')+ AND nc.nspname NOT IN (%s)+ AND nc.nspname NOT LIKE 'pg_temp%%'+ AND nc.nspname NOT LIKE 'pg_toast%%'+ORDER BY nc.nspname, c.relname, a.attnum;`, columnDefault, pgparser.SystemSchemaWhereClause)+}// getTableColumns gets the columns of a table.-func getTableColumns(txn *sql.Tx) (map[db.TableKey][]*storepb.ColumnMetadata, error) {+func getTableColumns(txn *sql.Tx, isAtLeastPG12 bool) (map[db.TableKey][]*storepb.ColumnMetadata, error) {columnsMap := make(map[db.TableKey][]*storepb.ColumnMetadata)- rows, err := txn.Query(listColumnQuery)+ rows, err := txn.Query(getListColumnQuery(isAtLeastPG12))if err != nil {return nil, err}@@ -1820,11 +1880,19 @@ func getFunctions(}func isAtLeastPG10(version string) bool {+ return isAtLeastMajorVersion(version, 10)+}++func isAtLeastPG12(version string) bool {+ return isAtLeastMajorVersion(version, 12)+}++func isAtLeastMajorVersion(version string, major uint64) bool {v, err := semver.ParseTolerant(version)if err != nil {slog.Error("invalid postgres version", slog.String("version", version))- // Assume the version is at least 10.0 for any error.+ // Assume the version is new enough for any error.return true}- return v.Major >= 10+ return v.Major >= major}diff --git a/backend/plugin/db/pg/sync_column_test.go b/backend/plugin/db/pg/sync_column_test.gonew file mode 100644index 0000000000..f440bfb6dc--- /dev/null+++ b/backend/plugin/db/pg/sync_column_test.go@@ -0,0 +1,329 @@+package pg++import (+ "context"+ "database/sql"+ "fmt"+ "slices"+ "strings"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/db"+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"+)++// legacyListColumnQuery is the previous INFORMATION_SCHEMA.COLUMNS-based column query.+// It is kept here as the oracle for TestListColumnQueryMatchesInformationSchema: the+// pg_catalog-based query must produce identical rows when the user has full privileges.+var legacyListColumnQuery = `+SELECT+ cols.table_schema,+ cols.table_name,+ cols.column_name,+ cols.data_type,+ cols.character_maximum_length,+ cols.numeric_precision,+ cols.numeric_scale,+ cols.datetime_precision,+ cols.ordinal_position,+ cols.column_default,+ cols.is_nullable,+ cols.collation_name,+ cols.udt_schema,+ cols.udt_name,+ cols.identity_generation,+ pg_catalog.col_description(c.oid, cols.ordinal_position::int) as column_comment+FROM INFORMATION_SCHEMA.COLUMNS AS cols+LEFT JOIN pg_namespace n ON n.nspname = cols.table_schema+LEFT JOIN pg_class c ON c.relname = cols.table_name AND c.relnamespace = n.oid AND c.relkind IN ('r', 'v', 'm', 'p', 'f')` + fmt.Sprintf(`+WHERE cols.table_schema NOT IN (%s)+ AND cols.table_schema NOT LIKE 'pg_temp%%'+ AND cols.table_schema NOT LIKE 'pg_toast%%'+ORDER BY cols.table_schema, cols.table_name, cols.ordinal_position;`, pgparser.SystemSchemaWhereClause)++type columnQueryRow struct {+ TableSchema string+ TableName string+ ColumnName string+ DataType string+ CharacterMaxLength sql.NullString+ NumericPrecision sql.NullString+ NumericScale sql.NullString+ DatetimePrecision sql.NullString+ OrdinalPosition int64+ ColumnDefault sql.NullString+ IsNullable string+ CollationName sql.NullString+ UdtSchema string+ UdtName string+ IdentityGeneration sql.NullString+ ColumnComment sql.NullString+}++func queryColumnRows(t *testing.T, txn *sql.Tx, query string) []columnQueryRow {+ t.Helper()+ rows, err := txn.Query(query)+ require.NoError(t, err)+ defer rows.Close()++ var result []columnQueryRow+ for rows.Next() {+ var r columnQueryRow+ require.NoError(t, rows.Scan(+ &r.TableSchema,+ &r.TableName,+ &r.ColumnName,+ &r.DataType,+ &r.CharacterMaxLength,+ &r.NumericPrecision,+ &r.NumericScale,+ &r.DatetimePrecision,+ &r.OrdinalPosition,+ &r.ColumnDefault,+ &r.IsNullable,+ &r.CollationName,+ &r.UdtSchema,+ &r.UdtName,+ &r.IdentityGeneration,+ &r.ColumnComment,+ ))+ result = append(result, r)+ }+ require.NoError(t, rows.Err())++ // The two queries may order rows differently for exotic identifier names+ // (information_schema sorts varchar with the database collation, pg_catalog sorts+ // the name type with C collation), so normalize the order before comparing.+ slices.SortFunc(result, func(a, b columnQueryRow) int {+ if c := strings.Compare(a.TableSchema, b.TableSchema); c != 0 {+ return c+ }+ if c := strings.Compare(a.TableName, b.TableName); c != 0 {+ return c+ }+ return int(a.OrdinalPosition - b.OrdinalPosition)+ })+ return result+}++// TestListColumnQueryMatchesInformationSchema verifies that the pg_catalog-based column+// query produces exactly the same rows as the previous INFORMATION_SCHEMA.COLUMNS-based+// query for a privileged user, across a wide range of column types. This guards the type+// rendering contract: the Go-side formatting in getTableColumns depends on the exact+// data_type / precision / udt values that information_schema used to produce.+func TestListColumnQueryMatchesInformationSchema(t *testing.T) {+ ctx := context.Background()++ pgContainer := testcontainer.GetTestPgContainer(ctx, t)+ defer pgContainer.Close(ctx)++ pgDB := pgContainer.GetDB()+ require.NoError(t, pgDB.Ping())++ setupSQL := `+CREATE SCHEMA app;+CREATE TYPE app.mood AS ENUM ('happy', 'sad');+CREATE TYPE public.shirt_size AS ENUM ('s', 'm', 'l');+CREATE DOMAIN app.positive_int AS integer NOT NULL CHECK (VALUE > 0);+CREATE DOMAIN public.us_zip AS varchar(10);+CREATE DOMAIN app.int_array AS integer[];+CREATE SEQUENCE app.seq1;++CREATE TABLE app.kitchen_sink (+ id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,+ id2 integer GENERATED BY DEFAULT AS IDENTITY,+ c_smallint smallint,+ c_int integer NOT NULL DEFAULT 42,+ c_bigint bigint DEFAULT nextval('app.seq1'),+ c_numeric numeric,+ c_numeric_p numeric(8),+ c_numeric_p0 numeric(8, 0),+ c_numeric_ps numeric(10, 2),+ c_real real,+ c_double double precision,+ c_money money,+ c_char char,+ c_char_n char(5),+ c_varchar varchar,+ c_varchar_n varchar(20) DEFAULT 'hello',+ c_text text COLLATE "C",+ c_bytea bytea,+ c_bool boolean DEFAULT true,+ c_date date,+ c_time time,+ c_time_p time(3),+ c_timetz time with time zone,+ c_timetz_p time(5) with time zone,+ c_ts timestamp,+ c_ts_p timestamp(2),+ c_tstz timestamp with time zone DEFAULT now(),+ c_tstz_p timestamp(4) with time zone,+ c_interval interval,+ c_interval_p interval(3),+ c_uuid uuid,+ c_json json,+ c_jsonb jsonb DEFAULT '{}'::jsonb,+ c_xml xml,+ c_inet inet,+ c_cidr cidr,+ c_macaddr macaddr,+ c_bit bit,+ c_bit_n bit(3),+ c_varbit varbit,+ c_varbit_n varbit(8),+ c_int_array integer[],+ c_text_matrix text[][],+ c_varchar_array varchar(5)[],+ c_enum app.mood DEFAULT 'happy',+ c_enum_public public.shirt_size,+ c_enum_array app.mood[],+ c_domain app.positive_int,+ c_domain_varchar public.us_zip DEFAULT 'x',+ c_domain_array app.int_array,+ c_point point,+ c_tsvector tsvector,+ c_int4range int4range,+ c_oid oid,+ c_name name,+ c_internal_char "char",+ c_generated integer GENERATED ALWAYS AS (c_int * 2) STORED,+ c_drop_me text+);+ALTER TABLE app.kitchen_sink DROP COLUMN c_drop_me;+COMMENT ON COLUMN app.kitchen_sink.c_int IS 'int comment';++CREATE TABLE app.parted (id integer, region text NOT NULL) PARTITION BY LIST (region);+CREATE TABLE app.parted_us PARTITION OF app.parted FOR VALUES IN ('us');++CREATE VIEW app.v_sink AS SELECT id, c_int, c_text FROM app.kitchen_sink;++CREATE TABLE public.plain (a serial, b text);+`+ _, err := pgDB.Exec(setupSQL)+ require.NoError(t, err)++ txn, err := pgDB.Begin()+ require.NoError(t, err)+ defer txn.Rollback()++ // SyncDBSchema clears the search path before the column sync; match it so default+ // expressions render identically.+ require.NoError(t, setTxSearchPath(txn, ""))++ legacyRows := queryColumnRows(t, txn, legacyListColumnQuery)+ newRows := queryColumnRows(t, txn, getListColumnQuery(true))++ // Sanity: the kitchen sink table alone has 50+ columns.+ require.Greater(t, len(legacyRows), 50)+ require.Equal(t, legacyRows, newRows)+}++// TestSyncColumnsWithoutTablePrivilege verifies that column sync no longer depends on the+// sync user's table privileges. information_schema.columns privilege-filters its rows, so+// a sync user without privileges on a table used to get table metadata with indexes and+// constraints (read from pg_catalog) but zero columns. The pg_catalog-based column query+// must return the columns regardless.+func TestSyncColumnsWithoutTablePrivilege(t *testing.T) {+ ctx := context.Background()++ pgContainer := testcontainer.GetTestPgContainer(ctx, t)+ defer pgContainer.Close(ctx)++ pgDB := pgContainer.GetDB()+ require.NoError(t, pgDB.Ping())++ setupSQL := `+CREATE SCHEMA restricted;+CREATE TABLE restricted.secret_table (+ id integer PRIMARY KEY,+ name varchar(50) NOT NULL DEFAULT 'anon',+ amount numeric(10, 2),+ CONSTRAINT positive_amount CHECK (amount >= 0)+);+CREATE INDEX idx_secret_name ON restricted.secret_table (name);+CREATE USER limited_user WITH LOGIN PASSWORD 'limited-password';+-- pg_read_all_stats lets the sync read pg_table_size/pg_indexes_size; it grants no+-- table or column privileges, so information_schema.columns still hides the table.+GRANT pg_read_all_stats TO limited_user;+`+ _, err := pgDB.Exec(setupSQL)+ require.NoError(t, err)++ limitedDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=limited_user password=limited-password database=postgres", pgContainer.GetHost(), pgContainer.GetPort()))+ require.NoError(t, err)+ defer limitedDB.Close()+ require.NoError(t, limitedDB.Ping())++ // Confirm the privilege filtering this fix works around: information_schema.columns+ // returns nothing for the table when queried by the unprivileged user.+ var count int+ require.NoError(t, limitedDB.QueryRow(`+ SELECT COUNT(*) FROM information_schema.columns+ WHERE table_schema = 'restricted' AND table_name = 'secret_table'`).Scan(&count))+ require.Zero(t, count)++ driver := &Driver{}+ config := db.ConnectionConfig{+ DataSource: &storepb.DataSource{+ Type: storepb.DataSourceType_ADMIN,+ Username: "limited_user",+ Host: pgContainer.GetHost(),+ Port: pgContainer.GetPort(),+ Database: "postgres",+ },+ Password: "limited-password",+ ConnectionContext: db.ConnectionContext{+ EngineVersion: "16.0",+ DatabaseName: "postgres",+ },+ }++ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)+ require.NoError(t, err)+ defer openedDriver.Close(ctx)++ pgDriver, ok := openedDriver.(*Driver)+ require.True(t, ok)++ metadata, err := pgDriver.SyncDBSchema(ctx)+ require.NoError(t, err)++ var secretTable *storepb.TableMetadata+ for _, schema := range metadata.Schemas {+ if schema.Name != "restricted" {+ continue+ }+ for _, table := range schema.Tables {+ if table.Name == "secret_table" {+ secretTable = table+ break+ }+ }+ }+ require.NotNil(t, secretTable, "restricted.secret_table not found in synced metadata")++ // Indexes and check constraints were always visible via pg_catalog; columns must now+ // be visible too instead of leaving inconsistent table metadata.+ require.NotEmpty(t, secretTable.Indexes)+ require.NotEmpty(t, secretTable.CheckConstraints)++ require.Len(t, secretTable.Columns, 3)+ id, name, amount := secretTable.Columns[0], secretTable.Columns[1], secretTable.Columns[2]++ require.Equal(t, "id", id.Name)+ require.Equal(t, "integer", id.Type)+ require.False(t, id.Nullable)++ require.Equal(t, "name", name.Name)+ require.Equal(t, "character varying(50)", name.Type)+ require.False(t, name.Nullable)+ require.Equal(t, "'anon'::character varying", name.Default)++ require.Equal(t, "amount", amount.Name)+ require.Equal(t, "numeric(10,2)", amount.Type)+ require.True(t, amount.Nullable)+}diff --git a/backend/plugin/db/pg/sync_foreign_table_test.go b/backend/plugin/db/pg/sync_foreign_table_test.gonew file mode 100644index 0000000000..f126b8f9b8--- /dev/null+++ b/backend/plugin/db/pg/sync_foreign_table_test.go@@ -0,0 +1,161 @@+package pg++import (+ "context"+ "database/sql"+ "fmt"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/db"+)++// legacyListForeignTableQuery is the previous information_schema.foreign_tables-based+// query, kept as the oracle: for a privileged user the pg_catalog-based query must+// produce identical rows.+const legacyListForeignTableQuery = `SELECT+ foreign_table.foreign_table_schema,+ foreign_table.foreign_table_name,+ foreign_table.foreign_server_catalog,+ foreign_table.foreign_server_name+FROM information_schema.foreign_tables AS foreign_table+ORDER BY foreign_table.foreign_table_schema, foreign_table.foreign_table_name;`++const foreignTableSetupSQL = `+CREATE EXTENSION postgres_fdw;+CREATE SERVER loopback_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'postgres');+CREATE SCHEMA fdw_schema;+CREATE FOREIGN TABLE fdw_schema.remote_orders (+ id integer NOT NULL,+ note varchar(30)+) SERVER loopback_server OPTIONS (schema_name 'public', table_name 'orders');+CREATE FOREIGN TABLE public.remote_items (+ item_id bigint+) SERVER loopback_server OPTIONS (schema_name 'public', table_name 'items');+`++func queryForeignTableRows(t *testing.T, conn *sql.DB, query string) [][4]string {+ t.Helper()+ rows, err := conn.Query(query)+ require.NoError(t, err)+ defer rows.Close()++ var result [][4]string+ for rows.Next() {+ var r [4]string+ require.NoError(t, rows.Scan(&r[0], &r[1], &r[2], &r[3]))+ result = append(result, r)+ }+ require.NoError(t, rows.Err())+ return result+}++// TestListForeignTableQueryMatchesInformationSchema verifies that the pg_catalog-based+// foreign table query produces exactly the same rows as the previous+// information_schema.foreign_tables-based query for a privileged user.+func TestListForeignTableQueryMatchesInformationSchema(t *testing.T) {+ ctx := context.Background()++ pgContainer := testcontainer.GetTestPgContainer(ctx, t)+ defer pgContainer.Close(ctx)++ pgDB := pgContainer.GetDB()+ require.NoError(t, pgDB.Ping())++ _, err := pgDB.Exec(foreignTableSetupSQL)+ require.NoError(t, err)++ legacyRows := queryForeignTableRows(t, pgDB, legacyListForeignTableQuery)+ newRows := queryForeignTableRows(t, pgDB, getListForeignTableQuery())++ require.Len(t, legacyRows, 2)+ require.Equal(t, legacyRows, newRows)+}++// TestSyncForeignTablesWithoutTablePrivilege verifies that foreign table sync no longer+// depends on the sync user's privileges. information_schema.foreign_tables+// privilege-filters its rows, so a sync user without privileges on a foreign table used+// to silently lose the whole table from the synced metadata.+func TestSyncForeignTablesWithoutTablePrivilege(t *testing.T) {+ ctx := context.Background()++ pgContainer := testcontainer.GetTestPgContainer(ctx, t)+ defer pgContainer.Close(ctx)++ pgDB := pgContainer.GetDB()+ require.NoError(t, pgDB.Ping())++ _, err := pgDB.Exec(foreignTableSetupSQL)+ require.NoError(t, err)++ // pg_read_all_stats lets the sync read pg_table_size/pg_indexes_size; it grants no+ // table or column privileges, so information_schema.foreign_tables still filters.+ _, err = pgDB.Exec(`+CREATE USER limited_user WITH LOGIN PASSWORD 'limited-password';+GRANT pg_read_all_stats TO limited_user;+`)+ require.NoError(t, err)++ limitedDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=limited_user password=limited-password database=postgres", pgContainer.GetHost(), pgContainer.GetPort()))+ require.NoError(t, err)+ defer limitedDB.Close()+ require.NoError(t, limitedDB.Ping())++ // Confirm the privilege filtering this fix works around: the unprivileged user sees+ // no foreign tables through information_schema.+ var count int+ require.NoError(t, limitedDB.QueryRow(`SELECT COUNT(*) FROM information_schema.foreign_tables`).Scan(&count))+ require.Zero(t, count)++ driver := &Driver{}+ config := db.ConnectionConfig{+ DataSource: &storepb.DataSource{+ Type: storepb.DataSourceType_ADMIN,+ Username: "limited_user",+ Host: pgContainer.GetHost(),+ Port: pgContainer.GetPort(),+ Database: "postgres",+ },+ Password: "limited-password",+ ConnectionContext: db.ConnectionContext{+ EngineVersion: "16.0",+ DatabaseName: "postgres",+ },+ }++ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)+ require.NoError(t, err)+ defer openedDriver.Close(ctx)++ pgDriver, ok := openedDriver.(*Driver)+ require.True(t, ok)++ metadata, err := pgDriver.SyncDBSchema(ctx)+ require.NoError(t, err)++ var fdwSchema *storepb.SchemaMetadata+ for _, schema := range metadata.Schemas {+ if schema.Name == "fdw_schema" {+ fdwSchema = schema+ break+ }+ }+ require.NotNil(t, fdwSchema, "fdw_schema not found in synced metadata")+ require.Len(t, fdwSchema.ExternalTables, 1)++ remoteOrders := fdwSchema.ExternalTables[0]+ require.Equal(t, "remote_orders", remoteOrders.Name)+ require.Equal(t, "loopback_server", remoteOrders.ExternalServerName)+ require.Equal(t, "postgres", remoteOrders.ExternalDatabaseName)++ // Columns of foreign tables come from the privilege-independent column sync.+ require.Len(t, remoteOrders.Columns, 2)+ require.Equal(t, "id", remoteOrders.Columns[0].Name)+ require.Equal(t, "integer", remoteOrders.Columns[0].Type)+ require.False(t, remoteOrders.Columns[0].Nullable)+ require.Equal(t, "note", remoteOrders.Columns[1].Name)+ require.Equal(t, "character varying(30)", remoteOrders.Columns[1].Type)+}