-
Notifications
You must be signed in to change notification settings - Fork 5
/
prepare_aurora_db.py
executable file
·173 lines (151 loc) · 5.66 KB
/
prepare_aurora_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
import argparse
import json
import boto3
from rich.console import Console
from botocore.exceptions import ClientError
parser = argparse.ArgumentParser()
parser.add_argument(
"--stack-name",
help="the name of the CloudFormation stack containing the cd2 Aurora database",
required=True,
)
args = parser.parse_args()
console = Console()
secrets_client = boto3.client("secretsmanager")
rds_data_client = boto3.client("rds-data")
cf_reource = boto3.resource("cloudformation")
stack = cf_reource.Stack(args.stack_name)
console.print("Starting database preparation", style="bold green")
# read the outputs and parameters from the Cloudformation stack
stack_outputs = {
output["OutputKey"]: output["OutputValue"]
for output in stack.outputs
}
stack_parameters = {
parameter["ParameterKey"]: parameter["ParameterValue"]
for parameter in stack.parameters
}
# get the database admin secret
admin_secret_arn = stack_outputs["AdminSecretArn"]
admin_secret = json.loads(
secrets_client.get_secret_value(SecretId=admin_secret_arn)["SecretString"]
)
admin_username = admin_secret["username"]
# get the database cluster ARN
aurora_cluster_arn = stack_outputs["AuroraClusterArn"]
# get the environment
env = stack_parameters["EnvironmentParameter"]
# get the database user secrets
secret_name_prefix = f"uw-cd2-db-user-{env}-"
user_secrets = secrets_client.list_secrets(
Filters=[{"Key": "name", "Values": [secret_name_prefix]}],
MaxResults=100,
)
# for each user secret, create the database user and schema
for s in user_secrets["SecretList"]:
secret_arn = s["ARN"]
secret_value = json.loads(
secrets_client.get_secret_value(SecretId=secret_arn)["SecretString"]
)
password = secret_value["password"]
username = secret_value["username"]
database_name = secret_value["dbname"]
schema_name = username
console.print(
f" - creating database user [bold]{username}[/bold] in database [bold]{database_name}[/bold]",
style="green",
)
# create the database user
try:
user_sql = f"CREATE USER {username} WITH PASSWORD '{password}' LOGIN"
rds_data_client.execute_statement(
resourceArn=aurora_cluster_arn,
secretArn=admin_secret_arn,
sql=user_sql,
database=database_name,
)
console.print(" - Created user", style="bold green")
except ClientError as e:
if "already exists" in e.response["Error"]["Message"]:
console.print(f" - User {username} already exists", style="bold red")
try:
change_sql = f"ALTER USER {username} WITH PASSWORD '{password}'"
rds_data_client.execute_statement(
resourceArn=aurora_cluster_arn,
secretArn=admin_secret_arn,
sql=change_sql,
database=database_name,
)
console.print(
f" - Updated password for user {username}", style="bold green"
)
except ClientError as e:
console.print(
f" ! Unexpected error when updating password for {username}: {e}", style="bold red"
)
continue
else:
console.print(
f" ! Unexpected error when creating user {username}: {e}", style="bold red"
)
continue
# Grant the role to the admin user
try:
grant_sql = f"GRANT {username} TO {admin_username}"
rds_data_client.execute_statement(
resourceArn=aurora_cluster_arn,
secretArn=admin_secret_arn,
sql=grant_sql,
database=database_name,
)
console.print(
f" - Granted user {username} to {admin_username}", style="bold green"
)
except ClientError as e:
console.print(f" ! Unexpected error granting {username} role to {admin_username}: {e}", style="bold red")
continue
# create the schema
try:
schema_sql = f"CREATE SCHEMA IF NOT EXISTS AUTHORIZATION {username}"
rds_data_client.execute_statement(
resourceArn=aurora_cluster_arn,
secretArn=admin_secret_arn,
sql=schema_sql,
database=database_name,
)
console.print(f" - Created schema [bold]{username}[/bold]", style="green")
except ClientError as e:
console.print(f" ! Unexpected error creating schema {username}: {e}", style="bold red")
continue
# create the instructure_dap schema
try:
schema_sql = f"CREATE SCHEMA IF NOT EXISTS instructure_dap AUTHORIZATION {username}"
rds_data_client.execute_statement(
resourceArn=aurora_cluster_arn,
secretArn=admin_secret_arn,
sql=schema_sql,
database=database_name,
)
console.print(
f" - Created schema [bold]instructure_dap[/bold] in database [bold]{database_name}[/bold]",
style="green",
)
except ClientError as e:
console.print(f" ! Unexpected error: {e}", style="bold red")
continue
# grant create permission on database to canvas user
try:
grant_sql = f"GRANT CREATE ON DATABASE {database_name} TO {username}"
rds_data_client.execute_statement(
resourceArn=aurora_cluster_arn,
secretArn=admin_secret_arn,
sql=grant_sql,
database="postgres",
)
console.print(
f" - Granted CREATE on database {database_name} to user {username}",
style="bold green",
)
except ClientError as e:
console.print(f" ! Unexpected error: {e}", style="bold red")