forked from mozilla-services/userplex
/
main.go
169 lines (153 loc) · 4.56 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent <ulfr@mozilla.com>
package main
import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
// modules
"github.com/mozilla-services/userplex/modules"
_ "github.com/mozilla-services/userplex/modules/authorizedkeys"
_ "github.com/mozilla-services/userplex/modules/aws"
_ "github.com/mozilla-services/userplex/modules/datadog"
"github.com/gorhill/cronexpr"
"github.com/mozilla-services/mozldap"
"gopkg.in/yaml.v2"
)
type conf struct {
Cron string
Ldap struct {
Uri, Username, Password, TLSCert, TLSKey, CACert string
Insecure, Starttls bool
cli mozldap.Client `yaml:"-",json:"-"`
}
Notifications struct {
Email struct {
Host, From, Cc, ReplyTo string
Port int
Auth struct {
User, Pass string
}
}
}
UidMap []struct {
LdapUid string
UsedUid string
}
Modules []modules.Configuration
}
var config = flag.String("c", "config.yaml", "Load configuration from file")
var dryrun = flag.Bool("dry", false, "Dry run, don't create/delete, just show stuff")
var drynotif = flag.Bool("drynotif", false, "Same as dry run, but sends notifications out")
var once = flag.Bool("once", false, "Run only once and exit, don't under the cron loop")
var runmod = flag.String("module", "all", "Module to run. if 'all', run all available modules (default)")
func main() {
var (
err error
conf conf
cli mozldap.Client
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s - Manage users in various SaaS based on a LDAP source\n"+
"Usage: %s -c config.yaml\n",
os.Args[0], os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *drynotif {
*dryrun = true
}
// safeguard, remove in prod
*dryrun = true
*drynotif = true
// load the local configuration file
fd, err := ioutil.ReadFile(*config)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(fd, &conf)
if err != nil {
log.Fatalf("error: %v", err)
}
// infinite loop, wake up at the cron period
for {
if !*once {
cexpr, err := cronexpr.Parse(conf.Cron)
if err != nil {
log.Fatal("failed to parse cron string %q: %v", conf.Cron, err)
}
// sleep until the next run is scheduled to happen
nrun := cexpr.Next(time.Now())
waitduration := nrun.Sub(time.Now())
log.Printf("[info] next run will start at %v (in %v)", nrun, waitduration)
time.Sleep(waitduration)
}
// instanciate an ldap client
if conf.Ldap.TLSCert != "" && conf.Ldap.TLSKey != "" {
cli, err = mozldap.NewTLSClient(
conf.Ldap.Uri,
conf.Ldap.Username,
conf.Ldap.Password,
conf.Ldap.TLSCert,
conf.Ldap.TLSKey,
conf.Ldap.CACert,
&tls.Config{InsecureSkipVerify: conf.Ldap.Insecure})
} else {
cli, err = mozldap.NewClient(
conf.Ldap.Uri,
conf.Ldap.Username,
conf.Ldap.Password,
conf.Ldap.CACert,
&tls.Config{InsecureSkipVerify: conf.Ldap.Insecure},
conf.Ldap.Starttls)
}
if err != nil {
log.Fatal(err)
}
defer cli.Close()
log.Printf("connected %s on %s:%d, tls:%v starttls:%v\n", cli.BaseDN, cli.Host, cli.Port, cli.UseTLS, cli.UseStartTLS)
conf.Ldap.cli = cli
// Channel where modules publish their notifications
// which are aggregated and sent by the main program
notifchan := make(chan modules.Notification)
notifdone := make(chan bool)
go processNotifications(conf, notifchan, notifdone)
// store the value of dryrun and the ldap client
// in the configuration of each module
for i := range conf.Modules {
conf.Modules[i].DryRun = *dryrun
conf.Modules[i].LdapCli = cli
conf.Modules[i].Notify.Channel = notifchan
}
// run each module in the order it appears in the configuration
for _, modconf := range conf.Modules {
if *runmod != "all" && *runmod != modconf.Name {
continue
}
if _, ok := modules.Available[modconf.Name]; !ok {
log.Printf("[warning] %s module not registered, skipping it", modconf.Name)
continue
}
log.Println("[info] invoking module", modconf.Name)
run := modules.Available[modconf.Name].NewRun(modconf)
err = run.Run()
if err != nil {
log.Printf("[error] %s module failed with error: %v", modconf.Name, err)
}
}
// Modules are done, close the notification channel to tell the goroutine
// that it can aggregate and send them, and wait for notifdone to come back
close(notifchan)
<-notifdone
if *once {
break
}
}
}