Skip to content

Commit 3c20100

Browse files
authored
[Fleet] Fix agent policy change action migration (#79046) (#79185)
* Fix agent policy change action migration for encrypted `data` property * Parse & re-stringify `config`->`policy` data
1 parent 3bafe41 commit 3c20100

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

x-pack/plugins/ingest_manager/server/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class IngestManagerPlugin
172172
this.encryptedSavedObjectsSetup = deps.encryptedSavedObjects;
173173
this.cloud = deps.cloud;
174174

175-
registerSavedObjects(core.savedObjects);
175+
registerSavedObjects(core.savedObjects, deps.encryptedSavedObjects);
176176
registerEncryptedSavedObjects(deps.encryptedSavedObjects);
177177

178178
// Register feature

x-pack/plugins/ingest_manager/server/saved_objects/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ import {
3333
* Please update typings in `/common/types` as well as
3434
* schemas in `/server/types` if mappings are updated.
3535
*/
36-
const savedObjectTypes: { [key: string]: SavedObjectsType } = {
36+
const getSavedObjectTypes = (
37+
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup
38+
): { [key: string]: SavedObjectsType } => ({
3739
[GLOBAL_SETTINGS_SAVED_OBJECT_TYPE]: {
3840
name: GLOBAL_SETTINGS_SAVED_OBJECT_TYPE,
3941
hidden: false,
@@ -111,7 +113,7 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
111113
},
112114
},
113115
migrations: {
114-
'7.10.0': migrateAgentActionToV7100,
116+
'7.10.0': migrateAgentActionToV7100(encryptedSavedObjects),
115117
},
116118
},
117119
[AGENT_EVENT_SAVED_OBJECT_TYPE]: {
@@ -304,9 +306,13 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
304306
},
305307
},
306308
},
307-
};
309+
});
308310

309-
export function registerSavedObjects(savedObjects: SavedObjectsServiceSetup) {
311+
export function registerSavedObjects(
312+
savedObjects: SavedObjectsServiceSetup,
313+
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup
314+
) {
315+
const savedObjectTypes = getSavedObjectTypes(encryptedSavedObjects);
310316
Object.values(savedObjectTypes).forEach((type) => {
311317
savedObjects.registerType(type);
312318
});

x-pack/plugins/ingest_manager/server/saved_objects/migrations/to_v7_10_0.ts

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

7-
import { SavedObjectMigrationFn } from 'kibana/server';
7+
import { SavedObjectMigrationFn, SavedObjectUnsanitizedDoc } from 'kibana/server';
8+
import { EncryptedSavedObjectsPluginSetup } from '../../../../encrypted_saved_objects/server';
89
import {
910
Agent,
1011
AgentEvent,
@@ -94,17 +95,42 @@ export const migrateSettingsToV7100: SavedObjectMigrationFn<
9495
return settingsDoc;
9596
};
9697

97-
export const migrateAgentActionToV7100: SavedObjectMigrationFn<AgentAction, AgentAction> = (
98-
agentActionDoc
99-
) => {
100-
// @ts-expect-error
101-
if (agentActionDoc.attributes.type === 'CONFIG_CHANGE') {
102-
agentActionDoc.attributes.type = 'POLICY_CHANGE';
103-
if (agentActionDoc.attributes.data?.config) {
104-
agentActionDoc.attributes.data.policy = agentActionDoc.attributes.data.config;
105-
delete agentActionDoc.attributes.data.config;
98+
export const migrateAgentActionToV7100 = (
99+
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup
100+
): SavedObjectMigrationFn<AgentAction, AgentAction> => {
101+
return encryptedSavedObjects.createMigration(
102+
(agentActionDoc): agentActionDoc is SavedObjectUnsanitizedDoc<AgentAction> => {
103+
// @ts-expect-error
104+
return agentActionDoc.attributes.type === 'CONFIG_CHANGE';
105+
},
106+
(agentActionDoc) => {
107+
let agentActionData;
108+
try {
109+
agentActionData = agentActionDoc.attributes.data
110+
? JSON.parse(agentActionDoc.attributes.data)
111+
: undefined;
112+
} catch (e) {
113+
// Silently swallow JSON parsing error
114+
}
115+
if (agentActionData && agentActionData.config) {
116+
const {
117+
attributes: { data, ...restOfAttributes },
118+
} = agentActionDoc;
119+
const { config, ...restOfData } = agentActionData;
120+
return {
121+
...agentActionDoc,
122+
attributes: {
123+
...restOfAttributes,
124+
type: 'POLICY_CHANGE',
125+
data: JSON.stringify({
126+
...restOfData,
127+
policy: config,
128+
}),
129+
},
130+
};
131+
} else {
132+
return agentActionDoc;
133+
}
106134
}
107-
}
108-
109-
return agentActionDoc;
135+
);
110136
};

0 commit comments

Comments
 (0)