Skip to content

Commit

Permalink
[Fleet] Prevent hosted policies space change (elastic#198043)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored and tiansivive committed Oct 29, 2024
1 parent ec54b09 commit b03646c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent<Props> =
}
>
<SpaceSelector
isDisabled={disabled}
isDisabled={disabled || agentPolicy.is_managed === true}
value={
'space_ids' in agentPolicy && agentPolicy.space_ids
? agentPolicy.space_ids
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/server/routes/agent_policy/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export const createAgentPolicyHandler: FleetRequestHandler<
currentSpaceId: spaceId,
newSpaceIds: spaceIds,
authorizedSpaces,
options: { force },
});
}

Expand Down Expand Up @@ -385,6 +386,7 @@ export const updateAgentPolicyHandler: FleetRequestHandler<
currentSpaceId: spaceId,
newSpaceIds: spaceIds,
authorizedSpaces,
options: { force },
});

spaceId = spaceIds[0];
Expand Down
17 changes: 17 additions & 0 deletions x-pack/plugins/fleet/server/services/spaces/agent_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ describe('updateAgentPolicySpaces', () => {
);
});

it('throw when trying to change a managed policies space', async () => {
jest.mocked(agentPolicyService.get).mockResolvedValue({
id: 'policy1',
space_ids: ['default'],
is_managed: true,
} as any);
jest.mocked(packagePolicyService.findAllForAgentPolicy).mockResolvedValue([] as any);
await expect(
updateAgentPolicySpaces({
agentPolicyId: 'policy1',
currentSpaceId: 'default',
newSpaceIds: ['test'],
authorizedSpaces: ['test', 'default'],
})
).rejects.toThrowError(/Cannot update hosted agent policy policy1 space/);
});

it('throw when trying to add a space with missing permissions', async () => {
await expect(
updateAgentPolicySpaces({
Expand Down
14 changes: 13 additions & 1 deletion x-pack/plugins/fleet/server/services/spaces/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { appContextService } from '../app_context';
import { agentPolicyService } from '../agent_policy';
import { ENROLLMENT_API_KEYS_INDEX } from '../../constants';
import { packagePolicyService } from '../package_policy';
import { FleetError } from '../../errors';
import { FleetError, HostedAgentPolicyRestrictionRelatedError } from '../../errors';

import { isSpaceAwarenessEnabled } from './helpers';

Expand All @@ -28,11 +28,13 @@ export async function updateAgentPolicySpaces({
currentSpaceId,
newSpaceIds,
authorizedSpaces,
options,
}: {
agentPolicyId: string;
currentSpaceId: string;
newSpaceIds: string[];
authorizedSpaces: string[];
options?: { force?: boolean };
}) {
const useSpaceAwareness = await isSpaceAwarenessEnabled();
if (!useSpaceAwareness || !newSpaceIds || newSpaceIds.length === 0) {
Expand All @@ -50,6 +52,16 @@ export async function updateAgentPolicySpaces({
agentPolicyId
);

if (!existingPolicy) {
return;
}

if (existingPolicy.is_managed && !options?.force) {
throw new HostedAgentPolicyRestrictionRelatedError(
`Cannot update hosted agent policy ${existingPolicy.id} space `
);
}

if (deepEqual(existingPolicy?.space_ids?.sort() ?? [DEFAULT_SPACE_ID], newSpaceIds.sort())) {
return;
}
Expand Down

0 comments on commit b03646c

Please sign in to comment.