-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpbac.js
183 lines (175 loc) · 6.62 KB
/
pbac.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
'use strict';
const policySchema = require('./schema.json');
const conditions = require('./conditions');
const ZSchema = require('z-schema');
const util = require('util');
const isPlainObject = require('lodash/isPlainObject');
const isBoolean = require('lodash/isBoolean');
const isArray = require('lodash/isArray');
const isUndefined = require('lodash/isUndefined');
const isEmpty = require('lodash/isEmpty');
const forEach = require('lodash/forEach');
const every = require('lodash/every');
const get = require('lodash/get');
const flow = require('lodash/fp/flow');
const map = require('lodash/fp/map');
const flatten = require('lodash/fp/flatten');
const find = require('lodash/fp/find');
const PBAC = function constructor(policies, options) {
options = isPlainObject(options) ? options : {};
const myconditions = isPlainObject(options.conditions) ? Object.assign(options.conditions, conditions) : conditions;
this.policies = [];
this.validateSchema = isBoolean(options.validateSchema) ? options.validateSchema : true;
this.validatePolicies = isBoolean(options.validatePolicies) ? options.validatePolicies : true;
this.schema = isPlainObject(options.schema) ? options.schema : policySchema;
this.conditions = myconditions;
this.addConditionsToSchema();
if (this.validateSchema) this._validateSchema();
this.add(policies);
};
Object.assign(PBAC.prototype, {
add: function add(policies) {
policies = isArray(policies) ? policies : [policies];
if (this.validatePolicies) this.validate(policies);
this.policies.push.apply(this.policies, policies);
},
addConditionsToSchema: function addConditionsToSchema() {
const definition = get(this.schema, 'definitions.Condition');
if (!definition) return;
const props = definition.properties = {};
forEach(this.conditions, function(condition, name) {
props[name] = {
type: 'object'
};
}, this);
},
_validateSchema() {
const validator = new ZSchema();
if (!validator.validateSchema(this.schema))
this.throw('schema validation failed with', validator.getLastError());
},
validate(policies) {
policies = isArray(policies) ? policies : [policies];
const validator = new ZSchema({
noExtraKeywords: true,
});
return every(policies, policy => {
const result = validator.validate(policy, this.schema);
if (!result)
this.throw('policy validation failed with', validator.getLastError());
return result;
});
},
evaluate(options) {
options = Object.assign({
action: '',
resource: '',
principal: {},
context: options.variables || {},
}, options || {});
if (this.filterPoliciesBy({
effect: 'Deny',
resource: options.resource,
action: options.action,
context: options.context,
principal: options.principal,
})) return false;
return !!this.filterPoliciesBy({
effect: 'Allow',
resource: options.resource,
action: options.action,
context: options.context,
principal: options.principal,
});
},
filterPoliciesBy(options) {
return flow(
map('Statement'),
flatten,
find(statement => {
if (statement.Effect !== options.effect) return false;
if (statement.Principal && !this.evaluatePrincipal(statement.Principal, options.principal, options.context))
return false;
if (statement.NotPrincipal && this.evaluateNotPrincipal(statement.NotPrincipal, options.principal, options.context))
return false;
if (statement.Resource && !this.evaluateResource(statement.Resource, options.resource, options.context))
return false;
if (statement.NotResource && this.evaluateResource(statement.NotResource, options.resource, options.context))
return false;
if (statement.Action && !this.evaluateAction(statement.Action, options.action))
return false;
if (statement.NotAction && this.evaluateAction(statement.NotAction, options.action))
return false;
return this.evaluateCondition(statement.Condition, options.context);
})
)(this.policies);
},
interpolateValue(value, variables) {
return value.replace(/\${(.+?)}/g, (match, variable) => {
return this.getVariableValue(variable, variables);
});
},
getContextValue(key, context) {
var parts = key.split(':');
if (isPlainObject(context[parts[0]]) && !isUndefined(context[parts[0]][parts[1]]))
return context[parts[0]][parts[1]];
else return key;
},
getVariableValue(variable, variables) {
const parts = variable.split(':');
if (isPlainObject(variables[parts[0]]) && !isUndefined(variables[parts[0]][parts[1]]))
return variables[parts[0]][parts[1]];
else return variable;
},
evaluateNotPrincipal(principals, reference) {
return Object.keys(reference).find(key => {
return this.conditions['ForAllValues:StringEquals'].call(this, principals[key], reference[key]);
});
},
evaluatePrincipal(principals, reference) {
return Object.keys(reference).find(key => {
if(isEmpty(reference[key])) return false;
return this.conditions['ForAnyValue:StringEquals'].call(this, principals[key], reference[key]);
});
},
evaluateAction(actions, reference) {
return actions.find(action => {
return this.conditions.StringLike.call(this, reference, action);
});
},
evaluateResource(resources, reference, context) {
resources = isArray(resources) ? resources : [resources];
return resources.find(resource => {
const value = this.interpolateValue(resource, context);
return this.conditions.StringLike.call(this, reference, value);
});
},
evaluateCondition(condition, context) {
if (!isPlainObject(condition)) return true;
const conditions = this.conditions;
return every(Object.keys(condition), key => {
const expression = condition[key];
const contextKey = Object.keys(expression)[0];
let values = expression[contextKey];
values = isArray(values) ? values : [values];
let prefix;
if (key.indexOf(':') !== -1) {
prefix = key.substr(0, key.indexOf(':'));
}
if (prefix === 'ForAnyValue' || prefix === 'ForAllValues') {
return conditions[key].call(this, this.getContextValue(contextKey, context), values);
} else {
return values.find(value => conditions[key].call(this, this.getContextValue(contextKey, context), value));
}
});
},
throw(name, message) {
const args = [].slice.call(arguments, 2);
args.unshift(message);
const e = new Error();
e.name = name;
e.message = util.format.apply(util, args);
throw e;
},
});
module.exports = PBAC;