Skip to content

Commit e001da8

Browse files
committed
Add force option to cancel request schema
1 parent 209d0b6 commit e001da8

2 files changed

Lines changed: 72 additions & 12 deletions

File tree

  • x-pack/solutions/security/plugins/security_solution/common

x-pack/solutions/security/plugins/security_solution/common/api/endpoint/actions/response_actions/cancel/cancel.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,37 @@ import type { TypeOf } from '@kbn/config-schema';
99
import { schema } from '@kbn/config-schema';
1010
import { BaseActionRequestSchema } from '../../common/base';
1111

12+
const IdParameterSchema = {
13+
id: schema.string({
14+
minLength: 1,
15+
validate: (value) => {
16+
if (!value.trim().length) {
17+
return 'id cannot be an empty string';
18+
}
19+
},
20+
}),
21+
};
22+
23+
const MSDefenderEndpointCancelActionRequestParamsSchema = schema.object(IdParameterSchema);
24+
25+
const EndpointCancelActionRequestParamsSchema = schema.object({
26+
...IdParameterSchema,
27+
force: schema.maybe(schema.boolean()),
28+
});
29+
1230
const CancelActionRequestBodySchema = schema.object({
1331
...BaseActionRequestSchema,
14-
parameters: schema.object({
15-
id: schema.string({
16-
minLength: 1,
17-
validate: (value) => {
18-
if (!value.trim().length) {
19-
return 'id cannot be an empty string';
20-
}
21-
},
22-
}),
23-
}),
32+
parameters: schema.conditional(
33+
schema.siblingRef('agent_type'),
34+
'microsoft_defender_endpoint',
35+
MSDefenderEndpointCancelActionRequestParamsSchema,
36+
schema.conditional(
37+
schema.siblingRef('agent_type'),
38+
'endpoint',
39+
EndpointCancelActionRequestParamsSchema,
40+
schema.never()
41+
)
42+
),
2443
});
2544

2645
export const CancelActionRequestSchema = {

x-pack/solutions/security/plugins/security_solution/common/endpoint/schema/actions.test.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from '../../api/endpoint';
2828
import type { MemoryDumpActionRequestBody } from '../../api/endpoint/actions/response_actions/memory_dump';
2929
import { MemoryDumpActionRequestSchema } from '../../api/endpoint/actions/response_actions/memory_dump';
30+
import { isActionSupportedByAgentType } from '../service/response_actions/is_response_action_supported';
3031

3132
// NOTE: Even though schemas are kept in common/api/endpoint - we keep tests here, because common/api should import from outside
3233
describe('actions schemas', () => {
@@ -1214,13 +1215,18 @@ describe('actions schemas', () => {
12141215
});
12151216
});
12161217
});
1217-
describe('CancelActionRequestSchema', () => {
1218+
1219+
describe.each(
1220+
RESPONSE_ACTION_AGENT_TYPE.filter((agentType) =>
1221+
isActionSupportedByAgentType(agentType, 'cancel', 'manual')
1222+
)
1223+
)('CancelActionRequestSchema for agent type: %s', (agentType) => {
12181224
it('should validate valid cancel request with all base fields', () => {
12191225
expect(() => {
12201226
CancelActionRequestSchema.body.validate({
12211227
endpoint_ids: ['endpoint-123'],
12221228
comment: 'Cancelling action due to change in requirements',
1223-
agent_type: 'microsoft_defender_endpoint',
1229+
agent_type: agentType,
12241230
parameters: {
12251231
id: '12345678-1234-5678-9012-123456789012',
12261232
},
@@ -1235,6 +1241,7 @@ describe('actions schemas', () => {
12351241
id: '12345678-1234-5678-9012-123456789012',
12361242
},
12371243
endpoint_ids: ['endpoint-123'],
1244+
agent_type: agentType,
12381245
});
12391246
}).not.toThrow();
12401247
});
@@ -1245,6 +1252,7 @@ describe('actions schemas', () => {
12451252
parameters: {
12461253
id: '',
12471254
},
1255+
agent_type: agentType,
12481256
endpoint_ids: ['endpoint-123'],
12491257
});
12501258
}).toThrow();
@@ -1257,6 +1265,7 @@ describe('actions schemas', () => {
12571265
id: ' ',
12581266
},
12591267
endpoint_ids: ['endpoint-123'],
1268+
agent_type: agentType,
12601269
});
12611270
}).toThrow();
12621271
});
@@ -1267,6 +1276,7 @@ describe('actions schemas', () => {
12671276
endpoint_ids: ['endpoint-123'],
12681277
comment: 'Cancel reason',
12691278
parameters: {},
1279+
agent_type: agentType,
12701280
});
12711281
}).toThrow();
12721282
});
@@ -1278,6 +1288,7 @@ describe('actions schemas', () => {
12781288
id: '12345678-1234-5678-9012-123456789012',
12791289
},
12801290
endpoint_ids: ['endpoint-123'],
1291+
agent_type: agentType,
12811292
comment: 'Cancelling due to policy change',
12821293
});
12831294
}).not.toThrow();
@@ -1289,6 +1300,7 @@ describe('actions schemas', () => {
12891300
parameters: {
12901301
id: '12345678-1234-5678-9012-123456789012',
12911302
},
1303+
agent_type: agentType,
12921304
endpoint_ids: ['endpoint-123'],
12931305
});
12941306
}).not.toThrow();
@@ -1301,12 +1313,41 @@ describe('actions schemas', () => {
13011313
id: '12345678-1234-5678-9012-123456789012',
13021314
},
13031315
endpoint_ids: ['endpoint-123'],
1316+
agent_type: agentType,
13041317
alert_ids: ['alert-456'],
13051318
case_ids: ['case-789'],
13061319
comment: 'Cancel with related alerts and cases',
13071320
});
13081321
}).not.toThrow();
13091322
});
1323+
1324+
if (agentType === 'endpoint') {
1325+
it('should accept `--force` argument is present', () => {
1326+
expect(() => {
1327+
CancelActionRequestSchema.body.validate({
1328+
parameters: {
1329+
id: '12345678-1234-5678-9012-123456789012',
1330+
force: true,
1331+
},
1332+
endpoint_ids: ['endpoint-123'],
1333+
agent_type: agentType,
1334+
});
1335+
}).not.toThrow();
1336+
});
1337+
} else {
1338+
it('should reject if `-force` argument is present', () => {
1339+
expect(() => {
1340+
CancelActionRequestSchema.body.validate({
1341+
parameters: {
1342+
id: '12345678-1234-5678-9012-123456789012',
1343+
force: true,
1344+
},
1345+
endpoint_ids: ['endpoint-123'],
1346+
agent_type: agentType,
1347+
});
1348+
}).toThrow();
1349+
});
1350+
}
13101351
});
13111352

13121353
describe('MemoryDumpActionRequestSchema', () => {

0 commit comments

Comments
 (0)