Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Make owner parameter optional in grantRoleDefaultPrivileges resource #443

Open
iamfj opened this issue Jun 6, 2024 · 1 comment

Comments

@iamfj
Copy link

iamfj commented Jun 6, 2024

Problem

It would be highly beneficial to use the grantRoleDefaultPrivileges resource to construct queries such as:

ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO read_only;

Unfortunately, specifying a owner is always mandatory, which translates into a FOR ROLE part in the query. This requirement prevents the creation of more generalized default privileges queries.

Solution

Make the owner optional, similar to how the schema is handled. Below is a draft solution for this issue.

Expected Solution

Note

I'm not a Go expert.

https://github.com/cyrilgdn/terraform-provider-postgresql/blob/f46ec221181b09b153c7fc816e75c7030a3e8ab9/postgresql/resource_postgresql_default_privileges.go#L298C2-L304C3

role := d.Get("role").(string)
pgOwner := d.Get("owner").(string)
pgSchema := d.Get("schema").(string)

(...)

// If an owner is specified, build the query string to include it
var forOwner string
if pgOwner != "" {
  forOwner = fmt.Sprintf("FOR ROLE %s", pq.QuoteIdentifier(pgOwner))
}

// If a schema is specified, build the query string to include it
var inSchema string
if pgSchema != "" {
  inSchema = fmt.Sprintf("IN SCHEMA %s", pq.QuoteIdentifier(pgSchema))
}

query := fmt.Sprintf("ALTER DEFAULT PRIVILEGES %s %s GRANT %s ON %sS TO %s",
  forOwner,
  inSchema,
  strings.Join(privileges, ","),
  strings.ToUpper(d.Get("object_type").(string)),
  pq.QuoteIdentifier(role),
)
@iamfj iamfj changed the title [Feature Request] Make role parameter optional in grantRoleDefaultPrivileges resource [Feature Request] Make owner parameter optional in grantRoleDefaultPrivileges resource Jun 6, 2024
@SaTae66
Copy link

SaTae66 commented Oct 10, 2024

Just here to bump this. This would be really nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants