diff --git a/oas_docs/output/kibana.serverless.yaml b/oas_docs/output/kibana.serverless.yaml index f63707d05c9bf..e059af49ad90b 100644 --- a/oas_docs/output/kibana.serverless.yaml +++ b/oas_docs/output/kibana.serverless.yaml @@ -13185,6 +13185,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -13278,6 +13280,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -13334,6 +13338,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -13402,6 +13408,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -13501,6 +13509,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -14209,6 +14219,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -15047,6 +15059,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -15720,6 +15734,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -16371,6 +16387,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -17218,6 +17236,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -17890,6 +17910,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -17948,6 +17970,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -17964,6 +17988,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -18322,6 +18348,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -18426,6 +18454,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -18494,6 +18524,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -18616,6 +18648,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -18711,6 +18745,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -18793,6 +18829,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -19179,6 +19217,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -19243,6 +19283,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -19301,6 +19343,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -19629,6 +19673,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -19972,6 +20018,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20116,6 +20164,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20178,6 +20228,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20247,6 +20299,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20355,6 +20409,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20435,6 +20491,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20600,6 +20658,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20694,6 +20754,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20739,6 +20801,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20812,6 +20876,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20885,6 +20951,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -20963,6 +21031,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21041,6 +21111,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21125,6 +21197,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21184,6 +21258,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21232,6 +21308,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21300,6 +21378,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21364,6 +21444,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21420,6 +21502,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21474,6 +21558,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21578,6 +21664,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21709,6 +21797,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21805,6 +21895,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21863,6 +21955,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -21938,6 +22032,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -22036,6 +22132,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -22107,6 +22205,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -22249,6 +22349,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -22331,6 +22433,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -22731,6 +22835,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -22857,6 +22963,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -23039,6 +23147,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -23154,6 +23264,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -23633,6 +23745,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -23782,6 +23896,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24250,6 +24366,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24516,6 +24634,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24570,6 +24690,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24723,6 +24845,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24768,6 +24892,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24875,6 +25001,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24919,6 +25047,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -24999,6 +25129,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25104,6 +25236,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25160,6 +25294,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25234,6 +25370,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25338,6 +25476,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25403,6 +25543,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25419,6 +25561,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25477,6 +25621,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25529,6 +25675,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25545,6 +25693,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25595,6 +25745,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25651,6 +25803,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -25667,6 +25821,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -26400,6 +26556,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -27812,6 +27970,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -27868,6 +28028,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -27884,6 +28046,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -28611,6 +28775,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -30008,6 +30174,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -30065,6 +30233,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -30538,6 +30708,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -31371,6 +31543,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -31387,6 +31561,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -31836,6 +32012,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -31908,6 +32086,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -32337,6 +32517,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -33180,6 +33362,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -33196,6 +33380,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -33332,6 +33518,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -33413,6 +33601,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34213,6 +34403,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34299,6 +34491,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34416,6 +34610,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34472,6 +34668,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34552,6 +34750,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34672,6 +34872,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34736,6 +34938,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34821,6 +35025,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -34954,6 +35160,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -35030,6 +35238,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -35142,6 +35352,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: @@ -35213,6 +35425,8 @@ paths: properties: error: type: string + errorType: + type: string message: type: string statusCode: diff --git a/x-pack/platform/plugins/shared/fleet/common/errors.ts b/x-pack/platform/plugins/shared/fleet/common/errors.ts index 9750fdbaf0d3b..903f76063933e 100644 --- a/x-pack/platform/plugins/shared/fleet/common/errors.ts +++ b/x-pack/platform/plugins/shared/fleet/common/errors.ts @@ -26,3 +26,4 @@ export class FleetActionsClientError extends FleetError {} export class UninstallTokenError extends FleetError {} export class AgentRequestInvalidError extends FleetError {} +export class OutputInvalidError extends FleetError {} diff --git a/x-pack/platform/plugins/shared/fleet/common/services/agent_policies_helpers.ts b/x-pack/platform/plugins/shared/fleet/common/services/agent_policies_helpers.ts index 5729947feea31..62c87fc2a85f6 100644 --- a/x-pack/platform/plugins/shared/fleet/common/services/agent_policies_helpers.ts +++ b/x-pack/platform/plugins/shared/fleet/common/services/agent_policies_helpers.ts @@ -24,9 +24,6 @@ export function getDefaultFleetServerpolicyId(spaceId?: string) { export function policyHasFleetServer( agentPolicy: Pick ) { - if (!agentPolicy.package_policies) { - return false; - } return ( agentPolicy.package_policies?.some((p) => p.package?.name === FLEET_SERVER_PACKAGE) || !!agentPolicy.has_fleet_server diff --git a/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.test.ts b/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.test.ts index da59d01e38d2c..dc0db549a4638 100644 --- a/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.test.ts +++ b/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.test.ts @@ -53,6 +53,14 @@ describe('getAllowedOutputTypesForAgentPolicy', () => { expect(res).toEqual(['elasticsearch']); }); + it('should return only elasticsearch for an agent policy with Fleet Server not yet installed', () => { + const res = getAllowedOutputTypesForAgentPolicy({ + has_fleet_server: true, + } as any); + + expect(res).toEqual(['elasticsearch']); + }); + it('should return only elasticsearch for an agentless agent policy', () => { const res = getAllowedOutputTypesForAgentPolicy({ supports_agentless: true } as any); diff --git a/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.ts b/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.ts index e664495d327f3..43f67ac348332 100644 --- a/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.ts +++ b/x-pack/platform/plugins/shared/fleet/common/services/output_helpers.ts @@ -32,10 +32,11 @@ const sameClusterRestrictedPackages = [ */ export function getAllowedOutputTypesForAgentPolicy(agentPolicy: Partial): string[] { const isRestrictedToSameClusterES = - agentPolicy.package_policies && - agentPolicy.package_policies.some( - (p) => p.package?.name && sameClusterRestrictedPackages.includes(p.package?.name) - ); + agentPolicy.has_fleet_server || + (agentPolicy.package_policies && + agentPolicy.package_policies.some( + (p) => p.package?.name && sameClusterRestrictedPackages.includes(p.package?.name) + )); if (isRestrictedToSameClusterES) { return [outputType.Elasticsearch]; diff --git a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts index 559fcad522351..c4c1cdcf29493 100644 --- a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts +++ b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/components/fleet_server_instructions/hooks/use_quick_start_form.ts @@ -8,10 +8,11 @@ import { useState, useCallback, useEffect, useMemo } from 'react'; import { i18n } from '@kbn/i18n'; +import { OutputInvalidError } from '../../../../../../common/errors'; import { getDefaultFleetServerpolicyId } from '../../../../../../common/services/agent_policies_helpers'; import type { useComboInput, useInput, useSwitchInput } from '../../../hooks'; import { - sendCreateAgentPolicy, + sendCreateAgentPolicyForRq, sendGetOneAgentPolicy, useFleetStatus, useStartServices, @@ -115,23 +116,40 @@ export const useQuickStartCreateForm = (): QuickStartCreateForm => { if (existingPolicy.data?.item) { setFleetServerPolicyId(existingPolicy.data?.item.id); } else { - const createPolicyResponse = await sendCreateAgentPolicy( + const createPolicyResponse = await sendCreateAgentPolicyForRq( quickStartFleetServerPolicyFields, { withSysMonitoring: true, } ); - setFleetServerPolicyId(createPolicyResponse.data?.item.id); + setFleetServerPolicyId(createPolicyResponse.item.id); } - setStatus('success'); } } catch (err) { - notifications.toasts.addError(err, { - title: i18n.translate('xpack.fleet.fleetServerSetup.errorAddingFleetServerHostTitle', { - defaultMessage: 'Error adding Fleet Server host', - }), - }); + if (err?.attributes?.type === OutputInvalidError.name) { + notifications.toasts.addError(err, { + title: i18n.translate( + 'xpack.fleet.fleetServerSetup.errorCreatingFleetServerPolicyTitle', + { + defaultMessage: 'Error creating a Fleet Server policy', + } + ), + toastMessage: i18n.translate( + 'xpack.fleet.fleetServerSetup.errorCreatingFleetServerPolicyMessage', + { + defaultMessage: + 'Fleet Server policy creation failed as your default output is not an elasticsearch output. Use the advanced section to use an elasticsearch output to create that policy.', + } + ), + }); + } else { + notifications.toasts.addError(err, { + title: i18n.translate('xpack.fleet.fleetServerSetup.errorAddingFleetServerHostTitle', { + defaultMessage: 'Error adding Fleet Server host', + }), + }); + } setStatus('error'); setError(err.message); diff --git a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.tsx b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.tsx index 5e851be571565..9b3adbe6c825b 100644 --- a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.tsx +++ b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.tsx @@ -69,6 +69,7 @@ export function useOutputOptions(agentPolicy: Partial getAllowedOutputTypesForAgentPolicy(agentPolicy as AgentPolicy), [agentPolicy] @@ -146,13 +147,21 @@ export function useOutputOptions(agentPolicy: Partial ({ dataOutputOptions, monitoringOutputOptions, + dataOutputValueOfSelected, isLoading: outputsRequest.isLoading, }), - [dataOutputOptions, monitoringOutputOptions, outputsRequest.isLoading] + [ + dataOutputOptions, + dataOutputValueOfSelected, + monitoringOutputOptions, + outputsRequest.isLoading, + ] ); } diff --git a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx index 644394956dc3c..424a3420ed0d9 100644 --- a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx +++ b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx @@ -98,6 +98,7 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent = const { dataOutputOptions, + dataOutputValueOfSelected, monitoringOutputOptions, isLoading: isLoadingOptions, } = useOutputOptions(agentPolicy); @@ -665,7 +666,7 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent = > { diff --git a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx index 96e1056f736cd..6680503ea81fc 100644 --- a/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx +++ b/x-pack/platform/plugins/shared/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx @@ -24,7 +24,7 @@ import { i18n } from '@kbn/i18n'; import { useSpaceSettingsContext } from '../../../../../hooks/use_space_settings_context'; import type { AgentPolicy, NewAgentPolicy } from '../../../types'; -import { sendCreateAgentPolicy, useStartServices, useAuthz } from '../../../hooks'; +import { useStartServices, useAuthz, sendCreateAgentPolicyForRq } from '../../../hooks'; import { generateNewAgentPolicyWithDefaults } from '../../../../../../common/services/generate_new_agent_policy'; import { agentPolicyFormValidation } from '.'; @@ -48,7 +48,7 @@ export const AgentPolicyCreateInlineForm: React.FunctionComponent = ({ isFleetServerPolicy, agentPolicyName, }) => { - const { docLinks } = useStartServices(); + const { docLinks, notifications } = useStartServices(); const authz = useAuthz(); const [touchedFields, setTouchedFields] = useState<{ [key: string]: boolean }>({}); @@ -83,17 +83,20 @@ export const AgentPolicyCreateInlineForm: React.FunctionComponent = ({ const createAgentPolicy = useCallback(async () => { try { setIsLoading(true); - const resp = await sendCreateAgentPolicy(newAgentPolicy, { withSysMonitoring }); - if (resp.error) throw resp.error; - if (resp.data) { - updateAgentPolicy(resp.data.item); - } + const data = await sendCreateAgentPolicyForRq(newAgentPolicy, { withSysMonitoring }); + + updateAgentPolicy(data.item); } catch (e) { + notifications.toasts.addError(e, { + title: i18n.translate('xpack.fleet.agentPolicyCreateInline.errorTitle', { + defaultMessage: 'Error creating agent policy', + }), + }); updateAgentPolicy(null, mapError(e)); } finally { setIsLoading(false); } - }, [newAgentPolicy, withSysMonitoring, updateAgentPolicy]); + }, [newAgentPolicy, withSysMonitoring, updateAgentPolicy, notifications.toasts]); function mapError(e: { statusCode: number }): JSX.Element | undefined { switch (e.statusCode) { diff --git a/x-pack/platform/plugins/shared/fleet/public/hooks/use_request/agent_policy.ts b/x-pack/platform/plugins/shared/fleet/public/hooks/use_request/agent_policy.ts index 9e4fb2344fc29..9fdfd52835f25 100644 --- a/x-pack/platform/plugins/shared/fleet/public/hooks/use_request/agent_policy.ts +++ b/x-pack/platform/plugins/shared/fleet/public/hooks/use_request/agent_policy.ts @@ -128,6 +128,22 @@ export const sendGetOneAgentPolicy = (agentPolicyId: string) => { }); }; +export const sendCreateAgentPolicyForRq = ( + body: CreateAgentPolicyRequest['body'], + { withSysMonitoring }: { withSysMonitoring: boolean } = { withSysMonitoring: false } +) => { + return sendRequestForRq({ + path: agentPolicyRouteService.getCreatePath(), + method: 'post', + body: JSON.stringify(body), + query: withSysMonitoring ? { sys_monitoring: true } : {}, + version: API_VERSIONS.public.v1, + }); +}; + +/** + * @deprecated use sendCreateAgentPolicyForRq instead + */ export const sendCreateAgentPolicy = ( body: CreateAgentPolicyRequest['body'], { withSysMonitoring }: { withSysMonitoring: boolean } = { withSysMonitoring: false } diff --git a/x-pack/platform/plugins/shared/fleet/server/errors/handlers.ts b/x-pack/platform/plugins/shared/fleet/server/errors/handlers.ts index 2bdd118e7fb40..7ebc33187c560 100644 --- a/x-pack/platform/plugins/shared/fleet/server/errors/handlers.ts +++ b/x-pack/platform/plugins/shared/fleet/server/errors/handlers.ts @@ -47,6 +47,7 @@ import { AgentlessPolicyExistsRequestError, PackageInvalidDeploymentMode, PackagePolicyContentPackageError, + OutputInvalidError, } from '.'; type IngestErrorHandler = ( @@ -154,6 +155,13 @@ const getHTTPResponseCode = (error: FleetError): number => { return 400; // Bad Request }; +function shouldRespondWithErrorType(error: FleetError) { + if (error instanceof OutputInvalidError) { + return true; + } + return false; +} + export function fleetErrorToResponseOptions(error: IngestErrorHandlerParams['error']) { const logger = appContextService.getLogger(); // our "expected" errors @@ -164,6 +172,7 @@ export function fleetErrorToResponseOptions(error: IngestErrorHandlerParams['err statusCode: getHTTPResponseCode(error), body: { message: error.message, + ...(shouldRespondWithErrorType(error) ? { attributes: { type: error.name } } : {}), ...(error.attributes && { attributes: error.attributes }), }, }; diff --git a/x-pack/platform/plugins/shared/fleet/server/errors/index.ts b/x-pack/platform/plugins/shared/fleet/server/errors/index.ts index ac6eca6b3b97d..992fc449788f6 100644 --- a/x-pack/platform/plugins/shared/fleet/server/errors/index.ts +++ b/x-pack/platform/plugins/shared/fleet/server/errors/index.ts @@ -17,7 +17,10 @@ export { } from './handlers'; export { isESClientError } from './utils'; -export { FleetError as FleetError } from '../../common/errors'; +export { + FleetError as FleetError, + OutputInvalidError as OutputInvalidError, +} from '../../common/errors'; export class RegistryError extends FleetError {} export class RegistryConnectionError extends RegistryError {} @@ -109,7 +112,6 @@ export class FleetNotFoundError extends FleetError {} export class FleetTooManyRequestsError extends FleetError {} export class OutputUnauthorizedError extends FleetError {} -export class OutputInvalidError extends FleetError {} export class OutputLicenceError extends FleetError {} export class DownloadSourceError extends FleetError {} export class DeleteUnenrolledAgentsPreconfiguredError extends FleetError {} diff --git a/x-pack/platform/plugins/shared/fleet/server/services/agent_policies/outputs_helpers.ts b/x-pack/platform/plugins/shared/fleet/server/services/agent_policies/outputs_helpers.ts index f2176604edf5e..37e8e567fdb03 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/agent_policies/outputs_helpers.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/agent_policies/outputs_helpers.ts @@ -54,6 +54,7 @@ export async function validateOutputForPolicy( allowedOutputTypeForPolicy: string[] = Object.values(outputType) ) { if ( + Object.keys(existingData).length !== 0 && newData.data_output_id === existingData.data_output_id && newData.monitoring_output_id === existingData.monitoring_output_id ) { @@ -66,7 +67,15 @@ export async function validateOutputForPolicy( allowedOutputTypeForPolicy.length !== Object.values(outputType).length; if (isOutputTypeRestricted) { - const dataOutput = await getDataOutputForAgentPolicy(soClient, data); + const dataOutput = await getDataOutputForAgentPolicy(soClient, data).catch((err) => { + if (err instanceof OutputNotFoundError) { + return; + } + throw err; + }); + if (!dataOutput) { + return; + } if (!allowedOutputTypeForPolicy.includes(dataOutput.type)) { throw new OutputInvalidError( `Output of type "${dataOutput.type}" is not usable with policy "${data.name}".` @@ -116,7 +125,15 @@ export async function validateAgentPolicyOutputForIntegration( allowedOutputTypeForPolicy.length !== Object.values(outputType).length; if (isOutputTypeRestricted) { - const dataOutput = await getDataOutputForAgentPolicy(soClient, agentPolicy); + const dataOutput = await getDataOutputForAgentPolicy(soClient, agentPolicy).catch((err) => { + if (err instanceof OutputNotFoundError) { + return; + } + throw err; + }); + if (!dataOutput) { + return; + } if (!allowedOutputTypeForPolicy.includes(dataOutput.type)) { if (isNewPackagePolicy) { throw new OutputInvalidError( diff --git a/x-pack/platform/plugins/shared/fleet/server/services/agent_policy.ts b/x-pack/platform/plugins/shared/fleet/server/services/agent_policy.ts index 1e00202beb063..3319cc02333dd 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/agent_policy.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/agent_policy.ts @@ -370,6 +370,7 @@ class AgentPolicyService { user?: AuthenticatedUser; authorizationHeader?: HTTPAuthorizationHeader | null; skipDeploy?: boolean; + hasFleetServer?: boolean; } = {} ): Promise { const savedObjectType = await getAgentPolicySavedObjectType(); @@ -402,11 +403,15 @@ class AgentPolicyService { spaceId: soClient.getCurrentNamespace(), namespace: agentPolicy.namespace, }); + const policyForOutputValidation = { + ...agentPolicy, + has_fleet_server: options?.hasFleetServer, + }; await validateOutputForPolicy( soClient, - agentPolicy, + policyForOutputValidation, {}, - getAllowedOutputTypesForAgentPolicy(agentPolicy) + getAllowedOutputTypesForAgentPolicy(policyForOutputValidation) ); const newSo = await soClient.create( diff --git a/x-pack/platform/plugins/shared/fleet/server/services/agent_policy_create.ts b/x-pack/platform/plugins/shared/fleet/server/services/agent_policy_create.ts index 3902548581595..c9fa5b591010f 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/agent_policy_create.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/agent_policy_create.ts @@ -149,6 +149,7 @@ export async function createAgentPolicyWithPackages({ user, id: agentPolicyId, authorizationHeader, + hasFleetServer, skipDeploy: true, // skip deploying the policy until package policies are added }); diff --git a/x-pack/test/fleet_api_integration/apis/agents/upgrade.ts b/x-pack/test/fleet_api_integration/apis/agents/upgrade.ts index b31f50295a1db..8be5976a9f308 100644 --- a/x-pack/test/fleet_api_integration/apis/agents/upgrade.ts +++ b/x-pack/test/fleet_api_integration/apis/agents/upgrade.ts @@ -67,6 +67,7 @@ export default function (providerContext: FtrProviderContext) { id: 'fleet-server-policy', namespace: 'default', has_fleet_server: true, + force: true, }); await kibanaServer.savedObjects.create({ @@ -687,6 +688,7 @@ export default function (providerContext: FtrProviderContext) { id: 'fleet-server-policy', namespace: 'default', has_fleet_server: true, + force: true, }); await kibanaServer.savedObjects.create({