Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Default privileges approach is incorrect #15

@sworisbreathing

Description

@sworisbreathing

The redshift_privilege resource currently creates grants but also runs ALTER DEFAULT PRIVILEGES. This appears to be due to a misunderstanding of how default privileges work in Redshift.

Per https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_DEFAULT_PRIVILEGES.html:

Defines the default set of access privileges to be applied to objects that are created in the future by the specified user.
...
FOR USER target_user
Optional. The name of the user for which default privileges are defined... The default value is the current user.

Default privileges are essentially grants that are automatically applied when a particular user creates an object. In other words, if you do:

resource "redshift_privilege" "readonly_tables" {
  group       = "test_group"
  schema      = "public"
  object_type = "table"
  privileges  = ["SELECT"]
}

What actually runs is this:

ALTER DEFAULT PRIVILEGES
IN SCHEMA "public"
GRANT SELECT ON TABLES
TO GROUP "test_group"

which is equivalent to this:

ALTER DEFAULT PRIVILEGES
FOR USER CURRENT_USER
IN SCHEMA "public"
GRANT SELECT ON TABLES
TO GROUP "test_group"

which actually means the select permissions will only be given on new tables created by whoever ran terraform.

If what we're really trying to achieve here is for test_group to have select permissions on newly created tables in public then what we actually have to do is run something akin to:

-- redshift doesn't actually support a for loop like this,
-- but you get the idea.
FOR db_user IN (SELECT usename FROM pg_user) DO
  ALTER DEFAULT PRIVILEGES
  FOR USER db_user
  IN SCHEMA "public"
  GRANT SELECT ON TABLES
  TO GROUP "test_group"
END FOR

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions