2016-07-25 20:00:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-03 22:23:56 +00:00
|
|
|
"encoding/base64"
|
2016-11-03 21:32:23 +00:00
|
|
|
"encoding/json"
|
2016-07-25 20:00:28 +00:00
|
|
|
"fmt"
|
2016-11-04 04:08:50 +00:00
|
|
|
"os"
|
2018-07-27 19:51:30 +00:00
|
|
|
"strings"
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
2019-02-22 12:19:23 +00:00
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/server"
|
|
|
|
"github.com/dexidp/dex/storage"
|
|
|
|
"github.com/dexidp/dex/storage/etcd"
|
|
|
|
"github.com/dexidp/dex/storage/kubernetes"
|
|
|
|
"github.com/dexidp/dex/storage/memory"
|
|
|
|
"github.com/dexidp/dex/storage/sql"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the config format for the main application.
|
|
|
|
type Config struct {
|
2017-12-20 15:03:32 +00:00
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
Storage Storage `json:"storage"`
|
|
|
|
Web Web `json:"web"`
|
|
|
|
Telemetry Telemetry `json:"telemetry"`
|
|
|
|
OAuth2 OAuth2 `json:"oauth2"`
|
|
|
|
GRPC GRPC `json:"grpc"`
|
|
|
|
Expiry Expiry `json:"expiry"`
|
|
|
|
Logger Logger `json:"logger"`
|
2016-08-05 16:50:22 +00:00
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
Frontend server.WebConfig `json:"frontend"`
|
2016-08-25 20:10:19 +00:00
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
// StaticConnectors are user defined connectors specified in the ConfigMap
|
|
|
|
// Write operations, like updating a connector, will fail.
|
|
|
|
StaticConnectors []Connector `json:"connectors"`
|
|
|
|
|
2016-10-05 23:50:02 +00:00
|
|
|
// StaticClients cause the server to use this list of clients rather than
|
|
|
|
// querying the storage. Write operations, like creating a client, will fail.
|
2016-11-03 21:32:23 +00:00
|
|
|
StaticClients []storage.Client `json:"staticClients"`
|
2016-10-05 23:50:02 +00:00
|
|
|
|
|
|
|
// If enabled, the server will maintain a list of passwords which can be used
|
|
|
|
// to identify a user.
|
2016-11-03 21:32:23 +00:00
|
|
|
EnablePasswordDB bool `json:"enablePasswordDB"`
|
2016-10-05 23:50:02 +00:00
|
|
|
|
|
|
|
// StaticPasswords cause the server use this list of passwords rather than
|
|
|
|
// querying the storage. Cannot be specified without enabling a passwords
|
|
|
|
// database.
|
2016-11-03 22:23:56 +00:00
|
|
|
StaticPasswords []password `json:"staticPasswords"`
|
|
|
|
}
|
|
|
|
|
2018-07-27 19:51:30 +00:00
|
|
|
//Validate the configuration
|
|
|
|
func (c Config) Validate() error {
|
|
|
|
// Fast checks. Perform these first for a more responsive CLI.
|
|
|
|
checks := []struct {
|
|
|
|
bad bool
|
|
|
|
errMsg string
|
|
|
|
}{
|
|
|
|
{c.Issuer == "", "no issuer specified in config file"},
|
|
|
|
{!c.EnablePasswordDB && len(c.StaticPasswords) != 0, "cannot specify static passwords without enabling password db"},
|
|
|
|
{c.Storage.Config == nil, "no storage supplied in config file"},
|
|
|
|
{c.Web.HTTP == "" && c.Web.HTTPS == "", "must supply a HTTP/HTTPS address to listen on"},
|
|
|
|
{c.Web.HTTPS != "" && c.Web.TLSCert == "", "no cert specified for HTTPS"},
|
|
|
|
{c.Web.HTTPS != "" && c.Web.TLSKey == "", "no private key specified for HTTPS"},
|
|
|
|
{c.GRPC.TLSCert != "" && c.GRPC.Addr == "", "no address specified for gRPC"},
|
|
|
|
{c.GRPC.TLSKey != "" && c.GRPC.Addr == "", "no address specified for gRPC"},
|
|
|
|
{(c.GRPC.TLSCert == "") != (c.GRPC.TLSKey == ""), "must specific both a gRPC TLS cert and key"},
|
|
|
|
{c.GRPC.TLSCert == "" && c.GRPC.TLSClientCA != "", "cannot specify gRPC TLS client CA without a gRPC TLS cert"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkErrors []string
|
|
|
|
|
|
|
|
for _, check := range checks {
|
|
|
|
if check.bad {
|
|
|
|
checkErrors = append(checkErrors, check.errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(checkErrors) != 0 {
|
2019-04-29 14:33:10 +00:00
|
|
|
return fmt.Errorf("invalid Config:\n\t-\t%s", strings.Join(checkErrors, "\n\t-\t"))
|
2018-07-27 19:51:30 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
type password storage.Password
|
|
|
|
|
|
|
|
func (p *password) UnmarshalJSON(b []byte) error {
|
|
|
|
var data struct {
|
2019-12-14 00:33:21 +00:00
|
|
|
Email string `json:"email"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
UserID string `json:"userID"`
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
HashFromEnv string `json:"hashFromEnv"`
|
2016-11-03 22:23:56 +00:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*p = password(storage.Password{
|
|
|
|
Email: data.Email,
|
|
|
|
Username: data.Username,
|
|
|
|
UserID: data.UserID,
|
|
|
|
})
|
2019-12-14 00:52:10 +00:00
|
|
|
if len(data.Hash) == 0 && len(data.HashFromEnv) > 0 {
|
|
|
|
data.Hash = os.Getenv(data.HashFromEnv)
|
|
|
|
}
|
2016-11-03 22:23:56 +00:00
|
|
|
if len(data.Hash) == 0 {
|
2020-07-08 20:30:08 +00:00
|
|
|
return fmt.Errorf("no password hash provided")
|
2016-11-03 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this value is a valid bcrypt, use it.
|
|
|
|
_, bcryptErr := bcrypt.Cost([]byte(data.Hash))
|
|
|
|
if bcryptErr == nil {
|
|
|
|
p.Hash = []byte(data.Hash)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// For backwards compatibility try to base64 decode this value.
|
|
|
|
hashBytes, err := base64.StdEncoding.DecodeString(data.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("malformed bcrypt hash: %v", bcryptErr)
|
|
|
|
}
|
|
|
|
if _, err := bcrypt.Cost(hashBytes); err != nil {
|
|
|
|
return fmt.Errorf("malformed bcrypt hash: %v", err)
|
|
|
|
}
|
|
|
|
p.Hash = hashBytes
|
|
|
|
return nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 00:24:49 +00:00
|
|
|
// OAuth2 describes enabled OAuth2 extensions.
|
|
|
|
type OAuth2 struct {
|
2016-11-03 21:32:23 +00:00
|
|
|
ResponseTypes []string `json:"responseTypes"`
|
2016-10-07 18:53:01 +00:00
|
|
|
// If specified, do not prompt the user to approve client authorization. The
|
|
|
|
// act of logging in implies authorization.
|
2016-11-03 21:32:23 +00:00
|
|
|
SkipApprovalScreen bool `json:"skipApprovalScreen"`
|
2019-08-06 17:18:46 +00:00
|
|
|
// If specified, show the connector selection screen even if there's only one
|
|
|
|
AlwaysShowLoginScreen bool `json:"alwaysShowLoginScreen"`
|
2018-01-03 03:15:01 +00:00
|
|
|
// This is the connector that can be used for password grant
|
|
|
|
PasswordConnector string `json:"passwordConnector"`
|
2016-08-20 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// Web is the config format for the HTTP server.
|
|
|
|
type Web struct {
|
2017-01-14 09:18:48 +00:00
|
|
|
HTTP string `json:"http"`
|
|
|
|
HTTPS string `json:"https"`
|
|
|
|
TLSCert string `json:"tlsCert"`
|
|
|
|
TLSKey string `json:"tlsKey"`
|
|
|
|
AllowedOrigins []string `json:"allowedOrigins"`
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 15:03:32 +00:00
|
|
|
// Telemetry is the config format for telemetry including the HTTP server config.
|
|
|
|
type Telemetry struct {
|
|
|
|
HTTP string `json:"http"`
|
|
|
|
}
|
|
|
|
|
2016-10-04 07:27:50 +00:00
|
|
|
// GRPC is the config for the gRPC API.
|
|
|
|
type GRPC struct {
|
|
|
|
// The port to listen on.
|
2016-11-03 21:32:23 +00:00
|
|
|
Addr string `json:"addr"`
|
|
|
|
TLSCert string `json:"tlsCert"`
|
|
|
|
TLSKey string `json:"tlsKey"`
|
|
|
|
TLSClientCA string `json:"tlsClientCA"`
|
2019-08-06 20:56:09 +00:00
|
|
|
Reflection bool `json:"reflection"`
|
2016-10-04 07:27:50 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// Storage holds app's storage configuration.
|
|
|
|
type Storage struct {
|
2016-11-03 21:32:23 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Config StorageConfig `json:"config"`
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 21:32:23 +00:00
|
|
|
// StorageConfig is a configuration that can create a storage.
|
|
|
|
type StorageConfig interface {
|
2019-02-22 12:19:23 +00:00
|
|
|
Open(logger log.Logger) (storage.Storage, error)
|
2016-11-03 21:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var storages = map[string]func() StorageConfig{
|
2017-10-03 13:33:58 +00:00
|
|
|
"etcd": func() StorageConfig { return new(etcd.Etcd) },
|
2016-11-03 21:32:23 +00:00
|
|
|
"kubernetes": func() StorageConfig { return new(kubernetes.Config) },
|
|
|
|
"memory": func() StorageConfig { return new(memory.Config) },
|
|
|
|
"sqlite3": func() StorageConfig { return new(sql.SQLite3) },
|
|
|
|
"postgres": func() StorageConfig { return new(sql.Postgres) },
|
2017-04-21 15:51:55 +00:00
|
|
|
"mysql": func() StorageConfig { return new(sql.MySQL) },
|
2016-11-03 21:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON allows Storage to implement the unmarshaler interface to
|
|
|
|
// dynamically determine the type of the storage config.
|
|
|
|
func (s *Storage) UnmarshalJSON(b []byte) error {
|
|
|
|
var store struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Config json.RawMessage `json:"config"`
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-03 21:32:23 +00:00
|
|
|
if err := json.Unmarshal(b, &store); err != nil {
|
|
|
|
return fmt.Errorf("parse storage: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-03 21:32:23 +00:00
|
|
|
f, ok := storages[store.Type]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown storage type %q", store.Type)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 21:32:23 +00:00
|
|
|
storageConfig := f()
|
|
|
|
if len(store.Config) != 0 {
|
2016-11-04 04:08:50 +00:00
|
|
|
data := []byte(os.ExpandEnv(string(store.Config)))
|
|
|
|
if err := json.Unmarshal(data, storageConfig); err != nil {
|
2017-04-14 03:30:05 +00:00
|
|
|
return fmt.Errorf("parse storage config: %v", err)
|
2016-11-03 21:32:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*s = Storage{
|
|
|
|
Type: store.Type,
|
|
|
|
Config: storageConfig,
|
|
|
|
}
|
|
|
|
return nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connector is a magical type that can unmarshal YAML dynamically. The
|
|
|
|
// Type field determines the connector type, which is then customized for Config.
|
|
|
|
type Connector struct {
|
2016-11-03 21:32:23 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID string `json:"id"`
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
Config server.ConnectorConfig `json:"config"`
|
2016-11-03 21:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON allows Connector to implement the unmarshaler interface to
|
|
|
|
// dynamically determine the type of the connector config.
|
|
|
|
func (c *Connector) UnmarshalJSON(b []byte) error {
|
|
|
|
var conn struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
Config json.RawMessage `json:"config"`
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-03 21:32:23 +00:00
|
|
|
if err := json.Unmarshal(b, &conn); err != nil {
|
|
|
|
return fmt.Errorf("parse connector: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2017-04-17 22:41:41 +00:00
|
|
|
f, ok := server.ConnectorsConfig[conn.Type]
|
2016-11-03 21:32:23 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown connector type %q", conn.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
connConfig := f()
|
|
|
|
if len(conn.Config) != 0 {
|
2016-11-04 04:08:50 +00:00
|
|
|
data := []byte(os.ExpandEnv(string(conn.Config)))
|
|
|
|
if err := json.Unmarshal(data, connConfig); err != nil {
|
2016-11-03 21:32:23 +00:00
|
|
|
return fmt.Errorf("parse connector config: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-03 21:32:23 +00:00
|
|
|
*c = Connector{
|
|
|
|
Type: conn.Type,
|
2016-11-03 22:23:56 +00:00
|
|
|
Name: conn.Name,
|
2016-11-03 21:32:23 +00:00
|
|
|
ID: conn.ID,
|
|
|
|
Config: connConfig,
|
|
|
|
}
|
|
|
|
return nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-03 00:52:49 +00:00
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
// ToStorageConnector converts an object to storage connector type.
|
|
|
|
func ToStorageConnector(c Connector) (storage.Connector, error) {
|
|
|
|
data, err := json.Marshal(c.Config)
|
|
|
|
if err != nil {
|
|
|
|
return storage.Connector{}, fmt.Errorf("failed to marshal connector config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage.Connector{
|
|
|
|
ID: c.ID,
|
|
|
|
Type: c.Type,
|
|
|
|
Name: c.Name,
|
|
|
|
Config: data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 00:52:49 +00:00
|
|
|
// Expiry holds configuration for the validity period of components.
|
|
|
|
type Expiry struct {
|
|
|
|
// SigningKeys defines the duration of time after which the SigningKeys will be rotated.
|
|
|
|
SigningKeys string `json:"signingKeys"`
|
|
|
|
|
|
|
|
// IdTokens defines the duration of time for which the IdTokens will be valid.
|
|
|
|
IDTokens string `json:"idTokens"`
|
2018-12-13 10:40:58 +00:00
|
|
|
|
|
|
|
// AuthRequests defines the duration of time for which the AuthRequests will be valid.
|
|
|
|
AuthRequests string `json:"authRequests"`
|
2020-01-28 19:14:30 +00:00
|
|
|
|
|
|
|
// DeviceRequests defines the duration of time for which the DeviceRequests will be valid.
|
|
|
|
DeviceRequests string `json:"deviceRequests"`
|
2016-11-03 00:52:49 +00:00
|
|
|
}
|
2016-11-22 23:35:46 +00:00
|
|
|
|
|
|
|
// Logger holds configuration required to customize logging for dex.
|
|
|
|
type Logger struct {
|
|
|
|
// Level sets logging level severity.
|
|
|
|
Level string `json:"level"`
|
|
|
|
|
|
|
|
// Format specifies the format to be used for logging.
|
|
|
|
Format string `json:"format"`
|
|
|
|
}
|