Skip to content

Commit 71aa34f

Browse files
committed
fix(engine): by default, dump only databases to which user have access
1 parent 21759a2 commit 71aa34f

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

Diff for: engine/.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ linters:
8282
- megacheck
8383
- misspell
8484
- prealloc
85-
# - revive # temporarily disabled: https://gitlab.com/postgres-ai/database-lab/-/merge_requests/498
85+
- revive
8686
- structcheck
8787
- stylecheck
8888
- unconvert

Diff for: engine/internal/retrieval/engine/postgres/logical/dump.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type dumper interface {
117117
SetConnectionOptions(context.Context, *Connection) error
118118

119119
// GetDatabaseListQuery provides the query to get the list of databases for dumping.
120-
GetDatabaseListQuery() string
120+
GetDatabaseListQuery(username string) string
121121
}
122122

123123
// Connection provides connection options.
@@ -379,7 +379,7 @@ func (d *DumpJob) getDBList(ctx context.Context) (map[string]DumpDefinition, err
379379
return nil, fmt.Errorf("failed to connect to DB: %w", err)
380380
}
381381

382-
rows, err := querier.Query(ctx, d.dumper.GetDatabaseListQuery())
382+
rows, err := querier.Query(ctx, d.dumper.GetDatabaseListQuery(d.config.db.Username))
383383
if err != nil {
384384
return nil, fmt.Errorf("failed to perform query listing databases: %w", err)
385385
}

Diff for: engine/internal/retrieval/engine/postgres/logical/dump_default.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package logical
66

77
import (
88
"context"
9+
"fmt"
910
)
1011

1112
type defaultDumper struct {
@@ -25,6 +26,7 @@ func (d *defaultDumper) SetConnectionOptions(_ context.Context, c *Connection) e
2526
return nil
2627
}
2728

28-
func (d *defaultDumper) GetDatabaseListQuery() string {
29-
return "select datname from pg_catalog.pg_database where not datistemplate"
29+
func (d *defaultDumper) GetDatabaseListQuery(username string) string {
30+
return fmt.Sprintf(`select datname from pg_catalog.pg_database
31+
where not datistemplate and has_database_privilege('%s', datname, 'CONNECT')`, username)
3032
}

Diff for: engine/internal/retrieval/engine/postgres/logical/dump_rds.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (r *rdsDumper) SetConnectionOptions(ctx context.Context, c *Connection) err
108108
return nil
109109
}
110110

111-
func (r *rdsDumper) GetDatabaseListQuery() string {
112-
return "select datname from pg_catalog.pg_database where not datistemplate"
111+
func (r *rdsDumper) GetDatabaseListQuery(username string) string {
112+
return fmt.Sprintf(`select datname from pg_catalog.pg_database
113+
where not datistemplate and has_database_privilege('%s', datname, 'CONNECT')`, username)
113114
}

0 commit comments

Comments
 (0)