diff --git a/Integrations/integration-DemistoRESTAPI.yml b/Integrations/integration-DemistoRESTAPI.yml index db2dfbac70f7..5946973ca3dd 100644 --- a/Integrations/integration-DemistoRESTAPI.yml +++ b/Integrations/integration-DemistoRESTAPI.yml @@ -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 += '/'; @@ -52,7 +92,8 @@ script: 'content-type': ['application/json'], 'authorization': [params.apikey] }, - Body: body + Body: body, + SaveToFile: raw }, params.insecure, params.proxy @@ -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; } }; @@ -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); + 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'; } @@ -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 diff --git a/Scripts/script-DemistoLogsBundle.yml b/Scripts/script-DemistoLogsBundle.yml new file mode 100644 index 000000000000..cfc1ce7c6cc3 --- /dev/null +++ b/Scripts/script-DemistoLogsBundle.yml @@ -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 \ No newline at end of file diff --git a/Scripts/script-DemistoUploadFile.yml b/Scripts/script-DemistoUploadFile.yml new file mode 100644 index 000000000000..d0e73ec39063 --- /dev/null +++ b/Scripts/script-DemistoUploadFile.yml @@ -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