[Fleet] Reset preconfiguration delete preconfiguration deletion record (#126273)

This commit is contained in:
Nicolas Chaulet 2022-02-23 14:17:34 -05:00 committed by GitHub
parent 7c1e67cafb
commit 448436f89f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 0 deletions

View file

@ -13,6 +13,7 @@ import * as kbnTestServer from 'src/core/test_helpers/kbn_server';
import type { HttpMethod } from 'src/core/test_helpers/kbn_server';
import type { AgentPolicySOAttributes } from '../types';
import { PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE } from '../../common';
import { useDockerRegistry } from './docker_registry_helper';
@ -304,5 +305,39 @@ describe('Fleet preconfiguration reset', () => {
])
);
});
it('Works and reset one preconfigured policies if the policy was deleted with a preconfiguration deletion record', async () => {
const soClient = kbnServer.coreStart.savedObjects.createInternalRepository();
await soClient.delete('ingest-agent-policies', POLICY_ID);
await soClient.create(PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE, {
id: POLICY_ID,
});
const resetAPI = getSupertestWithAdminUser(
kbnServer.root,
'post',
`/internal/fleet/reset_preconfigured_agent_policies/${POLICY_ID}`
);
await resetAPI.set('kbn-sxrf', 'xx').expect(200).send();
const agentPolicies = await kbnServer.coreStart.savedObjects
.createInternalRepository()
.find<AgentPolicySOAttributes>({
type: 'ingest-agent-policies',
perPage: 10000,
});
expect(agentPolicies.saved_objects).toHaveLength(2);
expect(agentPolicies.saved_objects.map((ap) => ({ ...ap.attributes }))).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'Elastic Cloud agent policy 0001',
}),
expect.objectContaining({
name: 'Second preconfigured policy',
}),
])
);
});
});
});

View file

@ -14,6 +14,7 @@ import {
AGENT_POLICY_SAVED_OBJECT_TYPE,
SO_SEARCH_LIMIT,
PACKAGE_POLICY_SAVED_OBJECT_TYPE,
PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE,
} from '../../constants';
import { agentPolicyService } from '../agent_policy';
import { packagePolicyService } from '../package_policy';
@ -30,6 +31,7 @@ export async function resetPreconfiguredAgentPolicies(
logger.warn('Reseting Fleet preconfigured agent policies');
await _deleteExistingData(soClient, esClient, logger, agentPolicyId);
await _deleteGhostPackagePolicies(soClient, esClient, logger);
await _deletePreconfigurationDeleteRecord(soClient, logger, agentPolicyId);
await setupFleet(soClient, esClient);
}
@ -78,6 +80,40 @@ async function _deleteGhostPackagePolicies(
);
}
async function _deletePreconfigurationDeleteRecord(
soClient: SavedObjectsClientContract,
logger: Logger,
agentPolicyId?: string
) {
const existingDeletionRecord = await soClient.find<{ id: string }>({
type: PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE,
perPage: SO_SEARCH_LIMIT,
});
const deletionRecordSavedObjects = agentPolicyId
? existingDeletionRecord.saved_objects.filter((so) => so.attributes.id === agentPolicyId)
: existingDeletionRecord.saved_objects;
if (deletionRecordSavedObjects.length > 0) {
await pMap(
deletionRecordSavedObjects,
(savedObject) =>
soClient
.delete(PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE, savedObject.id)
.catch((err) => {
if (soClient.errors.isNotFoundError(err)) {
return undefined;
}
throw err;
}),
{
concurrency: 20,
}
);
}
}
async function _deleteExistingData(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,