-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
410 lines (351 loc) · 12.7 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package main
import (
"bufio"
"crypto/tls"
"crypto/x509"
"database/sql"
"errors"
"flag"
"fmt"
"github.com/Symantec/keymaster/lib/instrumentedwriter"
"github.com/Symantec/ldap-group-management/lib/userinfo"
"github.com/Symantec/ldap-group-management/lib/userinfo/ldapuserinfo"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v2"
"html/template"
"io/ioutil"
"log"
"log/syslog"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/Symantec/ldap-group-management/lib/authn"
)
type baseConfig struct {
HttpAddress string `yaml:"http_address"`
TLSCertFilename string `yaml:"tls_cert_filename"`
TLSKeyFilename string `yaml:"tls_key_filename"`
StorageURL string `yaml:"storage_url"`
TemplatesPath string `yaml:"templates_path"`
SMTPserver string `yaml:"smtp_server"`
SmtpSenderAddress string `yaml:"smtp_sender_address"`
ClientCAFilename string `yaml:"client_ca_filename"`
LogDirectory string `yaml:"log_directory"`
ClusterSharedSecretFilename string `yaml:"cluster_shared_secret_filename"`
SharedSecrets []string
Hostname string `yaml:"hostname"`
AutoGroups []string `yaml:"auto_add_to_groups"`
}
type AppConfigFile struct {
Base baseConfig `yaml:"base"`
OpenID authn.OpenIDConfig `yaml:"openid"`
SourceLDAP ldapuserinfo.UserInfoLDAPSource `yaml:"source_config"`
TargetLDAP ldapuserinfo.UserInfoLDAPSource `yaml:"target_config"`
}
type pendingUserActionsCacheEntry struct {
Expiration time.Time
Groups [][]string
}
type RuntimeState struct {
Config AppConfigFile
dbType string
db *sql.DB
Userinfo userinfo.UserInfo
UserSourceinfo userinfo.UserInfo
htmlTemplate *template.Template
sysLog *syslog.Writer
authenticator *authn.Authenticator
allUsersRWLock sync.RWMutex
allUsersCacheValue map[string]time.Time
pendingUserActionsCacheMutex sync.Mutex
pendingUserActionsCache map[string]pendingUserActionsCacheEntry
}
type GetGroups struct {
AllGroups []string `json:"allgroups"`
}
type GetUsers struct {
Users []string `json:"Users"`
}
type GetUserGroups struct {
UserName string `json:"Username"`
UserGroups []string `json:"usergroups"`
}
type GetGroupUsers struct {
GroupName string `json:"groupname"`
Groupusers []string `json:"Groupusers"`
}
type Response struct {
UserName string
Groups [][]string
Users []string
PendingActions [][]string
GroupName string
GroupManagedbyValue string
GroupUsers []string
}
type httpLogger struct {
AccessLogger *log.Logger
}
var (
Version = "No version provided"
configFilename = flag.String("config", "/etc/smallpoint/config.yml", "The filename of the configuration")
)
const (
metricsPath = "/metrics"
cacheRefreshDuration = 6 * time.Hour
descriptionAttribute = "self-managed"
cookieExpirationHours = 12
cookieName = "smallpointauth"
creategroupWebPagePath = "/create_group"
deletegroupWebPagePath = "/delete_group"
creategroupPath = "/create_group/"
deletegroupPath = "/delete_group/"
requestaccessPath = "/requestaccess"
allLDAPgroupsPath = "/allGroups"
pendingactionsPath = "/pending-actions"
pendingrequestsPath = "/pending-requests"
deleterequestsPath = "/deleterequests"
exitgroupPath = "/exitgroup"
loginPath = "/login"
approverequestPath = "/approve-request"
rejectrequestPath = "/reject-request"
addmembersbuttonPath = "/addmembers/"
addmembersPath = "/addmembers"
deletemembersPath = "/deletemembers"
deletemembersbuttonPath = "/deletemembers/"
createServiceAccWebPagePath = "/create_serviceaccount"
createServiceAccountPath = "/create_serviceaccount/"
groupinfoPath = "/group_info/"
changeownershipbuttonPath = "/change_owner/"
changeownershipPath = "/change_owner"
myManagedGroupsWebPagePath = "/my_managed_groups"
permissionmanageWebPagePath = "/permissionmanage"
permissionmanagePath = "/permissionmanage/"
getGroupsJSPath = "/getGroups.js"
getUsersJSPath = "/getUsers.js"
indexPath = "/"
authPath = "/auth/oidcsimple/callback"
cssPath = "/css/"
imagesPath = "/images/"
jsPath = "/js/"
)
func (l httpLogger) Log(record instrumentedwriter.LogRecord) {
if l.AccessLogger != nil {
l.AccessLogger.Printf("%s - %s [%s] \"%s %s %s\" %d %d \"%s\"\n",
record.Ip, record.Username, record.Time, record.Method,
record.Uri, record.Protocol, record.Status, record.Size, record.UserAgent)
}
}
func (state *RuntimeState) loadTemplates() (err error) {
state.htmlTemplate = template.New("main")
//Load extra templates
templatesPath := state.Config.Base.TemplatesPath
if _, err = os.Stat(templatesPath); err != nil {
return err
}
//Eventally this will include the customization path
templateFiles := []string{}
for _, templateFilename := range templateFiles {
templatePath := filepath.Join(templatesPath, templateFilename)
_, err = state.htmlTemplate.ParseFiles(templatePath)
if err != nil {
return err
}
}
/// Load the oter built in templates
extraTemplates := []string{commonCSSText, commonJSText, headerHTMLText,
footerHTMLText, sidebarHTMLText, myGroupsPageText, allGroupsPageText,
pendingRequestsPageText, pendingActionsPageText,
createGroupPageText, deleteGroupPageText,
simpleMessagePageText, addMembersToGroupPageText, groupInfoPageText,
createServiceAccountPageText, changeGroupOwnershipPageText,
deleteMembersFromGroupPageText, commonHeadText, permManagePageText}
for _, templateString := range extraTemplates {
_, err = state.htmlTemplate.Parse(templateString)
if err != nil {
return err
}
}
return nil
}
func getClusterSecretsFile(clusterSecretsFilename string) ([]string, error) {
file, err := os.Open(clusterSecretsFilename)
if err != nil {
return nil, err
}
defer file.Close()
var rarray []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if len(line) < 2 {
continue
}
rarray = append(rarray, line)
}
if err := scanner.Err(); err != nil {
return nil, err
}
if len(rarray) < 1 {
return nil, errors.New("empty cluster secretFile")
}
return rarray, nil
}
//parses initializes from the config file
func loadConfig(configFilename string) (RuntimeState, error) {
var state RuntimeState
if _, err := os.Stat(configFilename); os.IsNotExist(err) {
err = fmt.Errorf("mising config file failure. Filename=%s", configFilename)
return state, err
}
//ioutil.ReadFile returns a byte slice (i.e)(source)
source, err := ioutil.ReadFile(configFilename)
if err != nil {
err = errors.New("cannot read config file")
return state, err
}
//Unmarshall(source []byte,out interface{})decodes the source byte slice/value and puts them in out.
err = yaml.Unmarshal(source, &state.Config)
if err != nil {
err = errors.New("Cannot parse config file")
log.Printf("Source=%s", source)
return state, err
}
//Load extra templates
err = state.loadTemplates()
if err != nil {
return state, err
}
err = initDB(&state)
if err != nil {
return state, err
}
state.Userinfo = &state.Config.TargetLDAP
state.allUsersCacheValue = make(map[string]time.Time)
state.pendingUserActionsCache = make(map[string]pendingUserActionsCacheEntry)
state.UserSourceinfo = &state.Config.SourceLDAP
if len(state.Config.Base.ClusterSharedSecretFilename) > 1 {
state.Config.Base.SharedSecrets, err = getClusterSecretsFile(state.Config.Base.ClusterSharedSecretFilename)
if err != nil {
return state, err
}
}
//
state.authenticator = authn.NewAuthenticator(state.Config.OpenID, "smallpoint", nil,
state.Config.Base.SharedSecrets, nil,
nil)
for _, group := range state.Config.Base.AutoGroups {
GroupExistsornot, _, err := state.Userinfo.GroupnameExistsornot(group)
if err != nil {
return state, err
}
if !GroupExistsornot {
err = errors.New("Group " + group + " doesn't exist in CPE LDAP")
return state, err
}
}
return state, err
}
type mailAttributes struct {
RequestedUser string
OtherUser string
Groupname string
Browser string
OS string
Hostname string
}
func Usage() {
fmt.Fprintf(os.Stderr, "Usage of %s (version %s):\n", os.Args[0], Version)
flag.PrintDefaults()
}
func main() {
flag.Usage = Usage
flag.Parse()
state, err := loadConfig(*configFilename)
if err != nil {
panic(err)
}
//start to log
state.sysLog, err = syslog.New(syslog.LOG_NOTICE|syslog.LOG_AUTHPRIV, "smallpoint")
if err != nil {
log.Fatalf("System log failed")
}
defer state.sysLog.Close()
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc(authn.Oauth2redirectPath, state.authenticator.Oauth2RedirectPathHandler)
http.Handle(creategroupWebPagePath, http.HandlerFunc(state.creategroupWebpageHandler))
http.Handle(deletegroupWebPagePath, http.HandlerFunc(state.deletegroupWebpageHandler))
http.Handle(creategroupPath, http.HandlerFunc(state.createGrouphandler))
http.Handle(deletegroupPath, http.HandlerFunc(state.deleteGrouphandler))
http.Handle(requestaccessPath, http.HandlerFunc(state.requestAccessHandler))
http.HandleFunc(indexPath, state.defaultPathHandler)
http.Handle(allLDAPgroupsPath, http.HandlerFunc(state.allGroupsHandler))
http.Handle(pendingactionsPath, http.HandlerFunc(state.pendingActions))
http.Handle(pendingrequestsPath, http.HandlerFunc(state.pendingRequests))
http.Handle(deleterequestsPath, http.HandlerFunc(state.deleteRequests))
http.Handle(exitgroupPath, http.HandlerFunc(state.exitfromGroup))
http.Handle(approverequestPath, http.HandlerFunc(state.approveHandler))
http.Handle(rejectrequestPath, http.HandlerFunc(state.rejectHandler))
http.Handle(addmembersPath, http.HandlerFunc(state.addmemberstoGroupWebpageHandler))
http.Handle(addmembersbuttonPath, http.HandlerFunc(state.addmemberstoExistingGroup))
http.Handle(changeownershipPath, http.HandlerFunc(state.changeownershipWebpageHandler))
http.Handle(changeownershipbuttonPath, http.HandlerFunc(state.changeownership))
http.Handle(deletemembersPath, http.HandlerFunc(state.deletemembersfromGroupWebpageHandler))
http.Handle(deletemembersbuttonPath, http.HandlerFunc(state.deletemembersfromExistingGroup))
http.Handle(createServiceAccWebPagePath, http.HandlerFunc(state.createserviceAccountPageHandler))
http.Handle(createServiceAccountPath, http.HandlerFunc(state.createServiceAccounthandler))
http.Handle(groupinfoPath, http.HandlerFunc(state.groupInfoWebpage))
http.Handle(getGroupsJSPath, http.HandlerFunc(state.getGroupsJSHandler))
http.Handle(getUsersJSPath, http.HandlerFunc(state.getUsersJSHandler))
http.Handle(myManagedGroupsWebPagePath, http.HandlerFunc(state.myManagedGroupsHandler))
http.Handle(permissionmanageWebPagePath, http.HandlerFunc(state.permissionmanageWebpageHandler))
http.Handle(permissionmanagePath, http.HandlerFunc(state.permissionManageHandler))
fs := http.FileServer(http.Dir(state.Config.Base.TemplatesPath))
http.Handle(cssPath, fs)
http.Handle(imagesPath, fs)
http.Handle(jsPath, fs)
var clientCACertPool *x509.CertPool
if len(state.Config.Base.ClientCAFilename) > 0 {
clientCACertPool = x509.NewCertPool()
caCert, err := ioutil.ReadFile(state.Config.Base.ClientCAFilename)
if err != nil {
log.Fatalf("cannot read clientCA file err=%s", err)
}
clientCACertPool.AppendCertsFromPEM(caCert)
}
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
},
ClientAuth: tls.VerifyClientCertIfGiven,
ClientCAs: clientCACertPool,
}
l := &lumberjack.Logger{
Filename: filepath.Join(state.Config.Base.LogDirectory, "access"),
MaxSize: 20, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, // disabled by default
}
accessLogger := httpLogger{AccessLogger: log.New(l, "", 0)}
serviceServer := &http.Server{
Addr: state.Config.Base.HttpAddress,
Handler: instrumentedwriter.NewLoggingHandler(http.DefaultServeMux, accessLogger),
TLSConfig: tlsConfig,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
err = serviceServer.ListenAndServeTLS(state.Config.Base.TLSCertFilename, state.Config.Base.TLSKeyFilename)
if err != nil {
log.Fatalf("Failed to start service server, err=%s", err)
}
}