Skip to content

Commit 7bc3b49

Browse files
authored
Add support for --schema for the postgres revoke and prepare command (#498)
Fixes #435
1 parent 9725d7e commit 7bc3b49

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

cmd/postgrescmd/preparecmd.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ This operation is only required to run once for each postgresql instance.`,
3636
Name: "namespace",
3737
Aliases: []string{"n"},
3838
},
39+
&cli.StringFlag{
40+
Name: "schema",
41+
Value: "public",
42+
Usage: "Schema to grant access to",
43+
},
3944
},
4045
Before: func(context *cli.Context) error {
4146
if context.Args().Len() < 1 {
@@ -51,6 +56,7 @@ This operation is only required to run once for each postgresql instance.`,
5156
allPrivs := context.Bool("all-privs")
5257
namespace := context.String("namespace")
5358
cluster := context.String("context")
59+
schema := context.String("schema")
5460

5561
fmt.Println(context.Command.Description)
5662

@@ -61,7 +67,7 @@ This operation is only required to run once for each postgresql instance.`,
6167
return fmt.Errorf("cancelled by user")
6268
}
6369

64-
return postgres.PrepareAccess(context.Context, appName, namespace, cluster, allPrivs)
70+
return postgres.PrepareAccess(context.Context, appName, namespace, cluster, schema, allPrivs)
6571
},
6672
}
6773
}

cmd/postgrescmd/revokecmd.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ This operation is only required to run once for each postgresql instance.`,
3232
Name: "namespace",
3333
Aliases: []string{"n"},
3434
},
35+
&cli.StringFlag{
36+
Name: "schema",
37+
Value: "public",
38+
Usage: "Schema to revoke access from",
39+
},
3540
},
3641
Before: func(context *cli.Context) error {
3742
if context.Args().Len() < 1 {
@@ -46,6 +51,7 @@ This operation is only required to run once for each postgresql instance.`,
4651

4752
namespace := context.String("namespace")
4853
cluster := context.String("context")
54+
schema := context.String("schema")
4955

5056
fmt.Println(context.Command.Description)
5157

@@ -56,7 +62,7 @@ This operation is only required to run once for each postgresql instance.`,
5662
return fmt.Errorf("cancelled by user")
5763
}
5864

59-
return postgres.RevokeAccess(context.Context, appName, namespace, cluster)
65+
return postgres.RevokeAccess(context.Context, appName, namespace, cluster, schema)
6066
},
6167
}
6268
}

pkg/postgres/access.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,42 @@ package postgres
33
import (
44
"context"
55
"database/sql"
6+
"strings"
7+
8+
"github.com/lib/pq"
69
)
710

8-
var grantAllPrivs = `ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO cloudsqliamuser;
9-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO cloudsqliamuser;
10-
GRANT ALL ON ALL TABLES IN SCHEMA public TO cloudsqliamuser;
11-
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO cloudsqliamuser;
12-
GRANT CREATE ON SCHEMA public TO cloudsqliamuser;`
11+
var grantAllPrivs = `ALTER DEFAULT PRIVILEGES IN SCHEMA $schema GRANT ALL ON TABLES TO cloudsqliamuser;
12+
ALTER DEFAULT PRIVILEGES IN SCHEMA $schema GRANT ALL ON SEQUENCES TO cloudsqliamuser;
13+
GRANT ALL ON ALL TABLES IN SCHEMA $schema TO cloudsqliamuser;
14+
GRANT ALL ON ALL SEQUENCES IN SCHEMA $schema TO cloudsqliamuser;
15+
GRANT CREATE ON SCHEMA $schema TO cloudsqliamuser;`
1316

14-
var grantSelectPrivs = `ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO cloudsqliamuser;
15-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON SEQUENCES TO cloudsqliamuser;
16-
GRANT SELECT ON ALL TABLES IN SCHEMA public TO cloudsqliamuser;
17-
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO cloudsqliamuser;`
17+
var grantSelectPrivs = `ALTER DEFAULT PRIVILEGES IN SCHEMA $schema GRANT SELECT ON TABLES TO cloudsqliamuser;
18+
ALTER DEFAULT PRIVILEGES IN SCHEMA $schema GRANT SELECT ON SEQUENCES TO cloudsqliamuser;
19+
GRANT SELECT ON ALL TABLES IN SCHEMA $schema TO cloudsqliamuser;
20+
GRANT SELECT ON ALL SEQUENCES IN SCHEMA $schema TO cloudsqliamuser;`
1821

1922
// this is used for all privileges and select, as it covers both cases
20-
var revokeAllPrivs = `ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE ALL ON TABLES FROM cloudsqliamuser;
21-
ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE ALL ON SEQUENCES FROM cloudsqliamuser;
22-
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM cloudsqliamuser;
23-
REVOKE ALL ON ALL SEQUENCES IN SCHEMA public FROM cloudsqliamuser;
24-
REVOKE CREATE ON SCHEMA public FROM cloudsqliamuser;`
23+
var revokeAllPrivs = `ALTER DEFAULT PRIVILEGES IN SCHEMA $schema REVOKE ALL ON TABLES FROM cloudsqliamuser;
24+
ALTER DEFAULT PRIVILEGES IN SCHEMA $schema REVOKE ALL ON SEQUENCES FROM cloudsqliamuser;
25+
REVOKE ALL ON ALL TABLES IN SCHEMA $schema FROM cloudsqliamuser;
26+
REVOKE ALL ON ALL SEQUENCES IN SCHEMA $schema FROM cloudsqliamuser;
27+
REVOKE CREATE ON SCHEMA $schema FROM cloudsqliamuser;`
2528

26-
func PrepareAccess(ctx context.Context, appName, namespace, cluster string, allPrivs bool) error {
29+
func PrepareAccess(ctx context.Context, appName, namespace, cluster, schema string, allPrivs bool) error {
2730
if allPrivs {
28-
return sqlExecAsAppUser(ctx, appName, namespace, cluster, grantAllPrivs)
31+
return sqlExecAsAppUser(ctx, appName, namespace, cluster, schema, grantAllPrivs)
2932
} else {
30-
return sqlExecAsAppUser(ctx, appName, namespace, cluster, grantSelectPrivs)
33+
return sqlExecAsAppUser(ctx, appName, namespace, cluster, schema, grantSelectPrivs)
3134
}
3235
}
3336

34-
func RevokeAccess(ctx context.Context, appName, namespace, cluster string) error {
35-
return sqlExecAsAppUser(ctx, appName, namespace, cluster, revokeAllPrivs)
37+
func RevokeAccess(ctx context.Context, appName, namespace, cluster, schema string) error {
38+
return sqlExecAsAppUser(ctx, appName, namespace, cluster, schema, revokeAllPrivs)
3639
}
3740

38-
func sqlExecAsAppUser(ctx context.Context, appName, namespace, cluster, statement string) error {
41+
func sqlExecAsAppUser(ctx context.Context, appName, namespace, cluster, schema, statement string) error {
3942
dbInfo, err := NewDBInfo(appName, namespace, cluster)
4043
if err != nil {
4144
return err
@@ -46,6 +49,8 @@ func sqlExecAsAppUser(ctx context.Context, appName, namespace, cluster, statemen
4649
return err
4750
}
4851

52+
schema = pq.QuoteIdentifier(schema)
53+
statement = strings.ReplaceAll(statement, "$schema", schema)
4954
db, err := sql.Open("cloudsqlpostgres", connectionInfo.ProxyConnectionString())
5055
if err != nil {
5156
return err

0 commit comments

Comments
 (0)