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

Ciac 6453/improve generic polling - hotfix #26372

Merged
merged 49 commits into from May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
445c41d
Fixed script issues
AradCarmi May 7, 2023
7a19533
updated rn
AradCarmi May 7, 2023
5113781
merged from master
AradCarmi May 7, 2023
cbf51f1
Updated rn
AradCarmi May 7, 2023
1ce951f
Apply suggestions from code review
AradCarmi May 7, 2023
10bd746
updated script min version
AradCarmi May 8, 2023
d04af7a
updated script min version
AradCarmi May 8, 2023
271e057
fix pre-commit issues
AradCarmi May 8, 2023
b951845
updated the script to support only XSOAR
AradCarmi May 8, 2023
1a3f9a2
Merge branch 'master' of github.com:demisto/content into CIAC-6453/Im…
AradCarmi May 8, 2023
5e6ef3a
Merged master into current branch.
May 8, 2023
e20eb28
Bump pack from version CommonScripts to 1.11.69.
May 8, 2023
3b9244f
update to use endTime instead of using the context
AradCarmi May 8, 2023
84f8f68
merged from master
AradCarmi May 8, 2023
898b7ad
updated rn
AradCarmi May 8, 2023
3391326
merged from master
AradCarmi May 8, 2023
ddaed11
Merged master into current branch.
May 8, 2023
a2c102d
Bump pack from version CommonScripts to 1.11.70.
May 8, 2023
6e3ce83
updated rn
AradCarmi May 8, 2023
207ea25
Merge branch 'CIAC-6453/Improve_GenericPolling_v2' of github.com:demi…
AradCarmi May 8, 2023
c3664ff
Update Packs/CommonScripts/ReleaseNotes/1_11_70.md
AradCarmi May 9, 2023
7e5a742
merged from master
AradCarmi May 9, 2023
bcf3af0
updated rn and readme
AradCarmi May 9, 2023
324388e
Merge branch 'master' of github.com:demisto/content into CIAC-6453/Im…
AradCarmi May 9, 2023
690bf14
Updated rn
AradCarmi May 9, 2023
596d2ef
updated rn
AradCarmi May 9, 2023
f36084f
Merge branch 'master' of github.com:demisto/content into CIAC-6453/Im…
AradCarmi May 9, 2023
566f50d
updated script
AradCarmi May 9, 2023
5396820
Merge branch 'master' of github.com:demisto/content into CIAC-6453/Im…
AradCarmi May 9, 2023
75766c7
Apply suggestions from code review
AradCarmi May 9, 2023
e810b38
Update Packs/CommonScripts/Scripts/ScheduleGenericPolling/ScheduleGen…
AradCarmi May 9, 2023
5e4a9f4
Merge branch 'master' into CIAC-6453/Improve_GenericPolling_v2
AradCarmi May 9, 2023
89ddc78
Merged master into current branch.
May 9, 2023
9b12da1
Bump pack from version CommonScripts to 1.11.72.
May 9, 2023
e433f4d
Merged master into current branch.
May 10, 2023
a804a12
Bump pack from version CommonScripts to 1.11.73.
May 10, 2023
67b7060
updated script
AradCarmi May 10, 2023
b773bff
updated from master
AradCarmi May 10, 2023
a31aab2
updated rn
AradCarmi May 10, 2023
8d1e08a
Merge branch 'master' of github.com:demisto/content into CIAC-6453/Im…
AradCarmi May 10, 2023
3ceff90
updated docker image
AradCarmi May 10, 2023
bbd5f07
Merge branch 'master' of github.com:demisto/content into CIAC-6453/Im…
AradCarmi May 10, 2023
a84c019
Merge branch 'master' into CIAC-6453/Improve_GenericPolling_v2
AradCarmi May 10, 2023
2d6c641
Merge branch 'master' into CIAC-6453/Improve_GenericPolling_v2
AradCarmi May 10, 2023
144e289
updated script
AradCarmi May 10, 2023
50d17e8
Merge branch 'master' of github.com:demisto/content into CIAC-6453/Im…
AradCarmi May 10, 2023
5142305
Merge branch 'CIAC-6453/Improve_GenericPolling_v2' of github.com:demi…
AradCarmi May 10, 2023
6869e26
Merge branch 'master' into CIAC-6453/Improve_GenericPolling_v2
AradCarmi May 11, 2023
12f108d
updated scripts
AradCarmi May 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Packs/CommonScripts/ReleaseNotes/1_11_71.md
@@ -0,0 +1,14 @@

#### Scripts

##### ScheduleCommand

- Added the *scheduledEntryGuid* arguments to support the new ***GenericPolling*** playbook mechanism.

##### GenericPollingScheduledTask

