-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
360 lines (289 loc) · 7.8 KB
/
main.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"log"
"os"
"time"
"os/exec"
"reflect"
"gopkg.in/yaml.v3"
)
type Config struct {
Debug bool `default:"false"`
Daemon bool `default:"true"`
Server string
Binddn string
Password string
Base string
Update int `yaml:"update_interval" default:"600"`
Host bool `yaml:"host_specific_entries" default:"true"`
Restart bool `yaml:"restart_service_on_change" default:"true"`
Override string `yaml:"override_hostname"`
}
type File struct {
DN string `ldap:"dn"`
Path string `ldap:"path"`
Description string `ldap:"description"`
CN string `ldap:"cn"`
ObjectClass []string `ldap:"objectClass"`
Data string `ldap:"data"`
Perm string `ldap:"permissions"`
ServiceName string `ldap:"serviceName"`
}
var (
paths map[string]string
needsUpdate []string
found bool
KeepalivedFiles map[string]string
)
func main() {
// load bureau config
conf, err := findConfig()
if err != nil {
log.Fatalf("Error reading config file: %v", err)
}
c := Config{}
err = yaml.Unmarshal([]byte(conf), &c)
if err != nil {
log.Fatalf("error: %v", err)
} else if c.Debug {
for i := 0; i <= 9; i++ {
key := reflect.Indirect(reflect.ValueOf(c)).Type().Field(i).Name
value := reflect.ValueOf(c)
if key == "Password" && c.Password != "" {
log.Printf(" === Loading configuration %v: %s\n", key, "***HIDDEN PASSWORD***")
} else {
log.Printf(" === Loading configuration %v: %v\n", key, value.FieldByName(key))
}
}
}
// get hostname and set search base
host, err := os.Hostname()
if err != nil {
log.Fatalf("error: %v", err)
} else if c.Debug {
log.Printf(" === Getting hostname: %s", host)
}
var hostdn string
if c.Host {
if c.Override != "" {
hostdn = "cn=" + c.Override + "," + c.Base
} else {
hostdn = "cn=" + host + "," + c.Base
}
} else {
hostdn = c.Base
}
if c.Debug {
log.Printf(" === Looking for files in: %s", hostdn)
}
// init paths map for tracking updates and data
paths = make(map[string]string)
KeepalivedFiles = make(map[string]string)
for {
// LDAP connect
l, err := LDAPConnect(c.Server)
if err != nil {
log.Fatalf("Connection error: %v\n", err)
}
// Bind and search for timestamps
result, err := LDAPSearch(l, c.Binddn, c.Password, hostdn, []string{"modifyTimestamp", "path"})
if err != nil {
log.Fatalf("LDAP search error: %v\n", err)
continue
}
// mark files that need updating
for _, entry := range result.Entries {
mostRecent, err := compareModificationTimes(entry.GetAttributeValue("path"), entry.GetAttributeValue("modifyTimestamp"))
if err != nil {
log.Printf("Could not compare modification times: %v\n", err)
}
switch mostRecent {
case "equal":
// nothing to do
case "file":
log.Printf("%s is outdated\n", entry.GetAttributeValue("path"))
needsUpdate = append(needsUpdate, entry.DN)
paths[entry.DN] = entry.GetAttributeValue("modifyTimestamp")
case "ldap":
log.Printf("%s is outdated\n", entry.DN)
fileData, err := os.ReadFile(entry.GetAttributeValue("path"))
if err != nil {
log.Printf("Could not read %s: %v\n", entry.GetAttributeValue("path"), err)
}
err = LDAPReplace(l, entry.DN, fileData)
if err != nil {
log.Printf("Could not update LDAP: %v\n", err)
}
log.Printf("%s has been updated", entry.DN)
}
}
// grab file data from LDAP
for _, dn := range needsUpdate {
// search for files needing updates
result, err = LDAPSearch(l, c.Binddn, c.Password, dn, []string{})
if err != nil {
// In case dn changes before we can search again
// Print error but do not exit
log.Printf("LDAP search error: %v\n", err)
continue
}
// generate files based on objectClass
entry := result.Entries[0]
found = false
for _, oc := range entry.GetAttributeValues("objectClass") {
switch oc {
case "keepalivedGlobalConfig":
if c.Debug {
log.Printf("Formatting keepalived config for %s at %s\n", entry.DN, entry.GetAttributeValues("path"))
}
err = FormatKeepalived(entry, "global")
if err != nil {
log.Fatalf("Formatting error: %v\n", err)
}
found = true
break
case "keepalivedVRRPGroupConfig":
if c.Debug {
log.Printf("Formatting keepalived config for %s at %s\n", entry.DN, entry.GetAttributeValues("path"))
}
err = FormatKeepalived(entry, "group")
if err != nil {
log.Fatalf("Formatting error: %v\n", err)
}
found = true
break
case "keepalivedVRRPInstanceConfig":
if c.Debug {
log.Printf("Formatting keepalived config for %s at %s\n", entry.DN, entry.GetAttributeValues("path"))
}
err = FormatKeepalived(entry, "instance")
if err != nil {
log.Fatalf("Formatting error: %v\n", err)
}
found = true
break
}
}
if found != true {
f := File{}
err = entry.Unmarshal(&f)
if err != nil {
log.Fatalf("Unmarshal error: %v\n", err)
}
if c.Debug {
log.Printf("Writing config file for %s at %s\n", f.CN, f.Path)
}
err = writeFile(f.Path, f.Data, f.Perm)
if err != nil {
log.Fatalf("File generation error: %v\n", err)
}
err = checkRestart(c.Restart, f.ServiceName)
if err != nil {
log.Printf("Service \"%s\" failed to restart: %v\n", f.ServiceName, err)
}
}
}
// write keepalived files
for file, data := range KeepalivedFiles {
if c.Debug {
log.Printf("Writing config file to %s\n", file)
}
err = writeFile(file, data, "")
if err != nil {
log.Fatalf("File generation error: %v\n", err)
// make this non-fatal later and then:
// continue
}
err = checkRestart(c.Restart, "keepalived")
if err != nil {
log.Printf("Service \"%s\" failed to restart: %v\n", "keepalived", err)
}
delete(KeepalivedFiles, file)
}
l.Close()
// loop if in daemon mode
if c.Daemon {
needsUpdate = nil
time.Sleep(time.Duration(c.Update) * time.Second)
} else {
os.Exit(0)
}
}
}
func compareModificationTimes(path string, timestamp string) (string, error) {
// parse file mod time
fileInfo, err := os.Stat(path)
if err != nil {
log.Fatalf("File read error for %s: %v\n", path, err)
}
fileMTime := fileInfo.ModTime().Truncate(60 * time.Second)
// parse ldap mod time
ldapMTime, _ := time.Parse("20060102150405Z0700", timestamp)
ldapMTime = ldapMTime.Truncate(60 * time.Second)
// compare
if fileMTime.Equal(ldapMTime) {
return "equal", nil
} else if ldapMTime.After(fileMTime) {
return "file", nil
} else {
return "ldap", nil
}
}
func writeFile(path string, data string, perm string) error {
// create tmp file
f, err := os.Create(path + ".tmp")
if err != nil {
return err
}
// set permissions
if perm == "" {
perm = "0600"
}
// use exec because of type issue with os.Chmod
cmd := exec.Command("chmod", perm, path+".tmp")
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
return err
}
// write to tmp file
_, err = f.WriteString(data)
defer f.Close()
if err != nil {
return err
}
// move into place if write is good
err = os.Rename(path+".tmp", path)
if err != nil {
return err
}
return nil
}
func findConfig() ([]byte, error) {
var conf string
home, err := os.UserHomeDir()
locations := []string{"bureau.yaml",
home + "/.bureau.yaml",
home + "/.config/bureau/bureau.yaml",
"/etc/bureau/bureau.yaml"}
for _, path := range locations {
if _, err = os.Stat(path); err == nil {
conf = path
break
}
}
data, err := os.ReadFile(conf)
if err != nil {
return nil, err
}
return data, nil
}
func checkRestart(enabled bool, svc string) error {
if enabled && svc != "" {
cmd := exec.Command("systemctl", "restart", svc)
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}