Skip to content

Commit d380894

Browse files
committed
Database user host is configurable.
1 parent a7add84 commit d380894

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
and this project adheres
7+
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
78

9+
## [0.2.2] - 2023-01-23
10+
11+
### Changed
12+
13+
- The user host is configurable, and not hardcoded to `%`. This allows to
14+
restrict the access to the database to a specific host. If not specified, it
15+
will use the default value `%`.
816

917
## [0.2.1] - 2023-01-19
1018

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ In addition, the script must be able to connect to the CloudSQL instance. In cas
2121

2222
| Name | Version |
2323
|------|---------|
24-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
24+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.2 |
2525
| <a name="requirement_google"></a> [google](#requirement\_google) | >= 4.47.0 |
2626
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1 |
2727
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.4.3 |
@@ -34,7 +34,7 @@ In addition, the script must be able to connect to the CloudSQL instance. In cas
3434
| <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 |
3535
| <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 |
3636
| <a name="input_cloudsql_proxy_port"></a> [cloudsql\_proxy\_port](#input\_cloudsql\_proxy\_port) | Port of the Cloud SQL Auth Proxy | `string` | `"1234"` | no |
37-
| <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. | <pre>list(object({<br> user = string<br> database = string<br> }))</pre> | n/a | yes |
37+
| <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 |
3838
| <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 |
3939
| <a name="input_region"></a> [region](#input\_region) | The region in which the resource belongs. | `string` | n/a | yes |
4040
| <a name="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 |

examples/test.tfvars

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ database_and_user_list = [
44
user = "user1"
55
},
66
{
7-
database = "db2"
8-
user = "user2"
7+
database = "db2"
8+
user = "user2"
99
},
1010
{
1111
database = "db3"
1212
user = "user3"
1313
},
1414
{
1515
database = "db4"
16+
user_host = "cloudsqlproxy~%"
1617
user = "user4"
1718
}
1819
]

main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ resource "google_sql_user" "sql_user" {
5656
instance = var.cloudsql_instance_name
5757
name = each.value.user
5858
password = random_password.sql_user_password[each.value.user].result
59-
host = "%"
59+
host = each.value.user_host
6060

6161
provisioner "local-exec" {
6262
command = templatefile(
@@ -71,6 +71,7 @@ resource "google_sql_user" "sql_user" {
7171
CLOUDSQL_PRIVILEGED_USER_PASSWORD = var.cloudsql_privileged_user_password
7272
MYSQL_VERSION = data.google_sql_database_instance.cloudsql_instance.database_version
7373
USER = each.value.user
74+
USER_HOST = each.value.user_host
7475
DATABASE = each.value.database
7576
}
7677
)

scripts/execute_sql.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ done
2121

2222
if [ "$READY" -eq 0 ]; then
2323
%{~ if trimspace(MYSQL_VERSION) == "MYSQL_5_7" }
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}'@'%'; GRANT ALL ON ${DATABASE}.* TO ${USER}@'%';"
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}';"
2525
%{ endif ~}
2626

2727
%{~ if trimspace(MYSQL_VERSION) == "MYSQL_8_0" }
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}'@'%'; GRANT ALL ON ${DATABASE}.* TO ${USER}@'%';"
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}';"
2929
%{ endif ~}
3030

3131
exit 0

variables.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ variable "cloudsql_privileged_user_password" {
4141
description = "The password of the privileged user of the Cloud SQL instance"
4242
}
4343

44+
# Optional value: refs https://developer.hashicorp.com/terraform/language/expressions/type-constraints#optional-object-type-attributes
4445
variable "database_and_user_list" {
4546
type = list(object({
46-
user = string
47-
database = string
47+
user = string
48+
user_host = optional(string, "%")
49+
database = string
4850
}))
49-
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."
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."
5052
}
51-

0 commit comments

Comments
 (0)