Skip to content

Commit ad6a42c

Browse files
committed
Fix after review
1 parent c8bcabb commit ad6a42c

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

x-pack/plugins/ingest_manager/common/types/models/agent.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface NewAgentAction {
2929
sent_at?: string;
3030
}
3131

32-
export interface AgentAction {
32+
export interface AgentAction extends NewAgentAction {
3333
type: AgentActionType;
3434
data?: any;
3535
sent_at?: string;
@@ -39,7 +39,7 @@ export interface AgentAction {
3939
ack_data?: any;
4040
}
4141

42-
export interface AgentPolicyAction {
42+
export interface AgentPolicyAction extends NewAgentAction {
4343
id: string;
4444
type: AgentActionType;
4545
data?: any;
@@ -58,12 +58,6 @@ interface CommonAgentActionSOAttributes {
5858
ack_data?: string;
5959
}
6060

61-
export type BaseAgentActionSOAttributes = CommonAgentActionSOAttributes & {
62-
agent_id?: string;
63-
policy_id?: string;
64-
policy_revision?: number;
65-
};
66-
6761
export type AgentActionSOAttributes = CommonAgentActionSOAttributes & {
6862
agent_id: string;
6963
};
@@ -72,6 +66,8 @@ export type AgentPolicyActionSOAttributes = CommonAgentActionSOAttributes & {
7266
policy_revision: number;
7367
};
7468

69+
export type BaseAgentActionSOAttributes = AgentActionSOAttributes | AgentPolicyActionSOAttributes;
70+
7571
export interface NewAgentEvent {
7672
type: 'STATE' | 'ERROR' | 'ACTION_RESULT' | 'ACTION';
7773
subtype: // State

x-pack/plugins/ingest_manager/server/services/agent_policy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ class AgentPolicyService {
398398
const packages = policy.inputs.reduce<string[]>((acc, input) => {
399399
const packageName = input.meta?.package?.name;
400400
if (packageName && acc.indexOf(packageName) < 0) {
401-
return [packageName, ...acc];
401+
acc.push(packageName);
402402
}
403403
return acc;
404404
}, []);

x-pack/plugins/ingest_manager/server/services/agents/actions.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { SavedObjectsClientContract, SavedObject } from 'kibana/server';
7+
import { SavedObjectsClientContract } from 'kibana/server';
88
import {
99
Agent,
1010
AgentAction,
@@ -14,7 +14,11 @@ import {
1414
AgentPolicyActionSOAttributes,
1515
} from '../../../common/types/models';
1616
import { AGENT_ACTION_SAVED_OBJECT_TYPE } from '../../../common/constants';
17-
import { savedObjectToAgentAction } from './saved_objects';
17+
import {
18+
isAgentActionSavedObject,
19+
isPolicyActionSavedObject,
20+
savedObjectToAgentAction,
21+
} from './saved_objects';
1822
import { appContextService } from '../app_context';
1923
import { nodeTypes } from '../../../../../../src/plugins/data/common';
2024

@@ -49,13 +53,18 @@ async function createAction(
4953
ack_data: newAgentAction.ack_data ? JSON.stringify(newAgentAction.ack_data) : undefined,
5054
});
5155

52-
const agentAction =
53-
so.attributes.agent_id !== undefined
54-
? savedObjectToAgentAction(so as SavedObject<AgentActionSOAttributes>)
55-
: savedObjectToAgentAction(so as SavedObject<AgentPolicyActionSOAttributes>);
56-
agentAction.data = newAgentAction.data;
56+
if (isAgentActionSavedObject(so)) {
57+
const agentAction = savedObjectToAgentAction(so);
58+
agentAction.data = newAgentAction.data;
59+
60+
return agentAction;
61+
} else if (isPolicyActionSavedObject(so)) {
62+
const agentAction = savedObjectToAgentAction(so);
63+
agentAction.data = newAgentAction.data;
5764

58-
return agentAction;
65+
return agentAction;
66+
}
67+
throw new Error('Invalid action');
5968
}
6069

6170
export async function getAgentActionsForCheckin(
@@ -163,12 +172,6 @@ export async function getAgentPolicyActionByIds(
163172
);
164173
}
165174

166-
function isAgentActionSavedObject(
167-
so: SavedObject<BaseAgentActionSOAttributes>
168-
): so is SavedObject<AgentActionSOAttributes> {
169-
return so.attributes.agent_id !== undefined;
170-
}
171-
172175
export async function getNewActionsSince(soClient: SavedObjectsClientContract, timestamp: string) {
173176
const filter = nodeTypes.function.buildNode('and', [
174177
nodeTypes.function.buildNode(
@@ -194,7 +197,7 @@ export async function getNewActionsSince(soClient: SavedObjectsClientContract, t
194197

195198
return res.saved_objects
196199
.filter(isAgentActionSavedObject)
197-
.map((so) => savedObjectToAgentAction(so as SavedObject<AgentActionSOAttributes>));
200+
.map((so) => savedObjectToAgentAction(so));
198201
}
199202

200203
export async function getLatestConfigChangeAction(

x-pack/plugins/ingest_manager/server/services/agents/checkin/state_new_actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export function agentCheckinStateNewActionsFactory() {
175175
if (!data) {
176176
return;
177177
}
178-
const newActions = data.filter((action) => action.agent_id);
178+
const newActions = data.filter((action) => action.agent_id === agent.id);
179179
if (newActions.length === 0) {
180180
return;
181181
}

x-pack/plugins/ingest_manager/server/services/agents/saved_objects.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function savedObjectToAgentAction(
5151
}
5252

5353
// If it's an AgentPolicyAction
54-
if (so.attributes.policy_id !== undefined && so.attributes.policy_revision !== undefined) {
54+
if (isPolicyActionSavedObject(so)) {
5555
return {
5656
id: so.id,
5757
type: so.attributes.type,
@@ -63,7 +63,7 @@ export function savedObjectToAgentAction(
6363
};
6464
}
6565

66-
if (so.attributes.agent_id === undefined) {
66+
if (!isAgentActionSavedObject(so)) {
6767
throw new Error(`Malformed saved object AgentAction ${so.id}`);
6868
}
6969

@@ -77,3 +77,15 @@ export function savedObjectToAgentAction(
7777
ack_data: so.attributes.ack_data ? JSON.parse(so.attributes.ack_data) : undefined,
7878
};
7979
}
80+
81+
export function isAgentActionSavedObject(
82+
so: SavedObject<BaseAgentActionSOAttributes>
83+
): so is SavedObject<AgentActionSOAttributes> {
84+
return (so.attributes as AgentActionSOAttributes).agent_id !== undefined;
85+
}
86+
87+
export function isPolicyActionSavedObject(
88+
so: SavedObject<BaseAgentActionSOAttributes>
89+
): so is SavedObject<AgentPolicyActionSOAttributes> {
90+
return (so.attributes as AgentPolicyActionSOAttributes).policy_id !== undefined;
91+
}

0 commit comments

Comments
 (0)