Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
a3cded2
add transforms to structs
CodeShellDev Oct 29, 2025
ca81663
add logging
CodeShellDev Oct 29, 2025
84b57b9
added new normalization func
CodeShellDev Oct 29, 2025
9530fa6
debugging nil values
CodeShellDev Oct 29, 2025
ca98ea1
more debugging
CodeShellDev Oct 29, 2025
5199ef4
handle root
CodeShellDev Oct 29, 2025
7327298
testing empty tags
CodeShellDev Oct 29, 2025
643c673
debugging early return
CodeShellDev Oct 29, 2025
0f36ece
fix strucschema
CodeShellDev Oct 29, 2025
3be831e
more debugging
CodeShellDev Oct 29, 2025
b619429
testing
CodeShellDev Oct 29, 2025
c9761ed
more debugging
CodeShellDev Oct 29, 2025
01a2a95
recusively traverse config
CodeShellDev Oct 30, 2025
714d60c
lowercase transformkeys
CodeShellDev Oct 30, 2025
b9913fd
rename childrentransform to childtransform
CodeShellDev Oct 30, 2025
4fd942f
give fullkey as key param instead of lastkey
CodeShellDev Oct 30, 2025
f46a990
fixed incorrect `.` at the beginning of fullkey
CodeShellDev Oct 30, 2025
1984d84
stop dot-appending if newkey is empty
CodeShellDev Oct 30, 2025
632c07e
debugging transformmap structure
CodeShellDev Oct 30, 2025
8a50b9a
fix typo in fieldPolicies
CodeShellDev Oct 30, 2025
e2e71d9
debugging incoming keys and transform in `applyTransform()`
CodeShellDev Oct 30, 2025
10aec59
investigate foreach skipping nested maps
CodeShellDev Oct 30, 2025
dd0ff10
added `Unflatten()` to use for transforming
CodeShellDev Oct 30, 2025
1b1e4b7
more debugging
CodeShellDev Oct 30, 2025
60a8c4f
fix defaults
CodeShellDev Oct 30, 2025
022d4e5
debugging config map for missing keys
CodeShellDev Oct 30, 2025
3cd42cb
fixed map overriding
CodeShellDev Oct 30, 2025
d4f4d44
rename for clarity
CodeShellDev Oct 30, 2025
349f985
only overwrite if no transformtarget present
CodeShellDev Oct 30, 2025
4500293
debugging transformmap changes afer walking recursively
CodeShellDev Oct 30, 2025
22bbdd4
fixed maps.Copy writing to src instead of dst
CodeShellDev Oct 30, 2025
78ab3ab
debugging keyparts array
CodeShellDev Oct 31, 2025
c54c0b7
debugging transformfunc's output
CodeShellDev Oct 31, 2025
ab1c920
fixed `uppercaseTransform()` lowercasing
CodeShellDev Oct 31, 2025
483bfcc
debugging output of applyTransform
CodeShellDev Oct 31, 2025
16896eb
only output lastkey instead of fullkey in `applyTransformToAny()`
CodeShellDev Oct 31, 2025
b8a5743
debug config loading
CodeShellDev Oct 31, 2025
a389a1a
delete config before loading
CodeShellDev Oct 31, 2025
f942065
misintepreted 2nd arg of `confmap.Provider()` being path
CodeShellDev Oct 31, 2025
bdba73d
cleanup code for release
CodeShellDev Oct 31, 2025
f1ec3ef
debug duplicate / old data in result
CodeShellDev Oct 31, 2025
135298b
remove unneeded debug
CodeShellDev Oct 31, 2025
e8b4d30
debugging loading via confmap
CodeShellDev Oct 31, 2025
598e023
compare before and after deletion
CodeShellDev Oct 31, 2025
04af17c
added custom `Delete()` for supporting unflattened paths
CodeShellDev Oct 31, 2025
26a4b9b
unflatten data and debug
CodeShellDev Oct 31, 2025
6cfd66b
fix recursive deletion
CodeShellDev Oct 31, 2025
d92e231
use flattened keys for deletion
CodeShellDev Oct 31, 2025
c84a2fd
more debugging
CodeShellDev Oct 31, 2025
8abdd35
investigating `Unflatten()` incorrectly unflattening
CodeShellDev Oct 31, 2025
fc356f5
save data in subpath
CodeShellDev Oct 31, 2025
d243615
delete whole structure
CodeShellDev Oct 31, 2025
653b312
investigate settingsdata being outside of `settings`
CodeShellDev Oct 31, 2025
f70457b
searching deeper for incorrect unmarshaling of settings
CodeShellDev Oct 31, 2025
18cf068
checking for issues in mergelayers
CodeShellDev Oct 31, 2025
e45eccc
found issues at mergelayers
CodeShellDev Oct 31, 2025
9d4b0fd
rename to mainConf
CodeShellDev Oct 31, 2025
19aea10
clear configs before reload
CodeShellDev Oct 31, 2025
9a99130
test again with fixed reloading
CodeShellDev Oct 31, 2025
1780b9d
unwatch after reload
CodeShellDev Oct 31, 2025
a24a1bb
added `config.Load(data, path)` to replace confmap procedures
CodeShellDev Oct 31, 2025
643431b
debugging new Load func
CodeShellDev Oct 31, 2025
e6491fd
fix empty key
CodeShellDev Oct 31, 2025
bd087e3
remove debugs / cleanup
CodeShellDev Oct 31, 2025
e2cde35
merge after normilization
CodeShellDev Nov 1, 2025
7983215
fix reloading for tokens
CodeShellDev Nov 1, 2025
ed65286
updated tokens normilization
CodeShellDev Nov 1, 2025
5d953c1
fix loading of tokens after normilization
CodeShellDev Nov 1, 2025
fafe74c
remove leftover line
CodeShellDev Nov 1, 2025
2d8fc17
renamed struct
CodeShellDev Nov 1, 2025
803ff89
debugging token configs
CodeShellDev Nov 1, 2025
93aba3e
remove debug
CodeShellDev Nov 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions data/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ settings:

