Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Prevent hosted policies space change #198043

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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