Skip to content

Commit

Permalink
PMM-12880 Proper skip for MySQL TLS certs. (#2909)
Browse files Browse the repository at this point in the history
* PMM-12880 Proper skip for TLS certs.

* PMM-12880 Lint.

* PMM-12880 Add comment.

* PMM-12880 Another comment.
  • Loading branch information
JiriCtvrtka authored May 29, 2024
1 parent e4bf62a commit 1ea2627
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion agent/agents/mysql/perfschema/perfschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func getPerfschemaHistorySize(q reform.Querier, l *logrus.Entry) uint {
// New creates new PerfSchema QAN service.
func New(params *Params, l *logrus.Entry) (*PerfSchema, error) {
if params.TextFiles != nil {
err := tlshelpers.RegisterMySQLCerts(params.TextFiles.Files)
err := tlshelpers.RegisterMySQLCerts(params.TextFiles.Files, params.TLSSkipVerify)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/agents/mysql/slowlog/slowlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type slowLogInfo struct {
// New creates new SlowLog QAN service.
func New(params *Params, l *logrus.Entry) (*SlowLog, error) {
if params.TextFiles != nil {
err := tlshelpers.RegisterMySQLCerts(params.TextFiles.Files)
err := tlshelpers.RegisterMySQLCerts(params.TextFiles.Files, params.TLSSkipVerify)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions agent/connectionchecker/connection_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func (cc *ConnectionChecker) sqlPing(ctx context.Context, db *sql.DB) error {
return err
}

func (cc *ConnectionChecker) checkMySQLConnection(ctx context.Context, dsn string, files *agentpb.TextFiles, tlsSkipVerify bool, id uint32) *agentpb.CheckConnectionResponse { //nolint:lll,unparam,revive
func (cc *ConnectionChecker) checkMySQLConnection(ctx context.Context, dsn string, files *agentpb.TextFiles, tlsSkipVerify bool, id uint32) *agentpb.CheckConnectionResponse { //nolint:lll
var res agentpb.CheckConnectionResponse
var err error

if files != nil {
err = tlshelpers.RegisterMySQLCerts(files.Files)
err = tlshelpers.RegisterMySQLCerts(files.Files, tlsSkipVerify)
if err != nil {
cc.l.Debugf("checkMySQLConnection: failed to register cert: %s", err)
res.Error = err.Error()
Expand Down
4 changes: 2 additions & 2 deletions agent/runner/actions/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func jsonRows(columns []string, dataRows [][]interface{}) ([]byte, error) {
}

// mysqlOpen returns *sql.DB for given MySQL DSN.
func mysqlOpen(dsn string, tlsFiles *agentpb.TextFiles) (*sql.DB, error) {
func mysqlOpen(dsn string, tlsFiles *agentpb.TextFiles, tlsSkipVerify bool) (*sql.DB, error) {
if tlsFiles != nil {
err := tlshelpers.RegisterMySQLCerts(tlsFiles.Files)
err := tlshelpers.RegisterMySQLCerts(tlsFiles.Files, tlsSkipVerify)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/runner/actions/mysql_explain_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (a *mysqlExplainAction) Run(ctx context.Context) ([]byte, error) {
// query has a copy of the original params.Query field if the query is a SELECT or the equivalent
// SELECT after converting DML queries.
query, changedToSelect := dmlToSelect(a.params.Query)
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles)
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles, a.params.TlsSkipVerify)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/runner/actions/mysql_query_select_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (a *mysqlQuerySelectAction) DSN() string {

// Run runs an Action and returns output and error.
func (a *mysqlQuerySelectAction) Run(ctx context.Context) ([]byte, error) {
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles)
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles, a.params.TlsSkipVerify)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/runner/actions/mysql_query_show_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (a *mysqlQueryShowAction) DSN() string {

// Run runs an Action and returns output and error.
func (a *mysqlQueryShowAction) Run(ctx context.Context) ([]byte, error) {
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles)
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles, a.params.TlsSkipVerify)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/runner/actions/mysql_show_create_table_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (a *mysqlShowCreateTableAction) DSN() string {

// Run runs an Action and returns output and error.
func (a *mysqlShowCreateTableAction) Run(ctx context.Context) ([]byte, error) {
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles)
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles, a.params.TlsSkipVerify)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/runner/actions/mysql_show_index_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (a *mysqlShowIndexAction) DSN() string {

// Run runs an Action and returns output and error.
func (a *mysqlShowIndexAction) Run(ctx context.Context) ([]byte, error) {
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles)
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles, a.params.TlsSkipVerify)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion agent/runner/actions/mysql_show_table_status_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (a *mysqlShowTableStatusAction) DSN() string {

// Run runs an Action and returns output and error.
func (a *mysqlShowTableStatusAction) Run(ctx context.Context) ([]byte, error) {
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles)
db, err := mysqlOpen(a.params.Dsn, a.params.TlsFiles, a.params.TlsSkipVerify)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions agent/serviceinfobroker/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (sib *ServiceInfoBroker) GetInfoFromService(ctx context.Context, msg *agent

switch msg.Type {
case inventorypb.ServiceType_MYSQL_SERVICE:
return sib.getMySQLInfo(ctx, msg.Dsn, msg.TextFiles, id)
return sib.getMySQLInfo(ctx, msg.Dsn, msg.TextFiles, msg.TlsSkipVerify, id)
case inventorypb.ServiceType_MONGODB_SERVICE:
return sib.getMongoDBInfo(ctx, msg.Dsn, msg.TextFiles, id)
case inventorypb.ServiceType_POSTGRESQL_SERVICE:
Expand All @@ -84,12 +84,12 @@ func (sib *ServiceInfoBroker) GetInfoFromService(ctx context.Context, msg *agent
}
}

func (sib *ServiceInfoBroker) getMySQLInfo(ctx context.Context, dsn string, files *agentpb.TextFiles, id uint32) *agentpb.ServiceInfoResponse {
func (sib *ServiceInfoBroker) getMySQLInfo(ctx context.Context, dsn string, files *agentpb.TextFiles, tlsSkipVerify bool, id uint32) *agentpb.ServiceInfoResponse {
var res agentpb.ServiceInfoResponse
var err error

if files != nil {
err = tlshelpers.RegisterMySQLCerts(files.Files)
err = tlshelpers.RegisterMySQLCerts(files.Files, tlsSkipVerify)
if err != nil {
sib.l.Debugf("getMySQLInfo: failed to register cert: %s", err)
res.Error = err.Error()
Expand Down
9 changes: 5 additions & 4 deletions agent/tlshelpers/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// RegisterMySQLCerts is used for register TLS config before sql.Open is called.
func RegisterMySQLCerts(files map[string]string) error {
func RegisterMySQLCerts(files map[string]string, tlsSkipVerify bool) error {
if files == nil {
return nil
}
Expand All @@ -36,9 +36,10 @@ func RegisterMySQLCerts(files map[string]string) error {
}

if ok := ca.AppendCertsFromPEM([]byte(files["tlsCa"])); ok {
err = mysql.RegisterTLSConfig("custom", &tls.Config{ //nolint:gosec
RootCAs: ca,
Certificates: []tls.Certificate{cert},
err = mysql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: ca,
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: tlsSkipVerify, // #nosec G402
})
if err != nil {
return errors.Wrap(err, "register MySQL CA cert failed")
Expand Down
12 changes: 8 additions & 4 deletions managed/models/agent_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,13 @@ func (s *Agent) DSN(service *Service, dsnParams DSNParams, tdp *DelimiterPair) s
cfg.DBName = dsnParams.Database
cfg.Params = make(map[string]string)
if s.TLS {
// It is mandatory to have "custom" as the first case.
// Skip verify for "custom" is handled on pmm-agent side.
switch {
case s.TLSSkipVerify:
cfg.Params["tls"] = skipVerify
case len(s.Files()) != 0:
cfg.Params["tls"] = "custom"
case s.TLSSkipVerify:
cfg.Params["tls"] = skipVerify
default:
cfg.Params["tls"] = trueStr
}
Expand All @@ -368,11 +370,13 @@ func (s *Agent) DSN(service *Service, dsnParams DSNParams, tdp *DelimiterPair) s
cfg.DBName = dsnParams.Database
cfg.Params = make(map[string]string)
if s.TLS {
// It is mandatory to have "custom" as the first case.
// Skip verify for "custom" is handled on pmm-agent side.
switch {
case s.TLSSkipVerify:
cfg.Params["tls"] = "skip-verify"
case len(s.Files()) != 0:
cfg.Params["tls"] = "custom"
case s.TLSSkipVerify:
cfg.Params["tls"] = skipVerify
default:
cfg.Params["tls"] = trueStr
}
Expand Down

0 comments on commit 1ea2627

Please sign in to comment.