Skip to content
43 changes: 29 additions & 14 deletions utils/config/loader.go → internals/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"strconv"
"strings"

"github.com/codeshelldev/secured-signal-api/utils/config/structure"
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
"github.com/codeshelldev/secured-signal-api/utils/configutils"
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
log "github.com/codeshelldev/secured-signal-api/utils/logger"

Expand All @@ -24,56 +25,70 @@ var ENV *structure.ENV = &structure.ENV{
INSECURE: false,
}

var defaultsConf = configutils.New()
var userConf = configutils.New()
var tokenConf = configutils.New()

var config = configutils.New()

func Load() {
InitReload()

LoadDefaults()

LoadConfig()

LoadTokens()

LoadEnv(userLayer)
userConf.LoadEnv()

config = mergeLayers()
config.MergeLayers(defaultsConf.Layer, userConf.Layer)

normalizeKeys(config)
templateConfig(config)
config.NormalizeKeys()
config.TemplateConfig()

InitTokens()

InitEnv()

log.Info("Finished Loading Configuration")

log.Dev("Loaded Config:\n" + jsonutils.ToJson(config.All()))
log.Dev("Loaded Token Configs:\n" + jsonutils.ToJson(tokensLayer.All()))
log.Dev("Loaded Config:\n" + jsonutils.ToJson(config.Layer.All()))
log.Dev("Loaded Token Configs:\n" + jsonutils.ToJson(tokenConf.Layer.All()))
}

func InitReload() {
defaultsConf.OnLoad(Load)
userConf.OnLoad(Load)
tokenConf.OnLoad(Load)
}

func InitEnv() {
ENV.PORT = strconv.Itoa(config.Int("service.port"))
ENV.PORT = strconv.Itoa(config.Layer.Int("service.port"))

ENV.LOG_LEVEL = strings.ToLower(config.String("loglevel"))
ENV.LOG_LEVEL = strings.ToLower(config.Layer.String("loglevel"))

ENV.API_URL = config.String("api.url")
ENV.API_URL = config.Layer.String("api.url")

var settings structure.SETTINGS

transformChildren(config, "settings.message.variables", transformVariables)
config.TransformChildren("settings.message.variables", transformVariables)

config.Unmarshal("settings", &settings)
config.Layer.Unmarshal("settings", &settings)

ENV.SETTINGS["*"] = &settings
}

func LoadDefaults() {
_, err := LoadFile(ENV.DEFAULTS_PATH, defaultsLayer, yaml.Parser())
_, err := defaultsConf.LoadFile(ENV.DEFAULTS_PATH, yaml.Parser())

if err != nil {
log.Warn("Could not Load Defaults", ENV.DEFAULTS_PATH)
}
}

