2016-08-05 16:50:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-14 00:33:21 +00:00
|
|
|
"os"
|
2016-08-05 16:50:22 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2016-08-05 16:50:22 +00:00
|
|
|
"github.com/kylelemons/godebug/pretty"
|
2018-09-03 06:44:44 +00:00
|
|
|
|
|
|
|
"github.com/dexidp/dex/connector/mock"
|
|
|
|
"github.com/dexidp/dex/connector/oidc"
|
2019-12-18 14:53:34 +00:00
|
|
|
"github.com/dexidp/dex/server"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/storage"
|
|
|
|
"github.com/dexidp/dex/storage/sql"
|
2016-08-05 16:50:22 +00:00
|
|
|
)
|
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
var _ = yaml.YAMLToJSON
|
|
|
|
|
2018-07-27 19:51:30 +00:00
|
|
|
func TestValidConfiguration(t *testing.T) {
|
|
|
|
configuration := Config{
|
|
|
|
Issuer: "http://127.0.0.1:5556/dex",
|
|
|
|
Storage: Storage{
|
|
|
|
Type: "sqlite3",
|
|
|
|
Config: &sql.SQLite3{
|
|
|
|
File: "examples/dex.db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Web: Web{
|
|
|
|
HTTP: "127.0.0.1:5556",
|
|
|
|
},
|
|
|
|
StaticConnectors: []Connector{
|
|
|
|
{
|
|
|
|
Type: "mockCallback",
|
|
|
|
ID: "mock",
|
|
|
|
Name: "Example",
|
|
|
|
Config: &mock.CallbackConfig{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := configuration.Validate(); err != nil {
|
|
|
|
t.Fatalf("this configuration should have been valid: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidConfiguration(t *testing.T) {
|
|
|
|
configuration := Config{}
|
|
|
|
err := configuration.Validate()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("this configuration should be invalid")
|
|
|
|
}
|
|
|
|
got := err.Error()
|
2019-04-29 14:33:10 +00:00
|
|
|
wanted := `invalid Config:
|
2018-07-27 19:51:30 +00:00
|
|
|
- no issuer specified in config file
|
|
|
|
- no storage supplied in config file
|
|
|
|
- must supply a HTTP/HTTPS address to listen on`
|
|
|
|
if got != wanted {
|
|
|
|
t.Fatalf("Expected error message to be %q, got %q", wanted, got)
|
|
|
|
}
|
|
|
|
}
|
2020-11-03 19:50:09 +00:00
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
func TestUnmarshalConfig(t *testing.T) {
|
|
|
|
rawConfig := []byte(`
|
|
|
|
issuer: http://127.0.0.1:5556/dex
|
|
|
|
storage:
|
2018-11-21 10:35:25 +00:00
|
|
|
type: postgres
|
2016-11-03 22:23:56 +00:00
|
|
|
config:
|
2018-11-21 10:35:25 +00:00
|
|
|
host: 10.0.0.1
|
|
|
|
port: 65432
|
|
|
|
maxOpenConns: 5
|
|
|
|
maxIdleConns: 3
|
|
|
|
connMaxLifetime: 30
|
|
|
|
connectionTimeout: 3
|
2016-11-03 22:23:56 +00:00
|
|
|
web:
|
|
|
|
http: 127.0.0.1:5556
|
2019-08-06 17:14:53 +00:00
|
|
|
|
|
|
|
frontend:
|
|
|
|
dir: ./web
|
|
|
|
extra:
|
|
|
|
foo: bar
|
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
staticClients:
|
2016-08-05 16:50:22 +00:00
|
|
|
- id: example-app
|
|
|
|
redirectURIs:
|
|
|
|
- 'http://127.0.0.1:5555/callback'
|
|
|
|
name: 'Example App'
|
|
|
|
secret: ZXhhbXBsZS1hcHAtc2VjcmV0
|
|
|
|
|
2019-08-06 17:18:46 +00:00
|
|
|
oauth2:
|
|
|
|
alwaysShowLoginScreen: true
|
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
connectors:
|
|
|
|
- type: mockCallback
|
|
|
|
id: mock
|
|
|
|
name: Example
|
|
|
|
- type: oidc
|
|
|
|
id: google
|
|
|
|
name: Google
|
|
|
|
config:
|
|
|
|
issuer: https://accounts.google.com
|
2016-11-04 04:08:50 +00:00
|
|
|
clientID: foo
|
|
|
|
clientSecret: bar
|
2016-11-03 22:23:56 +00:00
|
|
|
redirectURI: http://127.0.0.1:5556/dex/callback/google
|
|
|
|
|
|
|
|
enablePasswordDB: true
|
|
|
|
staticPasswords:
|
|
|
|
- email: "admin@example.com"
|
|
|
|
# bcrypt hash of the string "password"
|
|
|
|
hash: "$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy"
|
|
|
|
username: "admin"
|
|
|
|
userID: "08a8684b-db88-4b73-90a9-3cd1661f5466"
|
2020-12-29 12:54:02 +00:00
|
|
|
- email: "foo@example.com"
|
2016-11-03 22:23:56 +00:00
|
|
|
# base64'd value of the same bcrypt hash above. We want to be able to parse both of these
|
|
|
|
hash: "JDJhJDEwJDMzRU1UMGNWWVZsUHk2V0FNQ0xzY2VMWWpXaHVIcGJ6NXl1Wnh1L0dBRmowM0o5THl0anV5"
|
|
|
|
username: "foo"
|
|
|
|
userID: "41331323-6f44-45e6-b3b9-2c4b60c02be5"
|
2016-11-03 00:52:49 +00:00
|
|
|
|
|
|
|
expiry:
|
2018-12-13 13:47:51 +00:00
|
|
|
signingKeys: "7h"
|
|
|
|
idTokens: "25h"
|
|
|
|
authRequests: "25h"
|
2020-01-28 19:14:30 +00:00
|
|
|
deviceRequests: "10m"
|
2016-11-22 23:35:46 +00:00
|
|
|
|
|
|
|
logger:
|
|
|
|
level: "debug"
|
|
|
|
format: "json"
|
2019-12-14 00:33:21 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
want := Config{
|
|
|
|
Issuer: "http://127.0.0.1:5556/dex",
|
|
|
|
Storage: Storage{
|
|
|
|
Type: "postgres",
|
|
|
|
Config: &sql.Postgres{
|
|
|
|
NetworkDB: sql.NetworkDB{
|
|
|
|
Host: "10.0.0.1",
|
|
|
|
Port: 65432,
|
|
|
|
MaxOpenConns: 5,
|
|
|
|
MaxIdleConns: 3,
|
|
|
|
ConnMaxLifetime: 30,
|
|
|
|
ConnectionTimeout: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Web: Web{
|
|
|
|
HTTP: "127.0.0.1:5556",
|
|
|
|
},
|
|
|
|
Frontend: server.WebConfig{
|
|
|
|
Dir: "./web",
|
|
|
|
Extra: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StaticClients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "example-app",
|
|
|
|
Secret: "ZXhhbXBsZS1hcHAtc2VjcmV0",
|
|
|
|
Name: "Example App",
|
|
|
|
RedirectURIs: []string{
|
|
|
|
"http://127.0.0.1:5555/callback",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OAuth2: OAuth2{
|
|
|
|
AlwaysShowLoginScreen: true,
|
|
|
|
},
|
|
|
|
StaticConnectors: []Connector{
|
|
|
|
{
|
|
|
|
Type: "mockCallback",
|
|
|
|
ID: "mock",
|
|
|
|
Name: "Example",
|
|
|
|
Config: &mock.CallbackConfig{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "oidc",
|
|
|
|
ID: "google",
|
|
|
|
Name: "Google",
|
|
|
|
Config: &oidc.Config{
|
|
|
|
Issuer: "https://accounts.google.com",
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
RedirectURI: "http://127.0.0.1:5556/dex/callback/google",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
EnablePasswordDB: true,
|
|
|
|
StaticPasswords: []password{
|
|
|
|
{
|
|
|
|
Email: "admin@example.com",
|
|
|
|
Hash: []byte("$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy"),
|
|
|
|
Username: "admin",
|
|
|
|
UserID: "08a8684b-db88-4b73-90a9-3cd1661f5466",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Email: "foo@example.com",
|
|
|
|
Hash: []byte("$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy"),
|
|
|
|
Username: "foo",
|
|
|
|
UserID: "41331323-6f44-45e6-b3b9-2c4b60c02be5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expiry: Expiry{
|
2020-01-28 19:14:30 +00:00
|
|
|
SigningKeys: "7h",
|
|
|
|
IDTokens: "25h",
|
|
|
|
AuthRequests: "25h",
|
|
|
|
DeviceRequests: "10m",
|
2019-12-14 00:33:21 +00:00
|
|
|
},
|
|
|
|
Logger: Logger{
|
|
|
|
Level: "debug",
|
|
|
|
Format: "json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Config
|
|
|
|
if err := yaml.Unmarshal(rawConfig, &c); err != nil {
|
|
|
|
t.Fatalf("failed to decode config: %v", err)
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(c, want); diff != "" {
|
|
|
|
t.Errorf("got!=want: %s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:54:02 +00:00
|
|
|
func TestUnmarshalConfigWithEnvNoExpand(t *testing.T) {
|
|
|
|
// If the env variable DEX_EXPAND_ENV is set and has a "falsy" value, os.ExpandEnv is disabled.
|
|
|
|
// ParseBool: "It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False."
|
|
|
|
checkUnmarshalConfigWithEnv(t, "0", false)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "f", false)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "F", false)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "FALSE", false)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "false", false)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "False", false)
|
|
|
|
os.Unsetenv("DEX_EXPAND_ENV")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalConfigWithEnvExpand(t *testing.T) {
|
|
|
|
// If the env variable DEX_EXPAND_ENV is unset or has a "truthy" or unknown value, os.ExpandEnv is enabled.
|
|
|
|
// ParseBool: "It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False."
|
|
|
|
checkUnmarshalConfigWithEnv(t, "1", true)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "t", true)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "T", true)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "TRUE", true)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "true", true)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "True", true)
|
|
|
|
// Values that can't be parsed as bool:
|
|
|
|
checkUnmarshalConfigWithEnv(t, "UNSET", true)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "", true)
|
|
|
|
checkUnmarshalConfigWithEnv(t, "whatever - true is default", true)
|
|
|
|
os.Unsetenv("DEX_EXPAND_ENV")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkUnmarshalConfigWithEnv(t *testing.T, dexExpandEnv string, wantExpandEnv bool) {
|
|
|
|
// For hashFromEnv:
|
|
|
|
os.Setenv("DEX_FOO_USER_PASSWORD", "$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy")
|
|
|
|
// For os.ExpandEnv ($VAR -> value_of_VAR):
|
|
|
|
os.Setenv("DEX_FOO_POSTGRES_HOST", "10.0.0.1")
|
|
|
|
os.Setenv("DEX_FOO_OIDC_CLIENT_SECRET", "bar")
|
|
|
|
if dexExpandEnv != "UNSET" {
|
|
|
|
os.Setenv("DEX_EXPAND_ENV", dexExpandEnv)
|
|
|
|
} else {
|
|
|
|
os.Unsetenv("DEX_EXPAND_ENV")
|
2019-12-14 00:33:21 +00:00
|
|
|
}
|
2020-12-29 12:54:02 +00:00
|
|
|
|
2019-12-14 00:33:21 +00:00
|
|
|
rawConfig := []byte(`
|
|
|
|
issuer: http://127.0.0.1:5556/dex
|
|
|
|
storage:
|
|
|
|
type: postgres
|
|
|
|
config:
|
2020-12-29 12:54:02 +00:00
|
|
|
# Env variables are expanded in raw YAML source.
|
|
|
|
# Single quotes work fine, as long as the env variable doesn't contain any.
|
|
|
|
host: '$DEX_FOO_POSTGRES_HOST'
|
2019-12-14 00:33:21 +00:00
|
|
|
port: 65432
|
|
|
|
maxOpenConns: 5
|
|
|
|
maxIdleConns: 3
|
|
|
|
connMaxLifetime: 30
|
|
|
|
connectionTimeout: 3
|
|
|
|
web:
|
|
|
|
http: 127.0.0.1:5556
|
|
|
|
|
|
|
|
frontend:
|
|
|
|
dir: ./web
|
|
|
|
extra:
|
|
|
|
foo: bar
|
|
|
|
|
|
|
|
staticClients:
|
|
|
|
- id: example-app
|
|
|
|
redirectURIs:
|
|
|
|
- 'http://127.0.0.1:5555/callback'
|
|
|
|
name: 'Example App'
|
|
|
|
secret: ZXhhbXBsZS1hcHAtc2VjcmV0
|
|
|
|
|
|
|
|
oauth2:
|
|
|
|
alwaysShowLoginScreen: true
|
|
|
|
|
|
|
|
connectors:
|
|
|
|
- type: mockCallback
|
|
|
|
id: mock
|
|
|
|
name: Example
|
|
|
|
- type: oidc
|
|
|
|
id: google
|
|
|
|
name: Google
|
|
|
|
config:
|
|
|
|
issuer: https://accounts.google.com
|
|
|
|
clientID: foo
|
2020-12-29 12:54:02 +00:00
|
|
|
# Env variables are expanded in raw YAML source.
|
|
|
|
# Single quotes work fine, as long as the env variable doesn't contain any.
|
|
|
|
clientSecret: '$DEX_FOO_OIDC_CLIENT_SECRET'
|
2019-12-14 00:33:21 +00:00
|
|
|
redirectURI: http://127.0.0.1:5556/dex/callback/google
|
|
|
|
|
|
|
|
enablePasswordDB: true
|
|
|
|
staticPasswords:
|
|
|
|
- email: "admin@example.com"
|
|
|
|
# bcrypt hash of the string "password"
|
|
|
|
hash: "$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy"
|
|
|
|
username: "admin"
|
|
|
|
userID: "08a8684b-db88-4b73-90a9-3cd1661f5466"
|
|
|
|
- email: "foo@example.com"
|
2019-12-14 01:03:56 +00:00
|
|
|
hashFromEnv: "DEX_FOO_USER_PASSWORD"
|
2019-12-14 00:33:21 +00:00
|
|
|
username: "foo"
|
|
|
|
userID: "41331323-6f44-45e6-b3b9-2c4b60c02be5"
|
|
|
|
|
|
|
|
expiry:
|
|
|
|
signingKeys: "7h"
|
|
|
|
idTokens: "25h"
|
|
|
|
authRequests: "25h"
|
|
|
|
|
|
|
|
logger:
|
|
|
|
level: "debug"
|
|
|
|
format: "json"
|
2016-11-03 22:23:56 +00:00
|
|
|
`)
|
|
|
|
|
2020-12-29 12:54:02 +00:00
|
|
|
// This is not a valid hostname. It's only used to check whether os.ExpandEnv was applied or not.
|
|
|
|
wantPostgresHost := "$DEX_FOO_POSTGRES_HOST"
|
|
|
|
wantOidcClientSecret := "$DEX_FOO_OIDC_CLIENT_SECRET"
|
|
|
|
if wantExpandEnv {
|
|
|
|
wantPostgresHost = "10.0.0.1"
|
|
|
|
wantOidcClientSecret = "bar"
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
want := Config{
|
|
|
|
Issuer: "http://127.0.0.1:5556/dex",
|
|
|
|
Storage: Storage{
|
2018-11-21 10:35:25 +00:00
|
|
|
Type: "postgres",
|
|
|
|
Config: &sql.Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: sql.NetworkDB{
|
2020-12-29 12:54:02 +00:00
|
|
|
Host: wantPostgresHost,
|
2019-07-12 14:29:46 +00:00
|
|
|
Port: 65432,
|
|
|
|
MaxOpenConns: 5,
|
|
|
|
MaxIdleConns: 3,
|
|
|
|
ConnMaxLifetime: 30,
|
|
|
|
ConnectionTimeout: 3,
|
|
|
|
},
|
2016-11-03 22:23:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Web: Web{
|
|
|
|
HTTP: "127.0.0.1:5556",
|
|
|
|
},
|
2019-08-06 17:14:53 +00:00
|
|
|
Frontend: server.WebConfig{
|
|
|
|
Dir: "./web",
|
|
|
|
Extra: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
2016-11-03 22:23:56 +00:00
|
|
|
StaticClients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "example-app",
|
|
|
|
Secret: "ZXhhbXBsZS1hcHAtc2VjcmV0",
|
|
|
|
Name: "Example App",
|
|
|
|
RedirectURIs: []string{
|
|
|
|
"http://127.0.0.1:5555/callback",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-08-06 17:18:46 +00:00
|
|
|
OAuth2: OAuth2{
|
|
|
|
AlwaysShowLoginScreen: true,
|
|
|
|
},
|
2017-04-17 22:41:41 +00:00
|
|
|
StaticConnectors: []Connector{
|
2016-11-03 22:23:56 +00:00
|
|
|
{
|
|
|
|
Type: "mockCallback",
|
|
|
|
ID: "mock",
|
|
|
|
Name: "Example",
|
|
|
|
Config: &mock.CallbackConfig{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "oidc",
|
|
|
|
ID: "google",
|
|
|
|
Name: "Google",
|
|
|
|
Config: &oidc.Config{
|
|
|
|
Issuer: "https://accounts.google.com",
|
2016-11-04 04:08:50 +00:00
|
|
|
ClientID: "foo",
|
2020-12-29 12:54:02 +00:00
|
|
|
ClientSecret: wantOidcClientSecret,
|
2016-11-03 22:23:56 +00:00
|
|
|
RedirectURI: "http://127.0.0.1:5556/dex/callback/google",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
EnablePasswordDB: true,
|
|
|
|
StaticPasswords: []password{
|
|
|
|
{
|
|
|
|
Email: "admin@example.com",
|
|
|
|
Hash: []byte("$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy"),
|
|
|
|
Username: "admin",
|
|
|
|
UserID: "08a8684b-db88-4b73-90a9-3cd1661f5466",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Email: "foo@example.com",
|
|
|
|
Hash: []byte("$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy"),
|
|
|
|
Username: "foo",
|
|
|
|
UserID: "41331323-6f44-45e6-b3b9-2c4b60c02be5",
|
2016-08-05 16:50:22 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-03 00:52:49 +00:00
|
|
|
Expiry: Expiry{
|
2018-12-13 13:47:51 +00:00
|
|
|
SigningKeys: "7h",
|
|
|
|
IDTokens: "25h",
|
|
|
|
AuthRequests: "25h",
|
2016-11-03 00:52:49 +00:00
|
|
|
},
|
2016-11-22 23:35:46 +00:00
|
|
|
Logger: Logger{
|
|
|
|
Level: "debug",
|
|
|
|
Format: "json",
|
|
|
|
},
|
2016-08-05 16:50:22 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 22:23:56 +00:00
|
|
|
var c Config
|
|
|
|
if err := yaml.Unmarshal(rawConfig, &c); err != nil {
|
|
|
|
t.Fatalf("failed to decode config: %v", err)
|
2016-08-05 16:50:22 +00:00
|
|
|
}
|
2016-11-03 22:23:56 +00:00
|
|
|
if diff := pretty.Compare(c, want); diff != "" {
|
|
|
|
t.Errorf("got!=want: %s", diff)
|
|
|
|
}
|
2016-08-05 16:50:22 +00:00
|
|
|
}
|