-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
MongoPermissions.java
435 lines (364 loc) · 17.3 KB
/
MongoPermissions.java
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*-
* ========================LICENSE_START=================================
* restheart-security
* %%
* Copyright (C) 2018 - 2020 SoftInstigate
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* =========================LICENSE_END==================================
*/
package org.restheart.security;
import static org.restheart.plugins.ConfigurablePlugin.argValue;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.JsonParseException;
import org.bson.BsonDocument;
import org.bson.BsonValue;
import org.restheart.ConfigurationException;
import org.restheart.exchange.Request;
import org.restheart.utils.BsonUtils;
import org.restheart.utils.LambdaUtils;
/**
* Encapsulates the permissions specific to the MongoService, definined by `mongo` property of the permission
*/
public class MongoPermissions {
final boolean allowManagementRequests;
final boolean allowBulkPatch;
final boolean allowBulkDelete;
final boolean allowWriteMode;
private final BsonDocument readFilter;
private final BsonDocument writeFilter;
// an hidden property is removed from the response a GET requests
final Set<String> hideProps = Sets.newHashSet();
// a protected property cannot be in the body of a write request, otherwise 403 FORBBIDEN is returned
final Set<String> protectProps = Sets.newHashSet();
// the value of an overridden property is set by the server
final Map<String, BsonValue> overrideProps = Maps.newHashMap();
// a forbidden query parameter cannot be specified in the request URL, otherwise 403 FORBBIDEN is returned
final Set<String> forbidQueryParams = Sets.newHashSet();
public MongoPermissions() {
this.allowManagementRequests = false;
this.allowBulkPatch = false;
this.allowBulkDelete = false;
this.allowWriteMode = false;
this.readFilter = null;
this.writeFilter = null;
}
MongoPermissions(BsonDocument readFilter, BsonDocument writeFilter, boolean allowManagementRequests,
boolean allowBulkPatch, boolean allowBulkDelete, boolean allowWriteMode,
Set<String> hideProps, Set<String> protectProps, Map<String, BsonValue> overrideProps, Set<String> forbidQueryParams) {
this.readFilter = readFilter == null ? null
: readFilter.isNull() ? null : BsonUtils.escapeKeys(readFilter.asDocument(), true).asDocument();
this.writeFilter = writeFilter == null ? null
: writeFilter.isNull() ? null : BsonUtils.escapeKeys(writeFilter.asDocument(), true).asDocument();
this.allowManagementRequests = allowManagementRequests;
this.allowBulkPatch = allowBulkPatch;
this.allowBulkDelete = allowBulkDelete;
this.allowWriteMode = allowWriteMode;
if (hideProps != null) {
this.hideProps.addAll(hideProps);
}
if (protectProps != null) {
this.protectProps.addAll(protectProps);
}
if (overrideProps != null) {
this.overrideProps.putAll(overrideProps);
}
if (overrideProps != null) {
this.forbidQueryParams.addAll(forbidQueryParams);
}
}
public static MongoPermissions of(Request<?> request) throws ConfigurationException, IllegalArgumentException {
return from(BaseAclPermission.getRaw(request));
}
@SuppressWarnings("unchecked")
public static MongoPermissions from(Object raw) throws ConfigurationException, IllegalArgumentException {
if (raw == null) {
return new MongoPermissions();
} else if (raw instanceof BsonDocument) {
return from((BsonDocument)raw);
} else if (raw instanceof Map) {
return from((Map<String, Object>)raw);
} else {
throw new IllegalArgumentException("MongoPemissions cannot be built from " + raw.getClass().getSimpleName());
}
}
public static MongoPermissions from(BsonDocument args) throws ConfigurationException {
if (args == null || args.isEmpty() || !args.containsKey("mongo") || !args.get("mongo").isDocument()) {
// return default values
return new MongoPermissions();
} else {
args = args.get("mongo").asDocument();
var _readFilter = args.get("readFilter");
if (!(_readFilter == null || _readFilter.isNull()) && !_readFilter.isDocument()) {
throw new ConfigurationException("Wrong permission: readFilter must be a JSON object or null");
}
var readFilter = _readFilter == null ? null
: _readFilter.isNull() ? null : BsonUtils.escapeKeys(_readFilter.asDocument(), true).asDocument();
var _writeFilter = args.get("writeFilter");
if (!(_writeFilter == null || _writeFilter.isNull()) && !_writeFilter.isDocument()) {
throw new ConfigurationException("Wrong permission: writeFilter must be a JSON object or null");
}
var writeFilter = _writeFilter == null ? null
: _writeFilter.isNull() ? null : BsonUtils.escapeKeys(_writeFilter.asDocument(), true).asDocument();
return new MongoPermissions(readFilter, writeFilter, parseBooleanArg(args, "allowManagementRequests"),
parseBooleanArg(args, "allowBulkPatch"), parseBooleanArg(args, "allowBulkDelete"),
parseBooleanArg(args, "allowWriteMode"), parseSetArg(args, "hideProps"),
parseSetArg(args, "protectProps"), parseMapArg(args, "overrideProps"), parseSetArg(args, "forbidQueryParams"));
}
}
public BsonDocument asBson() {
var map = new HashMap<String, Object>();
map.put("allowManagementRequests", this.allowManagementRequests);
map.put("allowBulkPatch", this.allowBulkPatch);
map.put("allowBulkDelete", this.allowBulkDelete);
map.put("allowWriteMode", this.allowWriteMode);
map.put("readFilter", this.readFilter);
map.put("writeFilter", this.writeFilter);
return BsonUtils.toBsonDocument(map);
}
@SuppressWarnings("unchecked")
public static MongoPermissions from(Map<String, Object> args) throws ConfigurationException {
if (args == null || args.isEmpty() || !args.containsKey("mongo") || !(args.get("mongo") instanceof Map<?,?>)) {
// return default values
return new MongoPermissions();
} else {
args = (Map<String, Object>) args.get("mongo");
BsonDocument readFilter = null;
BsonDocument writeFilter = null;
if (args.containsKey("readFilter")) {
try {
String __readFilter = argValue(args, "readFilter");
var _readFilter = BsonDocument.parse(__readFilter);
if (!(_readFilter == null || _readFilter.isNull()) && !_readFilter.isDocument()) {
throw new IllegalArgumentException(
"Wrong permission: readFilter must be a JSON object or null");
}
readFilter = _readFilter == null ? null
: _readFilter.isNull() ? null
: BsonUtils.escapeKeys(_readFilter.asDocument(), true).asDocument();
} catch (ClassCastException | JsonParseException jpe) {
throw new ConfigurationException(
"Wrong permission: the readFilter is not a string containing a JSON Object", jpe);
}
} else {
readFilter = null;
}
if (args.containsKey("writeFilter")) {
try {
String __writeFilter = argValue(args, "writeFilter");
var _writeFilter = BsonDocument.parse(__writeFilter);
if (!(_writeFilter == null || _writeFilter.isNull()) && !_writeFilter.isDocument()) {
throw new ConfigurationException("writeFilter must be a JSON object or null");
}
writeFilter = _writeFilter == null ? null
: _writeFilter.isNull() ? null
: BsonUtils.escapeKeys(_writeFilter.asDocument(), true).asDocument();
} catch (ClassCastException | JsonParseException jpe) {
throw new ConfigurationException(
"Wrong permission: the writeFilter is not a string containing a JSON Object", jpe);
}
} else {
writeFilter = null;
}
return new MongoPermissions(readFilter, writeFilter, parseBooleanArg(args, "allowManagementRequests"),
parseBooleanArg(args, "allowBulkPatch"), parseBooleanArg(args, "allowBulkDelete"),
parseBooleanArg(args, "allowWriteMode"), parseSetArg(args, "hideProps"),
parseSetArg(args, "protectProps"), parseMapArg(args, "overrideProps"), parseSetArg(args, "forbidQueryParams"));
}
}
private static boolean parseBooleanArg(Map<String, Object> args, String key) throws ConfigurationException {
if (args.containsKey(key)) {
Object _value = argValue(args, key);
if (_value != null && _value instanceof Boolean) {
return (Boolean) _value;
} else {
throw new ConfigurationException("Wrong permission: mongo." + key + " must be a boolean");
}
} else {
// default value
return false;
}
}
private static Set<String> parseSetArg(Map<String, Object> args, String key) throws ConfigurationException {
if (args.containsKey(key)) {
Object _value = argValue(args, key);
if (_value != null && _value instanceof List<?>) {
HashSet<String> ret = Sets.newHashSet();
;
List<?> _set = (List<?>) _value;
for (var _entry : _set) {
if (_entry instanceof String) {
ret.add((String) _entry);
} else {
throw new ConfigurationException(
"Wrong permission: mongo." + key + " must be a list of strings");
}
}
return ret;
} else {
throw new ConfigurationException("Wrong permission: mongo." + key + " must be a list of strings");
}
} else {
return Sets.newHashSet();
}
}
private static Map<String, BsonValue> parseMapArg(Map<String, Object> args, String key)
throws ConfigurationException {
if (args.containsKey(key)) {
Object _value = argValue(args, key);
if (_value != null && _value instanceof Map<?, ?>) {
Map<String, BsonValue> ret = Maps.newHashMap();
var item = (Map<?, ?>) _value;
item.entrySet().stream().forEach(e -> {
var ikey = e.getKey();
var ivalue = e.getValue();
if (ikey instanceof String && ivalue instanceof String) {
try {
var svalue = ((String) ivalue).trim();
if (svalue.startsWith("@")) {
// quote value if starts with @
// this allows to use variables as follows
// userId: '@user._id' (and not the annoying userId: '"@user._id"')
svalue = "\"".concat(svalue).concat("\"");
}
ret.put((String) ikey, BsonUtils.parse(svalue));
} catch (Throwable t) {
var ex = new ConfigurationException("Wrong permission: mongo." + key
+ " must be an object. A valid example is:\n\toverrideProps:\n\t\tfoo: '\"bar\"'\n\t\tfoo: '{\"bar\": 1}'\n\t\tuser: \"@user._id\"",
t);
LambdaUtils.throwsSneakyException(ex);
}
} else {
var ex = new ConfigurationException("Wrong permission: mongo." + key
+ " must be an object. A valid example is:\n\toverrideProps:\n\t\tfoo: '\"bar\"'\n\t\tfoo: '{\"bar\": 1}'\n\t\tuser: \"@user._id\"");
LambdaUtils.throwsSneakyException(ex);
}
});
return ret;
} else {
throw new ConfigurationException("Wrong permission: mongo." + key
+ " must be an object. A valid example is:\n\toverrideProps:\n\t\tfoo: '\"bar\"'\n\t\tfoo: '{\"bar\": 1}'\n\t\tuser: \"@user._id\"");
}
} else {
return Maps.newHashMap();
}
}
private static boolean parseBooleanArg(BsonDocument args, String key) throws ConfigurationException {
if (args.containsKey(key)) {
var _value = args.get(key);
if (_value != null && _value.isBoolean()) {
return _value.asBoolean().getValue();
} else {
throw new ConfigurationException("Wrong permission: mongo." + key + " must be a boolean");
}
} else {
// default value
return false;
}
}
private static Set<String> parseSetArg(BsonDocument args, String key) throws ConfigurationException {
if (args.containsKey(key)) {
var _value = args.get(key);
if (_value != null && _value.isArray()) {
HashSet<String> ret = Sets.newHashSet();
;
var _array = _value.asArray();
for (var _entry : _array) {
if (_entry != null && _entry.isString()) {
ret.add(_entry.asString().getValue());
} else {
throw new ConfigurationException(
"Wrong permission: mongo." + key + " must be an array of strings");
}
}
return ret;
} else {
throw new ConfigurationException("Wrong permission: mongo." + key + " must be an array of strings");
}
} else {
return Sets.newHashSet();
}
}
private static Map<String, BsonValue> parseMapArg(BsonDocument args, String key) throws ConfigurationException {
if (args.containsKey(key)) {
var _value = args.get(key);
if (_value.isDocument()) {
HashMap<String, BsonValue> ret = Maps.newHashMap();
var doc = _value.asDocument();
doc.entrySet().stream().forEach(e -> ret.put(e.getKey(), e.getValue()));
return ret;
} else {
throw new ConfigurationException(
"Wrong permission: mongo." + key + " must be a JSON object {key1:json, key2:json, ..}");
}
} else {
return Maps.newHashMap();
}
}
/**
* @return the readFilter
*/
public BsonDocument getReadFilter() {
return readFilter == null || readFilter.isNull() ? null : BsonUtils.unescapeKeys(readFilter).asDocument();
}
/**
* @return the writeFilter
*/
public BsonDocument getWriteFilter() {
return writeFilter == null || writeFilter.isNull() ? writeFilter
: BsonUtils.unescapeKeys(writeFilter).asDocument();
}
public boolean getAllowManagementRequests() {
return this.allowManagementRequests;
}
public boolean isAllowManagementRequests() {
return this.allowManagementRequests;
}
public boolean getAllowBulkPatch() {
return this.allowBulkPatch;
}
public boolean isAllowBulkPatch() {
return this.allowBulkPatch;
}
public boolean getAllowBulkDelete() {
return this.allowBulkDelete;
}
public boolean isAllowBulkDelete() {
return this.allowBulkDelete;
}
public boolean getAllowWriteMode() {
return this.allowWriteMode;
}
public boolean isAllowWriteMode() {
return this.allowWriteMode;
}
public Set<String> getHideProps() {
return this.hideProps;
}
public Set<String> getProtectProps() {
return this.protectProps;
}
public Map<String, BsonValue> getOverrideProps() {
return this.overrideProps;
}
public Set<String> getForbidQueryParams() {
return this.forbidQueryParams;
}
}