2016-09-15 06:41:57 +05:30
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
2017-04-21 21:21:55 +05:30
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2016-09-15 06:41:57 +05:30
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2019-07-12 19:59:46 +05:30
|
|
|
"net"
|
2021-09-17 11:42:39 +05:30
|
|
|
"os"
|
2019-07-12 19:59:46 +05:30
|
|
|
"regexp"
|
2016-09-15 06:41:57 +05:30
|
|
|
"strconv"
|
2019-07-12 19:59:46 +05:30
|
|
|
"strings"
|
2018-11-21 16:05:25 +05:30
|
|
|
"time"
|
2016-09-15 06:41:57 +05:30
|
|
|
|
2017-04-21 21:21:55 +05:30
|
|
|
"github.com/go-sql-driver/mysql"
|
2017-02-22 04:30:22 +05:30
|
|
|
"github.com/lib/pq"
|
2018-09-03 12:14:44 +05:30
|
|
|
|
2019-02-22 17:49:23 +05:30
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2018-09-03 12:14:44 +05:30
|
|
|
"github.com/dexidp/dex/storage"
|
2017-02-22 04:30:22 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// postgres error codes
|
|
|
|
pgErrUniqueViolation = "23505" // unique_violation
|
2016-09-15 06:41:57 +05:30
|
|
|
)
|
|
|
|
|
2017-04-21 21:21:55 +05:30
|
|
|
const (
|
|
|
|
// MySQL error codes
|
|
|
|
mysqlErrDupEntry = 1062
|
|
|
|
mysqlErrDupEntryWithKeyName = 1586
|
2019-09-23 13:06:01 +05:30
|
|
|
mysqlErrUnknownSysVar = 1193
|
2017-04-21 21:21:55 +05:30
|
|
|
)
|
|
|
|
|
2019-07-30 14:38:57 +05:30
|
|
|
// nolint
|
2016-09-15 06:41:57 +05:30
|
|
|
const (
|
2017-04-21 21:21:55 +05:30
|
|
|
// postgres SSL modes
|
|
|
|
pgSSLDisable = "disable"
|
|
|
|
pgSSLRequire = "require"
|
|
|
|
pgSSLVerifyCA = "verify-ca"
|
|
|
|
pgSSLVerifyFull = "verify-full"
|
2016-09-15 06:41:57 +05:30
|
|
|
)
|
|
|
|
|
2019-07-30 14:38:57 +05:30
|
|
|
// nolint
|
2017-04-21 21:21:55 +05:30
|
|
|
const (
|
|
|
|
// MySQL SSL modes
|
|
|
|
mysqlSSLTrue = "true"
|
|
|
|
mysqlSSLFalse = "false"
|
|
|
|
mysqlSSLSkipVerify = "skip-verify"
|
|
|
|
mysqlSSLCustom = "custom"
|
|
|
|
)
|
2016-09-15 06:41:57 +05:30
|
|
|
|
2017-04-21 21:21:55 +05:30
|
|
|
// NetworkDB contains options common to SQL databases accessed over network.
|
|
|
|
type NetworkDB struct {
|
2016-09-15 06:41:57 +05:30
|
|
|
Database string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Host string
|
2018-08-24 01:06:08 +05:30
|
|
|
Port uint16
|
2016-09-15 06:41:57 +05:30
|
|
|
|
|
|
|
ConnectionTimeout int // Seconds
|
2018-11-21 16:05:25 +05:30
|
|
|
|
|
|
|
// database/sql tunables, see
|
|
|
|
// https://golang.org/pkg/database/sql/#DB.SetConnMaxLifetime and below
|
|
|
|
// Note: defaults will be set if these are 0
|
|
|
|
MaxOpenConns int // default: 5
|
|
|
|
MaxIdleConns int // default: 5
|
|
|
|
ConnMaxLifetime int // Seconds, default: not set
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
|
|
|
|
2017-04-21 21:21:55 +05:30
|
|
|
// SSL represents SSL options for network databases.
|
|
|
|
type SSL struct {
|
|
|
|
Mode string
|
|
|
|
CAFile string
|
|
|
|
// Files for client auth.
|
|
|
|
KeyFile string
|
|
|
|
CertFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Postgres options for creating an SQL db.
|
|
|
|
type Postgres struct {
|
|
|
|
NetworkDB
|
|
|
|
|
|
|
|
SSL SSL `json:"ssl" yaml:"ssl"`
|
|
|
|
}
|
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
// Open creates a new storage implementation backed by Postgres.
|
2019-02-22 17:49:23 +05:30
|
|
|
func (p *Postgres) Open(logger log.Logger) (storage.Storage, error) {
|
2019-07-12 19:59:46 +05:30
|
|
|
conn, err := p.open(logger)
|
2016-10-05 01:27:21 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-13 07:18:09 +05:30
|
|
|
return conn, nil
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
|
|
|
|
2018-08-24 01:06:08 +05:30
|
|
|
var strEsc = regexp.MustCompile(`([\\'])`)
|
|
|
|
|
|
|
|
func dataSourceStr(str string) string {
|
|
|
|
return "'" + strEsc.ReplaceAllString(str, `\$1`) + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
// createDataSourceName takes the configuration provided via the Postgres
|
|
|
|
// struct to create a data-source name that Go's database/sql package can
|
|
|
|
// make use of.
|
|
|
|
func (p *Postgres) createDataSourceName() string {
|
|
|
|
parameters := []string{}
|
|
|
|
|
|
|
|
addParam := func(key, val string) {
|
|
|
|
parameters = append(parameters, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
|
|
|
|
|
|
|
addParam("connect_timeout", strconv.Itoa(p.ConnectionTimeout))
|
|
|
|
|
2018-11-20 20:52:39 +05:30
|
|
|
// detect host:port for backwards-compatibility
|
|
|
|
host, port, err := net.SplitHostPort(p.Host)
|
|
|
|
if err != nil {
|
|
|
|
// not host:port, probably unix socket or bare address
|
|
|
|
|
|
|
|
host = p.Host
|
|
|
|
|
|
|
|
if p.Port != 0 {
|
|
|
|
port = strconv.Itoa(int(p.Port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if host != "" {
|
|
|
|
addParam("host", dataSourceStr(host))
|
2018-08-24 01:06:08 +05:30
|
|
|
}
|
|
|
|
|
2018-11-20 20:52:39 +05:30
|
|
|
if port != "" {
|
|
|
|
addParam("port", port)
|
2018-08-24 01:06:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if p.User != "" {
|
|
|
|
addParam("user", dataSourceStr(p.User))
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
2018-08-24 01:06:08 +05:30
|
|
|
|
|
|
|
if p.Password != "" {
|
|
|
|
addParam("password", dataSourceStr(p.Password))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Database != "" {
|
|
|
|
addParam("dbname", dataSourceStr(p.Database))
|
|
|
|
}
|
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
if p.SSL.Mode == "" {
|
|
|
|
// Assume the strictest mode if unspecified.
|
2019-07-12 19:59:46 +05:30
|
|
|
addParam("sslmode", dataSourceStr(pgSSLVerifyFull))
|
2018-08-24 01:06:08 +05:30
|
|
|
} else {
|
|
|
|
addParam("sslmode", dataSourceStr(p.SSL.Mode))
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
|
|
|
|
2018-08-24 01:06:08 +05:30
|
|
|
if p.SSL.CAFile != "" {
|
|
|
|
addParam("sslrootcert", dataSourceStr(p.SSL.CAFile))
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
|
|
|
|
2018-08-24 01:06:08 +05:30
|
|
|
if p.SSL.CertFile != "" {
|
|
|
|
addParam("sslcert", dataSourceStr(p.SSL.CertFile))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SSL.KeyFile != "" {
|
|
|
|
addParam("sslkey", dataSourceStr(p.SSL.KeyFile))
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
2018-08-24 01:06:08 +05:30
|
|
|
|
|
|
|
return strings.Join(parameters, " ")
|
|
|
|
}
|
|
|
|
|
2019-07-12 19:59:46 +05:30
|
|
|
func (p *Postgres) open(logger log.Logger) (*conn, error) {
|
|
|
|
dataSourceName := p.createDataSourceName()
|
|
|
|
|
2018-08-24 01:06:08 +05:30
|
|
|
db, err := sql.Open("postgres", dataSourceName)
|
2016-09-15 06:41:57 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-22 04:30:22 +05:30
|
|
|
|
2018-11-21 16:05:25 +05:30
|
|
|
// set database/sql tunables if configured
|
|
|
|
if p.ConnMaxLifetime != 0 {
|
|
|
|
db.SetConnMaxLifetime(time.Duration(p.ConnMaxLifetime) * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.MaxIdleConns == 0 {
|
|
|
|
db.SetMaxIdleConns(5)
|
|
|
|
} else {
|
|
|
|
db.SetMaxIdleConns(p.MaxIdleConns)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.MaxOpenConns == 0 {
|
|
|
|
db.SetMaxOpenConns(5)
|
|
|
|
} else {
|
|
|
|
db.SetMaxOpenConns(p.MaxOpenConns)
|
|
|
|
}
|
|
|
|
|
2017-02-22 04:30:22 +05:30
|
|
|
errCheck := func(err error) bool {
|
|
|
|
sqlErr, ok := err.(*pq.Error)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sqlErr.Code == pgErrUniqueViolation
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:43:38 +05:30
|
|
|
c := &conn{db, &flavorPostgres, logger, errCheck}
|
2016-09-15 06:41:57 +05:30
|
|
|
if _, err := c.migrate(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to perform migrations: %v", err)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
2017-04-21 21:21:55 +05:30
|
|
|
|
|
|
|
// MySQL options for creating a MySQL db.
|
|
|
|
type MySQL struct {
|
|
|
|
NetworkDB
|
|
|
|
|
|
|
|
SSL SSL `json:"ssl" yaml:"ssl"`
|
|
|
|
|
|
|
|
// TODO(pborzenkov): used by tests to reduce lock wait timeout. Should
|
|
|
|
// we make it exported and allow users to provide arbitrary params?
|
|
|
|
params map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open creates a new storage implementation backed by MySQL.
|
2019-07-12 19:59:46 +05:30
|
|
|
func (s *MySQL) Open(logger log.Logger) (storage.Storage, error) {
|
2017-04-21 21:21:55 +05:30
|
|
|
conn, err := s.open(logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 19:59:46 +05:30
|
|
|
func (s *MySQL) open(logger log.Logger) (*conn, error) {
|
2017-04-21 21:21:55 +05:30
|
|
|
cfg := mysql.Config{
|
2019-07-12 19:59:46 +05:30
|
|
|
User: s.User,
|
|
|
|
Passwd: s.Password,
|
|
|
|
DBName: s.Database,
|
|
|
|
AllowNativePasswords: true,
|
2017-04-21 21:21:55 +05:30
|
|
|
|
|
|
|
Timeout: time.Second * time.Duration(s.ConnectionTimeout),
|
|
|
|
|
|
|
|
ParseTime: true,
|
|
|
|
Params: map[string]string{
|
2019-07-12 19:59:46 +05:30
|
|
|
"transaction_isolation": "'SERIALIZABLE'",
|
2017-04-21 21:21:55 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
if s.Host != "" {
|
|
|
|
if s.Host[0] != '/' {
|
|
|
|
cfg.Net = "tcp"
|
|
|
|
cfg.Addr = s.Host
|
2021-04-26 14:39:52 +05:30
|
|
|
|
|
|
|
if s.Port != 0 {
|
|
|
|
cfg.Addr = net.JoinHostPort(s.Host, strconv.Itoa(int(s.Port)))
|
|
|
|
}
|
2017-04-21 21:21:55 +05:30
|
|
|
} else {
|
|
|
|
cfg.Net = "unix"
|
|
|
|
cfg.Addr = s.Host
|
|
|
|
}
|
|
|
|
}
|
2020-10-18 03:24:27 +05:30
|
|
|
|
|
|
|
switch {
|
|
|
|
case s.SSL.CAFile != "" || s.SSL.CertFile != "" || s.SSL.KeyFile != "":
|
2017-04-21 21:21:55 +05:30
|
|
|
if err := s.makeTLSConfig(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to make TLS config: %v", err)
|
|
|
|
}
|
|
|
|
cfg.TLSConfig = mysqlSSLCustom
|
2020-10-18 03:24:27 +05:30
|
|
|
case s.SSL.Mode == "":
|
2019-07-12 19:59:46 +05:30
|
|
|
cfg.TLSConfig = mysqlSSLTrue
|
2020-10-18 03:24:27 +05:30
|
|
|
default:
|
2017-04-21 21:21:55 +05:30
|
|
|
cfg.TLSConfig = s.SSL.Mode
|
|
|
|
}
|
2020-10-18 03:24:27 +05:30
|
|
|
|
2017-04-21 21:21:55 +05:30
|
|
|
for k, v := range s.params {
|
|
|
|
cfg.Params[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := sql.Open("mysql", cfg.FormatDSN())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-27 23:38:17 +05:30
|
|
|
if s.MaxIdleConns == 0 {
|
|
|
|
/*Override default behaviour to fix https://github.com/dexidp/dex/issues/1608*/
|
|
|
|
db.SetMaxIdleConns(0)
|
|
|
|
} else {
|
|
|
|
db.SetMaxIdleConns(s.MaxIdleConns)
|
|
|
|
}
|
|
|
|
|
2019-09-23 13:06:01 +05:30
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == mysqlErrUnknownSysVar {
|
2019-12-18 20:21:44 +05:30
|
|
|
logger.Info("reconnecting with MySQL pre-5.7.20 compatibility mode")
|
2019-09-23 13:06:01 +05:30
|
|
|
|
|
|
|
// MySQL 5.7.20 introduced transaction_isolation and deprecated tx_isolation.
|
|
|
|
// MySQL 8.0 doesn't have tx_isolation at all.
|
|
|
|
// https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_transaction_isolation
|
|
|
|
delete(cfg.Params, "transaction_isolation")
|
|
|
|
cfg.Params["tx_isolation"] = "'SERIALIZABLE'"
|
|
|
|
|
|
|
|
db, err = sql.Open("mysql", cfg.FormatDSN())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 21:21:55 +05:30
|
|
|
errCheck := func(err error) bool {
|
|
|
|
sqlErr, ok := err.(*mysql.MySQLError)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sqlErr.Number == mysqlErrDupEntry ||
|
|
|
|
sqlErr.Number == mysqlErrDupEntryWithKeyName
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:43:38 +05:30
|
|
|
c := &conn{db, &flavorMySQL, logger, errCheck}
|
2017-04-21 21:21:55 +05:30
|
|
|
if _, err := c.migrate(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to perform migrations: %v", err)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySQL) makeTLSConfig() error {
|
|
|
|
cfg := &tls.Config{}
|
|
|
|
if s.SSL.CAFile != "" {
|
|
|
|
rootCertPool := x509.NewCertPool()
|
2021-09-17 11:42:39 +05:30
|
|
|
pem, err := os.ReadFile(s.SSL.CAFile)
|
2017-04-21 21:21:55 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
|
|
|
|
return fmt.Errorf("failed to append PEM")
|
|
|
|
}
|
|
|
|
cfg.RootCAs = rootCertPool
|
|
|
|
}
|
|
|
|
if s.SSL.CertFile != "" && s.SSL.KeyFile != "" {
|
|
|
|
clientCert := make([]tls.Certificate, 0, 1)
|
|
|
|
certs, err := tls.LoadX509KeyPair(s.SSL.CertFile, s.SSL.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clientCert = append(clientCert, certs)
|
|
|
|
cfg.Certificates = clientCert
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql.RegisterTLSConfig(mysqlSSLCustom, cfg)
|
|
|
|
return nil
|
|
|
|
}
|