diff --git a/x-pack/plugins/fleet/common/services/package_to_package_policy.ts b/x-pack/plugins/fleet/common/services/package_to_package_policy.ts index a5b31408b737d6..0d40adb4bf7dc3 100644 --- a/x-pack/plugins/fleet/common/services/package_to_package_policy.ts +++ b/x-pack/plugins/fleet/common/services/package_to_package_policy.ts @@ -65,7 +65,10 @@ const varsReducer = ( /* * This service creates a package policy inputs definition from defaults provided in package info */ -export const packageToPackagePolicyInputs = (packageInfo: PackageInfo): NewPackagePolicyInput[] => { +export const packageToPackagePolicyInputs = ( + packageInfo: PackageInfo, + integrationToEnable?: string +): NewPackagePolicyInput[] => { const hasIntegrations = doesPackageHaveIntegrations(packageInfo); const inputs: NewPackagePolicyInput[] = []; const packageInputsByPolicyTemplateAndType: { @@ -114,10 +117,27 @@ export const packageToPackagePolicyInputs = (packageInfo: PackageInfo): NewPacka streamsForInput.push(...streams); + // Check if we should enable this input by the streams below it + // Enable it if at least one of its streams is enabled + let enableInput = streamsForInput.length + ? !!streamsForInput.find((stream) => stream.enabled) + : true; + + // If we are wanting to enabling this input, check if we only want + // to enable specific integrations (aka `policy_template`s) + if ( + enableInput && + hasIntegrations && + integrationToEnable && + integrationToEnable !== packageInput.policy_template + ) { + enableInput = false; + } + const input: NewPackagePolicyInput = { type: packageInput.type, policy_template: packageInput.policy_template, - enabled: streamsForInput.length ? !!streamsForInput.find((stream) => stream.enabled) : true, + enabled: enableInput, streams: streamsForInput, }; @@ -145,7 +165,8 @@ export const packageToPackagePolicy = ( outputId: string, namespace: string = '', packagePolicyName?: string, - description?: string + description?: string, + integrationToEnable?: string ): NewPackagePolicy => { const packagePolicy: NewPackagePolicy = { name: packagePolicyName || `${packageInfo.name}-1`, @@ -159,7 +180,7 @@ export const packageToPackagePolicy = ( enabled: true, policy_id: agentPolicyId, output_id: outputId, - inputs: packageToPackagePolicyInputs(packageInfo), + inputs: packageToPackagePolicyInputs(packageInfo, integrationToEnable), vars: undefined, }; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx index 6563918a5efb2c..300a943d296b5a 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx @@ -60,19 +60,26 @@ const StepsWithLessPadding = styled(EuiSteps)` } `; +interface AddToPolicyParams { + pkgkey: string; + integration?: string; +} + +interface AddFromPolicyParams { + policyId: string; +} + export const CreatePackagePolicyPage: React.FunctionComponent = () => { const { notifications } = useStartServices(); const { agents: { enabled: isFleetEnabled }, } = useConfig(); - const { - params: { policyId, pkgkey }, - } = useRouteMatch<{ policyId: string; pkgkey: string }>(); + const { params } = useRouteMatch(); const { getHref, getPath } = useLink(); const history = useHistory(); const handleNavigateTo = useNavigateToCallback(); const routeState = useIntraAppState(); - const from: CreatePackagePolicyFrom = policyId ? 'policy' : 'package'; + const from: CreatePackagePolicyFrom = 'policyId' in params ? 'policy' : 'package'; // Agent policy and package info states const [agentPolicy, setAgentPolicy] = useState(); @@ -215,9 +222,11 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => { return routeState.onCancelUrl; } return from === 'policy' - ? getHref('policy_details', { policyId: agentPolicyId || policyId }) - : getHref('integration_details_overview', { pkgkey }); - }, [agentPolicyId, policyId, from, getHref, pkgkey, routeState]); + ? getHref('policy_details', { + policyId: agentPolicyId || (params as AddFromPolicyParams).policyId, + }) + : getHref('integration_details_overview', { pkgkey: (params as AddToPolicyParams).pkgkey }); + }, [agentPolicyId, params, from, getHref, routeState]); const cancelClickHandler: ReactEventHandler = useCallback( (ev) => { @@ -255,7 +264,11 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => { : routeState.onSaveNavigateTo ); } else { - history.push(getPath('policy_details', { policyId: agentPolicy?.id || policyId })); + history.push( + getPath('policy_details', { + policyId: agentPolicy?.id || (params as AddFromPolicyParams).policyId, + }) + ); } notifications.toasts.addSuccess({ @@ -295,14 +308,14 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => { const stepSelectAgentPolicy = useMemo( () => ( ), - [pkgkey, updatePackageInfo, agentPolicy, updateAgentPolicy] + [params, updatePackageInfo, agentPolicy, updateAgentPolicy] ); const ExtensionView = useUIExtension(packagePolicy.package?.name ?? '', 'package-policy-create'); @@ -310,14 +323,14 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => { const stepSelectPackage = useMemo( () => ( ), - [policyId, updateAgentPolicy, packageInfo, updatePackageInfo] + [params, updateAgentPolicy, packageInfo, updatePackageInfo] ); const stepConfigurePackagePolicy = useMemo( @@ -333,6 +346,7 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => { updatePackagePolicy={updatePackagePolicy} validationResults={validationResults!} submitAttempted={formState === 'INVALID'} + integrationToEnable={(params as AddToPolicyParams).integration} /> {/* Only show the out-of-box configuration step if a UI extension is NOT registered */} @@ -357,6 +371,7 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => {
), [ + params, isLoadingSecondStep, agentPolicy, packageInfo, diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/step_define_package_policy.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/step_define_package_policy.tsx index 380e49a1d8dd97..7444bed6ed3fdb 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/step_define_package_policy.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/step_define_package_policy.tsx @@ -38,7 +38,7 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{ agentPolicy: AgentPolicy; packageInfo: PackageInfo; packagePolicy: NewPackagePolicy; - integration?: string; + integrationToEnable?: string; updatePackagePolicy: (fields: Partial) => void; validationResults: PackagePolicyValidationResults; submitAttempted: boolean; @@ -47,7 +47,7 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{ agentPolicy, packageInfo, packagePolicy, - integration, + integrationToEnable, updatePackagePolicy, validationResults, submitAttempted, @@ -95,7 +95,8 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{ ? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1 : 1 }`, - packagePolicy.description + packagePolicy.description, + integrationToEnable ) ); } @@ -107,7 +108,7 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{ namespace: agentPolicy.namespace, }); } - }, [packagePolicy, agentPolicy, packageInfo, updatePackagePolicy, integration]); + }, [packagePolicy, agentPolicy, packageInfo, updatePackagePolicy, integrationToEnable]); return validationResults ? (