Skip to content

Commit

Permalink
Fix agentPolicyUpdateEventHandler() to use app context soClient for c…
Browse files Browse the repository at this point in the history
…reation of actions (#79341) (#79393)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Paul Tavares <56442535+paul-tavares@users.noreply.github.com>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 4, 2020
1 parent 285e1a1 commit 52bf780
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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') {
Expand Down

0 comments on commit 52bf780

Please sign in to comment.