/
update_overwrite_exception_list_item.ts
85 lines (81 loc) · 2.53 KB
/
update_overwrite_exception_list_item.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { ExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';
import { getSavedObjectType } from '@kbn/securitysolution-list-utils';
import { ExceptionListSoSchema } from '../../schemas/saved_objects';
import {
transformSavedObjectUpdateToExceptionListItem,
transformUpdateCommentsToComments,
} from './utils';
import { getExceptionListItem } from './get_exception_list_item';
import { UpdateExceptionListItemOptions } from './update_exception_list_item';
export const updateOverwriteExceptionListItem = async ({
_version,
comments,
entries,
expireTime,
id,
savedObjectsClient,
namespaceType,
name,
osTypes,
description,
itemId,
meta,
user,
tags,
type,
}: UpdateExceptionListItemOptions): Promise<ExceptionListItemSchema | null> => {
const savedObjectType = getSavedObjectType({ namespaceType });
const exceptionListItem = await getExceptionListItem({
id,
itemId,
namespaceType,
savedObjectsClient,
});
if (exceptionListItem == null) {
return null;
} else {
const transformedComments = transformUpdateCommentsToComments({
comments,
existingComments: exceptionListItem.comments,
user,
});
const savedObject = await savedObjectsClient.create<ExceptionListSoSchema>(
savedObjectType,
{
comments: transformedComments,
created_at: exceptionListItem.created_at,
created_by: exceptionListItem.created_by,
description: description ?? exceptionListItem.description,
entries,
expire_time: expireTime,
immutable: undefined,
item_id: itemId ?? exceptionListItem.item_id,
list_id: exceptionListItem.list_id,
list_type: 'item',
meta,
name: name ?? exceptionListItem.name,
os_types: osTypes,
tags: tags ?? exceptionListItem.tags,
tie_breaker_id: exceptionListItem.tie_breaker_id,
type: type ?? exceptionListItem.type,
updated_by: user,
version: exceptionListItem._version ? parseInt(exceptionListItem._version, 10) : undefined,
},
{
id: id ?? exceptionListItem.id,
overwrite: true,
version: _version,
}
);
return transformSavedObjectUpdateToExceptionListItem({
exceptionListItem,
savedObject,
});
}
};