access:
endpoints:
- !/v1/about
- !/v1/configuration
- !/v1/devices
- !/v1/register
- !/v1/unregister
- !/v1/qrcodelink
- !/v1/accounts
- !/v1/contacts
- "!/v1/about"
- "!/v1/configuration"
- "!/v1/devices"
- "!/v1/register"
- "!/v1/unregister"
- "!/v1/qrcodelink"
- "!/v1/accounts"
- "!/v1/contacts"
87 changes: 68 additions & 19 deletions internals/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ var ENV *structure.ENV = &structure.ENV{
INSECURE: false,
}

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

var config = configutils.New()
var mainConf *configutils.Config

func Load() {
Clear()

InitReload()

LoadDefaults()
Expand All @@ -42,39 +44,90 @@ func Load() {

userConf.LoadEnv()

config.MergeLayers(defaultsConf.Layer, userConf.Layer)
NormalizeConfig(defaultsConf)
NormalizeConfig(userConf)

NormalizeTokens()

mainConf.MergeLayers(defaultsConf.Layer, userConf.Layer)

config.NormalizeKeys()
config.TemplateConfig()
mainConf.TemplateConfig()

InitTokens()

InitEnv()

log.Info("Finished Loading Configuration")

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

func Clear() {
defaultsConf = configutils.New()
userConf = configutils.New()
tokenConf = configutils.New()
mainConf = configutils.New()
}

func LowercaseKeys(config *configutils.Config) {
data := map[string]any{}

for _, key := range config.Layer.Keys() {
lower := strings.ToLower(key)

data[lower] = config.Layer.Get(key)
}

config.Layer.Delete("")
config.Load(data, "")
}

func NormalizeConfig(config *configutils.Config) {
Normalize(config, "settings", &structure.SETTINGS{})
}

func Normalize(config *configutils.Config, path string, structure any) {
data := config.Layer.Get(path)
old, ok := data.(map[string]any)

if !ok {
log.Warn("Could not load `"+path+"`")
return
}

// Create temporary config
tmpConf := configutils.New()
tmpConf.Load(old, "")

// Apply transforms to the new config
tmpConf.ApplyTransformFuncs(structure, "", transformFuncs)

// Lowercase actual config
LowercaseKeys(config)

// Load temporary config back into paths
config.Layer.Delete(path)

config.Load(tmpConf.Layer.All(), path)
}

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

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

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

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

var settings structure.SETTINGS

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

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

ENV.SETTINGS["*"] = &settings
}
Expand All @@ -101,8 +154,4 @@ func LoadConfig() {

log.Error("Could not Load Config ", ENV.CONFIG_PATH, ": ", err.Error())
}
}

func transformVariables(key string, value any) (string, any) {
return strings.ToUpper(key), value
}
}
23 changes: 23 additions & 0 deletions internals/config/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import (
"strings"
)

var transformFuncs = map[string]func(string, any) (string, any) {
"default": defaultTransform,
"lower": lowercaseTransform,
"upper": uppercaseTransform,
}

func defaultTransform(key string, value any) (string, any) {
return key, value
}

func lowercaseTransform(key string, value any) (string, any) {
return strings.ToLower(key), value
}

