-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
372 lines (333 loc) · 10.2 KB
/
index.js
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
'use strict';
const { Helper, Model, DefaultFilteredAdapter, Filter } = require('casbin');
const { createHash } = require('crypto')
/**
*
* @param {*} client
* @param {*} params
*/
const find = async (client, params) => {
const data = (params.KeyConditionExpression) ? await client.query(params).promise() : await client.scan(params).promise();
if (data.LastEvaluatedKey) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
data.Items = data.Items.concat(await find(client, params));
}
return data.Items;
};
/**
*
* @param {*} client
* @param {*} params
*/
const batchWrite = async (client, params) => {
const data = await client.batchWrite(params).promise();
if (Object.keys(data.UnprocessedItems).length) {
params.RequestItems = data.UnprocessedItems;
await batchWrite(client, params);
}
return data;
};
/**
* Implements a policy adapter for Casbin with DynamoDB support.
*
* @class
*/
class CasbinDynamoDBAdapter {
/**
*
* @param {object} client DynamoDB Document Client
* @param {object} opts Options
*/
constructor(client, opts = {}) {
this.client = client;
this.tableName = opts.tableName;
this.hashKey = opts.hashKey;
this.params = { TableName: opts.tableName };
this.index = opts.index;
if (opts.index && opts.index.name && opts.index.hashKey && opts.index.hashValue) {
this.params.IndexName = opts.index.name;
this.params.KeyConditionExpression = `#${opts.index.hashKey} = :${opts.index.hashKey}`;
this.params.ExpressionAttributeNames = {};
this.params.ExpressionAttributeNames[`#${opts.index.hashKey}`] = opts.index.hashKey;
this.params.ExpressionAttributeValues = {};
this.params.ExpressionAttributeValues[`:${opts.index.hashKey}`] = opts.index.hashValue;
}
}
/**
*
* @param {object} client DynamoDB Document Client
* @param {string} tableName DynamoDB Table Name
*/
static async newAdapter(client, tableName) {
return new CasbinDynamoDBAdapter(client, tableName);
}
policyLine(policy) {
let line = policy.pType;
if (policy.v0) {
line += ', ' + policy.v0;
}
if (policy.v1) {
line += ', ' + policy.v1;
}
if (policy.v2) {
line += ', ' + policy.v2;
}
if (policy.v3) {
line += ', ' + policy.v3;
}
if (policy.v4) {
line += ', ' + policy.v4;
}
if (policy.v5) {
line += ', ' + policy.v5;
}
return line;
}
/**
*
* @param {object} policy
* @param {Model} model
*/
loadPolicyLine(policy, model) {
const line = this.policyLine(policy);
Helper.loadPolicyLine(line, model);
}
/**
*
* @param {Model} model Model instance from enforcer
* @returns {Promise<void>}
*/
async loadPolicy(model) {
const items = await find(this.client, this.params);
for (const item of items) {
this.loadPolicyLine(item, model);
}
}
/**
*
* @param {string} pType
* @param {Array<string>} rule
* @returns {object}
*/
savePolicyLine(pType, rule) {
const [v0, v1, v2, v3, v4, v5] = rule;
const policy = { pType, v0, v1, v2, v3, v4, v5 };
if (this.index && this.index.hashKey && this.index.hashValue) {
policy[this.index.hashKey] = this.index.hashValue;
}
policy[this.hashKey] = createHash('md5').update(JSON.stringify(policy)).digest("hex");
return policy;
}
/**
*
* @param {Model} model Model instance from enforcer
* @returns {Promise<boolean>}
*/
async savePolicy(model) {
const policyRuleAST = model.model.get('p');
const groupingPolicyAST = model.model.get('g');
for (const [pType, ast] of policyRuleAST) {
for (const rule of ast.policy) {
const casbinPolicy = this.savePolicyLine(pType, rule);
await this.client.put({ TableName: this.tableName, Item: casbinPolicy }).promise();
}
}
for (const [pType, ast] of groupingPolicyAST) {
for (const rule of ast.policy) {
const casbinPolicy = this.savePolicyLine(pType, rule);
await this.client.put({ TableName: this.tableName, Item: casbinPolicy }).promise();
}
}
return true;
}
/**
*
* @param {string} sec
* @param {string} pType
* @param {Array<string>} rule
* @returns {Promise<void>}
*/
async addPolicy(sec, pType, rule) {
const policy = this.savePolicyLine(pType, rule);
await this.client.put({ TableName: this.tableName, Item: policy }).promise();
}
/**
*
* @param {string} sec
* @param {string} pType
* @param {Array<string>} rule
* @returns {Promise<void>}
*/
async removePolicy(sec, pType, rule) {
const policy = this.savePolicyLine(pType, rule);
const params = { TableName: this.tableName, Key: {} };
params.Key[this.hashKey] = policy[this.hashKey];
await this.client.delete(params).promise();
}
/**
*
* @param {string} sec
* @param {string} pType
* @param {number} fieldIndex
* @param {...string} fieldValues
* @returns {Promise<void>}
*/
async removeFilteredPolicy(sec, pType, fieldIndex, ...fieldValues) {
const params = Object.assign({}, this.params);
params.FilterExpression = '#pType = :pType';
params.ExpressionAttributeNames = { '#pType': 'pType' };
params.ExpressionAttributeValues = { ':pType': pType };
if (fieldIndex <= 0 && fieldIndex + fieldValues.length > 0 && !!fieldValues[0 - fieldIndex]) {
params.FilterExpression += ' AND #v0 = :v0';
params.ExpressionAttributeNames['#v0'] = 'v0';
params.ExpressionAttributeValues[':v0'] = fieldValues[0 - fieldIndex];
}
if (fieldIndex <= 1 && fieldIndex + fieldValues.length > 1 && !!fieldValues[1 - fieldIndex]) {
params.FilterExpression += ' AND #v1 = :v1';
params.ExpressionAttributeNames['#v1'] = 'v1';
params.ExpressionAttributeValues[':v1'] = fieldValues[1 - fieldIndex];
}
if (fieldIndex <= 2 && fieldIndex + fieldValues.length > 2 && !!fieldValues[2 - fieldIndex]) {
params.FilterExpression += ' AND #v2 = :v2';
params.ExpressionAttributeNames['#v2'] = 'v2';
params.ExpressionAttributeValues[':v2'] = fieldValues[2 - fieldIndex];
}
if (fieldIndex <= 3 && fieldIndex + fieldValues.length > 3 && !!fieldValues[3 - fieldIndex]) {
params.FilterExpression += ' AND #v3 = :v3';
params.ExpressionAttributeNames['#v3'] = 'v3';
params.ExpressionAttributeValues[':v3'] = fieldValues[3 - fieldIndex];
}
if (fieldIndex <= 4 && fieldIndex + fieldValues.length > 4 && !!fieldValues[4 - fieldIndex]) {
params.FilterExpression += ' AND #v4 = :v4';
params.ExpressionAttributeNames['#v4'] = 'v4';
params.ExpressionAttributeValues[':v4'] = fieldValues[4 - fieldIndex];
}
if (fieldIndex <= 5 && fieldIndex + fieldValues.length > 5 && !!fieldValues[5 - fieldIndex]) {
params.FilterExpression += ' AND #v5 = :v5';
params.ExpressionAttributeNames['#v5'] = 'v5';
params.ExpressionAttributeValues[':v5'] = fieldValues[5 - fieldIndex];
}
const items = await find(this.client, params);
const requestItems = [];
for (const item of items) {
const Key = {};
Key[this.hashKey] = item[this.hashKey];
requestItems.push({ DeleteRequest: { Key } });
}
const len = requestItems.length / 25;
for (let x = 0, i = 0; x < len; i += 25, x++) {
const params = { RequestItems: {} };
params.RequestItems[this.tableName] = requestItems.slice(i, i + 25);
await batchWrite(this.client, params);
}
}
/**
*
* @param {string} sec
* @param {string} pType
* @param {Array<Array<string>>} rules
* @returns {Promise<void>}
*/
async addPolicies(sec, pType, rules) {
const requestItems = [];
for (const rule of rules) {
const policy = this.savePolicyLine(pType, rule);
requestItems.push({ PutRequest: { Item: policy } });
}
const len = requestItems.length / 25;
for (let x = 0, i = 0; x < len; i += 25, x++) {
const params = { RequestItems: {} };
params.RequestItems[this.tableName] = requestItems.slice(i, i + 25);
await batchWrite(this.client, params);
}
}
/**
*
* @param {string} sec
* @param {string} pType
* @param {Array<Array<string>>} rules
* @returns {Promise<void>}
*/
async removePolicies(sec, pType, rules) {
const requestItems = [];
for (const rule of rules) {
const policy = this.savePolicyLine(pType, rule);
const Key = {};
Key[this.hashKey] = policy[this.hashKey];
requestItems.push({ DeleteRequest: { Key } });
}
const len = requestItems.length / 25;
for (let x = 0, i = 0; x < len; i += 25, x++) {
const params = { RequestItems: {} };
params.RequestItems[this.tableName] = requestItems.slice(i, i + 25);
await batchWrite(this.client, params);
}
}
}
/**
* Based on DefaultFilteredAdapter
*/
class CasbinDynamoDBFilteredAdapter extends CasbinDynamoDBAdapter {
/**
*
* @param {object} client DynamoDB Document Client
* @param {object} opts Options
*/
constructor(client, opts = {}) {
super(client, opts);
this.filtered = false;
}
/**
*
* @param {Model} model Model instance from enforcer
* @returns {Promise<void>}
*/
async loadPolicy(model) {
this.filtered = false;
await super.loadPolicy(model);
}
/**
*
* @param {Model} model Model instance from enforcer
* @param {Filter} filter Filter
* @returns {Promise<void>}
*/
async loadFilteredPolicy(model, filter) {
if (!filter) {
await this.loadPolicy(model);
return;
}
const items = await find(this.client, this.params);
for (const item of items) {
const line = this.policyLine(item);
if (!line || DefaultFilteredAdapter.filterLine(line, filter)) {
continue;
}
Helper.loadPolicyLine(line, model);
}
this.filtered = true;
}
/**
*
* @returns {void}
*/
isFiltered() {
return this.filtered;
}
/**
*
* @param {Model} model Model instance from enforcer
* @returns {Promise<boolean>}
*/
async savePolicy(model) {
if (this.filtered) {
throw new Error('cannot save a filtered policy');
}
await super.savePolicy(model);
return true;
}
}
module.exports = {
CasbinDynamoDBAdapter,
CasbinDynamoDBFilteredAdapter
}