1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  | 
package configuration
import (
	"fmt"
	"strings"
	"github.com/knadh/koanf"
	"github.com/knadh/koanf/providers/confmap"
	"github.com/authelia/authelia/v4/internal/configuration/schema"
	"github.com/authelia/authelia/v4/internal/utils"
)
func koanfGetKeys(ko *koanf.Koanf) (keys []string) {
	keys = ko.Keys()
	for key, value := range ko.All() {
		slc, ok := value.([]interface{})
		if !ok {
			continue
		}
		for _, item := range slc {
			m, mok := item.(map[string]interface{})
			if !mok {
				continue
			}
			for k := range m {
				full := fmt.Sprintf("%s[].%s", key, k)
				if !utils.IsStringInSlice(full, keys) {
					keys = append(keys, full)
				}
			}
		}
	}
	return keys
}
func koanfRemapKeys(val *schema.StructValidator, ko *koanf.Koanf, ds map[string]Deprecation) (final *koanf.Koanf, err error) {
	keys := ko.All()
	keys = koanfRemapKeysStandard(keys, val, ds)
	keys = koanfRemapKeysMapped(keys, val, ds)
	final = koanf.New(".")
	if err = final.Load(confmap.Provider(keys, "."), nil); err != nil {
		return nil, err
	}
	return final, nil
}
func koanfRemapKeysStandard(keys map[string]interface{}, val *schema.StructValidator, ds map[string]Deprecation) (keysFinal map[string]interface{}) {
	var (
		ok    bool
		d     Deprecation
		key   string
		value interface{}
	)
	keysFinal = make(map[string]interface{})
	for key, value = range keys {
		if d, ok = ds[key]; ok {
			if !d.AutoMap {
				val.Push(fmt.Errorf("invalid configuration key '%s' was replaced by '%s'", d.Key, d.NewKey))
				keysFinal[key] = value
				continue
			} else {
				val.PushWarning(fmt.Errorf("configuration key '%s' is deprecated in %s and has been replaced by '%s': "+
					"this has been automatically mapped for you but you will need to adjust your configuration to remove this message", d.Key, d.Version.String(), d.NewKey))
			}
			if !mapHasKey(d.NewKey, keys) && !mapHasKey(d.NewKey, keysFinal) {
				if d.MapFunc != nil {
					keysFinal[d.NewKey] = d.MapFunc(value)
				} else {
					keysFinal[d.NewKey] = value
				}
			}
			continue
		}
		keysFinal[key] = value
	}
	return keysFinal
}
func koanfRemapKeysMapped(keys map[string]interface{}, val *schema.StructValidator, ds map[string]Deprecation) (keysFinal map[string]interface{}) {
	var (
		key           string
		value         interface{}
		slc, slcFinal []interface{}
		ok            bool
		m             map[string]interface{}
		d             Deprecation
	)
	keysFinal = make(map[string]interface{})
	for key, value = range keys {
		if slc, ok = value.([]interface{}); !ok {
			keysFinal[key] = value
			continue
		}
		slcFinal = make([]interface{}, len(slc))
		for i, item := range slc {
			if m, ok = item.(map[string]interface{}); !ok {
				slcFinal[i] = item
				continue
			}
			itemFinal := make(map[string]interface{})
			for subkey, element := range m {
				prefix := fmt.Sprintf("%s[].", key)
				fullKey := prefix + subkey
				if d, ok = ds[fullKey]; ok {
					if !d.AutoMap {
						val.Push(fmt.Errorf("invalid configuration key '%s' was replaced by '%s'", d.Key, d.NewKey))
						itemFinal[subkey] = element
						continue
					} else {
						val.PushWarning(fmt.Errorf("configuration key '%s' is deprecated in %s and has been replaced by '%s': "+
							"this has been automatically mapped for you but you will need to adjust your configuration to remove this message", d.Key, d.Version.String(), d.NewKey))
					}
					newkey := strings.Replace(d.NewKey, prefix, "", 1)
					if !mapHasKey(newkey, m) && !mapHasKey(newkey, itemFinal) {
						if d.MapFunc != nil {
							itemFinal[newkey] = d.MapFunc(element)
						} else {
							itemFinal[newkey] = element
						}
					}
				} else {
					itemFinal[subkey] = element
				}
			}
			slcFinal[i] = itemFinal
		}
		keysFinal[key] = slcFinal
	}
	return keysFinal
}
  |