Fix agentPolicyUpdateEventHandler() to use app context soClient for creation of actions (#79341)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Paul Tavares 2020-10-03 19:24:39 -04:00 committed by GitHub
parent 513ebb50f9
commit e859fe7c65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,11 +4,27 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { SavedObjectsClientContract } from 'src/core/server';
import { KibanaRequest, SavedObjectsClientContract } from 'src/core/server';
import { generateEnrollmentAPIKey, deleteEnrollmentApiKeyForAgentPolicyId } from './api_keys';
import { unenrollForAgentPolicyId } from './agents';
import { outputService } from './output';
import { agentPolicyService } from './agent_policy';
import { appContextService } from './app_context';
const fakeRequest = ({
headers: {},
getBasePath: () => '',
path: '/',
route: { settings: {} },
url: {
href: '/',
},
raw: {
req: {
url: '/',
},
},
} as unknown) as KibanaRequest;
export async function agentPolicyUpdateEventHandler(
soClient: SavedObjectsClientContract,
@ -17,20 +33,25 @@ export async function agentPolicyUpdateEventHandler(
) {
const adminUser = await outputService.getAdminUser(soClient);
const outputId = await outputService.getDefaultOutputId(soClient);
// If no admin user and no default output fleet is not enabled just skip this hook
if (!adminUser || !outputId) {
return;
}
// `soClient` from ingest `appContextService` is used to create policy change actions
// to ensure encrypted SOs are handled correctly
const internalSoClient = appContextService.getInternalUserSOClient(fakeRequest);
if (action === 'created') {
await generateEnrollmentAPIKey(soClient, {
agentPolicyId,
});
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
await agentPolicyService.createFleetPolicyChangeAction(internalSoClient, agentPolicyId);
}
if (action === 'updated') {
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
await agentPolicyService.createFleetPolicyChangeAction(internalSoClient, agentPolicyId);
}
if (action === 'deleted') {