From 9bc57412bb84d9108e80acc62c4dc6d1753c5731 Mon Sep 17 00:00:00 2001 From: Jill Guyonnet Date: Fri, 26 Jul 2024 13:40:09 +0100 Subject: [PATCH] [Fleet] Fix namespaces property of created agent policies (#189199) ## Summary I found a small bug while working on https://github.com/elastic/kibana/issues/185040: when agent policies are created, there should be a root-level `namespaces` property, which is currently missing. `GET .fleet-policies/_mapping` contains a `namespaces` property with `keyword` type that was added in https://github.com/elastic/elasticsearch. Note: I was looking into removing the existing `data.namespaces` property, however I don't see any issues with it. It is coming from [here](https://github.com/nchaulet/kibana/blob/f77e4d243fca87a87eeae1409f27876cc7ea0836/x-pack/plugins/fleet/server/services/agent_policy.ts#L1140), i.e. the `data` property is generated from the full agent policy which already has a `namespaces` property. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --- x-pack/plugins/fleet/common/types/models/agent_policy.ts | 4 ++++ .../plugins/fleet/server/services/agent_policy.test.ts | 9 ++++++++- x-pack/plugins/fleet/server/services/agent_policy.ts | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/common/types/models/agent_policy.ts b/x-pack/plugins/fleet/common/types/models/agent_policy.ts index bc70714b791584..1468d2ac5b11e9 100644 --- a/x-pack/plugins/fleet/common/types/models/agent_policy.ts +++ b/x-pack/plugins/fleet/common/types/models/agent_policy.ts @@ -193,6 +193,10 @@ export interface FleetServerPolicy { * The coordinator index of the policy */ coordinator_idx: number; + /** + * The namespaces of the policy + */ + namespaces?: string[]; /** * The opaque payload. */ diff --git a/x-pack/plugins/fleet/server/services/agent_policy.test.ts b/x-pack/plugins/fleet/server/services/agent_policy.test.ts index dd63d09c9d7c56..a5dbbc6b233b3b 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.test.ts @@ -1230,6 +1230,7 @@ describe('Agent policy', () => { mockedGetFullAgentPolicy.mockResolvedValue({ id: 'policy123', revision: 1, + namespaces: ['mySpace'], inputs: [ { id: 'input-123', @@ -1282,10 +1283,16 @@ describe('Agent policy', () => { }), expect.objectContaining({ '@timestamp': expect.anything(), - data: { id: 'policy123', inputs: [{ id: 'input-123' }], revision: 1 }, + data: { + id: 'policy123', + inputs: [{ id: 'input-123' }], + revision: 1, + namespaces: ['mySpace'], + }, default_fleet_server: false, policy_id: 'policy123', revision_idx: 1, + namespaces: ['mySpace'], }), ], refresh: 'wait_for', diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index 36a62af378c318..d243ef8b60e168 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -1137,6 +1137,7 @@ class AgentPolicyService { '@timestamp': new Date().toISOString(), revision_idx: fullPolicy.revision, coordinator_idx: 0, + namespaces: fullPolicy.namespaces, data: fullPolicy as unknown as FleetServerPolicy['data'], policy_id: fullPolicy.id, default_fleet_server: policy.is_default_fleet_server === true,