Skip to content

Commit e15311c

Browse files
authored
Fix enforcement of unique policy name of policies across spaces (#239631)
Resolves: #228746 Fixes fleet policy name uniqueness not being consistently enforced across spaces when name or space changes occur: * When creating an agent policy with packages (like system), the name incrementation accounts for package names that already exist in the spaces to be set for the new policy. * Creating or updating an agent policy for multiple spaces that has the same name of another policy that is not in the current namespace now results in an error. * When an agent policy with multiple spaces has an integration policy name updated to one that already exists in another space that is not the current namespace now results in an error.
1 parent 82528fc commit e15311c

File tree

16 files changed

+649
-171
lines changed

16 files changed

+649
-171
lines changed

x-pack/platform/plugins/shared/fleet/server/routes/agent_policy/handlers.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,10 @@ export const createAgentPolicyHandler: FleetRequestHandler<
351351
const monitoringEnabled = request.body.monitoring_enabled;
352352
const logger = appContextService.getLogger().get('httpCreateAgentPolicyHandler');
353353

354-
const {
355-
has_fleet_server: hasFleetServer,
356-
force,
357-
space_ids: spaceIds,
358-
...newPolicy
359-
} = request.body;
354+
const { has_fleet_server: hasFleetServer, force, ...newPolicy } = request.body;
360355
const spaceId = fleetContext.spaceId;
361356
const authorizationHeader = HTTPAuthorizationHeader.parseFromRequest(request, user?.username);
357+
const { space_ids: spaceIds } = request.body;
362358

363359
logger.debug(`Creating agent policy [${newPolicy.name}]`);
364360

@@ -401,9 +397,11 @@ export const createAgentPolicyHandler: FleetRequestHandler<
401397
(spaceIds.length > 1 || (spaceIds.length === 0 && spaceIds[0]) !== spaceId)
402398
) {
403399
await updateAgentPolicySpaces({
404-
agentPolicyId: agentPolicy.id,
400+
agentPolicy: {
401+
...agentPolicy,
402+
space_ids: spaceIds,
403+
},
405404
currentSpaceId: spaceId,
406-
newSpaceIds: spaceIds,
407405
authorizedSpaces,
408406
options: { force },
409407
});
@@ -543,8 +541,9 @@ export const updateAgentPolicyHandler: FleetRequestHandler<
543541
const fleetContext = await context.fleet;
544542
const esClient = coreContext.elasticsearch.client.asInternalUser;
545543
const user = appContextService.getSecurityCore().authc.getCurrentUser(request) || undefined;
546-
const { force, bumpRevision, space_ids: spaceIds, ...data } = request.body;
544+
const { force, bumpRevision, ...data } = request.body;
547545

546+
const spaceIds = data.space_ids;
548547
let spaceId = fleetContext.spaceId;
549548

550549
logger.debug(`updating policy [${request.params.agentPolicyId}] in space [${spaceId}]`);
@@ -560,11 +559,10 @@ export const updateAgentPolicyHandler: FleetRequestHandler<
560559
context
561560
);
562561
await updateAgentPolicySpaces({
563-
agentPolicyId: request.params.agentPolicyId,
562+
agentPolicy: { ...data, id: request.params.agentPolicyId },
564563
currentSpaceId: spaceId,
565-
newSpaceIds: spaceIds,
566564
authorizedSpaces,
567-
options: { force, validateUniqueName: true },
565+
options: { force },
568566
});
569567

570568
spaceId = spaceIds[0];

0 commit comments

Comments
 (0)