Skip to content

Commit 00b83a5

Browse files
filippolmtCopilot
andcommitted
Update variables.tf
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c4feeb8 commit 00b83a5

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Terraform module for creating database and associated user on an existing Google CloudSQL instance
22

3-
This module creates database and users on an existing CloudSQL instance. The structure of the input variable is designed so that the database/user ratio is 1:1, so the module not only takes care of creating the database and its user, but also sets permissions on the user so that it has access to only the database for which it is responsible.
3+
This module creates databases and users on an existing CloudSQL instance. The structure of the input variable enforces a 1:1 database/user ratio. The module both creates each pair and applies the required permissions so that the user can access only its database.
44

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).
5+
To enforce permissions, the module executes SQL commands through the MySQL CLI, which therefore must be installed on the machine running `terraform apply`. The bundled proxy helper scripts rely on the [Cloud SQL Auth Proxy **v2** command syntax](https://cloud.google.com/sql/docs/mysql/connect-auth-proxy), so make sure you install proxy version 2.x (the newer `${CLOUDSQL_PROXY_BIN} \"${CONNECTION_NAME}\" --port ...` invocation) rather than the legacy 1.x binary that used `-instances=` flags.
66

77
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.
88

99
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).
1010

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:
11+
In addition, the module must be able to connect to the CloudSQL instance. If the instance is not directly reachable from the machine running `terraform apply`, the module can:
1212

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.
14-
2. Connect from a [CloudSQL Auth Proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy) instance not present in the filesystem.
13+
1. Start a local instance of [CloudSQL Auth Proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy). This creates two null resources per user and requires the [proxy executable](https://cloud.google.com/sql/docs/mysql/sql-proxy) to be present on the machine running `terraform apply`.
14+
2. Connect through an existing [CloudSQL Auth Proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy) instance that is already running elsewhere (outside this module).
1515

1616
### IMPORTANT
1717

@@ -42,7 +42,7 @@ CloudSQL Auth Proxy needs the CloudSQL instance to expose a public IP address in
4242
| <a name="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 |
4343
| <a name="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 |
4444
| <a name="input_cloudsql_proxy_port"></a> [cloudsql\_proxy\_port](#input\_cloudsql\_proxy\_port) | Port of the Cloud SQL Auth Proxy | `string` | `"1234"` | no |
45-
| <a name="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 |
45+
| <a name="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 note 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 |
4646
| <a name="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 |
4747
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | The ID of the project in which the resource belongs. | `string` | n/a | yes |
4848
| <a name="input_region"></a> [region](#input\_region) | The region in which the resource belongs. | `string` | n/a | yes |

scripts/execute_cloud_sql_proxy.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ log() {
1111
printf '[sql-proxy] %s\n' "${1}"
1212
}
1313

14-
PROXY_BIN=""
14+
CLOUDSQL_PROXY_BIN=""
1515
if command -v cloud_sql_proxy >/dev/null 2>&1; then
16-
PROXY_BIN="cloud_sql_proxy"
16+
CLOUDSQL_PROXY_BIN="cloud_sql_proxy"
1717
else
1818
log "Error: cannot find the Cloud SQL Auth Proxy executable cloud_sql_proxy. Please install it or add it to your PATH." >&2
1919
exit 1
@@ -26,9 +26,9 @@ fi
2626

2727
CONNECTION_NAME="${CLOUDSDK_CORE_PROJECT}:${GCLOUD_PROJECT_REGION}:${CLOUDSQL_INSTANCE_NAME}"
2828

29-
if ! pgrep -x "$PROXY_BIN" >/dev/null; then
30-
log "Starting Cloud SQL Auth Proxy (${PROXY_BIN}) for ${CONNECTION_NAME} on localhost:${CLOUDSQL_PROXY_PORT}."
31-
"${PROXY_BIN}" "${CONNECTION_NAME}" --port "${CLOUDSQL_PROXY_PORT}" >/dev/null 2>&1 &
29+
if ! pgrep -x "$CLOUDSQL_PROXY_BIN" >/dev/null; then
30+
log "Starting Cloud SQL Auth Proxy (${CLOUDSQL_PROXY_BIN}) for ${CONNECTION_NAME} on localhost:${CLOUDSQL_PROXY_PORT}."
31+
"${CLOUDSQL_PROXY_BIN}" "${CONNECTION_NAME}" --port "${CLOUDSQL_PROXY_PORT}" >/dev/null 2>&1 &
3232
sleep 1s
3333
else
3434
log "Cloud SQL Auth Proxy already running; skipping start."

scripts/execute_sql.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ log() {
1111
printf '[sql-grant] %s\n' "${1}"
1212
}
1313

14+
mysql_exec() {
15+
MYSQL_PWD="${CLOUDSQL_PRIVILEGED_USER_PASSWORD}" mysql \
16+
--host="${CLOUDSQL_PROXY_HOST}" \
17+
--port="${CLOUDSQL_PROXY_PORT}" \
18+
--user="${CLOUDSQL_PRIVILEGED_USER_NAME}" \
19+
"$@"
20+
}
21+
1422
if ! [ -x "$(command -v mysql)" ]; then
1523
log "Error: the mysql client is not installed or is not in your path. Please add the mysql client executable." >&2
1624
exit 1
@@ -41,7 +49,17 @@ if [ "$READY" -eq 0 ]; then
4149
SQL_COMMANDS="REVOKE ALL PRIVILEGES, GRANT OPTION FROM ${USER_IDENTIFIER}; GRANT ALL PRIVILEGES ON ${DATABASE_IDENTIFIER} TO ${USER_IDENTIFIER};"
4250
;;
4351
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};"
52+
if ! REVOKE_OUTPUT=$(mysql_exec --execute="REVOKE cloudsqlsuperuser FROM ${USER_IDENTIFIER};" 2>&1); then
53+
if printf '%s' "${REVOKE_OUTPUT}" | grep -qi "Operation REVOKE ROLE failed"; then
54+
log "cloudsqlsuperuser role already absent for ${USER_IDENTIFIER}; continuing."
55+
else
56+
log "ERROR: Failed to revoke cloudsqlsuperuser role for ${USER_IDENTIFIER}:\n${REVOKE_OUTPUT}" >&2
57+
exit 1
58+
fi
59+
else
60+
log "Removed cloudsqlsuperuser role from ${USER_IDENTIFIER}."
61+
fi
62+
SQL_COMMANDS="SET DEFAULT ROLE NONE TO ${USER_IDENTIFIER}; GRANT ALL PRIVILEGES ON ${DATABASE_IDENTIFIER} TO ${USER_IDENTIFIER};"
4563
;;
4664
*)
4765
log "ERROR: Unsupported MySQL version ${MYSQL_VERSION}." >&2
@@ -51,7 +69,7 @@ if [ "$READY" -eq 0 ]; then
5169

5270
printf '[sql-grant] Executing SQL statements:\n%s\n' "${SQL_COMMANDS}"
5371

54-
if ! MYSQL_PWD="${CLOUDSQL_PRIVILEGED_USER_PASSWORD}" mysql --host="${CLOUDSQL_PROXY_HOST}" --port="${CLOUDSQL_PROXY_PORT}" --user="${CLOUDSQL_PRIVILEGED_USER_NAME}" --execute="${SQL_COMMANDS}"; then
72+
if ! mysql_exec --execute="${SQL_COMMANDS}"; then
5573
log "ERROR: Failed to apply privileges for ${USER_IDENTIFIER} on ${DATABASE}." >&2
5674
exit 1
5775
fi

scripts/kill_cloud_sql_proxy.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ log() {
1111
printf '[sql-proxy] %s\n' "${1}"
1212
}
1313

14-
PROXY_BIN="cloud_sql_proxy"
15-
if pgrep -x "$PROXY_BIN" >/dev/null; then
16-
log "Detected running ${PROXY_BIN}; waiting 5 seconds before shutdown to avoid race conditions."
14+
CLOUDSQL_PROXY_BIN="cloud_sql_proxy"
15+
if pgrep -x "$CLOUDSQL_PROXY_BIN" >/dev/null; then
16+
log "Detected running ${CLOUDSQL_PROXY_BIN}; waiting 5 seconds before shutdown to avoid race conditions."
1717
sleep 5s
1818
# Obtain the PID of the running Cloud SQL Auth Proxy and terminate gently.
19-
PID="$(pgrep -x "$PROXY_BIN")"
20-
log "Stopping ${PROXY_BIN} (PID(s): ${PID})."
19+
PID="$(pgrep -x "$CLOUDSQL_PROXY_BIN")"
20+
log "Stopping ${CLOUDSQL_PROXY_BIN} (PID(s): ${PID})."
2121

2222
kill "${PID}" || true
2323
fi

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ variable "database_and_user_list" {
4848
user_host = optional(string, "%")
4949
database = string
5050
}))
51-
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."
51+
description = "The list with all the databases and the relative user. Please note 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."
5252
}
5353

5454
variable "permissions_refresh_id" {

0 commit comments

Comments
 (0)