feat #37: Use environment variables for most of the configuration
All checks were successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful

This commit is contained in:
Melora Hugues 2023-10-25 20:30:23 +02:00
parent 3bc17d6aba
commit 4909df4b04
4 changed files with 251 additions and 204 deletions

View file

@ -10,21 +10,13 @@ TODO
## Configuration ## Configuration
Here is an example config file As a temporary solution, the list of backends and applications, as well as the openconnect configuration
can only be handled through the JSON config file.
```json ```json
{ {
"log": {
"level": "debug" // debug,info,warn,error
},
"server": {
"port": 5000, // only used in net mode
"host": "0.0.0.0", // only used in net mode
// "sock": "/your/sock.sock" // path to your unix sock if "mode" is set to "unix"
"mode": "net" // net,unix
},
"openconnect": { "openconnect": {
"issuer": "https://polyculeconnect.domain", // hostname of your polyculeconnect server "issuer": "https://polyculeconnect.domain",
"clients": [ "clients": [
{ {
"name": "<name>", "name": "<name>",
@ -40,19 +32,42 @@ Here is an example config file
"id": "<unique_id>", "id": "<unique_id>",
"name": "<human_readable_name>", "name": "<human_readable_name>",
"local": true, "local": true,
"type": "oidc", // must be "oidc" for now "type": "oidc",
"config": { "config": {
"issuer": "https://polyculeconnect.domain", // must be the same as current issuer "issuer": "https://polyculeconnect.domain",
"clientID": "<client_id>", "clientID": "<client_id>",
"clientSecret": "<client_secret>", "clientSecret": "<client_secret>",
"redirectURI": "<redirect_uri>" "redirectURI": "<redirect_uri>"
} }
}, }
] ]
} }
} }
``` ```
The rest of the configuration is handled through environment variables
```ini
# Can be debug,info,warning,error
LOG_LEVEL = "info"
# Can be net,unix
SERVER_MODE = "net"
SERVER_HOST = "0.0.0.0"
SERVER_PORT = "5000"
# SERVER_SOCK_PATH = ""
STORAGE_TYPE = "sqlite"
STORAGE_FILEPATH = "./build/polyculeconnect.db"
# STORAGE_HOST = "127.0.0.1"
# STORAGE_PORT = "5432"
# STORAGE_DB = "polyculeconnect"
# STORAGE_USER = "polyculeconnect"
# STORAGE_PASSWORD = "polyculeconnect"
# STORAGE_SSL_MODE = "disable"
# STORAGE_SSL_CA_FILE = ""
```
You can register multiple backend and multiple clients (applications) You can register multiple backend and multiple clients (applications)
## Running the server ## Running the server

View file

@ -12,32 +12,32 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type ListeningMode int64 type envVar string
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 ( const (
ModeUnix ListeningMode = iota varLogLevel envVar = "LOG_LEVEL"
ModeNet
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"
)
type ListeningMode string
const (
ModeUnix ListeningMode = "unix"
ModeNet ListeningMode = "net"
) )
type BackendConfigType string type BackendConfigType string
@ -47,6 +47,25 @@ const (
SQLite BackendConfigType = "sqlite" 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 = ""
)
type BackendConfig struct { type BackendConfig struct {
Config *oidc.Config `json:"config"` Config *oidc.Config `json:"config"`
Name string `json:"name"` Name string `json:"name"`
@ -62,32 +81,19 @@ type OpenConnectConfig struct {
} }
type StorageConfig struct { type StorageConfig struct {
File string `json:"file"` File string
Host string `json:"host"` Host string
Port int `json:"port"` Port int
Database string `json:"database"` Database string
User string `json:"user"` User string
Password string `json:"password"` Password string
Ssl struct { Ssl struct {
Mode string `json:"mode"` Mode string
CaFile string `json:"caFile"` CaFile string
} `json:"ssl"` }
} }
type jsonConf struct { 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"`
Storage struct {
StorageType string `json:"type"`
Config *StorageConfig `json:"config"`
} `json:"storage"`
OpenConnectConfig *OpenConnectConfig `json:"openconnect"` OpenConnectConfig *OpenConnectConfig `json:"openconnect"`
} }
@ -116,43 +122,42 @@ func (ac *AppConfig) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &jsonConf); err != nil { if err := json.Unmarshal(data, &jsonConf); err != nil {
return fmt.Errorf("failed to read JSON: %w", err) 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)
}
ac.ServerMode = lm
ac.SockPath = jsonConf.Server.SockPath
ac.Host = jsonConf.Server.Host
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
} }
var defaultConfig AppConfig = AppConfig{ func (ac *AppConfig) getConfFromEnv() {
LogLevel: logrus.InfoLevel, ac.LogLevel = parseLevel(getStringFromEnv(varLogLevel, defaultLogLevel.String()))
ServerMode: ModeNet,
Host: "0.0.0.0", ac.ServerMode = ListeningMode(getStringFromEnv(varServerMode, string(defaultServerMode)))
Port: 5000, ac.Host = getStringFromEnv(varServerHost, defaultServerHost)
StorageType: "memory", 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)
} }
func New(filepath string) (*AppConfig, error) { func New(filepath string) (*AppConfig, error) {
var conf AppConfig
conf.StorageConfig = &StorageConfig{}
content, err := os.ReadFile(filepath) content, err := os.ReadFile(filepath)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrNotExist) { if !errors.Is(err, fs.ErrNotExist) {
conf := defaultConfig return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err)
return &conf, nil }
} else {
if err := json.Unmarshal(content, &conf); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
} }
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)
} }
conf.getConfFromEnv()
return &conf, nil return &conf, nil
} }

