-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
153 lines (129 loc) · 3.49 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
// SPDX-FileCopyrightText: 2021 Alvar Penning
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"os"
"os/signal"
"syscall"
"time"
log "github.com/sirupsen/logrus"
)
const (
// EnvDebug is the SYNC_DEBUG environment variable.
//
// If SYNC_DEBUG is set, the verbose debug log level will be used. This will
// log sensitive data.
EnvDebug = "SYNC_DEBUG"
// EnvInterval is the SYNC_INTERVAL environment variable.
//
// If SYNC_INTERVAL is set, scheduled syncs will be performed. The variables
// value needs to be a valid Go time.Duration string:
// <https://golang.org/pkg/time/#ParseDuration>
EnvInterval = "SYNC_INTERVAL"
)
// syncAction performs a single LDAP to PostgreSQL sync.
func syncAction() {
log.Info("Starting LDAP sync")
startTime := time.Now()
defer func() {
endTime := time.Now()
log.WithField("time", endTime.Sub(startTime)).Info("Finished LDAP sync")
}()
db, err := sqlOpen()
if err != nil {
log.WithError(err).Error("Cannot establish database connection")
return
}
defer db.Close()
users, err := sqlFetchUsers(db)
if err != nil {
log.WithError(err).Error("Cannot fetch users from SQL")
return
}
log.WithField("amount", len(users)).Debug("Fetched users from SQL")
ldap, err := ldapDial()
if err != nil {
log.WithError(err).Error("Cannot establish LDAP connection")
return
}
defer ldap.Close()
var updateUserAttrs []map[string]string
for user, userAttrSql := range users {
userAttrLdap, err := ldapUserSearch(ldap, user)
if err != nil {
log.WithField("user", user).WithError(err).Error("Failed to query LDAP user")
continue
}
log.WithFields(log.Fields{
"user": user,
"SQL data": userAttrSql,
"LDAP data": userAttrLdap,
}).Debug("Fetched user data")
changed := false
for attr, ldapV := range userAttrLdap {
sqlV := userAttrSql[attr]
if ldapV != sqlV {
log.WithFields(log.Fields{
"user": user,
"attribute": attr,
"old": sqlV,
"new": ldapV,
}).Debug("User attribute has changed")
changed = true
}
}
if changed {
updateUserAttrs = append(updateUserAttrs, userAttrLdap)
log.WithField("user", user).Info("User has changed")
}
}
if len(updateUserAttrs) == 0 {
return
}
if err = sqlUpdateUser(db, updateUserAttrs); err != nil {
log.WithError(err).Error("Failed to perform SQL update")
} else {
log.WithField("updates", len(updateUserAttrs)).Info("Updated SQL users")
}
}
// syncInterval performs scheduled syncs based on the EnvInterval environment variable.
func syncInterval(interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-ticker.C:
syncAction()
case <-sig:
log.Info("Received shutdown signal")
return
}
}
}
func main() {
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
DisableLevelTruncation: true,
PadLevelText: true,
})
if _, ok := os.LookupEnv(EnvDebug); ok {
log.SetLevel(log.DebugLevel)
}
var interval time.Duration
if intervalStr, ok := os.LookupEnv(EnvInterval); ok {
intervalShadow, err := time.ParseDuration(intervalStr)
if err != nil {
log.WithError(err).Fatalf("Cannot parse %s as a Go time.Duration", EnvInterval)
} else if intervalShadow <= 0 {
log.WithField("interval", intervalStr).Fatalf("Negative %s value", EnvInterval)
}
interval = intervalShadow
}
syncAction()
if interval > 0 {
syncInterval(interval)
}
}