Skip to content

Commit 1b59d61

Browse files
authored
Db role tweaks (#238)
* add more dbs into config
1 parent 64f6c5b commit 1b59d61

File tree

5 files changed

+360
-9
lines changed

5 files changed

+360
-9
lines changed

gcp/terraform/docs/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ Before updating Terraform configuration, ensure you have the following:
4747
principals # List of principals that can be granted the role
4848
role_type # Optional value, when set to 'custom' ensures custom role URI is properply generated
4949

50+
51+
instances = ... # Optional list of cloudsql instances
52+
instance # instance name
53+
databases # list of databases
54+
db_name # database name
55+
roles # list of created custom roles to be managed by Terraform, e.g. ["readonly", "readwrite", "admin"]
56+
owner # database owner/role creator
57+
database_role_assignment # map of custom role assignments
58+
readonly # list of [email protected] emails to be granted db custom roles
59+
readwrite # list of [email protected] emails to be granted db custom roles
60+
admin # list of [email protected] emails to be granted db custom roles
61+
5062
For example, if you want to grant sa-pubsub service account in Connect Dev an invoker role for Cloud Run in Business Dev:
5163
![invoker-grant](./images/cloud-run-invoker-role.png)
5264

@@ -64,6 +76,11 @@ For example, if you want to grant sa-pubsub service account in Connect Dev an in
6476
principals # List of principals that can be granted the role
6577
role_type # Optional value, when set to 'custom' ensures custom role URI is properply generated
6678

79+
database_role_assignment # Optional map of custom role assignments
80+
(roles will apply to all databases in the corresponnding env, that are listed in `project_account_bindings.auto.tfvars`)
81+
readonly # list of [email protected] emails to be granted db custom roles
82+
readwrite # list of [email protected] emails to be granted db custom roles
83+
admin # list of [email protected] emails to be granted db custom roles
6784

6885
`global_custom_roles.auto.tfvars`
6986

@@ -72,6 +89,12 @@ For example, if you want to grant sa-pubsub service account in Connect Dev an in
7289
title # Name of the custom IAM role
7390
permissions # List of permissions assigned to the role
7491
description # Description of the custom role
92+
93+
database_role_assignment # Optional map of custom role assignments
94+
(roles will apply to all databases, that are listed in `project_account_bindings.auto.tfvars`)
95+
readonly # list of [email protected] emails to be granted db custom roles
96+
readwrite # list of [email protected] emails to be granted db custom roles
97+
admin # list of [email protected] emails to be granted db custom roles
7598
4. Merging the new branch into main will trigger 'terraform plan'
7699
5. Output of terraform plan can be reviewed in https://app.terraform.io/app/BCRegistry/workspaces/gcp-iam/runs
77100
6. If no errors are present, and if Terraform state changes are as expected, 'terraform apply' can be executed for the run in the UI (will either need permissions to access or ask SRE team to review)

gcp/terraform/global_custom_roles.auto.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
global_database_role_assignment = {
22
readonly = []
33
readwrite = []
4-
admin = []
4+
55
}
66
global_custom_roles = {
77
rolestore = {

gcp/terraform/helper_scripts/cloud-functions/db-role-management/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def grant_db_role(request):
3131

3232
user = request_json['user']
3333
role = request_json['role']
34+
db = request_json['database']
3435

3536
if not re.match(r'^[a-zA-Z0-9_]+$', role):
3637
return {'error': 'Role name contains invalid characters (only letters, numbers and underscores allowed)'}, 400
@@ -53,7 +54,7 @@ def grant_db_role(request):
5354
return {'error': 'gcs_uri must start with gs://'}, 400
5455

5556
bucket_name = gcs_uri[5:].split('/')[0]
56-
blob_name = os.path.join(*gcs_uri[5:].split('/')[1:], f"grant_role_{role}_{user.replace('@', '_at_')}.sql")
57+
blob_name = os.path.join(*gcs_uri[5:].split('/')[1:], f"grant_role_{role}_{user.replace('@', '_at_')}_{db}.sql")
5758

5859
storage_client = storage.Client()
5960
bucket = storage_client.bucket(bucket_name)
@@ -70,7 +71,7 @@ def grant_db_role(request):
7071
import_body = {
7172
'importContext': {
7273
'uri': f"gs://{bucket_name}/{blob_name}",
73-
'database': request_json['database'],
74+
'database': db,
7475
'fileType': 'SQL'
7576
}
7677
}
@@ -119,7 +120,7 @@ def should_retry(exc):
119120
return {
120121
'status': 'success',
121122
'operationId': response['name'],
122-
'database': request_json['database'],
123+
'database': db,
123124
'user': user,
124125
'role_granted': role,
125126
'sql_executed': sql_content.strip(),

gcp/terraform/modules/db_roles/scripts/apply_roles.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)