-
-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathapi.ts
944 lines (750 loc) · 22.5 KB
/
api.ts
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy ofthe License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specificlanguage governing permissions and
* limitations under the License.
*
*/
declare var require: any
declare var Promise: any;
declare var $: any;
declare var global: any;
declare var Gremlin: any;
declare var request: any;
var makeRequest = null;
const Unauthorized = "Unauthorized";
import jsEnv = require('browser-or-node');
if (jsEnv) {
var querystring = require('qs')
var defaultsDeep = require('lodash.defaultsdeep')
// When using node, we use najax for $.ajax calls
if (jsEnv.isNode && !jsEnv.isBrowser) {
var najax = require('najax');
global.$ = <any>{ "ajax": najax };
}
makeRequest = function (client: Client, url: string, method: string, data: string, opts: Object) : any {
return new Promise(function (resolve, reject) {
return $.ajax(defaultsDeep(opts, {
dataType: "json",
url: client.baseURL + url,
data: data,
contentType: "application/json; charset=utf-8",
method: method
}))
.fail(function (jqXHR) {
if (jqXHR.status == 401) {
reject(Unauthorized)
} else {
reject(jqXHR)
}
})
.done(function (data, statusText, jqXHR) {
if (jqXHR.status >= 200 && jqXHR.status < 400) {
var cookie = jqXHR.getResponseHeader("Set-Cookie");
if (cookie) {
client.cookie = cookie;
}
resolve(data, jqXHR);
} else {
reject(jqXHR);
}
});
})
}
} else {
// We're running inside Otto. We use the 'request' method
// exported by Skydive that makes use of the REST client
// so we get support for authentication
makeRequest = function (client: Client, url: string, method: string, body: string, opts: Object) : any {
var data: any
var output = request(url, method, body)
if (output && output.length > 0) {
data = JSON.parse(output)
} else {
data = null
}
return {'then': function() { return data; } }
}
}
function paramsString(params: any[]): string {
var s = ""
for (let param in params) {
if (param !== "0") {
s += ", "
}
switch (typeof params[param]) {
case "string":
s += '"' + params[param] + '"';
break;
default:
s += params[param];
break;
}
}
return s
}
// Returns a string of the invocation of 'method' with its parameters 'params'
// like MyMethod(1, 2, "s")
function callString(method: string, ...params: any[]): string {
let s = method + "("
s += paramsString(params)
s += ")"
return s
}
export class SerializationHelper {
static toInstance<T>(obj: T, jsonObj: Object) : T {
if (typeof obj["fromJSON"] === "function") {
obj["fromJSON"](jsonObj);
}
else {
for (var propName in jsonObj) {
obj[propName] = jsonObj[propName]
}
}
return obj;
}
static unmarshalArray<T>(data, c: new () => T): T[] {
var items: T[] = [];
for (var obj in data) {
let newT: T = SerializationHelper.toInstance(new c(), data[obj]);
items.push(newT);
}
return items
}
static unmarshalMapArray<T>(data, c: new () => T): { [key: string]: T[]; } {
var items: { [key: string]: T[]; } = {};
for (var key in data) {
items[key] = this.unmarshalArray(data[key], c);
}
return items
}
static unmarshalMap<T>(data, c: new () => T): { [key: string]: T; } {
var items: { [key: string]: T; } = {};
for (var obj in data) {
let newT: T = SerializationHelper.toInstance(new c(), data[obj]);
items[obj] = newT;
}
return items
}
}
export class APIObject extends Object {
}
export class Alert extends APIObject {
}
export class Capture extends APIObject {
}
export class EdgeRule extends APIObject {
}
export class NodeRule extends APIObject {
}
export class PacketInjection extends APIObject {
}
export class Workflow extends APIObject {
}
export class API<T extends APIObject> {
Resource: string
Factory: new () => T
client: Client
constructor(client: Client, resource: string, c: new () => T) {
this.client = client;
this.Resource = resource;
this.Factory = c;
}
create(obj: T) {
let resource = this.Resource;
return this.client.request("/api/" + resource, "POST", JSON.stringify(obj), {})
.then(function (data) {
return SerializationHelper.toInstance(new (<any>obj.constructor)(), data);
})
}
list() {
let resource = this.Resource;
let factory = this.Factory;
return this.client.request('/api/' + resource, "GET", "", {})
.then(function (data) {
let resources = {};
for (var obj in data) {
let resource = SerializationHelper.toInstance(new factory(), data[obj]);
resources[obj] = resource
}
return resources
});
}
get(id) {
let resource = this.Resource;
let factory = this.Factory;
return this.client.request('/api/' + resource + "/" + id, "GET", "", {})
.then(function (data) {
return SerializationHelper.toInstance(new factory(), data)
})
}
delete(id: string) {
let resource = this.Resource;
return this.client.request('/api/' + resource + "/" + id, "DELETE", "", { "dataType": "" })
}
}
export class GraphElement {
}
export class GraphNode extends GraphElement {
}
export class GraphEdge extends GraphElement {
}
type NodeMap = { [key: string]: GraphNode; };
type EdgeMap = { [key: string]: GraphEdge; };
export class Graph {
nodes: NodeMap;
edges: EdgeMap;
constructor(nodes: NodeMap, edges: EdgeMap) {
this.nodes = nodes || {}
this.edges = edges || {}
}
}
export class GremlinAPI {
client: Client
constructor(client: Client) {
this.client = client;
}
query(s: string) {
return this.client.request('/api/topology', "POST", JSON.stringify({'GremlinQuery': s}), {})
.then(function (data) {
if (data === null)
return [];
// Result can be [Node] or [[Node, Node]]
if (data.length > 0 && data[0] instanceof Array)
data = data[0];
return data
});
}
G() : G {
return new G(this)
}
}
export class _Metadata {
params: any[]
constructor(...params: any[]) {
this.params = params
}
public toString(): string {
return "Metadata(" + paramsString(this.params) + ")"
}
}
export function Metadata(...params: any[]) : _Metadata {
return new _Metadata(...params)
}
export interface Step {
name(): string
serialize(data): any
}
export class Step implements Step {
api: GremlinAPI
gremlin: string
previous: Step
params: any[]
name() { return "InvalidStep"; }
constructor(api: GremlinAPI, previous: Step = null, ...params: any[]) {
this.api = api
this.previous = previous
this.params = params
}
public toString(): string {
if (this.previous !== null) {
let gremlin = this.previous.toString() + ".";
return gremlin + callString(this.name(), ...this.params)
} else {
return this.name()
}
}
serialize(data) {
return this.previous.serialize(data);
}
result() {
let data = this.api.query(this.toString())
return this.serialize(data)
}
}
export class G extends Step {
name() { return "G" }
constructor(api: GremlinAPI) {
super(api)
}
serialize(data) {
return {
"Nodes": SerializationHelper.unmarshalArray(data[0].Nodes, GraphNode),
"Edges": SerializationHelper.unmarshalArray(data[0].Edges, GraphEdge),
};
}
Context(...params: any[]): G {
return new Context(this.api);
}
V(...params: any[]): V {
return new V(this.api, this, ...params);
}
E(...params: any[]): E {
return new E(this.api, this, ...params);
}
Flows(...params: any[]): Flows {
return new Flows(this.api, this, ...params);
}
}
export class Context extends G {
name() { return "Context" }
constructor(api: GremlinAPI, previous: Step = null, ...params: any[]) {
super(api)
this.previous = previous
this.params = params
}
}
export class Subgraph extends G {
name() { return "Subgraph" }
constructor(api: GremlinAPI, previous: Step = null) {
super(api)
this.previous = previous
}
}
type Constructor<T> = new(...args: any[]) => T;
function MixinStep<T extends Constructor<{}>>(Base: T, name: string) {
return class extends Base {
name() { return name }
constructor(...args: any[]) {
super(...args);
}
}
}
export class V extends Step {
name() { return "V" }
serialize(data) {
return SerializationHelper.unmarshalArray(data, GraphNode);
}
Has(...params: any[]): V {
return new HasV(this.api, this, ...params);
}
HasEither(...params: any[]): V {
return new HasEitherV(this.api, this, ...params);
}
HasKey(...params: any[]): V {
return new HasKeyV(this.api, this, ...params);
}
HasNot(...params: any[]): V {
return new HasNotV(this.api, this, ...params);
}
Dedup(...params: any[]): V {
return new DedupV(this.api, this, ...params);
}
In(...params: any[]): V {
return new In(this.api, this, ...params);
}
Out(...params: any[]): V {
return new Out(this.api, this, ...params);
}
Both(...params: any[]): V {
return new Both(this.api, this, ...params);
}
Range(...params: any[]): V {
return new RangeV(this.api, this, ...params);
}
Limit(...params: any[]): V {
return new LimitV(this.api, this, ...params);
}
InE(...params: any[]): E {
return new InE(this.api, this, ...params);
}
OutE(...params: any[]): E {
return new OutE(this.api, this, ...params);
}
BothE(...params: any[]): E {
return new BothE(this.api, this, ...params);
}
ShortestPathTo(...params: any[]): ShortestPath {
return new ShortestPath(this.api, this, ...params);
}
Subgraph(): G {
return new Subgraph(this.api, this);
}
Count(): Value {
return new Count(this.api, this);
}
Values(...params: any[]): Value {
return new Values(this.api, this, ...params);
}
Keys(...params: any[]): Value {
return new Keys(this.api, this, ...params);
}
Sum(...params: any[]): Value {
return new Keys(this.api, this, ...params);
}
Sort(...params: any[]): V {
return new SortV(this.api, this, ...params);
}
Metrics(...params: any[]): Metrics {
return new Metrics(this.api, this, ...params);
}
Sockets(...params: any[]): Sockets {
return new Sockets(this.api, this, ...params);
}
Flows(...params: any[]): Flows {
return new Flows(this.api, this, ...params)
}
}
export class E extends Step {
name() { return "E" }
serialize(data) {
return SerializationHelper.unmarshalArray(data, GraphEdge);
}
Has(...params: any[]): E {
return new HasE(this.api, this, ...params);
}
HasEither(...params: any[]): E {
return new HasEitherE(this.api, this, ...params);
}
HasKey(...params: any[]): E {
return new HasKeyE(this.api, this, ...params);
}
HasNot(...params: any[]): E {
return new HasNotE(this.api, this, ...params);
}
Dedup(...params: any[]): E {
return new DedupE(this.api, this, ...params);
}
Range(...params: any[]): E {
return new RangeE(this.api, this, ...params);
}
Limit(...params: any[]): E {
return new LimitE(this.api, this, ...params);
}
InV(...params: any[]): V {
return new InV(this.api, this, ...params);
}
OutV(...params: any[]): V {
return new OutV(this.api, this, ...params);
}
BothV(...params: any[]): V {
return new BothV(this.api, this, ...params);
}
Subgraph(): G {
return new Subgraph(this.api, this);
}
Count(): Value {
return new Count(this.api, this);
}
Values(...params: any[]): Value {
return new Values(this.api, this, ...params);
}
Keys(...params: any[]): Value {
return new Keys(this.api, this, ...params);
}
Sort(...params: any[]): E {
return new SortE(this.api, this, ...params);
}
}
class HasV extends MixinStep(V, "Has") { }
class HasE extends MixinStep(E, "Has") { }
class HasEitherV extends MixinStep(V, "HasEither") { }
class HasEitherE extends MixinStep(E, "HasEither") { }
class HasKeyV extends MixinStep(V, "HasKey") { }
class HasKeyE extends MixinStep(E, "HasKey") { }
class HasNotV extends MixinStep(V, "HasNot") { }
class HasNotE extends MixinStep(E, "HasNot") { }
class DedupV extends MixinStep(V, "Dedup") { }
class DedupE extends MixinStep(E, "Dedup") { }
class RangeV extends MixinStep(V, "Range") { }
class RangeE extends MixinStep(E, "Range") { }
class LimitV extends MixinStep(V, "Limit") { }
class LimitE extends MixinStep(E, "Limit") { }
class SortV extends MixinStep(V, "Sort") { }
class SortE extends MixinStep(E, "Sort") { }
export class Out extends V {
name() { return "Out" }
}
export class In extends V {
name() { return "In" }
}
export class Both extends V {
name() { return "Both" }
}
export class OutV extends V {
name() { return "OutV" }
}
export class InV extends V {
name() { return "InV" }
}
export class OutE extends E {
name() { return "OutE" }
}
export class InE extends E {
name() { return "InE" }
}
export class BothE extends E {
name() { return "BothE" }
}
export class BothV extends V {
name() { return "BothV" }
}
export class ShortestPath extends Step {
name() { return "ShortestPathTo" }
public toString(): string {
if (this.previous !== null) {
let gremlin = this.previous.toString() + ".";
return gremlin + callString(this.name(), ...this.params)
} else {
return this.name()
}
}
serialize(data) {
var items: GraphNode[][] = [];
for (var obj in data) {
items.push(SerializationHelper.unmarshalArray(data[obj], GraphNode));
}
return items;
}
}
class Predicate {
name: string
params: any
constructor(name: string, ...params: any[]) {
this.name = name
this.params = params
}
public toString(): string {
return callString(this.name, ...this.params)
}
}
export function NE(param: any): Predicate {
return new Predicate("NE", param)
}
export function GT(param: any): Predicate {
return new Predicate("GT", param)
}
export function LT(param: any): Predicate {
return new Predicate("LT", param)
}
export function GTE(param: any): Predicate {
return new Predicate("GTE", param)
}
export function LTE(param: any): Predicate {
return new Predicate("LTE", param)
}
export function IPV4RANGE(param: any): Predicate {
return new Predicate("IPV4RANGE", param)
}
export function REGEX(param: any): Predicate {
return new Predicate("REGEX", param)
}
export function Within(...params: any[]): Predicate {
return new Predicate("Within", ...params)
}
export function WITHOUT(...params: any[]): Predicate {
return new Predicate("WITHOUT", ...params)
}
export function INSIDE(from, to: any): Predicate {
return new Predicate("INSIDE", from, to)
}
export function OUTSIDE(from, to: any): Predicate {
return new Predicate("OUTSIDE", from, to)
}
export function BETWEEN(from, to: any): Predicate {
return new Predicate("BETWEEN", from, to)
}
export var FOREVER = new Predicate("FOREVER")
export var NOW = new Predicate("NOW")
export var DESC = new Predicate("DESC")
export var ASC = new Predicate("ASC")
export class Metric {
}
export class FlowLayer {
}
export class ICMPLayer {
}
export class FlowMetric {
}
export class RawPacket {
}
export class TCPMetric {
}
export class Flow {
}
export class Flows extends Step {
name() { return "Flows" }
serialize(data) {
return SerializationHelper.unmarshalArray(data, Flow);
}
Has(...params: any[]): Flows {
return new HasFlows(this.api, this, ...params);
}
HasEither(...params: any[]): Flows {
return new HasEitherFlows(this.api, this, ...params);
}
Count(...params: any[]): Value {
return new Count(this.api, this);
}
Values(...params: any[]): Value {
return new Values(this.api, this, ...params);
}
Keys(...params: any[]): Value {
return new Keys(this.api, this, ...params);
}
Sum(...params: any[]): Value {
return new Keys(this.api, this, ...params);
}
Sort(...params: any[]): Flows {
return new SortFlows(this.api, this, ...params);
}
Out(...params: any[]): V {
return new Out(this.api, this, ...params);
}
In(...params: any[]): V {
return new In(this.api, this, ...params);
}
Both(...params: any[]): V {
return new Both(this.api, this, ...params);
}
Nodes(...params: any[]): V {
return new Nodes(this.api, this, ...params);
}
Hops(...params: any[]): V {
return new Hops(this.api, this, ...params);
}
CaptureNode(...params: any[]): V {
return new CaptureNode(this.api, this, ...params);
}
Metrics(...params: any[]): Metrics {
return new Metrics(this.api, this, ...params);
}
RawPackets(...params: any[]): RawPackets {
return new RawPackets(this.api, this, ...params);
}
Sockets(...params: any[]): Sockets {
return new Sockets(this.api, this, ...params);
}
Dedup(...params: any[]): Flows {
return new DedupFlows(this.api, this, ...params);
}
}
class HasFlows extends MixinStep(Flows, "Has") { }
class HasEitherFlows extends MixinStep(Flows, "HasEither") { }
class SortFlows extends MixinStep(Flows, "Sort") { }
class DedupFlows extends MixinStep(Flows, "Dedup") { }
export class Nodes extends V {
name() { return "Nodes" }
}
export class Hops extends V {
name() { return "Hops" }
}
export class CaptureNode extends V {
name() { return "CaptureNode" }
}
export class Value extends Step {
serialize(data) {
return data;
}
}
export class Count extends Value {
name() { return "Count" }
}
export class Values extends Value {
name() { return "Values" }
}
export class Keys extends Value {
name() { return "Keys" }
}
export class Sum extends Value {
name() { return "Sum" }
}
export class Metrics extends Step {
name() { return "Metrics" }
serialize(data) {
return SerializationHelper.unmarshalMapArray(data[0], Metric);
}
Sum(...params: any[]): Value {
return new Sum(this.api, this, ...params);
}
Aggregates(...params: any[]): Metrics {
return new Aggregates(this.api, this, ...params);
}
Count(): Value {
return new Count(this.api, this);
}
}
export class Aggregates extends Metrics {
name() { return "Aggregates" }
}
export class RawPackets extends Step {
name() { return "RawPackets" }
serialize(data) {
return SerializationHelper.unmarshalMapArray(data[0], RawPacket);
}
BPF(...params: any[]): RawPackets {
return new BPF(this.api, this, ...params);
}
}
export class BPF extends RawPackets {
name() { return "BPF" }
}
class Socket {
}
export class Sockets extends Step {
name() { return "Sockets" }
serialize(data) {
return SerializationHelper.unmarshalMapArray(data[0], Socket);
}
Values(...params: any[]): Value {
return new Values(this.api, this, ...params);
}
Has(...params: any[]): Sockets {
return new HasSockets(this.api, this, ...params);
}
}
class HasSockets extends MixinStep(Sockets, "Has") { }
export class Client {
baseURL: string
username: string
password: string
cookie: string
alerts: API<Alert>
captures: API<Capture>
edgeRules: API<EdgeRule>
nodeRules: API<NodeRule>
packetInjections: API<PacketInjection>
workflows: API<Workflow>
gremlin: GremlinAPI
G: G
constructor(options: Object) {
options = options || {};
this.baseURL = options["baseURL"] || "";
this.username = options["username"] || "";
this.password = options["password"] || "";
this.cookie = options["cookie"] || "";
this.alerts = new API(this, "alert", Alert);
this.captures = new API(this, "capture", Capture);
this.packetInjections = new API(this, "injectpacket", PacketInjection);
this.nodeRules = new API(this, "noderule", NodeRule);
this.edgeRules = new API(this, "edgerule", EdgeRule);
this.workflows = new API(this, "workflow", Workflow);
this.gremlin = new GremlinAPI(this);
this.G = this.gremlin.G();
}
login() {
var self = this;
if (this.username && this.password) {
return makeRequest(this, "/login", "POST",
querystring.stringify({"username": this.username, "password": this.password}),
{ "contentType": "application/x-www-form-urlencoded",
"dataType": "" })
}
return Promise.resolve();
}
request(url: string, method: string, data: any, opts: Object): any {
if (this.cookie) {
opts["headers"] = { "Cookie": this.cookie }
}
return makeRequest(this, url, method, data, opts);
}
}