func uppercaseTransform(key string, value any) (string, any) {
return strings.ToUpper(key), value
}
24 changes: 12 additions & 12 deletions internals/config/structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ type ENV struct {
PORT string
API_URL string
API_TOKENS []string
SETTINGS map[string]*SETTINGS
SETTINGS map[string]*SETTINGS `koanf:"settings"`
INSECURE bool
}

type SETTINGS struct {
ACCESS ACCESS_SETTINGS `koanf:"access"`
MESSAGE MESSAGE_SETTINGS `koanf:"message"`
ACCESS ACCESS_SETTINGS `koanf:"access" transform:"lower"`
MESSAGE MESSAGE_SETTINGS `koanf:"message" transform:"lower"`
}

type MESSAGE_SETTINGS struct {
VARIABLES map[string]any `koanf:"variables"`
FIELD_MAPPINGS map[string][]FieldMapping `koanf:"fieldmappings"`
TEMPLATE string `koanf:"template"`
VARIABLES map[string]any `koanf:"variables" childtransform:"upper"`
FIELD_MAPPINGS map[string][]FieldMapping `koanf:"fieldmappings" childtransform:"default"`
TEMPLATE string `koanf:"template" transform:"lower"`
}

type FieldMapping struct {
Field string `koanf:"field"`
Score int `koanf:"score"`
Field string `koanf:"field" transform:"lower"`
Score int `koanf:"score" transform:"lower"`
}

type ACCESS_SETTINGS struct {
ENDPOINTS []string `koanf:"endpoints"`
FIELD_POLOCIES map[string]FieldPolicy `koanf:"fieldpolicies"`
ENDPOINTS []string `koanf:"endpoints" transform:"lower"`
FIELD_POLICIES map[string]FieldPolicy `koanf:"fieldpolicies" transform:"lower" childtransform:"default"`
}

type FieldPolicy struct {
Value any `koanf:"value"`
Action string `koanf:"action"`
Value any `koanf:"value" transform:"lower"`
Action string `koanf:"action" transform:"lower"`
}
31 changes: 22 additions & 9 deletions internals/config/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"strconv"

"github.com/codeshelldev/secured-signal-api/internals/config/structure"
"github.com/codeshelldev/secured-signal-api/utils/configutils"
log "github.com/codeshelldev/secured-signal-api/utils/logger"
"github.com/knadh/koanf/parsers/yaml"
)

type TOKEN_CONFIG_ struct {
type TOKEN_CONFIG struct {
TOKENS []string `koanf:"tokens"`
OVERRIDES structure.SETTINGS `koanf:"overrides"`
}
Expand All @@ -22,17 +23,29 @@ func LoadTokens() {
log.Error("Could not Load Configs in ", ENV.TOKENS_DIR, ": ", err.Error())
}

tokenConf.NormalizeKeys()

tokenConf.TemplateConfig()
}

func InitTokens() {
apiTokens := config.Layer.Strings("api.tokens")
func NormalizeTokens() {
configArray := []map[string]any{}

for _, config := range tokenConf.Layer.Slices("tokenconfigs") {
tmpConf := configutils.New()
tmpConf.Load(config.All(), "")

Normalize(tmpConf, "overrides", &structure.SETTINGS{})

configArray = append(configArray, tmpConf.Layer.All())
}

// Merge token configs together into new temporary config
tokenConf.Layer.Set("tokenconfigs", configArray)
}

var tokenConfigs []TOKEN_CONFIG_
func InitTokens() {
apiTokens := mainConf.Layer.Strings("api.tokens")

tokenConf.TransformChildrenUnderArray("tokenconfigs", "overrides.message.variables", transformVariables)
var tokenConfigs []TOKEN_CONFIG

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

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

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

if len(apiTokens) > 0 {
Expand All @@ -63,7 +76,7 @@ func InitTokens() {
}
}

func parseTokenConfigs(configs []TOKEN_CONFIG_) map[string]structure.SETTINGS {
func parseTokenConfigs(configs []TOKEN_CONFIG) map[string]structure.SETTINGS {
settings := map[string]structure.SETTINGS{}

for _, config := range configs {
Expand Down
4 changes: 2 additions & 2 deletions internals/proxy/middlewares/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func policyHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
settings := getSettingsByReq(req)

policies := settings.ACCESS.FIELD_POLOCIES
policies := settings.ACCESS.FIELD_POLICIES

if policies == nil {
policies = getSettings("*").ACCESS.FIELD_POLOCIES
policies = getSettings("*").ACCESS.FIELD_POLICIES
}

body, err := request.GetReqBody(req)
Expand Down
Loading