2016-09-15 01:11:57 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2018-08-23 19:36:08 +00:00
|
|
|
"regexp"
|
2016-09-15 01:11:57 +00:00
|
|
|
"strconv"
|
2018-08-23 19:36:08 +00:00
|
|
|
"strings"
|
2016-09-15 01:11:57 +00:00
|
|
|
|
2017-02-21 23:00:22 +00:00
|
|
|
"github.com/lib/pq"
|
|
|
|
sqlite3 "github.com/mattn/go-sqlite3"
|
2017-07-25 20:45:17 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-03 06:44:44 +00:00
|
|
|
|
|
|
|
"github.com/dexidp/dex/storage"
|
2017-02-21 23:00:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// postgres error codes
|
|
|
|
pgErrUniqueViolation = "23505" // unique_violation
|
2016-09-15 01:11:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SQLite3 options for creating an SQL db.
|
|
|
|
type SQLite3 struct {
|
|
|
|
// File to
|
2016-11-03 21:32:23 +00:00
|
|
|
File string `json:"file"`
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open creates a new storage implementation backed by SQLite3
|
2016-11-22 23:35:46 +00:00
|
|
|
func (s *SQLite3) Open(logger logrus.FieldLogger) (storage.Storage, error) {
|
|
|
|
conn, err := s.open(logger)
|
2016-10-04 19:57:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-13 01:48:09 +00:00
|
|
|
return conn, nil
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:35:46 +00:00
|
|
|
func (s *SQLite3) open(logger logrus.FieldLogger) (*conn, error) {
|
2016-09-15 01:11:57 +00:00
|
|
|
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-21 23:00:22 +00:00
|
|
|
|
|
|
|
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 01:11:57 +00:00
|
|
|
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-23 19:36:08 +00:00
|
|
|
Port uint16
|
2016-09-15 01:11:57 +00:00
|
|
|
|
|
|
|
SSL PostgresSSL `json:"ssl" yaml:"ssl"`
|
|
|
|
|
|
|
|
ConnectionTimeout int // Seconds
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open creates a new storage implementation backed by Postgres.
|
2016-11-22 23:35:46 +00:00
|
|
|
func (p *Postgres) Open(logger logrus.FieldLogger) (storage.Storage, error) {
|
2018-08-23 19:36:08 +00:00
|
|
|
conn, err := p.open(logger, p.createDataSourceName())
|
2016-10-04 19:57:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-13 01:48:09 +00:00
|
|
|
return conn, nil
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:36:08 +00:00
|
|
|
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))
|
|
|
|
|
|
|
|
if p.Host != "" {
|
|
|
|
addParam("host", dataSourceStr(p.Host))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Port != 0 {
|
|
|
|
addParam("port", strconv.Itoa(int(p.Port)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.User != "" {
|
|
|
|
addParam("user", dataSourceStr(p.User))
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
2018-08-23 19:36:08 +00:00
|
|
|
|
|
|
|
if p.Password != "" {
|
|
|
|
addParam("password", dataSourceStr(p.Password))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Database != "" {
|
|
|
|
addParam("dbname", dataSourceStr(p.Database))
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:11:57 +00:00
|
|
|
if p.SSL.Mode == "" {
|
|
|
|
// Assume the strictest mode if unspecified.
|
2018-08-23 19:36:08 +00:00
|
|
|
addParam("sslmode", dataSourceStr(sslVerifyFull))
|
|
|
|
} else {
|
|
|
|
addParam("sslmode", dataSourceStr(p.SSL.Mode))
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:36:08 +00:00
|
|
|
if p.SSL.CAFile != "" {
|
|
|
|
addParam("sslrootcert", dataSourceStr(p.SSL.CAFile))
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:36:08 +00:00
|
|
|
if p.SSL.CertFile != "" {
|
|
|
|
addParam("sslcert", dataSourceStr(p.SSL.CertFile))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SSL.KeyFile != "" {
|
|
|
|
addParam("sslkey", dataSourceStr(p.SSL.KeyFile))
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
2018-08-23 19:36:08 +00:00
|
|
|
|
|
|
|
return strings.Join(parameters, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Postgres) open(logger logrus.FieldLogger, dataSourceName string) (*conn, error) {
|
|
|
|
db, err := sql.Open("postgres", dataSourceName)
|
2016-09-15 01:11:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-21 23:00:22 +00:00
|
|
|
|
|
|
|
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 01:11:57 +00:00
|
|
|
if _, err := c.migrate(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to perform migrations: %v", err)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|