Skip to content

Commit 4d0b086

Browse files
fix: revoke cloudsqlsuperuser permission
1 parent cd9003a commit 4d0b086

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
## [0.5.2] - 2026-03-18
12+
13+
[Compare with previous version](https://github.com/sparkfabrik/terraform-google-gcp-mysql-db-and-user-creation-helper/compare/0.5.1...0.5.2)
14+
15+
### Fixed
16+
17+
- Fix MySQL 8.0/8.4 provisioning failure caused by `Access Denied` (error 1045) on `REVOKE cloudsqlsuperuser`. The script now performs a `SHOW GRANTS` pre-check to verify whether the role is assigned before attempting the revoke, avoiding errors when the admin user lacks `ROLE_ADMIN` privileges.
18+
1119
## [0.5.1] - 2025-11-11
1220

1321
[Compare with previous version](https://github.com/sparkfabrik/terraform-google-gcp-mysql-db-and-user-creation-helper/compare/0.5.0...0.5.1)

scripts/execute_sql.sh

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,25 @@ if [ "$READY" -eq 0 ]; then
4949
SQL_COMMANDS="REVOKE ALL PRIVILEGES, GRANT OPTION FROM ${USER_IDENTIFIER}; GRANT ALL PRIVILEGES ON ${DATABASE_IDENTIFIER} TO ${USER_IDENTIFIER};"
5050
;;
5151
MYSQL_8_0*|MYSQL_8_4*)
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
52+
# Pre-check: verify whether the user has the cloudsqlsuperuser role
53+
# before attempting REVOKE, to avoid Access Denied (1045) errors when
54+
# the admin user lacks ROLE_ADMIN privileges.
55+
if ! GRANTS_OUTPUT=$(mysql_exec --execute="SHOW GRANTS FOR ${USER_IDENTIFIER};" 2>&1); then
56+
log "ERROR: Failed to retrieve grants for ${USER_IDENTIFIER}:\n${GRANTS_OUTPUT}" >&2
57+
exit 1
58+
fi
59+
60+
if printf '%s' "${GRANTS_OUTPUT}" | grep -qi "cloudsqlsuperuser"; then
61+
log "cloudsqlsuperuser role found for ${USER_IDENTIFIER}; revoking."
62+
if ! REVOKE_OUTPUT=$(mysql_exec --execute="REVOKE cloudsqlsuperuser FROM ${USER_IDENTIFIER};" 2>&1); then
5663
log "ERROR: Failed to revoke cloudsqlsuperuser role for ${USER_IDENTIFIER}:\n${REVOKE_OUTPUT}" >&2
5764
exit 1
5865
fi
59-
else
6066
log "Removed cloudsqlsuperuser role from ${USER_IDENTIFIER}."
67+
else
68+
log "cloudsqlsuperuser role not found for ${USER_IDENTIFIER}; skipping REVOKE."
6169
fi
70+
6271
SQL_COMMANDS="SET DEFAULT ROLE NONE TO ${USER_IDENTIFIER}; GRANT ALL PRIVILEGES ON ${DATABASE_IDENTIFIER} TO ${USER_IDENTIFIER};"
6372
;;
6473
*)

0 commit comments

Comments
 (0)