Skip to content

Commit de2eddf

Browse files
authored
Databases: Grant and Revoke privileges on DBs (#20)
1 parent 98fd46e commit de2eddf

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

pkg/connector/database.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,29 @@ func (r *databaseSyncer) Delete(ctx context.Context, resourceId *v2.ResourceId)
248248
}
249249

250250
func (r *databaseSyncer) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) ([]*v2.Grant, annotations.Annotations, error) {
251-
if principal.Id.ResourceType != databaseResourceType.Id {
251+
if principal.Id.ResourceType != roleResourceType.Id {
252252
return nil, nil, fmt.Errorf("baton-postgres: only users and roles can have roles granted")
253253
}
254254

255-
// TODO: pass IDs into client.Grant() and look up the names there
256-
dbName := entitlement.Resource.DisplayName
255+
// Parse the Entitlement ID to get the database ID and privilege name
256+
_, dbIdStr, privilegeName, isGrant, err := parseEntitlementID(entitlement.Id)
257+
if err != nil {
258+
return nil, nil, err
259+
}
260+
261+
dbID, err := strconv.ParseInt(dbIdStr, 10, 64)
262+
if err != nil {
263+
return nil, nil, err
264+
}
265+
266+
// Look up the database by ID
267+
pgDb, err := r.client.GetDatabase(ctx, dbID)
268+
if err != nil {
269+
return nil, nil, err
270+
}
271+
257272
principalName := principal.DisplayName
258-
err := r.client.GrantDatabase(ctx, dbName, principalName, entitlement.GetDisplayName())
273+
err = r.client.GrantDatabase(ctx, pgDb.Name, principalName, privilegeName, isGrant)
259274
return nil, nil, err
260275
}
261276

pkg/postgres/databases.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,24 @@ func (c *Client) DeleteDatabase(ctx context.Context, dbName string) error {
143143
return err
144144
}
145145

146-
func (c *Client) GrantDatabase(ctx context.Context, dbName string, principalName string, privilege string) error {
146+
func transformPrivilege(privilege string) string {
147+
return strings.ReplaceAll(privilege, "-", "")
148+
}
149+
150+
func (c *Client) GrantDatabase(ctx context.Context, dbName string, principalName string, privilege string, isGrant bool) error {
147151
l := ctxzap.Extract(ctx)
148152
l.Debug("granting database", zap.String("dbName", dbName), zap.String("principalName", principalName), zap.String("privilege", privilege))
149153

150154
sanitizedDbName := pgx.Identifier{dbName}.Sanitize()
151155
sanitizedPrincipalName := pgx.Identifier{principalName}.Sanitize()
152-
q := fmt.Sprintf("GRANT %s ON DATABASE %s TO %s", privilege, sanitizedDbName, sanitizedPrincipalName)
156+
sanitizedPrivilege := pgx.Identifier{transformPrivilege(privilege)}.Sanitize()
157+
var q string
158+
if isGrant {
159+
q = fmt.Sprintf("GRANT %s ON DATABASE %s TO %s WITH GRANT OPTION", sanitizedPrivilege, sanitizedDbName, sanitizedPrincipalName)
160+
} else {
161+
q = fmt.Sprintf("GRANT %s ON DATABASE %s TO %s", sanitizedPrivilege, sanitizedDbName, sanitizedPrincipalName)
162+
}
163+
153164
_, err := c.db.Exec(ctx, q)
154165
return err
155166
}
@@ -159,7 +170,13 @@ func (c *Client) RevokeDatabase(ctx context.Context, dbName string, target strin
159170

160171
sanitizedDbName := pgx.Identifier{dbName}.Sanitize()
161172
sanitizedTarget := pgx.Identifier{target}.Sanitize()
162-
q := fmt.Sprintf("REVOKE %s ON DATABASE %s FROM %s", privilege, sanitizedDbName, sanitizedTarget)
173+
sanitizedPrivilege := pgx.Identifier{transformPrivilege(privilege)}.Sanitize()
174+
var q string
175+
if isGrant {
176+
q = fmt.Sprintf("REVOKE GRANT OPTION for %s ON DATABASE %s FROM %s", sanitizedPrivilege, sanitizedDbName, sanitizedTarget)
177+
} else {
178+
q = fmt.Sprintf("REVOKE %s ON DATABASE %s FROM %s", sanitizedPrivilege, sanitizedDbName, sanitizedTarget)
179+
}
163180

164181
l.Debug("revoking role from member", zap.String("query", q))
165182
_, err := c.db.Exec(ctx, q)

0 commit comments

Comments
 (0)