feat #4: add storage config in config file

This commit is contained in:
chapeau 2023-10-21 23:50:53 +02:00
parent 8852e3de52
commit ffc217a0d5
2 changed files with 57 additions and 11 deletions

View file

@ -40,11 +40,18 @@ const (
ModeNet ModeNet
) )
type BackendConfigType string
const (
Memory BackendConfigType = "memory"
SQLite BackendConfigType = "sqlite"
)
type BackendConfig struct { type BackendConfig struct {
Config *oidc.Config `json:"config"` Config *oidc.Config `json:"config"`
Name string `json:"name"` Name string `json:"name"`
ID string `json:"ID"` ID string `json:"ID"`
Type string `json:"type"` Type BackendConfigType `json:"type"`
Local bool `json:"local"` Local bool `json:"local"`
} }
@ -54,6 +61,19 @@ type OpenConnectConfig struct {
Issuer string `json:"issuer"` Issuer string `json:"issuer"`
} }
type StorageConfig struct {
File string `json:"file"`
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"`
}
type jsonConf struct { type jsonConf struct {
Log struct { Log struct {
Level string `json:"level"` Level string `json:"level"`
@ -64,6 +84,10 @@ type jsonConf struct {
Mode string `json:"mode"` Mode string `json:"mode"`
SockPath string `json:"sock"` SockPath string `json:"sock"`
} `json:"server"` } `json:"server"`
Storage struct {
StorageType string `json:"type"`
Config *StorageConfig `json:"config"`
} `json:"storage"`
OpenConnectConfig *OpenConnectConfig `json:"openconnect"` OpenConnectConfig *OpenConnectConfig `json:"openconnect"`
} }
@ -73,6 +97,8 @@ type AppConfig struct {
Host string Host string
Port int Port int
SockPath string SockPath string
StorageType string
StorageConfig *StorageConfig
OpenConnectConfig *OpenConnectConfig OpenConnectConfig *OpenConnectConfig
} }
@ -102,6 +128,8 @@ func (ac *AppConfig) UnmarshalJSON(data []byte) error {
ac.Host = jsonConf.Server.Host ac.Host = jsonConf.Server.Host
ac.Port = jsonConf.Server.Port ac.Port = jsonConf.Server.Port
ac.OpenConnectConfig = jsonConf.OpenConnectConfig ac.OpenConnectConfig = jsonConf.OpenConnectConfig
ac.StorageType = jsonConf.Storage.StorageType
ac.StorageConfig = jsonConf.Storage.Config
return nil return nil
} }
@ -110,6 +138,7 @@ var defaultConfig AppConfig = AppConfig{
ServerMode: ModeNet, ServerMode: ModeNet,
Host: "0.0.0.0", Host: "0.0.0.0",
Port: 5000, Port: 5000,
StorageType: "memory",
} }
func New(filepath string) (*AppConfig, error) { func New(filepath string) (*AppConfig, error) {

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"os" "os"
"os/signal" "os/signal"
"time" "time"
@ -34,6 +35,17 @@ func parseArgs() *cliArgs {
} }
} }
func initStorage(conf *config.AppConfig) (storage.Storage, error) {
var storageType storage.Storage
switch conf.StorageType {
case "memory":
storageType = memory.New(logger.L)
default:
return storageType, fmt.Errorf("unsupported storage backend type: %s", conf.StorageType)
}
return storageType, nil
}
func main() { func main() {
args := parseArgs() args := parseArgs()
@ -47,12 +59,17 @@ func main() {
logger.Init(conf.LogLevel) logger.Init(conf.LogLevel)
logger.L.Infof("Initialized logger with level %v", conf.LogLevel) logger.L.Infof("Initialized logger with level %v", conf.LogLevel)
storageType, err := initStorage(conf)
if err != nil {
logger.L.Fatalf("Failed to initialize storage backend: %s", err.Error())
}
logger.L.Infof("Initialized storage backend %q", conf.StorageType)
dexConf := dex_server.Config{ dexConf := dex_server.Config{
Web: dex_server.WebConfig{ Web: dex_server.WebConfig{
Dir: "./", Dir: "./",
Theme: "default", Theme: "default",
}, },
Storage: memory.New(logger.L), Storage: storageType,
Issuer: conf.OpenConnectConfig.Issuer, Issuer: conf.OpenConnectConfig.Issuer,
SupportedResponseTypes: []string{"code"}, SupportedResponseTypes: []string{"code"},
SkipApprovalScreen: false, SkipApprovalScreen: false,
@ -82,7 +99,7 @@ func main() {
if err := dexConf.Storage.CreateConnector(storage.Connector{ if err := dexConf.Storage.CreateConnector(storage.Connector{
ID: backend.ID, ID: backend.ID,
Name: backend.Name, Name: backend.Name,
Type: backend.Type, Type: string(backend.Type),
Config: backendConfJson, Config: backendConfJson,
}); err != nil { }); err != nil {
logger.L.Errorf("Failed to add connector for backend %q to stage: %s", backend.Name, err.Error()) logger.L.Errorf("Failed to add connector for backend %q to stage: %s", backend.Name, err.Error())