Skip to content

Commit

Permalink
[Fleet] Use space prefixed default fleet server policy id (#197844)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Oct 31, 2024
1 parent 87177fa commit 83138e5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<AgentPolicy, 'package_policies' | 'has_fleet_server'>
) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"uiActions",
"dashboard",
"fieldsMetadata",
"logsDataAccess"
"logsDataAccess",
"spaces"
],
"optionalPlugins": [
"features",
Expand All @@ -40,7 +41,6 @@
"telemetry",
"discover",
"ingestPipelines",
"spaces",
"guidedOnboarding",
"integrationAssistant"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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,
}
Expand All @@ -134,6 +143,7 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => {
generateServiceToken,
setFleetServerPolicyId,
notifications.toasts,
quickStartFleetServerPolicyFields,
]);

return {
Expand Down
15 changes: 9 additions & 6 deletions x-pack/plugins/fleet/server/services/agent_policy_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<string | undefined> {
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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
});
}

0 comments on commit 83138e5

Please sign in to comment.