I have migrated my postgres database to Google Cloud SQL.
Without SSL enabled I can connect with no issues.
However I am struggling to get the SSL connection working.
I am using the pgx pool driver.
I have downloaded the server, client and private key pem files.
The error message I get back is
failed to write startup message (x509: certificate signed by unknown authority)
serverCert, err := ioutil.ReadFile("server-ca.pem")
if err != nil {
log.Fatal(err)
}
clientCert, err := ioutil.ReadFile("client-cert.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM(serverCert)
ok = caCertPool.AppendCertsFromPEM(clientCert)
fmt.Println(ok)
keypair, err := tls.LoadX509KeyPair("server-client-certs.pem", "client-key.pem")
if err != nil {
log.Fatal(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{keypair},
ServerName: s.Host,
ClientCAs: caCertPool,
ClientAuth: tls.RequestClientCert,
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &keypair, nil
},
}
connectionString := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s connect_timeout=%d sslmode=require",
s.Host, s.Port, s.User, s.Password, s.Name, s.ConnectTimeout)
connConfig, err := pgxpool.ParseConfig(connectionString)
if connConfig != nil {
connConfig.ConnConfig.TLSConfig = tlsConfig
}
var pool *pgxpool.Pool
pool, err = pgxpool.ConnectConfig(context.Background(), connConfig)
ParseConfigfunction configuresTLSConfigbased on the connection string. If you then overrideTLSConfigall that configuration is lost. E.g.connConfig.ConnConfig.TLSConfig = tlsConfig. One approach would be to set the values ofTLSConfigindividually. E.gconnConfig.ConnConfig.TLSConfig.ServerName = s.Host. - Phil Hale