-
Notifications
You must be signed in to change notification settings - Fork 98
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
Add tls support for mysql client #186
Open
db-will
wants to merge
8
commits into
pingcap:master
Choose a base branch
from
db-will:add-tls-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−5
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e7cfc4b
Add tls support for mysql client
db-will 52095d4
Update cmd/go-tpc/main.go
db-will 0c2b45d
Update cmd/go-tpc/main.go
db-will c8dffdf
Update cmd/go-tpc/main.go
db-will a7957cb
Update cmd/go-tpc/main.go
db-will 4b2c7db
Update cmd/go-tpc/main.go
db-will 9ad25f9
Update cmd/go-tpc/main.go
db-will 41368bb
Adjust code based on reviews
db-will File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,8 @@ package main | |||||||||
import ( | ||||||||||
"context" | ||||||||||
"crypto/sha1" | ||||||||||
"crypto/tls" | ||||||||||
"crypto/x509" | ||||||||||
"database/sql" | ||||||||||
sqldrv "database/sql/driver" | ||||||||||
"encoding/hex" | ||||||||||
|
@@ -18,6 +20,7 @@ import ( | |||||||||
"github.com/pingcap/go-tpc/pkg/util" | ||||||||||
"github.com/spf13/cobra" | ||||||||||
_ "go.uber.org/automaxprocs" | ||||||||||
|
||||||||||
// mysql package | ||||||||||
"github.com/go-sql-driver/mysql" | ||||||||||
// pg | ||||||||||
|
@@ -47,15 +50,19 @@ var ( | |||||||||
connParams string | ||||||||||
outputStyle string | ||||||||||
targets []string | ||||||||||
sslCA string | ||||||||||
sslCert string | ||||||||||
sslKey string | ||||||||||
|
||||||||||
globalDB *sql.DB | ||||||||||
globalCtx context.Context | ||||||||||
) | ||||||||||
|
||||||||||
const ( | ||||||||||
createDBDDL = "CREATE DATABASE " | ||||||||||
mysqlDriver = "mysql" | ||||||||||
pgDriver = "postgres" | ||||||||||
createDBDDL = "CREATE DATABASE " | ||||||||||
mysqlDriver = "mysql" | ||||||||||
pgDriver = "postgres" | ||||||||||
customTlsName = "custom" | ||||||||||
) | ||||||||||
|
||||||||||
type MuxDriver struct { | ||||||||||
|
@@ -93,18 +100,31 @@ func newDB(targets []string, driver string, user string, password string, dbName | |||||||||
hash.Write([]byte(password)) | ||||||||||
hash.Write([]byte(dbName)) | ||||||||||
hash.Write([]byte(connParams)) | ||||||||||
|
||||||||||
if driver == mysqlDriver && len(sslCA) > 0 { | ||||||||||
registerMysqlTLSConfig() | ||||||||||
} | ||||||||||
|
||||||||||
for i, addr := range targets { | ||||||||||
hash.Write([]byte(addr)) | ||||||||||
switch driver { | ||||||||||
case mysqlDriver: | ||||||||||
var tlsName string = "preferred" | ||||||||||
if len(sslCA) > 0 { | ||||||||||
tlsName = customTlsName | ||||||||||
} | ||||||||||
// allow multiple statements in one query to allow q15 on the TPC-H | ||||||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?multiStatements=true&tls=preferred", user, password, addr, dbName) | ||||||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?multiStatements=true&tls=%s", user, password, addr, dbName, tlsName) | ||||||||||
if len(connParams) > 0 { | ||||||||||
dsn = dsn + "&" + connParams | ||||||||||
} | ||||||||||
names[i] = dsn | ||||||||||
drv = &mysql.MySQLDriver{} | ||||||||||
case pgDriver: | ||||||||||
if len(sslCA) > 0 { | ||||||||||
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.
Suggested change
|
||||||||||
panic("postgresql driver doesn't support TLS yet") | ||||||||||
} | ||||||||||
|
||||||||||
dsn := fmt.Sprintf("postgres://%s:%s@%s/%s", user, password, addr, dbName) | ||||||||||
if len(connParams) > 0 { | ||||||||||
dsn = dsn + "?" + connParams | ||||||||||
|
@@ -150,9 +170,10 @@ func openDB() { | |||||||||
tmpDB, _ = newDB(targets, driver, user, password, "", connParams) | ||||||||||
defer tmpDB.Close() | ||||||||||
if _, err := tmpDB.Exec(createDBDDL + dbName); err != nil { | ||||||||||
panic(fmt.Errorf("failed to create database, err %v\n", err)) | ||||||||||
panic(fmt.Errorf("failed to create database, err %v", err)) | ||||||||||
} | ||||||||||
} else { | ||||||||||
fmt.Printf("failed to ping db, err %v\n", err) | ||||||||||
globalDB = nil | ||||||||||
} | ||||||||||
} else { | ||||||||||
|
@@ -209,6 +230,9 @@ func main() { | |||||||||
rootCmd.PersistentFlags().StringVar(&outputStyle, "output", util.OutputStylePlain, "output style, valid values can be { plain | table | json }") | ||||||||||
rootCmd.PersistentFlags().StringSliceVar(&targets, "targets", nil, "Target database addresses") | ||||||||||
rootCmd.PersistentFlags().MarkHidden("targets") | ||||||||||
rootCmd.PersistentFlags().StringVar(&sslCA, "ssl-ca", "", "Path of file that contains list of trusted SSL CAs for connection") | ||||||||||
rootCmd.PersistentFlags().StringVar(&sslCert, "ssl-cert", "", "Path of file that contains X509 certificate in PEM format for connection") | ||||||||||
rootCmd.PersistentFlags().StringVar(&sslKey, "ssl-key", "", "Path of file that contains X509 key in PEM format for connection") | ||||||||||
|
||||||||||
cobra.EnablePrefixMatching = true | ||||||||||
|
||||||||||
|
@@ -251,3 +275,41 @@ func main() { | |||||||||
|
||||||||||
cancel() | ||||||||||
} | ||||||||||
|
||||||||||
// registerMysqlTLSConfig constructs a `*tls.Config` from the CA, certification and key | ||||||||||
// paths, and register to mysql client. | ||||||||||
func registerMysqlTLSConfig() { | ||||||||||
// Load the client certificates from disk | ||||||||||
var certificates []tls.Certificate | ||||||||||
if len(sslCert) != 0 && len(sslKey) != 0 { | ||||||||||
cert, err := tls.LoadX509KeyPair(sslCert, sslKey) | ||||||||||
if err != nil { | ||||||||||
panic(fmt.Errorf("could not load client key pair, err %v", err)) | ||||||||||
} | ||||||||||
certificates = []tls.Certificate{cert} | ||||||||||
} | ||||||||||
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.
Suggested change
|
||||||||||
|
||||||||||
// Create a certificate pool from CA | ||||||||||
certPool := x509.NewCertPool() | ||||||||||
ca, err := os.ReadFile(sslCA) | ||||||||||
if err != nil { | ||||||||||
panic(fmt.Errorf("could not read CA certificate, err %v", err)) | ||||||||||
} | ||||||||||
|
||||||||||
// Append the certificates from the CA | ||||||||||
if !certPool.AppendCertsFromPEM(ca) { | ||||||||||
panic(fmt.Errorf("failed to append CA certs")) | ||||||||||
} | ||||||||||
|
||||||||||
tlsConfig := &tls.Config{ | ||||||||||
MinVersion: tls.VersionTLS12, | ||||||||||
dveeden marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
Certificates: certificates, | ||||||||||
RootCAs: certPool, | ||||||||||
ClientCAs: certPool, | ||||||||||
} | ||||||||||
|
||||||||||
err = mysql.RegisterTLSConfig(customTlsName, tlsConfig) | ||||||||||
if err != nil { | ||||||||||
panic(fmt.Errorf("failed to register TLS config, err %v", err)) | ||||||||||
} | ||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
Refer https://docs.pingcap.com/tidb/stable/enable-tls-between-clients-and-servers#enable-authentication
If sslKey and sslCert are not provided, we will only authenticate the TiDB server from the MySQL client.
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.
I think valid configurations are:
sslCA
providedsslCA
,sslCert
,sslKey
all provided.Not valid:
sslCert
and/orsslKey
set, but nosslCA
.sslCert
set, but notsslKey
sslKey
set, but notsslCert
So I think we should set the
tlsName
to custom if any ofsslCert
,sslKey
orsslCA
is set and then later on verify if it is valid (which might already be done)