44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7- import { SavedObjectsClientContract } from 'src/core/server' ;
7+ import { KibanaRequest , SavedObjectsClientContract } from 'src/core/server' ;
88import { generateEnrollmentAPIKey , deleteEnrollmentApiKeyForAgentPolicyId } from './api_keys' ;
99import { unenrollForAgentPolicyId } from './agents' ;
1010import { outputService } from './output' ;
1111import { agentPolicyService } from './agent_policy' ;
12+ import { appContextService } from './app_context' ;
13+
14+ const fakeRequest = ( {
15+ headers : { } ,
16+ getBasePath : ( ) => '' ,
17+ path : '/' ,
18+ route : { settings : { } } ,
19+ url : {
20+ href : '/' ,
21+ } ,
22+ raw : {
23+ req : {
24+ url : '/' ,
25+ } ,
26+ } ,
27+ } as unknown ) as KibanaRequest ;
1228
1329export async function agentPolicyUpdateEventHandler (
1430 soClient : SavedObjectsClientContract ,
@@ -17,20 +33,25 @@ export async function agentPolicyUpdateEventHandler(
1733) {
1834 const adminUser = await outputService . getAdminUser ( soClient ) ;
1935 const outputId = await outputService . getDefaultOutputId ( soClient ) ;
36+
2037 // If no admin user and no default output fleet is not enabled just skip this hook
2138 if ( ! adminUser || ! outputId ) {
2239 return ;
2340 }
2441
42+ // `soClient` from ingest `appContextService` is used to create policy change actions
43+ // to ensure encrypted SOs are handled correctly
44+ const internalSoClient = appContextService . getInternalUserSOClient ( fakeRequest ) ;
45+
2546 if ( action === 'created' ) {
2647 await generateEnrollmentAPIKey ( soClient , {
2748 agentPolicyId,
2849 } ) ;
29- await agentPolicyService . createFleetPolicyChangeAction ( soClient , agentPolicyId ) ;
50+ await agentPolicyService . createFleetPolicyChangeAction ( internalSoClient , agentPolicyId ) ;
3051 }
3152
3253 if ( action === 'updated' ) {
33- await agentPolicyService . createFleetPolicyChangeAction ( soClient , agentPolicyId ) ;
54+ await agentPolicyService . createFleetPolicyChangeAction ( internalSoClient , agentPolicyId ) ;
3455 }
3556
3657 if ( action === 'deleted' ) {
0 commit comments