Skip to content

Commit

Permalink
Only enable specific integrations by default in the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang committed Jun 7, 2021
1 parent 3cdf6e4 commit 5b13f9b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
29 changes: 25 additions & 4 deletions x-pack/plugins/fleet/common/services/package_to_package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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`,
Expand All @@ -159,7 +180,7 @@ export const packageToPackagePolicy = (
enabled: true,
policy_id: agentPolicyId,
output_id: outputId,
inputs: packageToPackagePolicyInputs(packageInfo),
inputs: packageToPackagePolicyInputs(packageInfo, integrationToEnable),
vars: undefined,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddToPolicyParams | AddFromPolicyParams>();
const { getHref, getPath } = useLink();
const history = useHistory();
const handleNavigateTo = useNavigateToCallback();
const routeState = useIntraAppState<CreatePackagePolicyRouteState>();
const from: CreatePackagePolicyFrom = policyId ? 'policy' : 'package';
const from: CreatePackagePolicyFrom = 'policyId' in params ? 'policy' : 'package';

// Agent policy and package info states
const [agentPolicy, setAgentPolicy] = useState<AgentPolicy>();
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -295,29 +308,29 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => {
const stepSelectAgentPolicy = useMemo(
() => (
<StepSelectAgentPolicy
pkgkey={pkgkey}
pkgkey={(params as AddToPolicyParams).pkgkey}
updatePackageInfo={updatePackageInfo}
agentPolicy={agentPolicy}
updateAgentPolicy={updateAgentPolicy}
setIsLoadingSecondStep={setIsLoadingSecondStep}
/>
),
[pkgkey, updatePackageInfo, agentPolicy, updateAgentPolicy]
[params, updatePackageInfo, agentPolicy, updateAgentPolicy]
);

const ExtensionView = useUIExtension(packagePolicy.package?.name ?? '', 'package-policy-create');

const stepSelectPackage = useMemo(
() => (
<StepSelectPackage
agentPolicyId={policyId}
agentPolicyId={(params as AddFromPolicyParams).policyId}
updateAgentPolicy={updateAgentPolicy}
packageInfo={packageInfo}
updatePackageInfo={updatePackageInfo}
setIsLoadingSecondStep={setIsLoadingSecondStep}
/>
),
[policyId, updateAgentPolicy, packageInfo, updatePackageInfo]
[params, updateAgentPolicy, packageInfo, updatePackageInfo]
);

const stepConfigurePackagePolicy = useMemo(
Expand All @@ -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 */}
Expand All @@ -357,6 +371,7 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => {
<div />
),
[
params,
isLoadingSecondStep,
agentPolicy,
packageInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
agentPolicy: AgentPolicy;
packageInfo: PackageInfo;
packagePolicy: NewPackagePolicy;
integration?: string;
integrationToEnable?: string;
updatePackagePolicy: (fields: Partial<NewPackagePolicy>) => void;
validationResults: PackagePolicyValidationResults;
submitAttempted: boolean;
Expand All @@ -47,7 +47,7 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
agentPolicy,
packageInfo,
packagePolicy,
integration,
integrationToEnable,
updatePackagePolicy,
validationResults,
submitAttempted,
Expand Down Expand Up @@ -95,7 +95,8 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1
: 1
}`,
packagePolicy.description
packagePolicy.description,
integrationToEnable
)
);
}
Expand All @@ -107,7 +108,7 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
namespace: agentPolicy.namespace,
});
}
}, [packagePolicy, agentPolicy, packageInfo, updatePackagePolicy, integration]);
}, [packagePolicy, agentPolicy, packageInfo, updatePackagePolicy, integrationToEnable]);

return validationResults ? (
<EuiDescribedFormGroup
Expand Down

0 comments on commit 5b13f9b

Please sign in to comment.