This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
272 lines (248 loc) · 7.36 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
package main
import (
"fmt"
"os"
"strings"
person_api "go.mozilla.org/person-api"
"go.mozilla.org/userplex/modules"
"go.mozilla.org/userplex/modules/authorizedkeys"
"go.mozilla.org/userplex/modules/aws"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const Version = "v1.0.5"
func main() {
app := cli.NewApp()
app.Name = "userplex"
app.Usage = "Propagate users from Mozilla's Person API to third party systems."
app.Version = Version
app.Authors = []*cli.Author{
{Name: "AJ Bahnken", Email: "ajvb@mozilla.com"},
{Name: "Julien Vehent", Email: "jvehent@mozilla.com"},
}
app.EnableBashCompletion = true
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Path to userplex config file",
EnvVars: []string{"USERPLEX_CONFIG_PATH"},
},
}
app.Commands = []*cli.Command{
{
Name: "aws",
Usage: "Operations within AWS",
Action: requiresSubcommand,
Subcommands: createModuleSubcommands(&aws.AWSModule{}),
},
{
Name: "authorizedkeys",
Usage: "Operations within authorizedkeys files",
Action: requiresSubcommand,
Subcommands: createModuleSubcommands(&authorizedkeys.AuthorizedKeysModule{}),
},
{
Name: "get-person",
Usage: "Get Person from Person API. Useful for finding the correct identifier",
ArgsUsage: "[user-identifier]",
Action: getPersonCmd,
},
}
err := app.Run(os.Args)
if err != nil {
log.Info(err)
}
}
func requiresSubcommand(c *cli.Context) error {
log.Error("You must specify a subcommand")
return nil
}
func createModuleSubcommands(module modules.Module) []*cli.Command {
return []*cli.Command{
{
Name: "create",
Usage: "Create user",
ArgsUsage: "[username]",
Action: func(c *cli.Context) error {
person, conf, moduleConfs, personClient := loadAndVerifyContext(c, module, true)
for _, mconf := range moduleConfs {
m := module.NewFromInterface(mconf, conf.Notifications, personClient)
err := m.Create(person.GetLDAPUsername(), person)
if err != nil {
log.Errorf("Error from module.Create for %s: %s", m.ModuleName(), err)
}
}
return nil
},
},
{
Name: "reset",
Usage: "Reset user credentials",
ArgsUsage: "[username]",
Action: func(c *cli.Context) error {
person, conf, moduleConfs, personClient := loadAndVerifyContext(c, module, true)
for _, mconf := range moduleConfs {
m := module.NewFromInterface(mconf, conf.Notifications, personClient)
err := m.Reset(person.GetLDAPUsername(), person)
if err != nil {
log.Errorf("Error from module.Reset for %s: %s", m.ModuleName(), err)
}
}
return nil
},
},
{
Name: "delete",
Usage: "Delete user",
ArgsUsage: "[username]",
Action: func(c *cli.Context) error {
person, conf, moduleConfs, personClient := loadAndVerifyContext(c, module, false)
for _, mconf := range moduleConfs {
m := module.NewFromInterface(mconf, conf.Notifications, personClient)
var err error
if person != nil {
err = m.Delete(person.GetLDAPUsername())
} else {
err = m.Delete(c.Args().Get(0))
}
if err != nil {
log.Errorf("Error from module.Delete for %s: %s", m.ModuleName(), err)
}
}
return nil
},
},
{
Name: "sync",
Usage: "Run sync operation",
Action: func(c *cli.Context) error {
conf, moduleConfs, personClient := loadConfigForSubcommand(c, module)
for _, mconf := range moduleConfs {
m := module.NewFromInterface(mconf, conf.Notifications, personClient)
err := m.Sync()
if err != nil {
log.Errorf("Error from module.Sync for %s: %s", m.ModuleName(), err)
}
}
return nil
},
},
{
Name: "verify",
Usage: "Verify users against Person API. Outputs report, use `sync` to fix discrepancies.",
Action: func(c *cli.Context) error {
conf, moduleConfs, personClient := loadConfigForSubcommand(c, module)
for _, mconf := range moduleConfs {
m := module.NewFromInterface(mconf, conf.Notifications, personClient)
err := m.Verify()
if err != nil {
log.Errorf("Error from module.Verify for %s: %s", m.ModuleName(), err)
}
}
return nil
},
},
}
}
func loadConfigForSubcommand(c *cli.Context, module modules.Module) (conf, []modules.Configuration, *person_api.Client) {
cfg, err := loadConf(c.String("config"))
if err != nil {
log.Fatalf("Couldn't load config: %s", err)
}
personClient, err := person_api.NewClient(
cfg.Person.PersonClientId,
cfg.Person.PersonClientSecret,
cfg.Person.PersonBaseURL,
cfg.Person.PersonAuth0URL,
)
if err != nil {
log.Fatalf("Could not create person api client: %s", err)
}
var moduleConfigs []modules.Configuration
switch module.(type) {
case *aws.AWSModule:
moduleConfigs = make([]modules.Configuration, len(cfg.Aws))
for i, c := range cfg.Aws {
moduleConfigs[i] = c
}
case *authorizedkeys.AuthorizedKeysModule:
moduleConfigs = make([]modules.Configuration, len(cfg.AuthorizedKeys))
for i, c := range cfg.AuthorizedKeys {
moduleConfigs[i] = c
}
}
return cfg, moduleConfigs, personClient
}
func loadAndVerifyContext(c *cli.Context, module modules.Module, exitOnError bool) (*person_api.Person, conf, []modules.Configuration, *person_api.Client) {
cfg, moduleConfigs, personClient := loadConfigForSubcommand(c, module)
username := c.Args().Get(0)
p, err := getPerson(personClient, username)
if err != nil {
if exitOnError {
log.Fatalf("Could not find user %s", username)
}
}
if p != nil && len(p.GetSSHPublicKeys()) == 0 {
if exitOnError {
log.Fatalf("User %s has no SSH public keys. A SSH public key must be added before userplex can be ran.", username)
}
}
if p != nil && len(p.GetPGPPublicKeys()) == 0 {
if exitOnError {
log.Fatalf("User %s has no PGP public keys. A PGP public key must be added before userplex can be ran.", username)
}
}
if len(moduleConfigs) == 0 {
log.Fatalf("No module configurations found for %s", module)
}
return p, cfg, moduleConfigs, personClient
}
func getPersonCmd(c *cli.Context) error {
cfg, err := loadConf(c.String("config"))
if err != nil {
log.Fatalf("Couldn't load config: %s", err)
}
personClient, err := person_api.NewClient(
cfg.Person.PersonClientId,
cfg.Person.PersonClientSecret,
cfg.Person.PersonBaseURL,
cfg.Person.PersonAuth0URL,
)
if err != nil {
log.Fatalf("Could not create person api client: %s", err)
}
username := c.Args().Get(0)
p, err := getPerson(personClient, username)
if err != nil {
log.Fatalf("Could not find user %s", username)
}
if p.PrimaryEmail.Value == "" {
log.Fatalf("Could not find user %s", username)
}
log.Infof("%s: %+v", p.GetLDAPUsername(), p)
return nil
}
func getPerson(personClient *person_api.Client, username string) (*person_api.Person, error) {
var (
p *person_api.Person
err error
)
if strings.Contains(username, "@mozilla.com") {
p, err = personClient.GetPersonByEmail(username)
if err != nil {
return nil, fmt.Errorf("Could not find user %s: %s", username, err)
}
} else {
p, err = personClient.GetPersonByUsername(username)
if err != nil || p.PrimaryEmail.Value == "" {
p, err = personClient.GetPersonByUserId(username)
if err != nil {
return nil, fmt.Errorf("Could not find user %s: %s", username, err)
}
}
}
if p.PrimaryEmail.Value == "" {
return nil, fmt.Errorf("Could not find user %s", username)
}
return p, err
}