-
Notifications
You must be signed in to change notification settings - Fork 6
/
file.go
275 lines (249 loc) · 7.55 KB
/
file.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
Copyright 2018 Mathieu Parent <math.parent@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"encoding/json"
"fmt"
"regexp"
"sync"
"github.com/lemonldap-ng-controller/lemonldap-ng-controller/internal/filesystem"
)
var validConfigurationName = regexp.MustCompile(`^lmConf-(\d+)\.js$`)
// Config defines a LemonLDAP::NG configuration loader
type Config struct {
sync.RWMutex
fs filesystem.Filesystem
configDir string
cfgNum int
overrides map[string]interface{}
vhosts map[string]*VHost
applications map[string]*Application
dirty bool
}
// NewConfig creates a new LemonLDAP::NG configuration loader
func NewConfig(fs filesystem.Filesystem, configDir string) *Config {
return &Config{
fs: fs,
configDir: configDir,
cfgNum: 1,
overrides: make(map[string]interface{}),
vhosts: make(map[string]*VHost),
applications: make(map[string]*Application),
}
}
// First returns the first configuration file name and number
func (c *Config) First() (string, int, error) {
return fmt.Sprintf("lmConf-%d.js", 1), 1, nil
}
// Last returns the current configuration file name and number
func (c *Config) Last() (string, int, error) {
c.RLock()
defer c.RUnlock()
return fmt.Sprintf("lmConf-%d.js", c.cfgNum), c.cfgNum, nil
}
// Next returns the following configuration file name and number
func (c *Config) Next() (string, int, error) {
c.RLock()
defer c.RUnlock()
return fmt.Sprintf("lmConf-%d.js", c.cfgNum+1), c.cfgNum + 1, nil
}
// Load loads a specific LemonLDAP::NG configuration
func (c *Config) Load(configName string) (map[string]interface{}, error) {
c.RLock()
defer c.RUnlock()
return c.loadNoLock(configName)
}
// Load loads a specific LemonLDAP::NG configuration
func (c *Config) loadNoLock(configName string) (map[string]interface{}, error) {
conf := make(map[string]interface{})
path := c.configDir + "/" + configName
content, err := c.fs.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Unable to read LemonLDAP::NG configuration file %s: %s", path, err)
}
err = json.Unmarshal(content, &conf)
if err != nil {
return nil, fmt.Errorf("Unable to parse LemonLDAP::NG configuration file %s: %s", path, err)
}
c.dirty = true
return conf, nil
}
// LoadFirst loads the first LemonLDAP::NG configuration
func (c *Config) LoadFirst() (map[string]interface{}, error) {
firstConfigName, _, _ := c.First()
return c.Load(firstConfigName)
}
// Save saves the current LemonLDAP::NG configuration as next
func (c *Config) Save() error {
c.Lock()
defer c.Unlock()
if !c.dirty {
return nil
}
nextConfigNum := c.cfgNum + 1
nextConfigName := fmt.Sprintf("lmConf-%d.js", c.cfgNum+1)
path := c.configDir + "/" + nextConfigName
conf, err := c.loadNoLock("lmConf-1.js")
if err != nil {
return err
}
for overridek, overridev := range c.overrides {
conf[overridek] = overridev
}
conf["cfgAuthor"] = "lemonldap-ng-controller"
conf["cfgNum"] = nextConfigNum
// Replace default reload url
reloadUrls, ok := conf["reloadUrls"].(map[string]interface{})
if ok {
if _, ok = reloadUrls["reload.example.com"]; ok {
delete(reloadUrls, "reload.example.com")
reloadUrls["localhost"] = "http://localhost/reload"
}
}
allExportedHeaders, ok := conf["exportedHeaders"].(map[string]interface{})
if !ok {
return fmt.Errorf("exportedHeaders should be a map, got %T", conf["exportedHeaders"])
}
allLocationRules, ok := conf["locationRules"].(map[string]interface{})
if !ok {
return fmt.Errorf("locationRules should be a map, got %T", conf["locationRules"])
}
for serverName, vhost := range c.vhosts {
allExportedHeaders[serverName] = vhost.ExportedHeaders
allLocationRules[serverName] = vhost.LocationRules
}
allApplications, ok := conf["applicationList"].(map[string]interface{})
if !ok {
return fmt.Errorf("applicationList should be a map, got %T", conf["applicationList"])
}
for _, a := range c.applications {
var cat map[string]interface{}
cat, ok = allApplications[a.Category].(map[string]interface{})
if !ok {
cat = make(map[string]interface{})
cat["type"] = "category"
cat["catname"] = a.Category
allApplications[a.Category] = cat
}
cat[a.Name] = map[string]interface{}{
"type": "application",
"options": map[string]string{
"description": a.Description,
"display": a.Display,
"logo": a.Logo,
"name": a.Name,
"uri": a.URI,
},
}
}
content, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return fmt.Errorf("Unable to encode LemonLDAP::NG configuration file %s: %s", nextConfigName, err)
}
err = c.fs.WriteFile(path, content, 0777)
if err != nil {
return fmt.Errorf("Unable to write LemonLDAP::NG configuration file %s: %s", path, err)
}
c.cfgNum++
c.ReloadLemonLDAPNG()
c.dirty = false
return nil
}
// stringifyKeysMapValue recurses into in and changes all instances of
// map[interface{}]interface{} to map[string]interface{}. This is useful to
// work around the impedence mismatch between JSON and YAML unmarshaling that's
// described here: https://github.com/go-yaml/yaml/issues/139
//
// Inspired by https://github.com/stripe/stripe-mock, MIT licensed
// and https://github.com/gohugoio/hugo/pull/4138
func stringifyYAMLMapKeys(in interface{}) interface{} {
switch in := in.(type) {
case []interface{}:
res := make([]interface{}, len(in))
for i, v := range in {
res[i] = stringifyYAMLMapKeys(v)
}
return res
case map[interface{}]interface{}:
res := make(map[string]interface{})
for k, v := range in {
res[fmt.Sprintf("%v", k)] = stringifyYAMLMapKeys(v)
}
return res
case map[string]interface{}:
for k, v := range in {
in[k] = stringifyYAMLMapKeys(v)
}
return in
default:
return in
}
}
// SetOverrides creates several new LemonLDAP::NG virtual hosts
func (c *Config) SetOverrides(overrides map[string]interface{}) error {
c.Lock()
defer c.Unlock()
m := map[string]interface{}{}
for k, v := range overrides {
m[k] = stringifyYAMLMapKeys(v)
}
c.overrides = m
c.dirty = true
return nil
}
// AddVHosts creates several new LemonLDAP::NG virtual hosts
func (c *Config) AddVHosts(vhosts map[string]*VHost) error {
c.Lock()
defer c.Unlock()
for _, vhost := range vhosts {
c.vhosts[vhost.ServerName] = &VHost{
vhost.ServerName,
vhost.LocationRules,
vhost.ExportedHeaders,
}
}
c.dirty = true
return nil
}
// DeleteVHosts deletes several LemonLDAP::NG virtual hosts
func (c *Config) DeleteVHosts(vhosts map[string]*VHost) error {
c.Lock()
defer c.Unlock()
for _, vhost := range vhosts {
delete(c.vhosts, vhost.ServerName)
}
c.dirty = true
return nil
}
// AddApplication creates a new LemonLDAP::NG application
func (c *Config) AddApplication(application *Application) error {
if application == nil {
return nil
}
c.Lock()
defer c.Unlock()
c.applications[application.Path()] = application
c.dirty = true
return nil
}
// DeleteApplication a LemonLDAP::NG application
func (c *Config) DeleteApplication(application *Application) error {
if application == nil {
return nil
}
c.Lock()
defer c.Unlock()
delete(c.applications, application.Path())
c.dirty = true
return nil
}