diff --git a/x-pack/plugins/fleet/common/services/agent_policies_helpers.ts b/x-pack/plugins/fleet/common/services/agent_policies_helpers.ts index 8a1e268614684..5729947feea31 100644 --- a/x-pack/plugins/fleet/common/services/agent_policies_helpers.ts +++ b/x-pack/plugins/fleet/common/services/agent_policies_helpers.ts @@ -5,6 +5,8 @@ * 2.0. */ +import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common'; + import type { NewAgentPolicy, AgentPolicy } from '../types'; import { FLEET_SERVER_PACKAGE, @@ -13,6 +15,12 @@ import { FLEET_ENDPOINT_PACKAGE, } from '../constants'; +export function getDefaultFleetServerpolicyId(spaceId?: string) { + return !spaceId || spaceId === '' || spaceId === DEFAULT_SPACE_ID + ? 'fleet-server-policy' + : `${spaceId}-fleet-server-policy`; +} + export function policyHasFleetServer( agentPolicy: Pick ) { diff --git a/x-pack/plugins/fleet/kibana.jsonc b/x-pack/plugins/fleet/kibana.jsonc index dec968457f294..823328da8ada6 100644 --- a/x-pack/plugins/fleet/kibana.jsonc +++ b/x-pack/plugins/fleet/kibana.jsonc @@ -29,7 +29,8 @@ "uiActions", "dashboard", "fieldsMetadata", - "logsDataAccess" + "logsDataAccess", + "spaces" ], "optionalPlugins": [ "features", @@ -40,7 +41,6 @@ "telemetry", "discover", "ingestPipelines", - "spaces", "guidedOnboarding", "integrationAssistant" ], diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts index e56ae45b1661a..559fcad522351 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts @@ -5,30 +5,35 @@ * 2.0. */ -import { useState, useCallback, useEffect } from 'react'; +import { useState, useCallback, useEffect, useMemo } from 'react'; import { i18n } from '@kbn/i18n'; +import { getDefaultFleetServerpolicyId } from '../../../../../../common/services/agent_policies_helpers'; import type { useComboInput, useInput, useSwitchInput } from '../../../hooks'; -import { sendCreateAgentPolicy, sendGetOneAgentPolicy, useStartServices } from '../../../hooks'; - +import { + sendCreateAgentPolicy, + sendGetOneAgentPolicy, + useFleetStatus, + useStartServices, +} from '../../../hooks'; import type { NewAgentPolicy } from '../../../types'; - import type { FleetServerHost } from '../../../types'; - import { useServiceToken } from '../../../hooks/use_service_token'; import { useSelectFleetServerPolicy } from './use_select_fleet_server_policy'; import { useFleetServerHost } from './use_fleet_server_host'; -const QUICK_START_FLEET_SERVER_POLICY_FIELDS: NewAgentPolicy = { - id: 'fleet-server-policy', - name: 'Fleet Server Policy', - description: 'Fleet Server policy generated by Kibana', - namespace: 'default', - has_fleet_server: true, - monitoring_enabled: ['logs', 'metrics'], - is_default_fleet_server: true, -}; +function getQuickStartFleetServerPolicyFields(spaceId?: string): NewAgentPolicy { + return { + id: getDefaultFleetServerpolicyId(spaceId), + name: 'Fleet Server Policy', + description: 'Fleet Server policy generated by Kibana', + namespace: 'default', + has_fleet_server: true, + monitoring_enabled: ['logs', 'metrics'], + is_default_fleet_server: true, + }; +} export type QuickStartCreateFormStatus = 'initial' | 'loading' | 'error' | 'success'; @@ -69,6 +74,7 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => { setFleetServerHost, inputs, } = useFleetServerHost(); + const { spaceId } = useFleetStatus(); // When a validation error is surfaced from the Fleet Server host form, we want to treat it // the same way we do errors from the service token or policy creation steps @@ -81,6 +87,11 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => { const { fleetServerPolicyId, setFleetServerPolicyId } = useSelectFleetServerPolicy(); const { serviceToken, generateServiceToken } = useServiceToken(); + const quickStartFleetServerPolicyFields = useMemo( + () => getQuickStartFleetServerPolicyFields(spaceId), + [spaceId] + ); + const submit = useCallback(async () => { try { if (!fleetServerHost || fleetServerHost) { @@ -98,16 +109,14 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => { await generateServiceToken(); - const existingPolicy = await sendGetOneAgentPolicy( - QUICK_START_FLEET_SERVER_POLICY_FIELDS.id! - ); + const existingPolicy = await sendGetOneAgentPolicy(quickStartFleetServerPolicyFields.id!); // Don't attempt to create the policy if it's already been created in a previous quick start flow if (existingPolicy.data?.item) { setFleetServerPolicyId(existingPolicy.data?.item.id); } else { const createPolicyResponse = await sendCreateAgentPolicy( - QUICK_START_FLEET_SERVER_POLICY_FIELDS, + quickStartFleetServerPolicyFields, { withSysMonitoring: true, } @@ -134,6 +143,7 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => { generateServiceToken, setFleetServerPolicyId, notifications.toasts, + quickStartFleetServerPolicyFields, ]); return { diff --git a/x-pack/plugins/fleet/server/services/agent_policy_create.ts b/x-pack/plugins/fleet/server/services/agent_policy_create.ts index f370867fc493b..3902548581595 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy_create.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy_create.ts @@ -11,6 +11,7 @@ import type { SavedObjectsClientContract, } from '@kbn/core/server'; +import { getDefaultFleetServerpolicyId } from '../../common/services/agent_policies_helpers'; import type { HTTPAuthorizationHeader } from '../../common/http_authorization_header'; import { @@ -27,23 +28,25 @@ import { bulkInstallPackages } from './epm/packages'; import { ensureDefaultEnrollmentAPIKeyForAgentPolicy } from './api_keys'; import { agentlessAgentService } from './agents/agentless_agent'; -const FLEET_SERVER_POLICY_ID = 'fleet-server-policy'; - async function getFleetServerAgentPolicyId( soClient: SavedObjectsClientContract ): Promise { let agentPolicyId; - // creating first fleet server policy with id 'fleet-server-policy' + // creating first fleet server policy with id '(space-)?fleet-server-policy' let agentPolicy; try { - agentPolicy = await agentPolicyService.get(soClient, FLEET_SERVER_POLICY_ID, false); + agentPolicy = await agentPolicyService.get( + soClient, + getDefaultFleetServerpolicyId(soClient.getCurrentNamespace()), + false + ); } catch (err) { if (!err.isBoom || err.output.statusCode !== 404) { throw err; } } if (!agentPolicy) { - agentPolicyId = FLEET_SERVER_POLICY_ID; + agentPolicyId = getDefaultFleetServerpolicyId(soClient.getCurrentNamespace()); } return agentPolicyId; } @@ -118,7 +121,7 @@ export async function createAgentPolicyWithPackages({ packagesToInstall.push(FLEET_SERVER_PACKAGE); agentPolicyId = agentPolicyId || (await getFleetServerAgentPolicyId(soClient)); - if (agentPolicyId === FLEET_SERVER_POLICY_ID) { + if (agentPolicyId === getDefaultFleetServerpolicyId(spaceId)) { // setting first fleet server policy to default, so that fleet server can enroll without setting policy_id newPolicy.is_default_fleet_server = true; } diff --git a/x-pack/test/fleet_api_integration/apis/space_awareness/agent_policies.ts b/x-pack/test/fleet_api_integration/apis/space_awareness/agent_policies.ts index 037ba332cfefb..3f6a26de52bb6 100644 --- a/x-pack/test/fleet_api_integration/apis/space_awareness/agent_policies.ts +++ b/x-pack/test/fleet_api_integration/apis/space_awareness/agent_policies.ts @@ -97,5 +97,21 @@ export default function (providerContext: FtrProviderContext) { ); }); }); + + describe('POST /agent_policies', () => { + it('should create fleet-server-policy in the default space', async () => { + const res = await apiClient.createAgentPolicy('default', { + has_fleet_server: true, + }); + expect(res.item.id).to.eql('fleet-server-policy'); + }); + + it('should create fleet-server-policy in the test space', async () => { + const res = await apiClient.createAgentPolicy(TEST_SPACE_1, { + has_fleet_server: true, + }); + expect(res.item.id).to.eql(`${TEST_SPACE_1}-fleet-server-policy`); + }); + }); }); }