Repository navigation
Support certificate refresh after computer awakes from sleep #1788
Copy link
Copy link
Closed
Labels
priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Description
Bug Description
After a few hours of running the cloud SQL proxy, new connections are automatically closed. The proxy prints out error messages on each connection:
2023/05/10 16:29:18 [account-intelligence-dev:us-central1:ogma-prod-1] accepted connection from 127.0.0.1:49983
2023/05/10 16:29:18 [account-intelligence-dev:us-central1:ogma-prod-1] connection aborted - error reading from instance: remote error: tls: bad certificate
2023/05/10 16:29:18 [account-intelligence-dev:us-central1:ogma-prod-1] accepted connection from 127.0.0.1:49985
2023/05/10 16:29:18 [account-intelligence-dev:us-central1:ogma-prod-1] connection aborted - error reading from instance: remote error: tls: bad certificate
2023/05/10 16:29:18 [account-intelligence-dev:us-central1:ogma-prod-1] accepted connection from 127.0.0.1:49987
2023/05/10 16:29:18 [account-intelligence-dev:us-central1:ogma-prod-1] connection aborted - error reading from instance: remote error: tls: bad certificate
2023/05/10 16:29:18 [account-intelligence-dev:us-central1:ogma-prod-1] accepted connection from 127.0.0.1:49989
2023/05/10 16:29:19 [account-intelligence-dev:us-central1:ogma-prod-1] connection aborted - error reading from instance: remote error: tls: bad certificate
2023/05/10 16:29:19 [account-intelligence-dev:us-central1:ogma-prod-1] accepted connection from 127.0.0.1:49991
2023/05/10 16:29:19 [account-intelligence-dev:us-central1:ogma-prod-1] connection aborted - error reading from instance: remote error: tls: bad certificate
2023/05/10 16:29:19 [account-intelligence-dev:us-central1:ogma-prod-1] accepted connection from 127.0.0.1:49993
2023/05/10 16:29:19 [account-intelligence-dev:us-central1:ogma-prod-1] connection aborted - error reading from instance: remote error: tls: bad certificate
Example code (or command)
// paste your code or command here
Stacktrace
No response
Steps to reproduce?
- ?
- ?
- ?
...
Environment
- OS type and version: Mac OS
- Cloud SQL Proxy version (
./cloud-sql-proxy --version):cloud-sql-proxy version 2.2.0+darwin.arm64 - Proxy invocation command (for example,
./cloud-sql-proxy --enable_iam_login --dir /path/to/dir INSTANCE_CONNECTION_NAME):cloud-sql-proxy urltest-266408:us-central1:ogma-dev-1?port=15432 account-intelligence-dev:us-central1:ogma-prod-1?port=15431
Additional Details
No response
Reacted by Forbes Lindesay, Mihkel Sokk, Onur Solmaz, Rustam Agzamov and Tom Raleigh
Reacted by Onur Solmaz
Metadata
Metadata
Assignees
Labels
priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Activity
enocom commentedon May 11, 2023
Does this include letting your machine go to sleep?
enocom commentedon May 11, 2023
Also what does your database connection string look like (removing private information of course).
carl-mastrangelo commentedon May 11, 2023
Yes
jdbc:postgresql://localhost:15431/postgresand
jdbc:postgresql://localhost:15432/postgresenocom commentedon May 11, 2023
I suspect the machine sleeping is the cause of the problem.
Nonetheless, I thought we'd fixed that here: GoogleCloudPlatform/cloud-sql-go-connector#471.
I'll investigate and report back.
carl-mastrangelo commentedon May 12, 2023
Sounds good. I can provide additional log info as needed.
[-]TLS Certificate is not renewed on expiration[/-][+]Support certificate refresh after computer awakes from sleep[/+]ForbesLindesay commentedon Sep 6, 2023
@enocom This appears to still be an issue. It makes the cloud-sql-proxy more-or-less unusable for our local development environments.
enocom commentedon Sep 6, 2023
Good to know. We tried removing the monotonic clock value when calculating refresh times (see GoogleCloudPlatform/cloud-sql-go-connector#471), but that seems not to have helped.
Perhaps the thing to do here is to check if the current cert is valid before initiating a connection, and blocking on a refresh if the cert is expired.
6 remaining items
enocom commentedon Nov 28, 2023
These bad certificate errors are not being caught by the Proxy (or the Go Connector) given an implementation detail of how TLS 1.3 works:
https://go.dev/doc/go1.12#tls_1_3
Right now the Go Connector in particular assumes an expired client certificate (what we have here) will cause the TLS handshake to fail. Instead, the handshake succeeds and then the first read fails. Because it's the read that fails, the Proxy assumes it's a network error instead of a problem with the TLS connection and so never tries to refresh the client certificate.
This is definitely a bug on our part. I have a fix in the works.
fix: ensure cert refresh recovers from sleep
enocom commentedon Dec 7, 2023
For extra context: https://stackoverflow.com/questions/62459802/tls-1-3-client-does-not-report-failed-handshake-when-client-certificate-verifica
enocom commentedon Dec 8, 2023
To close the loop on v1 not having this problem:
cloud-sql-proxy/proxy/proxy/client.go
Line 380 in d1e48b9
There's a check for the cert's expiration. We'll ship the fix for v2 next week.
fix: ensure cert refresh recovers from sleep (#686)
enocom commentedon Dec 8, 2023
We've fixed this in the Go Connector and will release this on Tuesday.
fix: strip monotonic clock reading in cert check
fix: strip monotonic clock reading in cert check
fix: strip monotonic clock reading in cert check
fix: strip monotonic clock reading in cert check (#750)
enocom commentedon Mar 12, 2024
FYI we've discovered and fixed another bug where running the Proxy with IAM Authentication would not correctly refresh the certificate.
A fix for that will go out in #2128 on Thursday.