Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Fix agent policy change action migration #79046

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
);
};