-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
crypto/x509: Google Cloud SQL TLS connections broken in Go 1.15+ #40748
Comments
cc @FiloSottile |
This came up before (#24151 (comment)) and I thought it was fixed in the client (GoogleCloudPlatform/cloud-sql-proxy#196) but apparently that's a component that not everyone uses. I also thought they started setting SANs. These certificates have two problems: they use the Common Name field, and they have names which are invalid hostnames. To be fair, CN is the right place to put a name which is not a hostname. But then the client can't be expected to match it as a hostname, which is what's happening here. This can be fixed server-side by putting the name in the SAN field, because I preserved 1:1 matching of invalid hostnames specifically for cases like this. I will reach out to the team. If you want a workaround in the meantime, you can see the code in the cloudsql-proxy PR. |
Actually, here's an even better way to do it correctly client-side with the new
|
@FiloSottile is use of There are potentially hundreds of thousands of devices with certificates with a common name similar to the usage here burned into their firmware that I will need to be able to support server side for the next several years. I'm just starting to look into this and exactly what changes we need to make client and server side. |
Absolutely, the field itself is not going anywhere, what's deprecated is |
@FiloSottile Am I right in saying that when a particular component is a TLS server instead of a TLS client, no hostname validation occurs, and therefore this snippet doesn't work without some modifying? I was digging through the stdlib code and it seems like there's two different handshake processes - One for when we're a client, one for when we're a server - And when we're a server, I couldn't seem to find hostname checks, which makes sense in the regular TLS context, but when working with mTLS as I am, it gets all a bit more confusing. I understand that the snippet you posted was more specific to this issue, however, more generally, that doesn't work when the component in question is a TLS server - Is this correct? Thanks for clarifying! |
@hugoamvieira The snippet won't work as intended on the server because Anyway, this is off-topic for this issue, so if you need to follow-up please see https://golang.org/wiki/Questions. |
Old issue, I know... but I am commenting in case anyone is as stuck as I was. Google now provides a dedicated connector library for their cloud sql instances. It provides an alternate dialer function to the standard sql and mysql libraries. Some links to help get you started: mysql specific example that helped me: I hope this saves someone a little time :) |
I think we can probably close this. FWIW the connector libraries shine when connecting to public IP. For private IP, a direct connection works like any other database connection. |
They are equally great at Private IP, particularly if you want maximum security 😉 |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Only the latest release.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I've tried to connect to a Google Cloud MySQL instance using TLS. This worked flawlessly on previous releases, however with Go 1.15+ I'm getting the following error
x509: certificate is not valid for any names, but wanted to match project-name:db-name
. Am using the same code as before as well as the same certificates. While debugging and stepping through the TLS verification I found that commonNameAsHostname function fails onvalidHostnamePattern
which calls validHostname with the hostname string andisPattern
set to true.Since the Google Cloud SQL ServerName is expected to be in format of
project-name:db-name
, thevalidHostname
function would return false because it does not consider:
to be valid. This change has been done in the following commit where the:
character has been removed which causes the issue.My code to connect to the Google Cloud SQL:
What did you expect to see?
A successful database connection and command execution.
What did you see instead?
x509: certificate is not valid for any names, but wanted to match project-name:db-name
while trying to connect to the database with TLS on Google Cloud SQL.The text was updated successfully, but these errors were encountered: