forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencti_vocabulary.py
177 lines (168 loc) · 5.73 KB
/
opencti_vocabulary.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
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
import json
class Vocabulary:
def __init__(self, opencti):
self.opencti = opencti
self.properties = """
id
name
category {
key
fields {
key
}
}
"""
def list(self, **kwargs):
filters = kwargs.get("filters", None)
self.opencti.log(
"info", "Listing Vocabularies with filters " + json.dumps(filters) + "."
)
query = (
"""
query Vocabularies($filters: [VocabularyFiltering!]) {
vocabularies(filters: $filters) {
edges {
node {
"""
+ self.properties
+ """
}
}
}
}
"""
)
result = self.opencti.query(
query,
{
"filters": filters,
},
)
return self.opencti.process_multiple(result["data"]["vocabularies"])
def read(self, **kwargs):
id = kwargs.get("id", None)
filters = kwargs.get("filters", None)
if id is not None:
self.opencti.log("info", "Reading vocabulary {" + id + "}.")
query = (
"""
query Vocabulary($id: String!) {
vocabulary(id: $id) {
"""
+ self.properties
+ """
}
}
"""
)
result = self.opencti.query(query, {"id": id})
return self.opencti.process_multiple_fields(result["data"]["vocabulary"])
elif filters is not None:
result = self.list(filters=filters)
if len(result) > 0:
return result[0]
else:
return None
else:
self.opencti.log(
"error", "[opencti_vocabulary] Missing parameters: id or filters"
)
return None
def handle_vocab(self, vocab, cache, field):
if "vocab_" + vocab in cache:
vocab_data = cache["vocab_" + vocab]
else:
vocab_data = self.read_or_create_unchecked(
name=vocab,
required=field["required"],
category=cache["category_" + field["key"]],
)
if vocab_data is not None:
cache["vocab_" + vocab] = vocab_data
return vocab_data
def create(self, **kwargs):
stix_id = kwargs.get("stix_id", None)
name = kwargs.get("name", None)
category = kwargs.get("category", None)
description = kwargs.get("description", None)
created = kwargs.get("created", None)
modified = kwargs.get("modified", None)
aliases = kwargs.get("aliases", None)
x_opencti_stix_ids = kwargs.get("x_opencti_stix_ids", None)
update = kwargs.get("update", False)
if name is not None and category is not None:
self.opencti.log(
"info", "Creating or Getting aliased Vocabulary {" + name + "}."
)
query = (
"""
mutation VocabularyAdd($input: VocabularyAddInput!) {
vocabularyAdd(input: $input) {
"""
+ self.properties
+ """
}
}
"""
)
result = self.opencti.query(
query,
{
"input": {
"stix_id": stix_id,
"x_opencti_stix_ids": x_opencti_stix_ids,
"name": name,
"description": description,
"category": category,
"created": created,
"modified": modified,
"aliases": aliases,
"update": update,
}
},
)
return result["data"]["vocabularyAdd"]
else:
self.opencti.log(
"error",
"[opencti_vocabulary] Missing parameters: name or category",
)
def read_or_create_unchecked(self, **kwargs):
value = kwargs.get("name", None)
vocab = self.read(filters=[{"key": "name", "values": [value]}])
if vocab is None:
try:
return self.create(**kwargs)
except ValueError:
return None
return vocab
def update_field(self, **kwargs):
id = kwargs.get("id", None)
input = kwargs.get("input", None)
if id is not None and input is not None:
self.opencti.log("info", "Updating Vocabulary {" + id + "}.")
query = """
mutation VocabularyEdit($id: ID!, $input: [EditInput!]!) {
vocabularyFieldPatch(id: $id, input: $input) {
id
standard_id
entity_type
}
}
"""
result = self.opencti.query(
query,
{
"id": id,
"input": input,
},
)
return self.opencti.process_multiple_fields(
result["data"]["vocabularyFieldPatch"]
)
else:
self.opencti.log(
"error",
"[opencti_vocabulary] Missing parameters: id and key and value",
)
return None