- Added the *scheduledEntryGuid* and *endTime* arguments to support the new ***GenericPolling*** playbook mechanism.

##### ScheduleGenericPolling

- Added the *scheduledEntryGuid* argument to support the new ***GenericPolling*** playbook mechanism.
Expand Up @@ -16,6 +16,14 @@
* ID - the key that contains the id for polling
*/

// Constant to verify the minimum build number and XSIAM version for the new polling command (stopScheduleEntry feature).
//const MINIMUM_XSIAM_VERSION = '8.3.0';
//const MINIMUM_BUILD_NUMBER_XSIAM = 313276;
const MINIMUM_XSOAR_VERSION = '8.2.0';
const MINIMUM_BUILD_NUMBER_XSOAR = 309463;



function listOfStrings(v) {
if (!Array.isArray(v)) {
v = [v];
Expand All @@ -37,7 +45,7 @@ function intersect(a, b) {
});
}

function finish(playbookId, tag, err) {
function finish(playbookId, tag, err, entryGUID) {
var params = { 'id': tag };
if (err === undefined) {
params.input = 'YES';
Expand All @@ -47,9 +55,16 @@ function finish(playbookId, tag, err) {
if (playbookId) {
params.parentPlaybookID = playbookId;
}
if (entryGUID) {
var res = executeCommand("stopScheduleEntry", {'scheduledEntryGuid': entryGUID});
if (isError(res[0])) {
logError('Failed to stop scheduled entry: ' + res[0]);
}
}
return executeCommand("taskComplete", params);
}


function setNextRun(ids, playbookId, pollingCommand, pollingCommandArgName, pendingIds, interval, timeout, tag, additionalArgNames, additionalArgValues) {
var idsStr = ids.replace(/"/g, '\\"');
var playbookIdStr = '';
Expand All @@ -65,71 +80,167 @@ function setNextRun(ids, playbookId, pollingCommand, pollingCommandArgName, pend
'times': 1
});
}
function genericPollingScheduledTaskWithoutGuid() {
try {
if (args.timeout <= 0) {
return finish(args.playbookId, args.tag);
}

try {
if (args.timeout <= 0) {
return finish(args.playbookId, args.tag);
}
// Get ids that have not finished yet
var ids = argToList(args.ids);
for (var i = 0; i < ids.length; i++) {
ids[i] = ids[i].replace(/[\\]*"/g, '');
}

// Get ids that have not finished yet
var ids = argToList(args.ids);
for (var i = 0; i < ids.length; i++) {
ids[i] = ids[i].replace(/[\\]*"/g, '');
}

// Set the context of the scheduled task to the local playbook context
var idsToPoll = ids;
var pendingPath = args.pendingIds;

// Set the context of the scheduled task to the local playbook context
var idsToPoll = ids;
var pendingPath = args.pendingIds;
if ('playbookId' in args) {
playbookContext = 'subplaybook-' + args.playbookId;
pendingPath = playbookContext + "." + args.pendingIds;
}
var pendings = dq(invContext, pendingPath);
if ('playbookId' in args) {
playbookContext = 'subplaybook-' + args.playbookId;
pendingPath = playbookContext + "." + args.pendingIds;
}
var pendings = dq(invContext, pendingPath);

if (pendings === null) {
return finish(args.playbookId, args.tag);
}
if (pendings === null) {
return finish(args.playbookId, args.tag);
}

var idsStrArr = listOfStrings(ids);
var pendingsStrArr = listOfStrings(pendings);
idsToPoll = intersect(idsStrArr, pendingsStrArr);
if (idsToPoll.length === 0) {
return finish(args.playbookId, args.tag);
}

// Run the polling command for each id
var pollingCommandArgs = {};
var names = argToList(args.additionalPollingCommandArgNames);
var values = argToList(args.additionalPollingCommandArgValues);

for (var index = 0; index < names.length; index++)
pollingCommandArgs[names[index]] = values[index];

pollingCommandArgs[args.pollingCommandArgName] = idsToPoll.join(',');
var res = executeCommand(args.pollingCommand, pollingCommandArgs);

// Change the context output of the polling results to the local playbook context
if ('playbookId' in args) {
for (var i = 0; i < res.length; i++) {
if ('EntryContext' in res[i]) {
for (var k in res[i].EntryContext) {
res[i].EntryContext[playbookContext + "." + k] = res[i].EntryContext[k];
delete res[i].EntryContext[k];
}
}
}
}

// Schedule the next iteration
var scheduleTaskRes = setNextRun(args.ids, args.playbookId, args.pollingCommand, args.pollingCommandArgName, args.pendingIds, args.interval, args.timeout, args.tag, args.additionalPollingCommandArgNames, args.additionalPollingCommandArgValues);
if (isError(scheduleTaskRes[0])) {
res.push(scheduleTaskRes);
}

var idsStrArr = listOfStrings(ids);
var pendingsStrArr = listOfStrings(pendings);
idsToPoll = intersect(idsStrArr, pendingsStrArr);
if (idsToPoll.length === 0) {
return finish(args.playbookId, args.tag);
return res;
}
catch (err) {
finish(args.playbookId, args.tag, err);
throw err;
}
}

// Run the polling command for each id
var pollingCommandArgs = {};
var names = argToList(args.additionalPollingCommandArgNames);
var values = argToList(args.additionalPollingCommandArgValues);
function genericPollingScheduledTaskWithGuid() {
try {
var guid = args.scheduledEntryGuid;
var endTime = stringToDate(args.endTime, "%Y-%m-%d %H:%M:%S");
AradCarmi marked this conversation as resolved.
Show resolved Hide resolved
var currentTime = new Date();
if (currentTime >= endTime) {
return finish(args.playbookId, args.tag, undefined, guid);
}

// Get ids that have not finished yet
var ids = argToList(args.ids);
for (var i = 0; i < ids.length; i++) {
ids[i] = ids[i].replace(/[\\]*"/g, '');
}

for (var index = 0; index < names.length; index++)
pollingCommandArgs[names[index]] = values[index];

pollingCommandArgs[args.pollingCommandArgName] = idsToPoll.join(',');
var res = executeCommand(args.pollingCommand, pollingCommandArgs);
// Set the context of the scheduled task to the local playbook context
var idsToPoll = ids;
var pendingPath = args.pendingIds;
if ('playbookId' in args) {
playbookContext = 'subplaybook-' + args.playbookId;
pendingPath = playbookContext + "." + args.pendingIds;
}
var pendings = dq(invContext, pendingPath);

if (pendings === null) {
return finish(args.playbookId, args.tag, undefined, guid);
}

var idsStrArr = listOfStrings(ids);
var pendingsStrArr = listOfStrings(pendings);
idsToPoll = intersect(idsStrArr, pendingsStrArr);
if (idsToPoll.length === 0) {
return finish(args.playbookId, args.tag, undefined, guid);
}

// Change the context output of the polling results to the local playbook context
if ('playbookId' in args) {
for (var i = 0; i < res.length; i++) {
if ('EntryContext' in res[i]) {
for (var k in res[i].EntryContext) {
res[i].EntryContext[playbookContext + "." + k] = res[i].EntryContext[k];
delete res[i].EntryContext[k];
// Run the polling command for each id
var pollingCommandArgs = {};
var names = argToList(args.additionalPollingCommandArgNames);
var values = argToList(args.additionalPollingCommandArgValues);

for (var index = 0; index < names.length; index++)
pollingCommandArgs[names[index]] = values[index];

pollingCommandArgs[args.pollingCommandArgName] = idsToPoll.join(',');
var res = executeCommand(args.pollingCommand, pollingCommandArgs);

// Change the context output of the polling results to the local playbook context
if ('playbookId' in args) {
for (var i = 0; i < res.length; i++) {
if ('EntryContext' in res[i]) {
for (var k in res[i].EntryContext) {
res[i].EntryContext[playbookContext + "." + k] = res[i].EntryContext[k];
delete res[i].EntryContext[k];
}
}
}
}
}

// Schedule the next iteration
var scheduleTaskRes = setNextRun(args.ids, args.playbookId, args.pollingCommand, args.pollingCommandArgName, args.pendingIds, args.interval, args.timeout, args.tag, args.additionalPollingCommandArgNames, args.additionalPollingCommandArgValues);
if (isError(scheduleTaskRes[0])) {
res.push(scheduleTaskRes);
return res;
}
catch (err) {
finish(args.playbookId, args.tag, err, guid);
throw err;
}
}

function shouldRunWithGuid() {
res = getDemistoVersion();
platform = res.platform;
AradCarmi marked this conversation as resolved.
Show resolved Hide resolved
version = res.version;
buildNumber = res.buildNumber;

// Feature needs: to support XSIAM.
AradCarmi marked this conversation as resolved.
Show resolved Hide resolved
// ((platform === "x2") && (compareVersions(version, MINIMUM_XSIAM_VERSION) >= 0) && (parseInt(buildNumber) >= MINIMUM_BUILD_NUMBER_XSIAM))

return res;
// Checking if the stopScheduleEntry command is available.
// If not, we are running on an older version of platform and we need to use the old polling mechanism.
if ((buildNumber !== "REPLACE_THIS_WITH_CI_BUILD_NUM") &&
((platform === "xsoar") && (compareVersions(version, MINIMUM_XSOAR_VERSION) >= 0) && (parseInt(buildNumber) >= MINIMUM_BUILD_NUMBER_XSOAR))) {
return true;
}
}
catch (err) {
finish(args.playbookId, args.tag, err);
throw err;


function main() {
if (shouldRunWithGuid()) {
return genericPollingScheduledTaskWithGuid();
}
return genericPollingScheduledTaskWithoutGuid();
AradCarmi marked this conversation as resolved.
Show resolved Hide resolved
}

return main();
Expand Up @@ -42,6 +42,11 @@ args:
- name: additionalPollingCommandArgValues
description: Commas separated arguments values of the polling command
isArray: true
- name: scheduledEntryGuid
description: The GUID of the scheduled entry that runs the polling command.
- name: endTime
description: The time to end the polling.

scripttarget: 0
AradCarmi marked this conversation as resolved.
Show resolved Hide resolved
runonce: false
tests:
Expand Down
29 changes: 17 additions & 12 deletions Packs/CommonScripts/Scripts/GenericPollingScheduledTask/README.md
@@ -1,29 +1,34 @@
Runs the polling command repeatedly and completes a blocking manual task when polling is done.
Runs the polling command repeatedly, completes a blocking manual task when polling is done.

## Script Data

---

| **Name** | **Description** |
| --- | --- |
| Script Type | javascript |
| Tags | - |

| Cortex XSOAR Version | 5.0.0 |

## Inputs

---

| **Argument Name** | **Description** |
| --- | --- |
| ids | The list of IDs to poll. |
| pendingIds | The IDs with pending status. |
| pollingCommand | The name of the polling command to run. |
| pollingCommandArgName | The name of the argument of the polling command. |
| interval | The frequency to poll. How often the polling command should run (in minutes). |
| timeout | The amount of time to poll before declaring a timeout and resuming the playbook (in minutes). |
| ids | List of IDs to poll |
| pendingIds | IDs with pending status |
| pollingCommand | Name of the polling command to run |
| pollingCommandArgName | Name of the argument of the polling command |
| interval | Polling frequency - how often the polling command should run \(minutes\) |
| timeout | How much time to poll before declaring a timeout and resuming the playbook \(minutes\) |
| playbookId | The ID of the playbook that contains the manual task which will be completed once the polling is done. |
| tag | The tag of the blocking manual task ("Wait For Polling Task To Finish"). |
| additionalPollingCommandArgNames | The names of the additional arguments for the polling command. For example, arg1,arg2,... |
| additionalPollingCommandArgValues | The commas-separated arguments values of the polling command. |
| tag | The tag of the blocking manual task \("Wait For Polling Task To Finish"\) |
| additionalPollingCommandArgNames | Names of additional arguments for the polling command \(e.g. arg1,arg2,...\) |
| additionalPollingCommandArgValues | Commas separated arguments values of the polling command |
| scheduledEntryGuid | The GUID of the scheduled entry that runs the polling command. |
| endTime | The time to end the polling. |

## Outputs

---
There are no outputs for this script.
21 changes: 16 additions & 5 deletions Packs/CommonScripts/Scripts/ScheduleCommand/README.md
@@ -1,24 +1,35 @@
Schedules a command to run inside the War Room at a future time. Can be once or reoccurring.
Schedule a command to run inside the war room at a future time (once or reoccurring)

AradCarmi marked this conversation as resolved.
Show resolved Hide resolved
## Script Data

---

| **Name** | **Description** |
| --- | --- |
| Script Type | javascript |
| Tags | Utility |
| Cortex XSOAR Version | 5.0.0 |

## Used In

---
This script is used in the following playbooks and scripts.

* Schedule Task and Poll

## Inputs

---

| **Argument Name** | **Description** |
| --- | --- |
| command | The command to schedule. |
| cron | The scheduled time to run. |
| endDate | When should the schedule end. This is relevant only if "times" are not provided (Optional). The format is "Mon, 02 Jan 2006 15:04:05 MST". |
| times | The number of times to run (Optional). |
| command | The command to schedule |
| cron | The scheduled time to run |
| endDate | When should we end the schedule. Will be only relevant if times is not provided. Optional. Format is 'Mon, 02 Jan 2006 15:04:05 MST' |
| times | The number of times to run. Optional. |
| scheduledEntryGuid | The GUID of the scheduled entry that runs the polling command. |

## Outputs

---
There are no outputs for this script.
Expand Up @@ -22,6 +22,9 @@ args:
not provided. Optional. Format is 'Mon, 02 Jan 2006 15:04:05 MST'
- name: times
description: The number of times to run. Optional.
- name: scheduledEntryGuid
description: The GUID of the scheduled entry that runs the polling command.

scripttarget: 0
AradCarmi marked this conversation as resolved.
Show resolved Hide resolved
dependson: {}
timeout: 0s
Expand Down