-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
actions.ts
129 lines (117 loc) · 3.71 KB
/
actions.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { SavedObjectsClientContract } from 'kibana/server';
import { Agent, AgentAction, AgentActionSOAttributes } from '../../../common/types/models';
import { AGENT_ACTION_SAVED_OBJECT_TYPE } from '../../../common/constants';
import { savedObjectToAgentAction } from './saved_objects';
import { appContextService } from '../app_context';
import { nodeTypes } from '../../../../../../src/plugins/data/common';
export async function createAgentAction(
soClient: SavedObjectsClientContract,
newAgentAction: Omit<AgentAction, 'id'>
): Promise<AgentAction> {
const so = await soClient.create<AgentActionSOAttributes>(AGENT_ACTION_SAVED_OBJECT_TYPE, {
...newAgentAction,
data: newAgentAction.data ? JSON.stringify(newAgentAction.data) : undefined,
});
const agentAction = savedObjectToAgentAction(so);
agentAction.data = newAgentAction.data;
return agentAction;
}
export async function getAgentActionsForCheckin(
soClient: SavedObjectsClientContract,
agentId: string
): Promise<AgentAction[]> {
const filter = nodeTypes.function.buildNode('and', [
nodeTypes.function.buildNode(
'not',
nodeTypes.function.buildNode(
'is',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`,
'*'
)
),
nodeTypes.function.buildNode(
'is',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.agent_id`,
agentId
),
]);
const res = await soClient.find<AgentActionSOAttributes>({
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
filter,
});
return Promise.all(
res.saved_objects.map(async (so) => {
// Get decrypted actions
return savedObjectToAgentAction(
await appContextService
.getEncryptedSavedObjects()
.getDecryptedAsInternalUser<AgentActionSOAttributes>(
AGENT_ACTION_SAVED_OBJECT_TYPE,
so.id
)
);
})
);
}
export async function getAgentActionByIds(
soClient: SavedObjectsClientContract,
actionIds: string[]
) {
const actions = (
await soClient.bulkGet<AgentActionSOAttributes>(
actionIds.map((actionId) => ({
id: actionId,
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
}))
)
).saved_objects.map(savedObjectToAgentAction);
return Promise.all(
actions.map(async (action) => {
// Get decrypted actions
return savedObjectToAgentAction(
await appContextService
.getEncryptedSavedObjects()
.getDecryptedAsInternalUser<AgentActionSOAttributes>(
AGENT_ACTION_SAVED_OBJECT_TYPE,
action.id
)
);
})
);
}
export async function getNewActionsSince(soClient: SavedObjectsClientContract, timestamp: string) {
const filter = nodeTypes.function.buildNode('and', [
nodeTypes.function.buildNode(
'not',
nodeTypes.function.buildNode(
'is',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`,
'*'
)
),
nodeTypes.function.buildNode(
'range',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.created_at`,
{
gte: timestamp,
}
),
]);
const res = await soClient.find<AgentActionSOAttributes>({
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
filter,
});
return res.saved_objects.map(savedObjectToAgentAction);
}
export interface ActionsService {
getAgent: (soClient: SavedObjectsClientContract, agentId: string) => Promise<Agent>;
createAgentAction: (
soClient: SavedObjectsClientContract,
newAgentAction: AgentActionSOAttributes
) => Promise<AgentAction>;
}