Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demisto REST API - new commands to upload and download files #1748

Merged
merged 6 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 92 additions & 10 deletions Integrations/integration-DemistoRESTAPI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,47 @@ script:
serverURL = serverURL.slice(0,-1);
}

var sendRequest = function(method, uri, body) {
sendMultipart = function (uri, entryID, body) {
var requestUrl = serverURL;
if (uri.slice(-1) !== '/') {
requestUrl += '/';
}
requestUrl += uri;

var res = httpMultipart(
requestUrl,
entryID,
{
Headers: {
'Authorization': [params.apikey],
'Content-Type': ['multipart/form-data'],
'Accept': ['application/json']
},
},
body,
params.insecure,
params.proxy,
undefined,
'file'
);
if (res.StatusCode < 200 || res.StatusCode >= 300) {
throw 'Demisto REST APIs - Request Failed.\nStatus code: ' + res.StatusCode + '.\nBody: ' + JSON.stringify(res) + '.';
}
try {
var response = res.Body;
try {
response = JSON.parse(res.Body);
} catch (ex) {
// do nothing, already handled prior the try/catch
}
return {response: response};
} catch (ex) {
throw 'Demisto REST APIs - Error parsing response - ' + ex + '\nBody:' + res.Body;
}

};

var sendRequest = function(method, uri, body, raw) {
var requestUrl = serverURL;
if (uri.slice(-1) !== '/') {
requestUrl += '/';
Expand All @@ -52,7 +92,8 @@ script:
'content-type': ['application/json'],
'authorization': [params.apikey]
},
Body: body
Body: body,
SaveToFile: raw
},
params.insecure,
params.proxy
Expand All @@ -61,17 +102,20 @@ script:
if (res.StatusCode < 200 || res.StatusCode >= 300) {
throw 'Demisto REST APIs - Request Failed.\nStatus code: ' + res.StatusCode + '.\nBody: ' + JSON.stringify(res) + '.';
}

try {
var response = res.Body;
if (raw) {
return res;
} else {
try {
response = JSON.parse(res.Body);
var response = res.Body;
try {
response = JSON.parse(res.Body);
} catch (ex) {
// do nothing, already handled prior the try/catch
}
return {response: response};
} catch (ex) {
// do nothing, already handled prior the try/catch
throw 'Demisto REST APIs - Error parsing response - ' + ex + '\nBody:' + res.Body;
}
return {response: response};
} catch (ex) {
throw 'Demisto REST APIs - Error parsing response - ' + ex;
}
};

Expand All @@ -89,6 +133,21 @@ script:
return sendRequest('PUT',args.uri, args.body);
case 'demisto-api-delete':
return sendRequest('DELETE',args.uri);
case 'demisto-api-multipart':
return sendMultipart(args.uri, args.entryID, args.body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this function defined? maybe httpMultipart?

case 'demisto-api-download':
var res = sendRequest('GET',args.uri,args.body,true);
var filename = res.Path;
if (args.filename) {
filename = args.filename;
} else {
var disposition = res.Headers['Content-Disposition'][0].split('=');
if (disposition.length === 2) {
filename = disposition[1];
}
}
var desc = args.description || '';
return ({Type: entryTypes.file, FileID: res.Path, File: filename, Contents: desc});
default:
throw 'Demisto REST APIs - unknown command';
}
Expand Down Expand Up @@ -129,3 +188,26 @@ script:
description: Request URI (i.e. /user)
description: send HTTP DELETE request
execution: true
- name: demisto-api-download
arguments:
- name: uri
required: true
description: Request URI
- name: filename
description: File name of download
- name: description
description: Description of file entry
description: Download files from Demisto server
- name: demisto-api-multipart
arguments:
- name: uri
required: true
description: Request URI
- name: entryID
required: true
description: File entry ID
- name: body
description: Request body
description: Send HTTP Multipart request to upload files to Demisto server
runonce: false
releaseNotes: Added demisto-api-multipart and demisto-api-download commands to upload and download files from demisto server
16 changes: 16 additions & 0 deletions Scripts/script-DemistoLogsBundle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
commonfields:
id: DemistoLogsBundle
version: -1
name: DemistoLogsBundle
script: |
return executeCommand('demisto-api-download', {uri: '/log/bundle'});
type: javascript
tags:
- DemistoAPI
comment: Gets Demisto Log Bundle to war room
enabled: true
scripttarget: 0
runonce: false
dependson:
must:
- demisto-api-download
31 changes: 31 additions & 0 deletions Scripts/script-DemistoUploadFile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
commonfields:
id: DemistoUploadFile
version: -1
name: DemistoUploadFile
script: |
var res = executeCommand("demisto-api-multipart", {"uri":'entry/upload/' + args.incidentID,"entryID":args.entryID});
if (isError(res[0])) {
return res;
}
var entryId = dq(res,'Contents.response.entries.id');

var md = 'File uploaded successfully. Entry ID is ' + entryId;
return { ContentsFormat: formats.json, Type: entryTypes.note, Contents: res, HumanReadable: md } ;
type: javascript
tags:
- DemistoAPI
comment: Upload file from incident war room to another incident's war room
enabled: true
args:
- name: entryID
required: true
description: File entry ID
- name: incidentID
required: true
description: Incident ID to upload the file to
scripttarget: 0
runonce: false

dependson:
must:
- demisto-api-multipart