Skip to content

Commit

Permalink
[Fleet] Fix agent policy change action migration (#79046) (#79185)
Browse files Browse the repository at this point in the history
* Fix agent policy change action migration for encrypted `data` property

* Parse & re-stringify `config`->`policy` data
  • Loading branch information
jen-huang authored Oct 1, 2020
1 parent 3bafe41 commit 3c20100
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/ingest_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class IngestManagerPlugin
this.encryptedSavedObjectsSetup = deps.encryptedSavedObjects;
this.cloud = deps.cloud;

registerSavedObjects(core.savedObjects);
registerSavedObjects(core.savedObjects, deps.encryptedSavedObjects);
registerEncryptedSavedObjects(deps.encryptedSavedObjects);

// Register feature
Expand Down
14 changes: 10 additions & 4 deletions x-pack/plugins/ingest_manager/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import {
* Please update typings in `/common/types` as well as
* schemas in `/server/types` if mappings are updated.
*/
const savedObjectTypes: { [key: string]: SavedObjectsType } = {
const getSavedObjectTypes = (
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup
): { [key: string]: SavedObjectsType } => ({
[GLOBAL_SETTINGS_SAVED_OBJECT_TYPE]: {
name: GLOBAL_SETTINGS_SAVED_OBJECT_TYPE,
hidden: false,
Expand Down Expand Up @@ -111,7 +113,7 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
},
},
migrations: {
'7.10.0': migrateAgentActionToV7100,
'7.10.0': migrateAgentActionToV7100(encryptedSavedObjects),
},
},
[AGENT_EVENT_SAVED_OBJECT_TYPE]: {
Expand Down Expand Up @@ -304,9 +306,13 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
},
},
},
};
});

export function registerSavedObjects(savedObjects: SavedObjectsServiceSetup) {
export function registerSavedObjects(
savedObjects: SavedObjectsServiceSetup,
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup
) {
const savedObjectTypes = getSavedObjectTypes(encryptedSavedObjects);
Object.values(savedObjectTypes).forEach((type) => {
savedObjects.registerType(type);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectMigrationFn } from 'kibana/server';
import { SavedObjectMigrationFn, SavedObjectUnsanitizedDoc } from 'kibana/server';
import { EncryptedSavedObjectsPluginSetup } from '../../../../encrypted_saved_objects/server';
import {
Agent,
AgentEvent,
Expand Down Expand Up @@ -94,17 +95,42 @@ export const migrateSettingsToV7100: SavedObjectMigrationFn<
return settingsDoc;
};

export const migrateAgentActionToV7100: SavedObjectMigrationFn<AgentAction, AgentAction> = (
agentActionDoc
) => {
// @ts-expect-error
if (agentActionDoc.attributes.type === 'CONFIG_CHANGE') {
agentActionDoc.attributes.type = 'POLICY_CHANGE';
if (agentActionDoc.attributes.data?.config) {
agentActionDoc.attributes.data.policy = agentActionDoc.attributes.data.config;
delete agentActionDoc.attributes.data.config;
export const migrateAgentActionToV7100 = (
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup
): SavedObjectMigrationFn<AgentAction, AgentAction> => {
return encryptedSavedObjects.createMigration(
(agentActionDoc): agentActionDoc is SavedObjectUnsanitizedDoc<AgentAction> => {
// @ts-expect-error
return agentActionDoc.attributes.type === 'CONFIG_CHANGE';
},
(agentActionDoc) => {
let agentActionData;
try {
agentActionData = agentActionDoc.attributes.data
? JSON.parse(agentActionDoc.attributes.data)
: undefined;
} catch (e) {
// Silently swallow JSON parsing error
}
if (agentActionData && agentActionData.config) {
const {
attributes: { data, ...restOfAttributes },
} = agentActionDoc;
const { config, ...restOfData } = agentActionData;
return {
...agentActionDoc,
attributes: {
...restOfAttributes,
type: 'POLICY_CHANGE',
data: JSON.stringify({
...restOfData,
policy: config,
}),
},
};
} else {
return agentActionDoc;
}
}
}

return agentActionDoc;
);
};

0 comments on commit 3c20100

Please sign in to comment.