2016-09-15 06:41:57 +05:30
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2018-11-20 20:52:39 +05:30
|
|
|
"net"
|
2018-08-24 01:06:08 +05:30
|
|
|
"regexp"
|
2016-09-15 06:41:57 +05:30
|
|
|
"strconv"
|
2018-08-24 01:06:08 +05:30
|
|
|
"strings"
|
2018-11-21 16:05:25 +05:30
|
|
|
"time"
|
2016-09-15 06:41:57 +05:30
|
|
|
|
2017-02-22 04:30:22 +05:30
|
|
|
"github.com/lib/pq"
|
|
|
|
sqlite3 "github.com/mattn/go-sqlite3"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// SQLite3 options for creating an SQL db.
|
|
|
|
type SQLite3 struct {
|
|
|
|
// File to
|
2016-11-04 03:02:23 +05:30
|
|
|
File string `json:"file"`
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Open creates a new storage implementation backed by SQLite3
|
2019-02-22 17:49:23 +05:30
|
|
|
func (s *SQLite3) Open(logger log.Logger) (storage.Storage, error) {
|
2016-11-23 05:05:46 +05:30
|
|
|
conn, err := s.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
|
|
|
}
|
|
|
|
|
2019-02-22 17:49:23 +05:30
|
|
|
func (s *SQLite3) open(logger log.Logger) (*conn, error) {
|
2016-09-15 06:41:57 +05:30
|
|
|
db, err := sql.Open("sqlite3", s.File)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s.File == ":memory:" {
|
|
|
|
// sqlite3 uses file locks to coordinate concurrent access. In memory
|
|
|
|
// doesn't support this, so limit the number of connections to 1.
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
}
|
2017-02-22 04:30:22 +05:30
|
|
|
|
|
|
|
errCheck := func(err error) bool {
|
|
|
|
sqlErr, ok := err.(sqlite3.Error)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sqlErr.ExtendedCode == sqlite3.ErrConstraintPrimaryKey
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &conn{db, flavorSQLite3, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
sslDisable = "disable"
|
|
|
|
sslRequire = "require"
|
|
|
|
sslVerifyCA = "verify-ca"
|
|
|
|
sslVerifyFull = "verify-full"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PostgresSSL represents SSL options for Postgres databases.
|
|
|
|
type PostgresSSL struct {
|
|
|
|
Mode string
|
|
|
|
CAFile string
|
|
|
|
// Files for client auth.
|
|
|
|
KeyFile string
|
|
|
|
CertFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Postgres options for creating an SQL db.
|
|
|
|
type Postgres struct {
|
|
|
|
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
|
|
|
|
|
|
|
SSL PostgresSSL `json:"ssl" yaml:"ssl"`
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-08-24 01:06:08 +05:30
|
|
|
conn, err := p.open(logger, p.createDataSourceName())
|
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.
|
2018-08-24 01:06:08 +05:30
|
|
|
addParam("sslmode", dataSourceStr(sslVerifyFull))
|
|
|
|
} 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-02-22 17:49:23 +05:30
|
|
|
func (p *Postgres) open(logger log.Logger, dataSourceName string) (*conn, error) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|