8000 Add MySQL TLS support by fghanmi · Pull Request #261 · securesign/trillian · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add MySQL TLS support #261

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 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## HEAD

* Add TLS support for MySQL: https://github.com/google/trillian/pull/3593
* `--mysql_tls_ca`: users can provide a CA certificate, that is used to establish a secure communication with MySQL server.
* `--mysql_server_name`: users can provide the name of the MySQL server to be used as the Server Name in the TLS configuration.

## Notable Changes

* Updated go version 1.20 -> 1.21
Expand Down
46 changes: 41 additions & 5 deletions storage/mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@
package mysql

import (
"crypto/tls"
"crypto/x509"
"database/sql"
"errors"
"flag"
"os"
"sync"

"github.com/google/trillian/monitoring"
"github.com/google/trillian/storage"
"k8s.io/klog/v2"

// Load MySQL driver
_ "github.com/go-sql-driver/mysql"
"github.com/go-sql-driver/mysql"
)

var (
mySQLURI = flag.String("mysql_uri", "test:zaphod@tcp(127.0.0.1:3306)/test", "Connection URI for MySQL database")
maxConns = flag.Int("mysql_max_conns", 0, "Maximum connections to the database")
maxIdle = flag.Int("mysql_max_idle_conns", -1, "Maximum idle database connections in the connection pool")
mySQLURI = flag.String("mysql_uri", "test:zaphod@tcp(127.0.0.1:3306)/test", "Connection URI for MySQL database")
maxConns = flag.Int("mysql_max_conns", 0, "Maximum connections to the database")
maxIdle = flag.Int("mysql_max_idle_conns", -1, "Maximum idle database connections in the connection pool")
mySQLTLSCA = flag.String("mysql_tls_ca", "", "Path to the CA certificate file for MySQL TLS connection ")
mySQLServerName = flag.String("mysql_server_name", "", "Name of the MySQL server to be used as the Server Name in the TLS configuration")

mysqlMu sync.Mutex
mysqlErr error
Expand Down Expand Up @@ -81,7 +87,14 @@ func getMySQLDatabaseLocked() (*sql.DB, error) {
if mysqlDB != nil || mysqlErr != nil {
return mysqlDB, mysqlErr
}
db, err := OpenDB(*mySQLURI)
dsn := *mySQLURI
if *mySQLTLSCA != "" {
if err := registerMySQLTLSConfig(); err != nil {
return nil, err
}
dsn += "?tls=custom"
}
db, err := OpenDB(dsn)
if err != nil {
mysqlErr = err
return nil, err
Expand All @@ -107,3 +120,26 @@ func (s *mysqlProvider) AdminStorage() storage.AdminStorage {
func (s *mysqlProvider) Close() error {
return s.db.Close()
}

// registerMySQLTLSConfig registers a custom TLS config for MySQL using a provided CA certificate and optional server name.
// Returns an error if the CA certificate can't be read or added to the root cert pool, or when the registration of the TLS config fails.
func registerMySQLTLSConfig() error {
if *mySQLTLSCA == "" {
return nil
}
rootCertPool := x509.NewCertPool()
pem, err := os.ReadFile(*mySQLTLSCA)
if err != nil {
return err
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
return errors.New("failed to append PEM")
}
tlsConfig := &tls.Config{
RootCAs: rootCertPool,
}
if *mySQLServerName != "" {
tlsConfig.ServerName = *mySQLServerName
}
return mysql.RegisterTLSConfig("custom", tlsConfig)
}
Loading
0