You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+8-1Lines changed: 8 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,10 @@ This module creates database and users on an existing CloudSQL instance. The str
4
4
5
5
To enforce permissions, the module executes SQL commands with the mysql cli, which is therefore a prerequisite (it must be present in the filesystem where terraform apply is executed).
6
6
7
+
For MySQL 8.x instances, the module automatically removes the default `cloudsqlsuperuser` role, clears any global privileges and assigns the target database as the only default role so that new users are scoped exclusively to their database.
8
+
9
+
If you ever need to rerun all local scripts (start proxy → grant privileges → stop proxy) without recreating the module-managed users, set a different value for the `permissions_refresh_id` variable (use the `YYYYMMDD` format, e.g. `20251110`) and run `terraform apply`; changing the value forces Terraform to recreate the null resources that execute those scripts while keeping the `google_sql_user` resources in place (see `examples/main.tf` for a ready-to-use snippet).
10
+
7
11
In addition, the script must be able to connect to the CloudSQL instance. In case this is not easily accessible from the terraform cli, the module is able to:
8
12
9
13
1. Start an instance of [CloudSQL Auth Proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy), for this purpose two null resources will be created for each user added to the database, enabling this option requires the [presence of the proxy executable](https://cloud.google.com/sql/docs/mysql/sql-proxy) in the filesystem where `terraform apply` is executed.
@@ -33,12 +37,13 @@ CloudSQL Auth Proxy needs the CloudSQL instance to expose a public IP address in
33
37
34
38
| Name | Description | Type | Default | Required |
| <aname="input_cloudsql_instance_name"></a> [cloudsql\_instance\_name](#input\_cloudsql\_instance\_name)| The name of the existing Google CloudSQL Instance name. Actually only a MySQL 5.7 or 8 instance is supported. |`string`| n/a | yes |
40
+
| <aname="input_cloudsql_instance_name"></a> [cloudsql\_instance\_name](#input\_cloudsql\_instance\_name)| The name of the existing Google CloudSQL Instance name. MySQL 5.7, 8.0 and 8.4 are supported. |`string`| n/a | yes |
37
41
| <aname="input_cloudsql_privileged_user_name"></a> [cloudsql\_privileged\_user\_name](#input\_cloudsql\_privileged\_user\_name)| The name of the privileged user of the Cloud SQL instance |`string`| n/a | yes |
38
42
| <aname="input_cloudsql_privileged_user_password"></a> [cloudsql\_privileged\_user\_password](#input\_cloudsql\_privileged\_user\_password)| The password of the privileged user of the Cloud SQL instance |`string`| n/a | yes |
39
43
| <aname="input_cloudsql_proxy_host"></a> [cloudsql\_proxy\_host](#input\_cloudsql\_proxy\_host)| The host of the Cloud SQL Auth Proxy; if a value other than localhost or 127.0.0.1 (default) is entered, it is assumed that there is a CloudSQL Auth Proxy instance defined and already configured outside this module, and therefore the proxy will not be launched. |`string`|`"127.0.0.1"`| no |
40
44
| <aname="input_cloudsql_proxy_port"></a> [cloudsql\_proxy\_port](#input\_cloudsql\_proxy\_port)| Port of the Cloud SQL Auth Proxy |`string`|`"1234"`| no |
41
45
| <aname="input_database_and_user_list"></a> [database\_and\_user\_list](#input\_database\_and\_user\_list)| The list with all the databases and the relative user. Please not that you can assign only a database to a single user, the same user cannot be assigned to multiple databases. `user_host` is optional, has a default value of '%' to allow the user to connect from any host, or you can specify it for the given user for a more restrictive access. | <pre>list(object({<br/> user = string<br/> user_host = optional(string, "%")<br/> database = string<br/> }))</pre> | n/a | yes |
46
+
| <aname="input_permissions_refresh_id"></a> [permissions\_refresh\_id](#input\_permissions\_refresh\_id)| Optional identifier (use format YYYYMMDD, e.g. 20251110) used only to force Terraform to rerun the proxy/grant scripts without recreating users. Change the value whenever you need to reapply permissions. |`string`|`""`| no |
42
47
| <aname="input_project_id"></a> [project\_id](#input\_project\_id)| The ID of the project in which the resource belongs. |`string`| n/a | yes |
43
48
| <aname="input_region"></a> [region](#input\_region)| The region in which the resource belongs. |`string`| n/a | yes |
44
49
| <aname="input_terraform_start_cloud_sql_proxy"></a> [terraform\_start\_cloud\_sql\_proxy](#input\_terraform\_start\_cloud\_sql\_proxy)| If `true` terraform will automatically start the Cloud SQL Proxy instance present in the filesystem at the condition that cloudsql\_proxy\_host is set to a supported value. If `false` you have to start the Cloud SQL Proxy manually. This variable is used to prevent the creation of a Cloud SQL Proxy instance even if cloudsql\_proxy\_host has a supported value. |`bool`|`true`| no |
@@ -54,6 +59,8 @@ CloudSQL Auth Proxy needs the CloudSQL instance to expose a public IP address in
|[google_sql_database_instance.cloudsql_instance](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/sql_database_instance)| data source |
echo"Connection with with CloudSQL Auth Proxy established at ${CLOUDSQL_PROXY_HOST}."
26
+
log"Connection with CloudSQL Auth Proxy established at ${CLOUDSQL_PROXY_HOST}:${CLOUDSQL_PROXY_PORT}."
16
27
break
17
28
fi
18
-
echo"Waiting for Cloud SQL Proxy to start... $j"
29
+
log"Waiting for Cloud SQL Proxy to start (attempt ${j}/10)..."
19
30
sleep 1s
20
31
done
21
32
22
33
if [ "$READY"-eq 0 ];then
23
-
if [ "${MYSQL_VERSION:0:9}"="MYSQL_5_7" ];then
24
-
mysql --host=${CLOUDSQL_PROXY_HOST} --port=${CLOUDSQL_PROXY_PORT} --user=${CLOUDSQL_PRIVILEGED_USER_NAME} --password=${CLOUDSQL_PRIVILEGED_USER_PASSWORD} --execute="REVOKE ALL PRIVILEGES, GRANT OPTION FROM '${USER}'@'${USER_HOST}'; GRANT ALL ON ${DATABASE}.* TO ${USER}@'${USER_HOST}';"
25
-
fi
34
+
USER_IDENTIFIER="'${USER}'@'${USER_HOST}'"
35
+
DATABASE_IDENTIFIER="\`${DATABASE}\`.*"
26
36
27
-
if [ "${MYSQL_VERSION:0:9}"="MYSQL_8_0" ];then
28
-
mysql --host=${CLOUDSQL_PROXY_HOST} --port=${CLOUDSQL_PROXY_PORT} --user=${CLOUDSQL_PRIVILEGED_USER_NAME} --password=${CLOUDSQL_PRIVILEGED_USER_PASSWORD} --execute="REVOKE cloudsqlsuperuser FROM '${USER}'@'${USER_HOST}'; GRANT ALL ON ${DATABASE}.* TO ${USER}@'${USER_HOST}';"
37
+
log "Preparing privilege statements for ${USER_IDENTIFIER} on database \`${DATABASE}\` (MySQL ${MYSQL_VERSION})."
38
+
39
+
case"${MYSQL_VERSION}"in
40
+
MYSQL_5_7*)
41
+
SQL_COMMANDS="REVOKE ALL PRIVILEGES, GRANT OPTION FROM ${USER_IDENTIFIER}; GRANT ALL PRIVILEGES ON ${DATABASE_IDENTIFIER} TO ${USER_IDENTIFIER};"
42
+
;;
43
+
MYSQL_8_0*|MYSQL_8_4*)
44
+
SQL_COMMANDS="REVOKE cloudsqlsuperuser FROM ${USER_IDENTIFIER}; SET DEFAULT ROLE NONE TO ${USER_IDENTIFIER}; GRANT ALL PRIVILEGES ON ${DATABASE_IDENTIFIER} TO ${USER_IDENTIFIER};"
45
+
;;
46
+
*)
47
+
log "ERROR: Unsupported MySQL version ${MYSQL_VERSION}.">&2
description="The list with all the databases and the relative user. Please not that you can assign only a database to a single user, the same user cannot be assigned to multiple databases. `user_host` is optional, has a default value of '%' to allow the user to connect from any host, or you can specify it for the given user for a more restrictive access."
52
52
}
53
+
54
+
variable"permissions_refresh_id" {
55
+
type=string
56
+
default=""
57
+
description="Optional identifier (use format YYYYMMDD, e.g. 20251110) used only to force Terraform to rerun the proxy/grant scripts without recreating users. Change the value whenever you need to reapply permissions."
0 commit comments