polycule-connect/polyculeconnect/config/config.go

170 lines
4.7 KiB
Go
Raw Normal View History

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 envVar string
2023-10-12 18:36:34 +00:00
const (
varLogLevel envVar = "LOG_LEVEL"
varServerMode envVar = "SERVER_MODE"
varServerHost envVar = "SERVER_HOST"
varServerPort envVar = "SERVER_PORT"
varServerSocket envVar = "SERVER_SOCK_PATH"
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
type ListeningMode string
2023-10-12 18:36:34 +00:00
const (
ModeUnix ListeningMode = "unix"
ModeNet ListeningMode = "net"
2023-10-12 18:36:34 +00:00
)
type BackendConfigType string
const (
Memory BackendConfigType = "memory"
SQLite BackendConfigType = "sqlite"
)
const (
defaultLogLevel = logrus.InfoLevel
defaultServerMode = ModeNet
defaultServerHost = "0.0.0.0"
defaultServerPort = 5000
defaultServerSocket = ""
defaultStorageType = Memory
defaultStorageFile = "./polyculeconnect.db"
defaultStorageHost = "127.0.0.1"
defaultStoragePort = 5432
defaultStorageDB = "polyculeconnect"
defaultStorageUser = "polyculeconnect"
defaultStoragePassword = "polyculeconnect"
defaultStorageSSLMode = "disable"
defaultStorageSSLCaFile = ""
)
// Deprecated: remove when we finally drop the JSON config
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 BackendConfigType `json:"type"`
Local bool `json:"local"`
2023-10-14 16:06:02 +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"`
Issuer string `json:"issuer"`
2023-10-14 16:06:02 +00:00
}
type StorageConfig struct {
File string
Host string
Port int
Database string
User string
Password string
Ssl struct {
Mode string
CaFile string
}
}
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
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
2023-10-12 18:36:34 +00:00
return nil
}
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)
2023-10-12 18:36:34 +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) {
var conf AppConfig
conf.StorageConfig = &StorageConfig{}
2023-10-12 18:36:34 +00:00
content, err := os.ReadFile(filepath)
if err != nil {
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
}
}
conf.getConfFromEnv()
2023-10-12 18:36:34 +00:00
return &conf, nil
}