Skip to content

Commit 3ead04b

Browse files
Gilad Shrikidorsha
authored andcommitted
Demisto REST API - new commands to upload and download files (#1748)
* added multipart and download commands * added multipart and download commands * added multipart and download commands * added multipart and download commands * added multipart and download commands * added scripts to download logs bundle, and upload files to war room
1 parent 6495e67 commit 3ead04b

File tree

3 files changed

+139
-10
lines changed

3 files changed

+139
-10
lines changed

Integrations/integration-DemistoRESTAPI.yml

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,47 @@ script:
3636
serverURL = serverURL.slice(0,-1);
3737
}
3838
39-
var sendRequest = function(method, uri, body) {
39+
sendMultipart = function (uri, entryID, body) {
40+
var requestUrl = serverURL;
41+
if (uri.slice(-1) !== '/') {
42+
requestUrl += '/';
43+
}
44+
requestUrl += uri;
45+
46+
var res = httpMultipart(
47+
requestUrl,
48+
entryID,
49+
{
50+
Headers: {
51+
'Authorization': [params.apikey],
52+
'Content-Type': ['multipart/form-data'],
53+
'Accept': ['application/json']
54+
},
55+
},
56+
body,
57+
params.insecure,
58+
params.proxy,
59+
undefined,
60+
'file'
61+
);
62+
if (res.StatusCode < 200 || res.StatusCode >= 300) {
63+
throw 'Demisto REST APIs - Request Failed.\nStatus code: ' + res.StatusCode + '.\nBody: ' + JSON.stringify(res) + '.';
64+
}
65+
try {
66+
var response = res.Body;
67+
try {
68+
response = JSON.parse(res.Body);
69+
} catch (ex) {
70+
// do nothing, already handled prior the try/catch
71+
}
72+
return {response: response};
73+
} catch (ex) {
74+
throw 'Demisto REST APIs - Error parsing response - ' + ex + '\nBody:' + res.Body;
75+
}
76+
77+
};
78+
79+
var sendRequest = function(method, uri, body, raw) {
4080
var requestUrl = serverURL;
4181
if (uri.slice(-1) !== '/') {
4282
requestUrl += '/';
@@ -52,7 +92,8 @@ script:
5292
'content-type': ['application/json'],
5393
'authorization': [params.apikey]
5494
},
55-
Body: body
95+
Body: body,
96+
SaveToFile: raw
5697
},
5798
params.insecure,
5899
params.proxy
@@ -61,17 +102,20 @@ script:
61102
if (res.StatusCode < 200 || res.StatusCode >= 300) {
62103
throw 'Demisto REST APIs - Request Failed.\nStatus code: ' + res.StatusCode + '.\nBody: ' + JSON.stringify(res) + '.';
63104
}
64-
65-
try {
66-
var response = res.Body;
105+
if (raw) {
106+
return res;
107+
} else {
67108
try {
68-
response = JSON.parse(res.Body);
109+
var response = res.Body;
110+
try {
111+
response = JSON.parse(res.Body);
112+
} catch (ex) {
113+
// do nothing, already handled prior the try/catch
114+
}
115+
return {response: response};
69116
} catch (ex) {
70-
// do nothing, already handled prior the try/catch
117+
throw 'Demisto REST APIs - Error parsing response - ' + ex + '\nBody:' + res.Body;
71118
}
72-
return {response: response};
73-
} catch (ex) {
74-
throw 'Demisto REST APIs - Error parsing response - ' + ex;
75119
}
76120
};
77121
@@ -89,6 +133,21 @@ script:
89133
return sendRequest('PUT',args.uri, args.body);
90134
case 'demisto-api-delete':
91135
return sendRequest('DELETE',args.uri);
136+
case 'demisto-api-multipart':
137+
return sendMultipart(args.uri, args.entryID, args.body);
138+
case 'demisto-api-download':
139+
var res = sendRequest('GET',args.uri,args.body,true);
140+
var filename = res.Path;
141+
if (args.filename) {
142+
filename = args.filename;
143+
} else {
144+
var disposition = res.Headers['Content-Disposition'][0].split('=');
145+
if (disposition.length === 2) {
146+
filename = disposition[1];
147+
}
148+
}
149+
var desc = args.description || '';
150+
return ({Type: entryTypes.file, FileID: res.Path, File: filename, Contents: desc});
92151
default:
93152
throw 'Demisto REST APIs - unknown command';
94153
}
@@ -129,3 +188,26 @@ script:
129188
description: Request URI (i.e. /user)
130189
description: send HTTP DELETE request
131190
execution: true
191+
- name: demisto-api-download
192+
arguments:
193+
- name: uri
194+
required: true
195+
description: Request URI
196+
- name: filename
197+
description: File name of download
198+
- name: description
199+
description: Description of file entry
200+
description: Download files from Demisto server
201+
- name: demisto-api-multipart
202+
arguments:
203+
- name: uri
204+
required: true
205+
description: Request URI
206+
- name: entryID
207+
required: true
208+
description: File entry ID
209+
- name: body
210+
description: Request body
211+
description: Send HTTP Multipart request to upload files to Demisto server
212+
runonce: false
213+
releaseNotes: Added demisto-api-multipart and demisto-api-download commands to upload and download files from demisto server

Scripts/script-DemistoLogsBundle.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
commonfields:
2+
id: DemistoLogsBundle
3+
version: -1
4+
name: DemistoLogsBundle
5+
script: |
6+
return executeCommand('demisto-api-download', {uri: '/log/bundle'});
7+
type: javascript
8+
tags:
9+
- DemistoAPI
10+
comment: Gets Demisto Log Bundle to war room
11+
enabled: true
12+
scripttarget: 0
13+
runonce: false
14+
dependson:
15+
must:
16+
- demisto-api-download

Scripts/script-DemistoUploadFile.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
commonfields:
2+
id: DemistoUploadFile
3+
version: -1
4+
name: DemistoUploadFile
5+
script: |
6+
var res = executeCommand("demisto-api-multipart", {"uri":'entry/upload/' + args.incidentID,"entryID":args.entryID});
7+
if (isError(res[0])) {
8+
return res;
9+
}
10+
var entryId = dq(res,'Contents.response.entries.id');
11+
12+
var md = 'File uploaded successfully. Entry ID is ' + entryId;
13+
return { ContentsFormat: formats.json, Type: entryTypes.note, Contents: res, HumanReadable: md } ;
14+
type: javascript
15+
tags:
16+
- DemistoAPI
17+
comment: Upload file from incident war room to another incident's war room
18+
enabled: true
19+
args:
20+
- name: entryID
21+
required: true
22+
description: File entry ID
23+
- name: incidentID
24+
required: true
25+
description: Incident ID to upload the file to
26+
scripttarget: 0
27+
runonce: false
28+
29+
dependson:
30+
must:
31+
- demisto-api-multipart

0 commit comments

Comments
 (0)