(connCfg db.ConnectionConfig)
| 113 | } |
| 114 | |
| 115 | func (d *Driver) getMySQLConnection(connCfg db.ConnectionConfig) (string, error) { |
| 116 | protocol := "tcp" |
| 117 | if strings.HasPrefix(connCfg.DataSource.Host, "/") { |
| 118 | protocol = "unix" |
| 119 | } |
| 120 | |
| 121 | params := []string{"multiStatements=true", "maxAllowedPacket=0"} |
| 122 | if err := validateMySQLExtraConnectionParameters(connCfg.DataSource.GetExtraConnectionParameters()); err != nil { |
| 123 | return "", err |
| 124 | } |
| 125 | for key, value := range connCfg.DataSource.GetExtraConnectionParameters() { |
| 126 | params = append(params, fmt.Sprintf("%s=%s", key, value)) |
| 127 | } |
| 128 | if connCfg.DataSource.GetSshHost() != "" { |
| 129 | sshClient, err := util.GetSSHClient(connCfg.DataSource) |
| 130 | if err != nil { |
| 131 | return "", err |
| 132 | } |
| 133 | d.sshClient = sshClient |
| 134 | protocol = "mysql-tcp-" + uuid.NewString()[:8] |
| 135 | // Now we register the dialer with the ssh connection as a parameter. |
| 136 | mysql.RegisterDialContext(protocol, func(_ context.Context, addr string) (net.Conn, error) { |
| 137 | return sshClient.Dial("tcp", addr) |
| 138 | }) |
| 139 | } |
| 140 | |
| 141 | tlscfg, err := util.GetTLSConfig(connCfg.DataSource) |
| 142 | if err != nil { |
| 143 | return "", errors.Wrap(err, "sql: tls config error") |
| 144 | } |
| 145 | tlsKey := uuid.NewString() |
| 146 | if tlscfg != nil { |
| 147 | if err := mysql.RegisterTLSConfig(tlsKey, tlscfg); err != nil { |
| 148 | return "", errors.Wrap(err, "sql: failed to register tls config") |
| 149 | } |
| 150 | // TLS config is only used during sql.Open, so should be safe to deregister afterwards. |
| 151 | d.openCleanUp = append(d.openCleanUp, func() { mysql.DeregisterTLSConfig(tlsKey) }) |
| 152 | params = append(params, fmt.Sprintf("tls=%s", tlsKey)) |
| 153 | } |
| 154 | return fmt.Sprintf("%s:%s@%s(%s:%s)/%s?%s", connCfg.DataSource.Username, connCfg.Password, protocol, connCfg.DataSource.Host, connCfg.DataSource.Port, connCfg.ConnectionContext.DatabaseName, strings.Join(params, "&")), nil |
| 155 | } |
| 156 | |
| 157 | // getRDSCertPool downloads and returns the RDS CA certificate pool. |
| 158 | // AWS RDS connection with IAM require TLS connection. |
no test coverage detected