-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraylog.go
More file actions
284 lines (260 loc) · 7.85 KB
/
graylog.go
File metadata and controls
284 lines (260 loc) · 7.85 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/sirupsen/logrus"
)
type (
// A Graylog user and associated roles
graylogUser struct {
Username string
Roles []string
}
// The response obtained when querying the Graylog server for a list of users.
graylogUsers struct {
Users []struct {
graylogUser
External bool
}
}
// Privilege information
privInfo struct {
otp, oid string // Type and identifier of object
priv int // Privilege level
}
)
var (
// Privilege levels
privLevels = map[string]int{
"read": 0,
"write": 1,
}
// Privilege level string representation
privStr = []string{"read", "write"}
// Graylog items on which privileges may be set
graylogItems = map[string]bool{
"dashboard": true,
"search": true,
"stream": true,
}
// Grayog privilege string templates
graylogPriv = map[string][]string{
"dashboard:read": {"dashboards:read:%s", "view:read:%s"},
"dashboard:write": {"dashboards:read:%s", "dashboards:edit:%s", "view:read:%s", "view:edit:%s"},
"search:read": {"view:read:%s"},
"search:write": {"view:read:%s", "view:edit:%s"},
"stream:read": {"streams:read:%s"},
"stream:write": {"streams:read:%s", "streams:edit:%s", "streams:changestate:%s"},
}
)
// Execute a Graylog API request, returning the status code and the body
func executeAPICall(cfg graylogConfig, method string, path string, data io.Reader) (status int, body []byte) {
log := log.WithFields(logrus.Fields{
"base": cfg.APIBase,
"username": cfg.Username,
"method": method,
"path": path,
})
log.Trace("Executing Graylog API call")
client := &http.Client{}
request, err := http.NewRequest(method, fmt.Sprintf("%s/%s", cfg.APIBase, path), data)
if err != nil {
log.WithField("error", err).Fatal("Could not create HTTP request")
}
request.SetBasicAuth(cfg.Username, cfg.Password)
if data != nil {
request.Header.Add("Content-Type", "application/json")
}
request.Header.Add("X-Requested-By", "graylog-groups")
response, err := client.Do(request)
if err != nil {
log.WithField("error", err).Fatal("Could not execute HTTP request")
}
defer response.Body.Close()
status = response.StatusCode
body, err = ioutil.ReadAll(response.Body)
if err != nil {
log.WithField("error", err).Fatal("Could not read Graylog response")
}
log.WithField("status", status).Trace("Executed Graylog API call")
return
}
// Get the list of Graylog users that have been imported from LDAP
func getGraylogUsers(configuration graylogConfig) (users []graylogUser) {
log.Trace("Getting users from the Graylog API")
status, body := executeAPICall(configuration, "GET", "users", nil)
if status != 200 {
log.WithField("status", status).Fatal("Could not read users")
}
data := graylogUsers{}
if err := json.Unmarshal(body, &data); err != nil {
log.WithField("error", err).Fatal("Could not parse Graylog's user list")
}
users = make([]graylogUser, 0)
for _, item := range data.Users {
if item.External {
users = append(users, item.graylogUser)
}
}
log.WithField("users", len(users)).Info("Obtained users from the Graylog API")
return
}
// List groups an user is a member of.
func getUserGroups(user string, membership ldapGroupMembers) (groups []string) {
groups = make([]string, 0)
for group, members := range membership {
for _, member := range members {
if member == user {
groups = append(groups, group)
break
}
}
}
return
}
// Compute roles that should apply to an user
func computeRoles(mapping groupMapping, membership []string) (roles []string) {
rset := make(map[string]bool)
for _, group := range membership {
for _, role := range mapping[group].Roles {
rset[role] = true
}
}
roles = make([]string, len(rset))
i := 0
for group := range rset {
roles[i] = group
i++
}
return
}
// Compute privilege levels for each Graylog object based on the user's group membership
func getObjectPrivileges(mapping groupMapping, membership []string) map[string]privInfo {
rset := make(map[string]privInfo)
for _, group := range membership {
for _, priv := range mapping[group].Privileges {
key := fmt.Sprintf("%s:%s", priv.Type, priv.ID)
record, ok := rset[key]
level := privLevels[priv.Level]
if ok && level <= record.priv {
continue
}
if !ok {
record.otp = priv.Type
record.oid = priv.ID
}
record.priv = level
rset[key] = record
}
}
return rset
}
// Compute privileges on Graylog objects that should be granted to an user
func computePrivileges(mapping groupMapping, membership []string) []string {
privileges := make([]string, 0)
for _, record := range getObjectPrivileges(mapping, membership) {
key := fmt.Sprintf("%s:%s", record.otp, privStr[record.priv])
for _, p := range graylogPriv[key] {
pval := fmt.Sprintf(p, record.oid)
privileges = append(privileges, pval)
}
}
return privileges
}
// Delete a Graylog user account
func deleteAccount(cfg graylogConfig, user string) {
log := log.WithField("user", user)
log.Warning("Deleting Graylog account")
code, body := executeAPICall(cfg, "DELETE", fmt.Sprintf("/users/%s", user), nil)
if code != 204 {
log.WithFields(logrus.Fields{
"status": code,
"body": string(body),
}).Error("Could not delete user")
}
}
// Returns the strings that are in a but not in b.
func getDifference(a []string, b []string) (diff []string) {
diff = make([]string, 0)
for _, sa := range a {
found := false
for _, sb := range b {
if sa == sb {
found = true
break
}
}
if !found {
diff = append(diff, sa)
}
}
return
}
// Set an account's roles and grant it access to Graylog objects
func setUserPrivileges(cfg graylogConfig, user graylogUser, roles []string, privileges []string) {
log := log.WithField("user", user.Username)
type perms struct {
Permissions []string `json:"permissions"`
}
p := perms{Permissions: privileges}
data, err := json.Marshal(p)
if err != nil {
log.WithField("error", err).Fatal("Unable to generate permissions JSON")
}
log.WithField("privileges", privileges).Info("Setting permissions")
code, body := executeAPICall(cfg, "PUT",
fmt.Sprintf("users/%s/permissions", user.Username),
bytes.NewBuffer(data))
if code != 204 {
log.WithFields(logrus.Fields{
"status": code,
"body": string(body),
}).Error("Could not set permissions")
}
placeholder := bytes.NewBuffer([]byte("{}"))
for _, role := range getDifference(roles, user.Roles) {
ep := fmt.Sprintf("roles/%s/members/%s", role, user.Username)
log.WithField("role", role).Info("Adding role")
code, body := executeAPICall(cfg, "PUT", ep, placeholder)
if code != 204 {
log.WithFields(logrus.Fields{
"status": code,
"body": string(body),
"role": role,
}).Error("Could not add role")
}
}
for _, role := range getDifference(user.Roles, roles) {
ep := fmt.Sprintf("roles/%s/members/%s", role, user.Username)
log.WithField("role", role).Info("Removing role")
code, body := executeAPICall(cfg, "DELETE", ep, nil)
if code != 204 {
log.WithFields(logrus.Fields{
"status": code,
"body": string(body),
"role": role,
}).Error("Could not remove role")
}
}
}
// Apply privilege mappings to the external Graylog users
func applyMapping(cfg configuration, users []graylogUser, groups ldapGroupMembers) {
for _, user := range users {
log := log.WithField("user", user.Username)
membership := getUserGroups(user.Username, groups)
log.WithField("groups", membership).Trace("Computed group membership")
roles := computeRoles(cfg.Mapping, membership)
log.WithField("roles", roles).Trace("Computed roles")
privileges := computePrivileges(cfg.Mapping, membership)
log.WithField("privileges", privileges).Trace("Computed privileges")
if cfg.Graylog.DeleteAccounts && len(roles) == 0 && len(privileges) == 0 {
deleteAccount(cfg.Graylog, user.Username)
} else {
setUserPrivileges(cfg.Graylog, user, roles, privileges)
}
}
}