role_test.go44 lines, 1.2 KB
| 1 | package wallet_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/url" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/jackc/pgx/v5" |
| 9 | "github.com/jackc/pgx/v5/pgconn" |
| 10 | ) |
| 11 | |
| 12 | // TestRuntimeRole connects as wallet_app, the role the wallet runs as, and |
| 13 | // tries what a stolen password would: rewrite the ledger, switch its |
| 14 | // triggers off, or change the schema. Postgres must refuse every one. |
| 15 | func TestRuntimeRole(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | if testing.Short() { |
| 18 | t.Skip("integration test") |
| 19 | } |
| 20 | u, err := url.Parse(postgresURL) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | u.User = url.UserPassword("wallet_app", "wallet_app") |
| 25 | conn, err := pgx.Connect(t.Context(), u.String()) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | defer conn.Close(t.Context()) |
| 30 | if _, err := conn.Exec(t.Context(), "SELECT count(*) FROM entries"); err != nil { |
| 31 | t.Fatalf("read the ledger: %v", err) |
| 32 | } |
| 33 | for _, sql := range []string{ |
| 34 | "UPDATE entries SET amount = amount + 1000", |
| 35 | "DELETE FROM transactions", |
| 36 | "SET session_replication_role = replica", |
| 37 | "CREATE TABLE backdoor (id int)", |
| 38 | } { |
| 39 | _, err := conn.Exec(t.Context(), sql) |
| 40 | if pgErr, ok := errors.AsType[*pgconn.PgError](err); !ok || pgErr.Code != "42501" { |
| 41 | t.Errorf("%s: %v, want insufficient_privilege", sql, err) |
| 42 | } |
| 43 | } |
| 44 | } |