go-sql-driver/mysql — pull request #101 by lukescott (2013-06-19 22:07:14+00:00)
Title: Add support for customizing tls.Config

This adds the ability to customize the tls.Config object so you can:

- Specify a custom RootCA (instead of skip verify)
- Specify a client certificate for authentication w/ or w/out a password

Example of how it would be used:

```go
package main

import (
"crypto/tls"
"crypto/x509"
"database/sql"
"fmt"
"github.com/lukescott/mysql"
"io/ioutil"
"log"
)

func main() {
rootCAs := x509.NewCertPool()
{
pem, err := ioutil.ReadFile("/Users/lukescott/ca-cert.pem")
if err != nil {
log.Fatal(err)
}
if ok := rootCAs.AppendCertsFromPEM(pem); !ok {
log.Fatal("Failed to append PEM.")
}
}
clientCerts := make([]tls.Certificate, 0, 1)
{
certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
if err != nil {
log.Fatal(err)
}
clientCerts = append(clientCerts, certs)
}
db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
db.Driver().(mysql.TLSConfig).SetTLSConfig("custom", &tls.Config{
RootCAs: rootCAs,
Certificates: clientCerts,
})
if err != nil {
log.Fatal(err)
}
var row string
err = db.QueryRow("SHOW DATABASES").Scan(&row)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", row)
}
```