-
Notifications
You must be signed in to change notification settings - Fork 23
/
acl.go
317 lines (296 loc) · 10.5 KB
/
acl.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
//Package acl implements /node/:id/acl resource
package acl
import (
"errors"
"github.com/MG-RAST/Shock/shock-server/conf"
e "github.com/MG-RAST/Shock/shock-server/errors"
"github.com/MG-RAST/Shock/shock-server/logger"
"github.com/MG-RAST/Shock/shock-server/node"
"github.com/MG-RAST/Shock/shock-server/node/locker"
"github.com/MG-RAST/Shock/shock-server/request"
"github.com/MG-RAST/Shock/shock-server/responder"
"github.com/MG-RAST/Shock/shock-server/user"
"github.com/MG-RAST/golib/go-uuid/uuid"
"github.com/MG-RAST/golib/stretchr/goweb/context"
mgo "gopkg.in/mgo.v2"
"net/http"
"strings"
)
var (
validAclTypes = map[string]bool{"all": true, "read": true, "write": true, "delete": true, "owner": true,
"public_all": true, "public_read": true, "public_write": true, "public_delete": true}
)
// GET, POST, PUT, DELETE: /node/{nid}/acl/
// GET is the only action implemented here.
func AclRequest(ctx context.Context) {
nid := ctx.PathValue("nid")
rmeth := ctx.HttpRequest().Method
u, err := request.Authenticate(ctx.HttpRequest())
if err != nil && err.Error() != e.NoAuth {
request.AuthError(err, ctx)
return
}
// public user (no auth) can perform a GET operation with the proper node permissions
if u == nil {
if rmeth == "GET" && conf.ANON_READ {
u = &user.User{Uuid: "public"}
} else {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth)
return
}
}
// Load node by id
n, err := node.Load(nid)
if err != nil {
if err == mgo.ErrNotFound {
logger.Error("err@node_Acl: (node.Load) id=" + nid + ": " + e.NodeNotFound)
responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound)
return
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
err_msg := "err@node_Acl: (node.Load) id=" + nid + ": " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg)
return
}
}
// Only the owner, an admin, or someone with read access can view acl's.
//
// NOTE: If the node is publicly owned, then anyone can view all acl's. The owner can only
// be "public" when anonymous node creation (ANON_WRITE) is enabled in Shock config.
rights := n.Acl.Check(u.Uuid)
if n.Acl.Owner != u.Uuid && u.Admin == false && n.Acl.Owner != "public" && rights["read"] == false {
logger.Error("err@node_Acl: (Authenticate) id=" + nid + ": " + e.UnAuth)
responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth)
return
}
if rmeth == "GET" {
query := ctx.HttpRequest().URL.Query()
verbosity := ""
if _, ok := query["verbosity"]; ok {
verbosity = query.Get("verbosity")
}
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
} else {
responder.RespondWithError(ctx, http.StatusNotImplemented, "This request type is not implemented.")
}
return
}
// GET, POST, PUT, DELETE: /node/{nid}/acl/{type}
func AclTypedRequest(ctx context.Context) {
nid := ctx.PathValue("nid")
rtype := ctx.PathValue("type")
rmeth := ctx.HttpRequest().Method
query := ctx.HttpRequest().URL.Query()
verbosity := ""
if _, ok := query["verbosity"]; ok {
verbosity = query.Get("verbosity")
}
u, err := request.Authenticate(ctx.HttpRequest())
if err != nil && err.Error() != e.NoAuth {
request.AuthError(err, ctx)
return
}
if !validAclTypes[rtype] {
responder.RespondWithError(ctx, http.StatusBadRequest, "Invalid acl type")
return
}
// Load node by id
n, err := node.Load(nid)
if err != nil {
if err == mgo.ErrNotFound {
logger.Error("err@node_Acl: (node.Load) id=" + nid + ": " + e.NodeNotFound)
responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound)
return
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
err_msg := "err@node_Acl: (node.Load) id=" + nid + ": " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg)
return
}
}
// public user (no auth) can perform a GET operation given the proper node permissions
if u == nil {
rights := n.Acl.Check("public")
if rmeth == "GET" && conf.ANON_READ && (rights["read"] || n.Acl.Owner == "public") {
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
} else {
responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth)
return
}
}
// lock node
err = locker.NodeLockMgr.LockNode(nid)
if err != nil {
err_msg := "err@node_Acl: (LockNode) id=" + nid + ": " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
defer locker.NodeLockMgr.UnlockNode(nid)
// Users that are not an admin or the node owner can only delete themselves from an ACL.
if n.Acl.Owner != u.Uuid && u.Admin == false {
// Users that are not an admin or the node owner cannot remove public from ACL's.
if rtype == "public_read" || rtype == "public_write" || rtype == "public_delete" || rtype == "public_all" {
err_msg := "Users that are not node owners can only delete themselves from ACLs."
logger.Error("err@node_Acl: id=" + nid + ": " + err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
// Parse user list
ids, err := parseAclRequestTyped(ctx)
if err != nil {
err_msg := "err@node_Acl: (parseAclRequestTyped) id=" + nid + ": " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
if rmeth == "DELETE" {
if len(ids) != 1 || (len(ids) == 1 && ids[0] != u.Uuid) {
err_msg := "Users that are not node owners can delete only themselves from ACLs."
logger.Error("err@node_Acl: id=" + nid + ": " + err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
if rtype == "owner" {
err_msg := "Deleting node ownership is not a supported request type."
logger.Error("err@node_Acl: id=" + nid + ": " + err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
if rtype == "all" {
n.Acl.UnSet(ids[0], map[string]bool{"read": true, "write": true, "delete": true})
} else {
n.Acl.UnSet(ids[0], map[string]bool{rtype: true})
}
n.Save()
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
}
err_msg := "Users that are not node owners can only delete themselves from ACLs."
logger.Error("err@node_Acl: id=" + nid + ": " + err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
// At this point we know we're dealing with an admin or the node owner.
// Admins and node owners can view/edit/delete ACLs
if rmeth == "GET" {
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
} else if rmeth == "POST" || rmeth == "PUT" {
if rtype == "public_read" || rtype == "public_write" || rtype == "public_delete" || rtype == "public_all" {
if rtype == "public_read" {
n.Acl.Set("public", map[string]bool{"read": true})
} else if rtype == "public_write" {
n.Acl.Set("public", map[string]bool{"write": true})
} else if rtype == "public_delete" {
n.Acl.Set("public", map[string]bool{"delete": true})
} else if rtype == "public_all" {
n.Acl.Set("public", map[string]bool{"read": true, "write": true, "delete": true})
}
n.Save()
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
}
// Parse user list
ids, err := parseAclRequestTyped(ctx)
if err != nil {
err_msg := "err@node_Acl: (parseAclRequestTyped) id=" + nid + ": " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
if rtype == "owner" {
if len(ids) == 1 {
n.Acl.SetOwner(ids[0])
} else {
err_msg := "Too many users. Nodes may have only one owner."
logger.Error("err@node_Acl: id=" + nid + ": " + err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
} else if rtype == "all" {
for _, i := range ids {
n.Acl.Set(i, map[string]bool{"read": true, "write": true, "delete": true})
}
} else {
for _, i := range ids {
n.Acl.Set(i, map[string]bool{rtype: true})
}
}
n.Save()
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
} else if rmeth == "DELETE" {
if rtype == "public_read" || rtype == "public_write" || rtype == "public_delete" || rtype == "public_all" {
if rtype == "public_read" {
n.Acl.UnSet("public", map[string]bool{"read": true})
} else if rtype == "public_write" {
n.Acl.UnSet("public", map[string]bool{"write": true})
} else if rtype == "public_delete" {
n.Acl.UnSet("public", map[string]bool{"delete": true})
} else if rtype == "public_all" {
n.Acl.UnSet("public", map[string]bool{"read": true, "write": true, "delete": true})
}
n.Save()
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
}
// Parse user list
ids, err := parseAclRequestTyped(ctx)
if err != nil {
err_msg := "err@node_Acl: (parseAclRequestTyped) id=" + nid + ": " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
}
if rtype == "owner" {
err_msg := "Deleting ownership is not a supported request type."
logger.Error("err@node_Acl: id=" + nid + ": " + err_msg)
responder.RespondWithError(ctx, http.StatusBadRequest, err_msg)
return
} else if rtype == "all" {
for _, i := range ids {
n.Acl.UnSet(i, map[string]bool{"read": true, "write": true, "delete": true})
}
} else {
for _, i := range ids {
n.Acl.UnSet(i, map[string]bool{rtype: true})
}
}
n.Save()
responder.RespondWithData(ctx, n.Acl.FormatDisplayAcl(verbosity))
return
} else {
responder.RespondWithError(ctx, http.StatusNotImplemented, "This request type is not implemented.")
return
}
}
func parseAclRequestTyped(ctx context.Context) (ids []string, err error) {
var users []string
query := ctx.HttpRequest().URL.Query()
params, _, err := request.ParseMultipartForm(ctx.HttpRequest())
if _, ok := query["users"]; ok && err != nil && err.Error() == "request Content-Type isn't multipart/form-data" {
users = strings.Split(query.Get("users"), ",")
} else if params["users"] != "" {
users = strings.Split(params["users"], ",")
} else {
return nil, errors.New("Action requires list of comma separated usernames in 'users' parameter")
}
for _, v := range users {
if uuid.Parse(v) != nil {
ids = append(ids, v)
} else {
u := user.User{Username: v}
if err := u.SetMongoInfo(); err != nil {
return nil, err
}
ids = append(ids, u.Uuid)
}
}
return ids, nil
}