使用Terraform创建只读的PostgreSQL用户。

6

我正在尝试找到正确的terraform模块组合,以在postgres RDS实例中创建只读用户。

https://www.terraform.io/docs/providers/postgresql/

我有一个包含两个模式的数据库 - publicwww
我从以下内容开始:
resource "postgresql_role" "readonly" {                                                                                                                                                      
  name = "readonly"                                                                                                                                                                          
}                                                                                                                                                                                            

resource postgresql_grant "readonly_public" {                                                                                                                                                
  database    = "db_name"                                                                                                                                                                    
  role        = postgresql_role.readonly.name                                                                                                                                                
  schema      = "public"                                                                                                                                                                     
  object_type = "table"                                                                                                                                                                      
  privileges  = ["SELECT"]                                                                                                                                                                   
}                                                                                                                                                                                            

resource postgresql_grant "readonly_www" {                                                                                                                                                   
  database    = "db_name"                                                                                                                                                                    
  role        = postgresql_role.readonly.name                                                                                                                                                
  schema      = "www"                                                                                                                                                                        
  object_type = "table"                                                                                                                                                                      
  privileges  = ["SELECT"]                                                                                                                                                                   
}                                                                                                                                                                                            

resource "postgresql_role" "readonly_user" {                                                                                                                                                 
  name     = "readonly_user"                                                                                                                                                                 
  password = "some_password_123"                                                                                                                                                             
  login    = true                                                                                                                                                                            
  roles = [postgresql_role.readonly.name]                                                                                                                                                    
}

旨在:

1)创建一个名为readonly的角色,该角色仅对db_name数据库中的两个模式具有SELECT访问权限。

2)创建一个名为readonly_user的可登录用户,并将角色readonly授予他们。

这样做后,创建的用户仍然可以(例如)创建表。 但是,他们确实对表本身具有只读访问权限。

\du的轻微编辑结果:

db_name=> \du

     Role name  |   Attributes                   |  Member of
----------------+--------------------------------+---------------------------
 readonly_user  | Password valid until infinity  | {readonly}
 readonly       | Cannot login                  +| {}
                | Password valid until infinity  | 

如果有一种方法可以通过terraform创建一个只能读取信息的用户,我将非常感激。

有没有一个 Terraform 资源可以用来撤销“CREATE”权限? - The Unknown Dev
1个回答

2
我不认为tf提供的postgres提供程序会这样做。您需要手动撤销这些,因为这是postgres的默认行为。

https://github.com/terraform-providers/terraform-provider-postgresql/issues/85

https://www.postgresql.org/docs/11/ddl-schemas.html#DDL-SCHEMAS-PRIV

A user can also be allowed to create objects in someone else's schema. To allow that, the CREATE privilege on the schema needs to be granted. Note that by default, everyone has CREATE and USAGE privileges on the schema public. This allows all users that are able to connect to a given database to create objects in its public schema. Some usage patterns call for revoking that privilege:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接