Skip to content

Commit 4cac855

Browse files
authored
Merge pull request #105 from fly-apps/user-perm-fix
Grant normal users access to the public schema
2 parents 03eeb7f + 93862b9 commit 4cac855

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

internal/api/handle_databases.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,31 @@ func handleCreateDatabase(w http.ResponseWriter, r *http.Request) {
6565
}
6666
defer close()
6767

68-
input := createDatabaseRequest{}
69-
err = json.NewDecoder(r.Body).Decode(&input)
70-
if err != nil {
68+
var input createDatabaseRequest
69+
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
7170
renderErr(w, err)
7271
return
7372
}
7473
defer r.Body.Close()
7574

76-
err = admin.CreateDatabase(ctx, conn, input.Name)
75+
if err := admin.CreateDatabase(ctx, conn, input.Name); err != nil {
76+
renderErr(w, err)
77+
return
78+
}
79+
80+
dbConn, close, err := localConnection(ctx, input.Name)
7781
if err != nil {
7882
renderErr(w, err)
7983
return
8084
}
85+
defer close()
8186

82-
res := &Response{Result: true}
87+
if err := admin.GrantCreateOnPublic(ctx, dbConn); err != nil {
88+
renderErr(w, err)
89+
return
90+
}
8391

92+
res := &Response{Result: true}
8493
renderJSON(w, res, http.StatusOK)
8594
}
8695

internal/flypg/admin/admin.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
func GrantAccess(ctx context.Context, pg *pgx.Conn, username string) error {
1313
sql := fmt.Sprintf("GRANT pg_read_all_data, pg_write_all_data TO %q", username)
14-
1514
_, err := pg.Exec(ctx, sql)
1615
return err
1716
}
@@ -66,6 +65,14 @@ func CreateDatabase(ctx context.Context, pg *pgx.Conn, name string) error {
6665
return err
6766
}
6867

68+
// GrantCreateOnPublic re-enables the public schema for normal users.
69+
// We should look into creating better isolation in the future.
70+
func GrantCreateOnPublic(ctx context.Context, pg *pgx.Conn) error {
71+
sql := "GRANT CREATE on SCHEMA PUBLIC to PUBLIC;"
72+
_, err := pg.Exec(ctx, sql)
73+
return err
74+
}
75+
6976
func DeleteDatabase(ctx context.Context, pg *pgx.Conn, name string) error {
7077
sql := fmt.Sprintf("DROP DATABASE %s;", name)
7178

0 commit comments

Comments
 (0)