-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Enrich agent with agent policies
- Loading branch information
Showing
5 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
x-pack/plugins/fleet/server/services/agent_policies/agent_policies_enrich.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import pMap from 'p-map'; | ||
import { type ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
import { type SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; | ||
import { type BulkResponseItem } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
|
||
import { agentPolicyService } from '../agent_policy'; | ||
import { appContextService } from '../app_context'; | ||
|
||
export async function updatePoliciesEnrich( | ||
soClient: SavedObjectsClientContract, | ||
esClient: ElasticsearchClient, | ||
agentPolicyIds: string[] | ||
) { | ||
// TODO move outside of that service | ||
const agentPoliciesBulkBody = ( | ||
await pMap(agentPolicyIds, (policyId) => { | ||
return agentPolicyService.get(soClient, policyId, true); | ||
}) | ||
).flatMap((agentPolicy) => | ||
agentPolicy | ||
? [ | ||
{ | ||
update: { | ||
_id: agentPolicy.id, | ||
_index: '.fleet-agent-policies-metadata', | ||
retry_on_conflict: 3, | ||
}, | ||
}, | ||
{ | ||
doc: { | ||
policy_id: agentPolicy.id, | ||
agent_policy: { | ||
id: agentPolicy.id, | ||
name: agentPolicy.name, | ||
namespace: agentPolicy.namespace, | ||
inactivity_timeout: agentPolicy.inactivity_timeout, | ||
is_managed: agentPolicy.is_managed, | ||
package_policies: agentPolicy.package_policies?.map((packagePolicy) => ({ | ||
id: agentPolicy.id, | ||
name: packagePolicy.name, | ||
namespace: packagePolicy.namespace, | ||
package: packagePolicy.package | ||
? { | ||
name: packagePolicy.package.name, | ||
version: packagePolicy.package.version, | ||
} | ||
: undefined, | ||
})), | ||
}, | ||
}, | ||
doc_as_upsert: true, | ||
}, | ||
] | ||
: [] | ||
); | ||
|
||
// Deploy fleet-policies-metadata | ||
// Could be optimized if feature flag is adopted | ||
const agentPoliciesBulkResponse = await esClient.bulk({ | ||
index: '.fleet-agent-policies-metadata', // TODO use a constant | ||
operations: agentPoliciesBulkBody, | ||
refresh: 'wait_for', | ||
}); | ||
|
||
if (agentPoliciesBulkResponse.errors) { | ||
const logger = appContextService.getLogger(); | ||
const erroredDocuments = agentPoliciesBulkResponse.items.reduce((acc, item) => { | ||
const value: BulkResponseItem | undefined = item.index; | ||
if (!value || !value.error) { | ||
return acc; | ||
} | ||
|
||
acc.push(value); | ||
return acc; | ||
}, [] as BulkResponseItem[]); | ||
|
||
logger.warn( | ||
`Failed to index agent policy metadata during policy deployment: ${JSON.stringify( | ||
erroredDocuments | ||
)}` | ||
); | ||
} | ||
// execute enrich policy | ||
await esClient.enrich.executePolicy({ | ||
name: 'fleet-agents-enrich-agent-policies', | ||
wait_for_completion: true, | ||
}); | ||
|
||
await pMap(agentPolicyIds, (policyId) => { | ||
// Update will go through the ingest pipeline again | ||
return esClient.updateByQuery({ | ||
index: '.fleet-agents', | ||
q: `policy_id:"${policyId}"`, | ||
ignore_unavailable: true, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters