-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 2 commits
481dc97
4974720
b18f20a
17ee918
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
// { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed what? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you want this done in a different way? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
cfg.tls = tlsConfig | ||
} | ||
|
||
default: | ||
|
There was a problem hiding this comment.
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.