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"
|
|
|
|
)
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
type envVar string
|
2023-10-12 18:36:34 +00:00
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
const (
|
|
|
|
varLogLevel envVar = "LOG_LEVEL"
|
|
|
|
|
|
|
|
varServerMode envVar = "SERVER_MODE"
|
|
|
|
varServerHost envVar = "SERVER_HOST"
|
|
|
|
varServerPort envVar = "SERVER_PORT"
|
|
|
|
varServerSocket envVar = "SERVER_SOCK_PATH"
|
|
|
|
|
2024-01-03 16:10:20 +00:00
|
|
|
varIssuer envVar = "ISSUER"
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
varStorageType envVar = "STORAGE_TYPE"
|
|
|
|
varStorageFile envVar = "STORAGE_FILEPATH"
|
|
|
|
varStorageHost envVar = "STORAGE_HOST"
|
|
|
|
varStoragePort envVar = "STORAGE_PORT"
|
|
|
|
varStorageDB envVar = "STORAGE_DB"
|
|
|
|
varStorageUser envVar = "STORAGE_USER"
|
|
|
|
varStoragePassword envVar = "STORAGE_PASSWORD"
|
|
|
|
varStorageSSLMode envVar = "STORAGE_SSL_MODE"
|
|
|
|
varStorageSSLCaFile envVar = "STORAGE_SSL_CA_FILE"
|
|
|
|
)
|
2023-10-12 18:36:34 +00:00
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
type ListeningMode string
|
2023-10-12 18:36:34 +00:00
|
|
|
|
|
|
|
const (
|
2023-10-25 18:30:23 +00:00
|
|
|
ModeUnix ListeningMode = "unix"
|
|
|
|
ModeNet ListeningMode = "net"
|
2023-10-12 18:36:34 +00:00
|
|
|
)
|
|
|
|
|
2023-10-21 21:50:53 +00:00
|
|
|
type BackendConfigType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
Memory BackendConfigType = "memory"
|
|
|
|
SQLite BackendConfigType = "sqlite"
|
|
|
|
)
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
const (
|
|
|
|
defaultLogLevel = logrus.InfoLevel
|
|
|
|
|
|
|
|
defaultServerMode = ModeNet
|
|
|
|
defaultServerHost = "0.0.0.0"
|
|
|
|
defaultServerPort = 5000
|
|
|
|
defaultServerSocket = ""
|
|
|
|
|
2024-01-03 16:10:20 +00:00
|
|
|
defaultIssuer = "locahost"
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
defaultStorageType = Memory
|
|
|
|
defaultStorageFile = "./polyculeconnect.db"
|
|
|
|
defaultStorageHost = "127.0.0.1"
|
|
|
|
defaultStoragePort = 5432
|
|
|
|
defaultStorageDB = "polyculeconnect"
|
|
|
|
defaultStorageUser = "polyculeconnect"
|
|
|
|
defaultStoragePassword = "polyculeconnect"
|
|
|
|
defaultStorageSSLMode = "disable"
|
|
|
|
defaultStorageSSLCaFile = ""
|
|
|
|
)
|
|
|
|
|
2023-10-29 12:31:52 +00:00
|
|
|
// Deprecated: remove when we finally drop the JSON config
|
2023-10-14 16:06:02 +00:00
|
|
|
type BackendConfig struct {
|
2023-10-21 21:50:53 +00:00
|
|
|
Config *oidc.Config `json:"config"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID string `json:"ID"`
|
|
|
|
Type BackendConfigType `json:"type"`
|
|
|
|
Local bool `json:"local"`
|
2023-10-14 16:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 18:31:34 +00:00
|
|
|
// Deprecated: remove when we finally drop the JSON config
|
2023-10-14 16:06:02 +00:00
|
|
|
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 StorageConfig struct {
|
2023-10-25 18:30:23 +00:00
|
|
|
File string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Database string
|
|
|
|
User string
|
|
|
|
Password string
|
2023-10-21 21:50:53 +00:00
|
|
|
Ssl struct {
|
2023-10-25 18:30:23 +00:00
|
|
|
Mode string
|
|
|
|
CaFile string
|
|
|
|
}
|
2023-10-21 21:50:53 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 18:36:34 +00:00
|
|
|
type jsonConf struct {
|
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 *StorageConfig
|
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)
|
|
|
|
}
|
2023-10-14 16:06:02 +00:00
|
|
|
ac.OpenConnectConfig = jsonConf.OpenConnectConfig
|
2024-01-03 16:10:20 +00:00
|
|
|
if ac.OpenConnectConfig == nil {
|
|
|
|
ac.OpenConnectConfig = &OpenConnectConfig{}
|
|
|
|
}
|
2023-10-12 18:36:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
func (ac *AppConfig) getConfFromEnv() {
|
|
|
|
ac.LogLevel = parseLevel(getStringFromEnv(varLogLevel, defaultLogLevel.String()))
|
|
|
|
|
|
|
|
ac.ServerMode = ListeningMode(getStringFromEnv(varServerMode, string(defaultServerMode)))
|
|
|
|
ac.Host = getStringFromEnv(varServerHost, defaultServerHost)
|
|
|
|
ac.Port = getIntFromEnv(varServerPort, defaultServerPort)
|
|
|
|
ac.SockPath = getStringFromEnv(varServerSocket, defaultServerSocket)
|
|
|
|
|
|
|
|
ac.StorageType = getStringFromEnv(varStorageType, string(defaultStorageType))
|
|
|
|
ac.StorageConfig.Database = getStringFromEnv(varStorageDB, defaultStorageDB)
|
|
|
|
ac.StorageConfig.File = getStringFromEnv(varStorageFile, defaultStorageFile)
|
|
|
|
ac.StorageConfig.Host = getStringFromEnv(varStorageHost, defaultStorageHost)
|
|
|
|
ac.StorageConfig.Port = getIntFromEnv(varStoragePort, defaultStoragePort)
|
|
|
|
ac.StorageConfig.User = getStringFromEnv(varStorageUser, defaultStorageUser)
|
|
|
|
ac.StorageConfig.Password = getStringFromEnv(varStoragePassword, defaultStoragePassword)
|
|
|
|
ac.StorageConfig.Ssl.CaFile = getStringFromEnv(varStorageSSLCaFile, defaultStorageSSLCaFile)
|
|
|
|
ac.StorageConfig.Ssl.Mode = getStringFromEnv(varStorageSSLMode, defaultStorageSSLMode)
|
2024-01-03 16:10:20 +00:00
|
|
|
|
|
|
|
ac.OpenConnectConfig.Issuer = getStringFromEnv(varIssuer, defaultIssuer)
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 12:31:52 +00:00
|
|
|
func (ac *AppConfig) RedirectURI() string {
|
|
|
|
return ac.OpenConnectConfig.Issuer + "/callback"
|
|
|
|
}
|
|
|
|
|
2023-10-12 18:36:34 +00:00
|
|
|
func New(filepath string) (*AppConfig, error) {
|
2023-10-25 18:30:23 +00:00
|
|
|
var conf AppConfig
|
|
|
|
conf.StorageConfig = &StorageConfig{}
|
2024-01-03 16:10:20 +00:00
|
|
|
conf.OpenConnectConfig = &OpenConnectConfig{}
|
2023-10-12 18:36:34 +00:00
|
|
|
content, err := os.ReadFile(filepath)
|
|
|
|
if err != nil {
|
2023-10-25 18:30:23 +00:00
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := json.Unmarshal(content, &conf); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-25 18:30:23 +00:00
|
|
|
conf.getConfFromEnv()
|
2023-10-12 18:36:34 +00:00
|
|
|
return &conf, nil
|
|
|
|
}
|