2023-10-12 18:36:34 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
|
2023-10-14 16:06:02 +00:00
|
|
|
"github.com/dexidp/dex/connector/oidc"
|
|
|
|
"github.com/dexidp/dex/storage"
|
2023-10-12 18:36:34 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ListeningMode int64
|
|
|
|
|
|
|
|
func (lm ListeningMode) String() string {
|
|
|
|
mapping := map[ListeningMode]string{
|
|
|
|
ModeNet: "net",
|
|
|
|
ModeUnix: "unix",
|
|
|
|
}
|
|
|
|
val := mapping[lm]
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func listeningModeFromString(rawVal string) (ListeningMode, error) {
|
|
|
|
mapping := map[string]ListeningMode{
|
|
|
|
"unix": ModeUnix,
|
|
|
|
"net": ModeNet,
|
|
|
|
}
|
|
|
|
if typedVal, ok := mapping[rawVal]; !ok {
|
|
|
|
return ModeNet, fmt.Errorf("invalid listening mode %s", rawVal)
|
|
|
|
} else {
|
|
|
|
return typedVal, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ModeUnix ListeningMode = iota
|
|
|
|
ModeNet
|
|
|
|
)
|
|
|
|
|
2023-10-14 16:06:02 +00:00
|
|
|
type BackendConfig struct {
|
|
|
|
Config *oidc.Config `json:"config"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID string `json:"ID"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Local bool `json:"local"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type OpenConnectConfig struct {
|
|
|
|
ClientConfigs []*storage.Client `json:"clients"`
|
|
|
|
BackendConfigs []*BackendConfig `json:"backends"`
|
2023-10-15 15:49:33 +00:00
|
|
|
Issuer string `json:"issuer"`
|
2023-10-14 16:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 21:50:53 +00:00
|
|
|
type StorageConfigConfig struct {
|
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
Database string `json:"database"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Ssl struct {
|
|
|
|
Mode string `json:"mode"`
|
|
|
|
CaFile string `json:"caFile"`
|
|
|
|
} `json:"ssl"`
|
|
|
|
}
|
|
|
|
|
2023-10-12 18:36:34 +00:00
|
|
|
type jsonConf struct {
|
|
|
|
Log struct {
|
|
|
|
Level string `json:"level"`
|
|
|
|
} `json:"log"`
|
|
|
|
Server struct {
|
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
Mode string `json:"mode"`
|
|
|
|
SockPath string `json:"sock"`
|
|
|
|
} `json:"server"`
|
2023-10-21 21:50:53 +00:00
|
|
|
Storage struct {
|
|
|
|
StorageType string `json:"type"`
|
|
|
|
StorageConfig *StorageConfigConfig `json:"config"`
|
|
|
|
} `json:"storage"`
|
2023-10-14 16:06:02 +00:00
|
|
|
OpenConnectConfig *OpenConnectConfig `json:"openconnect"`
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AppConfig struct {
|
2023-10-14 16:06:02 +00:00
|
|
|
LogLevel logrus.Level
|
|
|
|
ServerMode ListeningMode
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
SockPath string
|
2023-10-21 21:50:53 +00:00
|
|
|
StorageType string
|
|
|
|
StorageConfig *StorageConfigConfig
|
2023-10-14 16:06:02 +00:00
|
|
|
OpenConnectConfig *OpenConnectConfig
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseLevel(lvlStr string) logrus.Level {
|
|
|
|
for _, lvl := range logrus.AllLevels {
|
|
|
|
if lvl.String() == lvlStr {
|
|
|
|
return lvl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logrus.InfoLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AppConfig) UnmarshalJSON(data []byte) error {
|
|
|
|
var jsonConf jsonConf
|
|
|
|
if err := json.Unmarshal(data, &jsonConf); err != nil {
|
|
|
|
return fmt.Errorf("failed to read JSON: %w", err)
|
|
|
|
}
|
|
|
|
ac.LogLevel = parseLevel(jsonConf.Log.Level)
|
|
|
|
|
|
|
|
lm, err := listeningModeFromString(jsonConf.Server.Mode)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse server listening mode: %w", err)
|
|
|
|
}
|
2023-10-14 16:06:02 +00:00
|
|
|
|
2023-10-12 18:36:34 +00:00
|
|
|
ac.ServerMode = lm
|
|
|
|
ac.SockPath = jsonConf.Server.SockPath
|
|
|
|
ac.Host = jsonConf.Server.Host
|
|
|
|
ac.Port = jsonConf.Server.Port
|
2023-10-14 16:06:02 +00:00
|
|
|
ac.OpenConnectConfig = jsonConf.OpenConnectConfig
|
2023-10-21 21:50:53 +00:00
|
|
|
ac.StorageType = jsonConf.Storage.StorageType
|
|
|
|
ac.StorageConfig = jsonConf.Storage.StorageConfig
|
2023-10-12 18:36:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultConfig AppConfig = AppConfig{
|
2023-10-21 21:50:53 +00:00
|
|
|
LogLevel: logrus.InfoLevel,
|
|
|
|
ServerMode: ModeNet,
|
|
|
|
Host: "0.0.0.0",
|
|
|
|
Port: 5000,
|
|
|
|
StorageType: "memory",
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(filepath string) (*AppConfig, error) {
|
|
|
|
content, err := os.ReadFile(filepath)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
conf := defaultConfig
|
|
|
|
return &conf, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err)
|
|
|
|
}
|
|
|
|
var conf AppConfig
|
|
|
|
if err := json.Unmarshal(content, &conf); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
|
|
|
}
|
2023-10-21 21:50:53 +00:00
|
|
|
fmt.Printf(conf.StorageType)
|
2023-10-12 18:36:34 +00:00
|
|
|
return &conf, nil
|
|
|
|
}
|