Open Anton-Shutik opened 1 year ago
@cyrilgdn
I think following approach might be used to workaround this issue:
connect with default user defined in provider:
SET ROLE database_owner;
CREATE DATABASE database_name;
RESET ROLE;
Does it make a sense ?
I'm facing this same issue, trying to find a workaround as this would block the complete usage of IAM on db owners.
First try:
resource "postgresql_role" "owner" {
name = "owner_${var.database}"
skip_reassign_owned = "true"
lifecycle {
ignore_changes = [roles]
}
}
resource "postgresql_role" "iam_owner" {
name = "iam_owner_${var.database}"
login = true
roles = ["rds_iam", postgresql_role.owner.name]
skip_reassign_owned = "true"
}
resource "postgresql_database" "database" {
name = var.database
owner = postgresql_role.owner.name
lc_collate = "en_US.UTF-8"
lc_ctype = "en_US.UTF-8"
}
Terraform Version
Affected Resource(s)
Terraform Configuration Files
Debug Output
RDS postgres logs
Logs
```sql 2023-03-10 11:10:07 UTC:82.214.175.74(13708):[unknown]@[unknown]:[835]:LOG: connection received: host=82.214.175.74 port=13708 2023-03-10 11:10:07 UTC:82.214.175.74(13708):terraformuser@postgres:[835]:LOG: connection authenticated: identity="terraformuser" method=md5 (/rdsdbdata/config/pg_hba.conf:15) 2023-03-10 11:10:07 UTC:82.214.175.74(13708):terraformuser@postgres:[835]:LOG: connection authorized: user=terraformuser database=postgres application_name=Terraform provider SSL enabled (protocol=TLSv1.2, cipher=ECDHE-RSA-AES256-GCM-SHA384, bits=256) 2023-03-10 11:10:07 UTC:82.214.175.74(13708):terraformuser@postgres:[835]:LOG: statement: SELECT VERSION() 2023-03-10 11:10:07 UTC:82.214.175.74(13708):terraformuser@postgres:[835]:LOG: duration: 0.428 ms 2023-03-10 11:10:07 UTC:82.214.175.74(13708):terraformuser@postgres:[835]:LOG: disconnection: session time: 0:00:00.847 user=terraformuser database=postgres host=82.214.175.74 port=13708 2023-03-10 11:10:08 UTC:82.214.175.74(9156):[unknown]@[unknown]:[836]:LOG: connection received: host=82.214.175.74 port=9156 2023-03-10 11:10:09 UTC:82.214.175.74(9156):terraformuser@postgres:[836]:LOG: connection authenticated: identity="terraformuser" method=md5 (/rdsdbdata/config/pg_hba.conf:15) 2023-03-10 11:10:09 UTC:82.214.175.74(9156):terraformuser@postgres:[836]:LOG: connection authorized: user=terraformuser database=postgres application_name=Terraform provider SSL enabled (protocol=TLSv1.2, cipher=ECDHE-RSA-AES256-GCM-SHA384, bits=256) 2023-03-10 11:10:09 UTC:82.214.175.74(9156):terraformuser@postgres:[836]:LOG: statement: BEGIN READ WRITE 2023-03-10 11:10:09 UTC:82.214.175.74(9156):terraformuser@postgres:[836]:LOG: duration: 0.148 ms 2023-03-10 11:10:09 UTC:82.214.175.74(9156):terraformuser@postgres:[836]:LOG: statement: SET statement_timeout = 0 2023-03-10 11:10:09 UTC:82.214.175.74(9156):terraformuser@postgres:[836]:LOG: duration: 0.111 ms 2023-03-10 11:10:09 UTC:82.214.175.74(9156):terraformuser@postgres:[836]:LOG: duration: 0.621 ms parseExpected Behavior
All the resources on the postges server are in sync with terraform config
Actual Behavior
When communicating with postgres server and creating new database with OWNER other than provider's user it fails if that database OWNER has
rds_iam
role (which require IAM auth rather than password). That happens because provider user temporarily GRANTs the OWNER to itself in order to runCREATE DATABASE <name> WITH OWNER <OWNER>;
. The problem is that provider grants the new database OWNER in one connection, and in the other it tries to connect to runCREATE DATABASE....
query. But it cannot be done, since provider has to authenticate with IAM already, and thus, fails. And then can't REVOKE that membership back for same reason.Are there any options to avoid this ? That what I tried to do in database and it worked:
But it should run within same database connection, but not transaction, since we cannot run
CREATE DATABASE
in transaction.So, is there any option to manage that
database
resource using one database connection ?Steps to Reproduce
Please list the steps required to reproduce the issue, for example:
terraform apply
References