-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.js
223 lines (191 loc) · 6.19 KB
/
rule.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
// L7mp: A programmable L7 meta-proxy
//
// Copyright 2019 by its authors.
// Some rights reserved. See AUTHORS.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const log = require('npmlog');
const _ = require('lodash');
const jsonPredicate = require("json-predicate");
// use json-predicate.dataAtPath to query metadata
// see doc at https://tools.ietf.org/html/draft-snell-json-test-07
// this is pretty basic: eventually, we should monkey-patch dataAtPath
// with with something like json-query or jsonpath to enable complex
// queries
//------------------------------------
//
// Match
//
//------------------------------------
class Match {
apply(s) { log.error("Match.apply", "Base class called"); }
}
class Wildcard extends Match {
constructor() { super(); }
apply(s){
log.silly("Wildcard.apply", `Session: ${s.name}`);
return true;
}
toJSON(){
log.silly('WildCard.toJSON');
return { match: '*' };
}
};
class JSONPredicate extends Match {
constructor(m) {
super();
// TODO: validate query. We now accept invalid queries and
// silently fail during runtime
this.predicate = m;
}
apply(s){
log.silly("JSONPredicate.apply", `Session: ${s.name}`,
`predicate:`, dumper(this.predicate, 5),
`on metadata:`, dumper(s.metadata, 5));
let res = jsonPredicate.test(s.metadata, this.predicate);
log.silly("JSONPredicate.apply", `results: "${res}"`);
return res === true;
}
toJSON(){ return this.predicate; }
};
Match.create = (m) => {
log.silly("Match.create:", dumper(m));
if(typeof m === 'string'){
return new Wildcard();
} else {
return new JSONPredicate(m);
}
}
//------------------------------------
//
// Rule
//
//------------------------------------
class Rule {
constructor(r){
this.name = r.name;
this.match = Match.create(r.match || "*");
this.action = r.action;
this.stats = { total_applied: 0 };
this.autogenerated = r.autogenerated;
}
// apply rule to session
apply(s){
log.silly('Rule.apply:', `"${this.name}"`);
if(this.match.apply(s)){
log.silly(`Rule.apply: "${this.name}": Match`);
// _.extend(s.metadata, this.action.set);
this.stats.total_applied++;
return this.action;
}
log.silly(`Rule.apply: "${this.name}": No match`);
}
};
Rule.index = 0;
Rule.create = (r) => {
log.silly("Rule.create:", dumper(r, 5));
return new Rule(r);
}
// no equivalent setAtPath for dataAtPath, we have to special-case
// this here:
// if data under path does not exist, we create it
// value can be object, in this case it will be deepcopied
Rule.getAtPath = (data, path) => {
var result, loc, locs = path.split('/');
// remove leading /
if(locs.length && locs[0] === '')
locs.shift();
// ready: '/'
if(locs.length===1 && locs[0] === '')
return data;
while(data && (loc = locs.shift()) && loc!=='') {
result = data = data[loc];
}
return result;
}
Rule.setAtPath = (data, path, value) => {
let ret = data;
var loc, locs = path.split('/');
// remove trailing /
if(locs.length && locs[locs.length-1] === '')
locs.pop();
// remove leading /
if(locs.length && locs[0] === '')
locs.shift();
// ready: '/'
if(locs.length===0){
ret = typeof value === 'object' ? _.cloneDeep(value) : value;
return ret;
}
while((loc = locs.shift())) {
let index = parseInt(loc, 10);
if(!isNaN(index))loc=index;
if(locs.length === 0) {
if(typeof value !== 'object'){
data[loc] = value;
} else {
data[loc] = _.cloneDeep(value);
}
} else {
if(data[loc]){
data = data[loc];
} else {
data = data[loc] = {};
}
}
}
return ret;
}
module.exports.Rule = Rule;
//------------------------------------
//
// RuleList (aka: MatchActionTable)
//
//------------------------------------
class RuleList {
constructor(rs){
this.name = rs.name;
this.rules = rs.rules;
this.autogenerated = rs.autogenerated;
}
// // apply rule to session
// apply(s){
// log.silly('RuleList.apply:', `"${this.name}"`);
// // late-bind rules
// for(let i = 0; i < this.rules.length; i++){
// // this is a rule name, substitute ref to Rule
// let ru = this.getRule(this.rules[i]);
// if(!ru){
// let e = `Cannot find named rule "${this.rules[i]}"`;
// log.warn(`RuleList.apply: "${this.name}":`, e);
// throw new Error(`Cannot apply RuleList "${this.name}": ${e}`);
// }
// let action = ru.apply(s)
// if (action)
// return action;
// }
// log.silly(`RuleList.apply: "${this.name}": No match`);
// }
};
RuleList.index = 0;
RuleList.create = (r) => {
log.silly("RuleList.create:", dumper(r, 5));
return new RuleList(r);
}
module.exports.RuleList = RuleList;