View file

@ -10,128 +10,129 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestListeningModeString(t *testing.T) { var defaultConfig = AppConfig{
assert.Equal(t, "net", ModeNet.String(), "Unexpected string value") LogLevel: defaultLogLevel,
assert.Equal(t, "unix", ModeUnix.String(), "Unexpected string value") ServerMode: defaultServerMode,
Host: defaultServerHost,
Port: defaultServerPort,
SockPath: defaultServerSocket,
StorageType: string(defaultStorageType),
StorageConfig: &StorageConfig{
File: defaultStorageFile,
Host: defaultStorageHost,
Port: defaultStoragePort,
Database: defaultStorageDB,
User: defaultStorageUser,
Password: defaultStoragePassword,
Ssl: struct {
Mode string
CaFile string
}{Mode: defaultStorageSSLMode, CaFile: defaultStorageSSLCaFile},
},
}
func initJson(t *testing.T, content string) string {
tmpPath := t.TempDir()
confPath := path.Join(tmpPath, "config.json")
err := os.WriteFile(confPath, []byte(content), 0o644)
require.NoError(t, err)
return confPath
}
func setEnv(t *testing.T, envVars map[string]string) {
for key, val := range envVars {
t.Setenv(key, val)
}
} }
// Test returning a default config when providing a path that does not exist
func TestDefault(t *testing.T) { func TestDefault(t *testing.T) {
conf, err := New("/this/path/does/not/exist") t.Run("no file", func(t *testing.T) {
if assert.Nil(t, err, "Unexpected error") { conf, err := New("/this/path/does/not/exist")
assert.Equal(t, defaultConfig, *conf, "Unexpected config") require.NoError(t, err)
} assert.Equal(t, defaultConfig, *conf)
} })
// Test creating a valid config (net mode) t.Run("empty config", func(t *testing.T) {
func TestOKNet(t *testing.T) { confPath := initJson(t, `{}`)
tmpPath := t.TempDir() conf, err := New(confPath)
content := `{ require.NoError(t, err)
"log": { assert.Equal(t, defaultConfig, *conf)
"level": "error" })
},
"server": {
"mode": "net",
"host": "127.0.0.1",
"port": 8888
}
}`
confPath := path.Join(tmpPath, "config.json")
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
expectedConf := AppConfig{
LogLevel: logrus.ErrorLevel,
ServerMode: ModeNet,
Host: "127.0.0.1",
Port: 8888,
}
conf, err := New(confPath)
if assert.Nil(t, err, "Unexpected error") {
assert.Equal(t, expectedConf, *conf, "Unexpected config")
}
}
// Test creating a valid config (unix mode)
func TestOKUnix(t *testing.T) {
tmpPath := t.TempDir()
content := `{
"log": {
"level": "error"
},
"server": {
"mode": "unix",
"sock": "/run/toto.sock"
}
}`
confPath := path.Join(tmpPath, "config.json")
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
expectedConf := AppConfig{
LogLevel: logrus.ErrorLevel,
ServerMode: ModeUnix,
SockPath: "/run/toto.sock",
}
conf, err := New(confPath)
if assert.Nil(t, err, "Unexpected error") {
assert.Equal(t, expectedConf, *conf, "Unexpected config")
}
}
// Test creating a valid config, no log level provided, should be info
func TestOKNoLogLevel(t *testing.T) {
tmpPath := t.TempDir()
content := `{
"server": {
"mode": "net",
"host": "127.0.0.1",
"port": 8888
}
}`
confPath := path.Join(tmpPath, "config.json")
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
expectedConf := AppConfig{
LogLevel: logrus.InfoLevel,
ServerMode: ModeNet,
Host: "127.0.0.1",
Port: 8888,
}
conf, err := New(confPath)
if assert.Nil(t, err, "Unexpected error") {
assert.Equal(t, expectedConf, *conf, "Unexpected config")
}
}
// Test giving an invalid server mode
func TestErrMode(t *testing.T) {
tmpPath := t.TempDir()
content := `{
"log": {
"level": "error"
},
"server": {
"mode": "toto",
"sock": "/run/toto.sock"
}
}`
confPath := path.Join(tmpPath, "config.json")
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
_, err := New(confPath)
if assert.Error(t, err, "Unexpected nil error") {
errMsg := "failed to parse config file: failed to parse server listening mode: invalid listening mode toto"
assert.Equal(t, errMsg, err.Error(), "Unexpected error message")
}
} }
// Since we still use a JSON conf for the OIDC config, we still need to check this for now
// But as soon as the config file is not necessary, this will probably disappear
func TestInvalidJSON(t *testing.T) { func TestInvalidJSON(t *testing.T) {
tmpPath := t.TempDir() confPath := initJson(t, "toto")
content := "toto" errMsg := "failed to parse config file: invalid character 'o' in literal true (expecting 'r')"
confPath := path.Join(tmpPath, "config.json")
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
_, err := New(confPath) _, err := New(confPath)
if assert.Error(t, err, "Unexpected nil error") { assert.ErrorContains(t, err, errMsg)
errMsg := "failed to parse config file: invalid character 'o' in literal true (expecting 'r')" }
assert.Equal(t, errMsg, err.Error(), "Unexpected error message")
} func TestHostNetMode(t *testing.T) {
envVars := map[string]string{
string(varServerMode): string(ModeNet),
string(varServerHost): "127.0.0.1",
string(varServerPort): "8888",
}
setEnv(t, envVars)
conf, err := New("")
require.NoError(t, err)
assert.Equal(t, ModeNet, conf.ServerMode)
assert.Equal(t, "127.0.0.1", conf.Host)
assert.Equal(t, 8888, conf.Port)
}
func TestHostSocketMode(t *testing.T) {
envVars := map[string]string{
string(varServerMode): string(ModeUnix),
string(varServerSocket): "/run/polyculeconnect.sock",
}
setEnv(t, envVars)
conf, err := New("")
require.NoError(t, err)
assert.Equal(t, ModeUnix, conf.ServerMode)
assert.Equal(t, "/run/polyculeconnect.sock", conf.SockPath)
}
func TestLogLevel(t *testing.T) {
envVars := map[string]string{
string(varLogLevel): "error",
}
setEnv(t, envVars)
conf, err := New("")
require.NoError(t, err)
assert.Equal(t, logrus.ErrorLevel, conf.LogLevel)
}
func TestLogLevelInvalidValue(t *testing.T) {
envVars := map[string]string{
string(varLogLevel): "toto",
}
setEnv(t, envVars)
conf, err := New("")
require.NoError(t, err)
assert.Equal(t, logrus.InfoLevel, conf.LogLevel) // if invalid, no error should occur, but info level should be used
}
func TestSqliteConfig(t *testing.T) {
envVars := map[string]string{
string(varStorageType): "sqlite",
string(varStorageFile): "/data/polyculeconnect.db",
}
setEnv(t, envVars)
conf, err := New("")
require.NoError(t, err)
assert.Equal(t, string(SQLite), conf.StorageType)
assert.Equal(t, "/data/polyculeconnect.db", conf.StorageConfig.File)
} }

View file

@ -0,0 +1,26 @@
package config
import (
"os"
"strconv"
)
func getStringFromEnv(key envVar, defaultValue string) string {
val, ok := os.LookupEnv(string(key))
if !ok {
return defaultValue
}
return val
}
func getIntFromEnv(key envVar, defaultValue int) int {
rawVal, ok := os.LookupEnv(string(key))
if !ok {
return defaultValue
}
val, err := strconv.Atoi(rawVal)
if err != nil {
return defaultValue
}
return val
}