Skip to content

postgresq_grant for schema not working when provider has no expected version #49

Open
@nadirsidi

Description

@nadirsidi

Very similar issue to #38, I figured I would provide some additional information since I'm not seeing the error response. Instead, my Terraform apply runs but then when I try to run queries with the user, I can see the user doesn't have the necessary schema rights.

Terraform Version

0.14.15

  • provider registry.terraform.io/cyrilgdn/postgresql v1.11.0
  • provider registry.terraform.io/hashicorp/aws v3.25.0

Affected Resource(s)

postgresql_grant, specifically with object_type ="schema"

Terraform Configuration Files

provider "postgresql" {
  scheme = var.pg_scheme  # postgres provider 1.11+ specifies awspostgres for RDS, but the GO CDK doesn't verify the x509 cert correctly
  host = data.aws_rds_cluster.data_science_postgresql.endpoint
  port = data.aws_rds_cluster.data_science_postgresql.port
  database = data.aws_rds_cluster.data_science_postgresql.database_name
  username = jsondecode(data.aws_secretsmanager_secret_version.master_db_credentials.secret_string)["username"]
  password = jsondecode(data.aws_secretsmanager_secret_version.master_db_credentials.secret_string)["password"]
  superuser = false
  sslmode = "require"
  sslrootcert = "/code/manifest/AmazonRootCA1.pem"
  expected_version = "10.12"
}

resource "postgresql_schema" "price" {
    name = "price"
    owner = "username"  # The username specified in provider config above
}

resource "postgresql_role" "price_readwrite" {
    name = "price_readwrite"  
    login = false
}

resource "postgresql_grant" "schema_usage" {
    database = data.aws_rds_cluster.data_science_postgresql.database_name
    role = postgresql_role.price_readwrite.name
    schema = "price"
    object_type = "schema"
    privileges = ["CREATE", "USAGE"]
}

Expected Behavior

I would expect the price_readwrite role to have CREATE privileges on the price schema even if the expected_version is not set for the provider.

Actual Behavior

The price_readwrite user does not have permission to create tables in the price schema, unless the Postgres version is specified in the provider config.

I do have other postgresql_grant resources but they are to grant database CONNECT and table CRUD privileges. The CONNECT on database seems to work fine. I can't say about the CRUD because it's dependent on the user have schema privileges to being with.

Steps to Reproduce

  1. Configure Postgres DB (10.12) with non-super-user credentials, but with the ability to create roles, databases, and schemas.
  2. Run the configuration above to create the price schema, price_readwrite role, and attempt to pass privileges to the role.

Important Factoids

  • Output of SELECT version() = PostgreSQL 10.12 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.9.3, 64-bit

  • Running Terraform in a docker container using terraform binary from Docker image, COPY --from=hashicorp/terraform:0.14.5 /bin/terraform /bin/terraform

  • Docker container is running in AWS CodeBuild in private subnet with network access to RDS, also in private subnet.

  • RDS is Aurora Serverless Postgres-- This seems to work great, even if it is scaled down to 0 it comes back up and allows the provider connection fast enough for Terraform to do its thing.

  • Helpful query to check schema rights:

SET ROLE price_readwrite;

WITH "names"("name") AS (
  SELECT n.nspname AS "name"
    FROM pg_catalog.pg_namespace n
      WHERE n.nspname !~ '^pg_'
        AND n.nspname <> 'information_schema'
) SELECT "name",
  pg_catalog.has_schema_privilege(current_user, "name", 'CREATE') AS "create",
  pg_catalog.has_schema_privilege(current_user, "name", 'USAGE') AS "usage"
    FROM "names";
  • Contrary to the documentation, I am not using the awspostgres connection scheme because the Go Cloud seemed to be downloading the certificate itself, and it kept giving me a x509 error. I found I was only able to connect with the provider configuration above. Upon investigation, this might be a Go 1.15+ thing and how AWS chooses to make their certs, or it looks like Go Cloud is looking at old *.pem files. I am not support confident about this, but I can open a separate issue if you'd like.

References

Thank you for your great work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions