Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UNIX socket connection fails with access denied when caching_sha2_password is set. #4452

Closed
hulto opened this issue Oct 28, 2024 · 4 comments
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. samples Issues that are directly related to samples. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@hulto
Copy link

hulto commented Oct 28, 2024

In which file did you encounter the issue?

cloudsql/mysql/database-sql/connect_unix.go

Did you change the file? If so, how?

No files have been changed.

Describe the issue

Following this tutorial: https://cloud.google.com/sql/docs/mysql/connect-instance-cloud-run#go
With two changes:

  • Enabling the caching_sha2_password database flag
  • Authenticating over unix socket instead of with cloud connector by removing the INSTANCE_CONNECTION_NAME env var.

I run into an error where the application is unable to authenticate to the database using the Unix socket.

Steps to reproduce:

gcloud sql instances create quickstart-instance \
--database-version=MYSQL_8_0 \
--cpu=1 \
--memory=4GB \
--region=us-central1 \
--database-flags=default_authentication_plugin=caching_sha2_password \
--root-password=DB_ROOT_PASSWORD

gcloud sql databases create quickstart-db --instance=quickstart-instance

gcloud sql users create quickstart-user \
--instance=quickstart-instance \
--password=PASSWORD123@

gcloud iam service-accounts list

gcloud projects add-iam-policy-binding ccdc-red-team-infra \
  --member="serviceAccount:[email protected]" \
  --role="roles/cloudsql.client"

gcloud run deploy run-sql --image gcr.io/ccdc-red-team-infra/run-sql \
  --add-cloudsql-instances ccdc-red-team-infra:us-central1:quickstart-instance \
  --set-env-vars INSTANCE_UNIX_SOCKET="/cloudsql/ccdc-red-team-infra:us-central1:quickstart-instance" \
  --set-env-vars DB_NAME="quickstart-db" \
  --set-env-vars DB_USER="quickstart-user" \
  --set-env-vars DB_PASS="PASSWORD123@" \
  --region us-central1

https://run-sql-rtqmb25lsq-uc.a.run.app

Navigating to the app it's unable to load
image

Checking the logs we see Access denied
image

Workaround
A work around I found to this (aside from not using caching_sha2_password) is to manually authenticate to the SQL database using the cloud sql connector.
https://cloud.google.com/sql/docs/mysql/connect-auth-proxy

$ ./cloud-sql-proxy ccdc-red-team-infra:us-central1:quickstart-instance &

$ mysql --get-server-public-key -h 127.0.0.1 -u quickstart-user -p
Enter password: 

mysql>

image

I would like to be able to use the UNIX socket auth with caching_sha2_password because of the security and flexibility that option provides.
If there is a better place to post this issue happy to move it.

@hulto hulto added priority: p2 Moderately-important priority. Fix may not be included in next release. triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Oct 28, 2024
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Oct 28, 2024
@jackwotherspoon jackwotherspoon self-assigned this Oct 29, 2024
@jackwotherspoon
Copy link
Contributor

@hulto Hi there,

This is currently a known limitation of the https://github.com/GoogleCloudPlatform/cloud-sql-proxy, the unix socket mode does not currently support caching_sha2_password plugin at the moment.

This is documented a little on https://cloud.google.com/sql/docs/mysql/connect-auth-proxy#connect-unix but I can probably add the warning to a few more spots to make it even more clear.

I created a tracking issue on the Proxy repo itself for this: GoogleCloudPlatform/cloud-sql-proxy#2317

@jackwotherspoon jackwotherspoon removed the triage me I really want to be triaged. label Oct 29, 2024
@hulto
Copy link
Author

hulto commented Oct 29, 2024

Thanks Jack!
I had read that warning three times I swear but missed the last sentence 😅

Did UNIX socket auth with caching used to work?
I had a project use it successfully ~3months ago.

@jackwotherspoon
Copy link
Contributor

Did UNIX socket auth with caching used to work?
I had a project use it successfully ~3months ago.

@hulto From my understanding it has never been supported/worked, so that is strange about your project...

If you had logged in via another method with the same DB user (Connector, direct TCP connection etc.) then it could be that the result was cached and you were then able to login via UNIX socket with the cached entry from the DB.

@hulto
Copy link
Author

hulto commented Oct 31, 2024

Thanks for your help Jack!

@hulto hulto closed this as completed Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. samples Issues that are directly related to samples. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants