Skip to content

Add support for custom tls.Configs #101

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

Merged
merged 4 commits into from
Jul 1, 2013
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ A MySQL-Driver for Go's [database/sql](http://golang.org/pkg/database/sql) packa
* [Address](#address)
* [Parameters](#parameters)
* [Examples](#examples)
* [TLS support](#tls-support)
* [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
* [time.Time support](#timetime-support)
* [Unicode support](#unicode-support)
Expand Down Expand Up @@ -113,7 +114,7 @@ Possible Parameters are:
* `parseTime`: `parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
* `strict`: Enable strict mode. MySQL warnings are treated as errors.
* `timeout`: **Driver** side connection timeout. The value must be a string of decimal numbers, each with optional fraction and a unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*. To set a server side timeout, use the parameter [`wait_timeout`](http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_wait_timeout).
* `tls`: `true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side)
* `tls`: `true` enables TLS / SSL encrypted connection to the server. For other values see [TLS support](#tls-support).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my delayed response.
Please keep it up here. It is not necessary to make a new section for a single parameter. Just note that you can register your own configurations and refer to the godoc.
Ignore the INFILE section, this will be reworked before the next release.


All other parameters are interpreted as system variables:
* `autocommit`: *"SET autocommit=`value`"*
Expand Down Expand Up @@ -143,6 +144,14 @@ No Database preselected:
user:password@/
```

### TLS support
For TLS support set the `tls` parameter to one of the following values:

* `true`: Server certificate is signed by a trusted authority.
* `skip-verify`: Server certificate is self-signed with no root authority.
* `custom`: Server certifiate is signed by a self-managed authority, and/or a client certificate is used. `custom` can be any value that coorisponds to a custom `tls.Config` registered with [`mysql.RegisterTLSConfig`](http://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).


### `LOAD DATA LOCAL INFILE` support
For this feature you need direct access to the package. Therefore you must change the import path (no `_`):
```go
Expand Down
46 changes: 46 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,50 @@ func (nt NullTime) Value() (driver.Value, error) {
return nt.Time, nil
}

var tlsConfigMap map[string]*tls.Config

// Registers a custom tls.Config to be used with sql.Open.
// Use the key as a value in the DSN where tls=value.
//
// rootCertPool := x509.NewCertPool()
// {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this one... the extra scopes are a bit confusing since they are not conventional Go-style.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed what?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed the note on this line 😉

// pem, err := ioutil.ReadFile("/path/ca-cert.pem")
// if err != nil {
// log.Fatal(err)
// }
// if ok := rootCAs.AppendCertsFromPEM(pem); !ok {
// log.Fatal("Failed to append PEM.")
// }
// }
// clientCert := make([]tls.Certificate, 0, 1)
// {
// certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
// if err != nil {
// log.Fatal(err)
// }
// clientCert = append(clientCerts, certs)
// }
// mysql.RegisterTLSConfig("custom", tls.Config{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be &tls.Config{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how that happened... I copied it from the opening post. Only thing I really did was rename a couple variables.

// RootCAs: rootCertPool,
// Certificates: clientCert,
// })
// db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
//
func RegisterTLSConfig(key string, config *tls.Config) {
if tlsConfigMap == nil {
tlsConfigMap = make(map[string]*tls.Config)
}
tlsConfigMap[key] = config
}

// Removes tls.Config associated with key.
func DeregisterTLSConfig(key string) {
if tlsConfigMap == nil {
return
}
delete(tlsConfigMap, key)
}

// Logger
var (
errLog *log.Logger
Expand Down Expand Up @@ -152,6 +196,8 @@ func parseDSN(dsn string) (cfg *config, err error) {
cfg.tls = &tls.Config{}
} else if strings.ToLower(value) == "skip-verify" {
cfg.tls = &tls.Config{InsecureSkipVerify: true}
} else if tlsConfig, ok := tlsConfigMap[value]; ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add TODO: Check for Boolean false here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want this done in a different way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan is to edit the readBool function / introduce another function which reports if the input was a valid bool representation (so also checks if the input was 0 / false)

cfg.tls = tlsConfig
}

default:
Expand Down