func LoadConfig() {
_, err := LoadFile(ENV.CONFIG_PATH, userLayer, yaml.Parser())
_, err := userConf.LoadFile(ENV.CONFIG_PATH, yaml.Parser())

if err != nil {
_, fsErr := os.Stat(ENV.CONFIG_PATH)
Expand Down
16 changes: 8 additions & 8 deletions utils/config/tokens.go → internals/config/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"strconv"

"github.com/codeshelldev/secured-signal-api/utils/config/structure"
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
log "github.com/codeshelldev/secured-signal-api/utils/logger"
"github.com/knadh/koanf/parsers/yaml"
)
Expand All @@ -16,25 +16,25 @@ type TOKEN_CONFIG_ struct {
func LoadTokens() {
log.Debug("Loading Configs in ", ENV.TOKENS_DIR)

err := LoadDir("tokenconfigs", ENV.TOKENS_DIR, tokensLayer, yaml.Parser())
err := tokenConf.LoadDir("tokenconfigs", ENV.TOKENS_DIR, ".yml", yaml.Parser())

if err != nil {
log.Error("Could not Load Configs in ", ENV.TOKENS_DIR, ": ", err.Error())
}

normalizeKeys(tokensLayer)
tokenConf.NormalizeKeys()

templateConfig(tokensLayer)
tokenConf.TemplateConfig()
}

func InitTokens() {
apiTokens := config.Strings("api.tokens")
apiTokens := config.Layer.Strings("api.tokens")

var tokenConfigs []TOKEN_CONFIG_

transformChildrenUnderArray(tokensLayer, "tokenconfigs", "overrides.message.variables", transformVariables)
tokenConf.TransformChildrenUnderArray("tokenconfigs", "overrides.message.variables", transformVariables)

tokensLayer.Unmarshal("tokenconfigs", &tokenConfigs)
tokenConf.Layer.Unmarshal("tokenconfigs", &tokenConfigs)

overrides := parseTokenConfigs(tokenConfigs)

Expand All @@ -53,7 +53,7 @@ func InitTokens() {

// Set Blocked Endpoints on Config to User Layer Value
// => effectively ignoring Default Layer
config.Set("settings.access.endpoints", userLayer.Strings("settings.access.endpoints"))
config.Layer.Set("settings.access.endpoints", userConf.Layer.Strings("settings.access.endpoints"))
}

if len(apiTokens) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internals/proxy/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"slices"
"strings"

"github.com/codeshelldev/secured-signal-api/utils/config"
"github.com/codeshelldev/secured-signal-api/internals/config"
log "github.com/codeshelldev/secured-signal-api/utils/logger"
)

Expand Down
4 changes: 2 additions & 2 deletions internals/proxy/middlewares/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package middlewares
import (
"net/http"

"github.com/codeshelldev/secured-signal-api/utils/config"
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
"github.com/codeshelldev/secured-signal-api/internals/config"
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
)

type Context struct {
Expand Down
2 changes: 1 addition & 1 deletion internals/proxy/middlewares/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"strconv"

"github.com/codeshelldev/secured-signal-api/utils/config/structure"
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
log "github.com/codeshelldev/secured-signal-api/utils/logger"
request "github.com/codeshelldev/secured-signal-api/utils/request"
Expand Down
9 changes: 1 addition & 8 deletions internals/proxy/middlewares/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"net/http"
"strings"

"github.com/codeshelldev/secured-signal-api/utils/config/structure"
"github.com/codeshelldev/secured-signal-api/utils/jsonutils"
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
log "github.com/codeshelldev/secured-signal-api/utils/logger"
request "github.com/codeshelldev/secured-signal-api/utils/request"
)
Expand Down Expand Up @@ -103,9 +102,6 @@ func doBlock(body map[string]any, headers map[string]any, policies map[string]st
for field, policy := range allowed {
value, err := getField(field, body, headers)

log.Dev("Checking ", field, "...")
log.Dev("Got Value of ", jsonutils.ToJson(value))

if value == policy.Value && err == nil {
isExplictlyAllowed = true
cause = field
Expand All @@ -116,9 +112,6 @@ func doBlock(body map[string]any, headers map[string]any, policies map[string]st
for field, policy := range blocked {
value, err := getField(field, body, headers)

log.Dev("Checking ", field, "...")
log.Dev("Got Value of ", jsonutils.ToJson(value))

if value == policy.Value && err == nil {
isExplicitlyBlocked = true
cause = field
Expand Down
2 changes: 1 addition & 1 deletion internals/proxy/middlewares/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middlewares
import (
"net/http"

"github.com/codeshelldev/secured-signal-api/utils/config"
"github.com/codeshelldev/secured-signal-api/internals/config"
)

var Server Middleware = Middleware{
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"net/http"
"os"

config "github.com/codeshelldev/secured-signal-api/internals/config"
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
reverseProxy "github.com/codeshelldev/secured-signal-api/internals/proxy"
config "github.com/codeshelldev/secured-signal-api/utils/config"
"github.com/codeshelldev/secured-signal-api/utils/config/structure"
docker "github.com/codeshelldev/secured-signal-api/utils/docker"
log "github.com/codeshelldev/secured-signal-api/utils/logger"
)
Expand Down
Loading