-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ1_graphQL.py
179 lines (144 loc) · 4.31 KB
/
J1_graphQL.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
178
179
import requests
import json
def const():
# Account-Level API Key - Admin Group
acct = "x"
token = "x"
return [acct, token]
def make_headers(token, acct):
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(token),
"Jupiterone-Account": acct
}
return headers
def make_request(method, url, headers, data):
if method == "POST":
r = requests.post(url, headers=headers, json=data, verify=True)
return r
else:
return "method not implemented"
def run_query(q):
query = '''
query J1QL($query: String!, $variables: JSON, $cursor: String, $scopeFilters: [JSON!], $flags: QueryV1Flags) {
queryV1(query: $query, variables: $variables, cursor: $cursor, scopeFilters: $scopeFilters, flags: $flags) {
type
data
cursor
}
}
'''
data = {
"query": query,
"variables": {
"query": q
}
}
headers = make_headers(const()[1], const()[0])
r = make_request("POST", "https://graphql.us.jupiterone.io", headers, data)
rdict = json.loads(r.content.decode('utf-8'))
return rdict
def create_entity():
query = '''
mutation CreateEntity (
$entityK ey: String!
$entityType: String!
$entityClass: [String!]!
$timestamp: Long
$properties: JSON
) {
createEntity (
entityKey: $entityKey,
entityType: $entityType,
entityClass: $entityClass,
timestamp: $timestamp,
properties: $properties
) {
entity {
_id
}
vertex {
id,
entity {
_id
}
properties
}
}
}
'''
data = {
"query": query,
"variables": {
"entityKey": "graphQL Device 1",
"entityType": "custom_device",
"entityClass": "Device",
"properties": {
"propertyKey": "propertyValue",
"displayName": "graphQL Device Upload 1",
"owner": "[email protected]",
"customProperty": "graphQL-updated-value",
"tag.Production": "false"
}
}
}
headers = make_headers(const()[1], const()[0])
r = make_request("POST", "https://graphql.us.jupiterone.io", headers, data)
rdict = json.loads(r.content.decode('utf-8'))
return rdict
def get_assets():
# GraphQL query to get assets
query = '''
query J1QL($query: String!, $variables: JSON, $cursor: String, $scopeFilters: [JSON!], $flags: QueryV1Flags) {
queryV1(query: $query, variables: $variables, cursor: $cursor, scopeFilters: $scopeFilters, flags: $flags) {
type
data
cursor
}
}
'''
data = {
"query": query,
"variables": {
"query": "FIND (Device | Host)"
}
}
headers = make_headers(const()[1], const()[0])
r = make_request("POST", "https://graphql.us.jupiterone.io", headers, data)
rdict = json.loads(r.content.decode('utf-8'))
return rdict
def get_identities():
# GraphQL query to get identities
query = '''
query J1QL($query: String!, $variables: JSON, $cursor: String, $scopeFilters: [JSON!], $flags: QueryV1Flags) {
queryV1(query: $query, variables: $variables, cursor: $cursor, scopeFilters: $scopeFilters, flags: $flags) {
type
data
cursor
}
}
'''
data = {
"query": query,
"variables": {
"query": "FIND User"
}
}
headers = make_headers(const()[1], const()[0])
r = make_request("POST", "https://graphql.us.jupiterone.io", headers, data)
rdict = json.loads(r.content.decode('utf-8'))
return rdict
if __name__ == '__main__':
# Execute GraphQL Query
# query = "FIND Device"
# r = run_query(query)
# print(json.dumps(r, indent=1))
# Create/Update Entity via GraphQL
# r = create_entity()
# print(json.dumps(r, indent=1))
# Run GraphQL query to fetch all Device & Host entities from J1
# r = get_assets()
# print(json.dumps(r, indent=1))
# Run GraphQL query to fetch all Device & Host entities from J1
r = get_identities()
print(json.dumps(r, indent=1))