This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grapl_provision.py
145 lines (103 loc) · 3.3 KB
/
grapl_provision.py
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
import time
import json
import pydgraph
from grpc import RpcError, StatusCode
from pydgraph import DgraphClient, DgraphClientStub
from grapl_analyzerlib.schemas import *
from grapl_analyzerlib.schemas.schema_builder import ManyToMany
def set_schema(client, schema, engagement=False):
op = pydgraph.Operation(schema=schema)
print(client.alter(op))
def drop_all(client):
op = pydgraph.Operation(drop_all=True)
client.alter(op)
def format_schemas(schema_defs):
schemas = "\n\n".join([schema.to_schema_str() for schema in schema_defs])
types = "\n\n".join([schema.generate_type() for schema in schema_defs])
return "\n".join(
[" # Type Definitions", types, "\n # Schema Definitions", schemas,]
)
def get_type_dict(client, type_name):
query = f"""
schema(type: {type_name}) {{
type
index
}}
"""
txn = client.txn(read_only=True)
try:
res = json.loads(txn.query(query).json)
finally:
txn.discard()
type_dict = {}
for d in res["types"][0]["fields"]:
if d["name"][0] == "~":
name = f"<{d['name']}>"
else:
name = d["name"]
type_dict[name] = d["type"]
return type_dict
def update_reverse_edges(client, schema):
type_dicts = {}
rev_edges = set()
for edge in schema.forward_edges:
edge_n = edge[0]
edge_t = edge[1]._inner_type.self_type()
if edge_t == "Any":
continue
rev_edges.add(("<~" + edge_n + ">", edge_t))
if not type_dicts.get(edge_t):
type_dicts[edge_t] = get_type_dict(client, edge_t)
if not rev_edges:
return
for (rev_edge_n, rev_edge_t) in rev_edges:
type_dicts[rev_edge_t][rev_edge_n] = "uid"
type_strs = ""
for t in type_dicts.items():
type_name = t[0]
type_d = t[1]
predicates = []
for predicate_name, predicate_type in type_d.items():
predicates.append(f"\t{predicate_name}: {predicate_type}")
predicates = "\n".join(predicates)
type_str = f"""
type {type_name} {{
{predicates}
}}
"""
type_strs += "\n"
type_strs += type_str
op = pydgraph.Operation(schema=type_strs)
client.alter(op)
def provision(mclient):
# drop_all(mclient)
# drop_all(___local_dg_provision_client)
schemas = (
AssetSchema(),
ProcessSchema(),
FileSchema(),
IpConnectionSchema(),
IpAddressSchema(),
IpPortSchema(),
NetworkConnectionSchema(),
ProcessInboundConnectionSchema(),
ProcessOutboundConnectionSchema(),
)
mg_schema_str = format_schemas(schemas)
set_schema(mclient, mg_schema_str)
if __name__ == "__main__":
local_dg_provision_client = DgraphClient(DgraphClientStub("localhost:9080"))
num_retries = 150
for i in range(0, num_retries):
try:
provision(local_dg_provision_client,)
except Exception as e:
hint = ""
if isinstance(e, RpcError) and e.code() == StatusCode.UNAVAILABLE:
hint = "have you booted the local dgraph server?"
print(f"Retry {i}/{num_retries}: {hint}\n {e}")
time.sleep(1)
else:
break
time.sleep(1)
print("grapl_provision complete!\n")