Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cloud Security] CSPM AWS agentless selector #171671

Merged
merged 18 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { i18n } from '@kbn/i18n';
import { AZURE_ARM_TEMPLATE_CREDENTIAL_TYPE } from './azure_credentials_form/azure_credentials_form';
import { CspRadioGroupProps, RadioGroup } from './csp_boxed_radio_group';
import { assert } from '../../../common/utils/helpers';
import type { PostureInput, CloudSecurityPolicyTemplate } from '../../../common/types';
import type { CloudSecurityPolicyTemplate, PostureInput } from '../../../common/types';
import {
CLOUDBEAT_AWS,
CLOUDBEAT_VANILLA,
Expand All @@ -41,14 +41,14 @@ import {
SUPPORTED_POLICY_TEMPLATES,
} from '../../../common/constants';
import {
getPosturePolicy,
getMaxPackageName,
getPostureInputHiddenVars,
getPosturePolicy,
getVulnMgmtCloudFormationDefaultValue,
POSTURE_NAMESPACE,
type NewPackagePolicyPostureInput,
isPostureInput,
getMaxPackageName,
isBelowMinVersion,
type NewPackagePolicyPostureInput,
POSTURE_NAMESPACE,
} from './utils';
import {
PolicyTemplateInfo,
Expand All @@ -58,6 +58,8 @@ import {
} from './policy_template_selectors';
import { usePackagePolicyList } from '../../common/api/use_package_policy_list';
import { gcpField, getInputVarsFields } from './gcp_credential_form';
import { SetupTechnologySelector } from './setup_technology_selector/setup_technology_selector';
import { useSetupTechnology } from './setup_technology_selector/use_setup_technology';

const DEFAULT_INPUT_TYPE = {
kspm: CLOUDBEAT_VANILLA,
Expand Down Expand Up @@ -520,14 +522,29 @@ const IntegrationSettings = ({ onChange, fields }: IntegrationInfoFieldsProps) =
);

export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensionComponentProps>(
({ newPolicy, onChange, validationResults, isEditPage, packageInfo }) => {
({
agentPolicy,
newPolicy,
onChange,
validationResults,
isEditPage,
packageInfo,
handleSetupTechnologyChange,
agentlessPolicy,
}) => {
const integrationParam = useParams<{ integration: CloudSecurityPolicyTemplate }>().integration;
const integration = SUPPORTED_POLICY_TEMPLATES.includes(integrationParam)
? integrationParam
: undefined;
// Handling validation state
const [isValid, setIsValid] = useState(true);
const input = getSelectedOption(newPolicy.inputs, integration);
const { isAgentlessAvailable, setupTechnology, setSetupTechnology } = useSetupTechnology({
input,
agentPolicy,
agentlessPolicy,
handleSetupTechnologyChange,
});

const updatePolicy = useCallback(
(updatedPolicy: NewPackagePolicy) => {
Expand Down Expand Up @@ -722,6 +739,13 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
onChange={(field, value) => updatePolicy({ ...newPolicy, [field]: value })}
/>

{isAgentlessAvailable && (
<SetupTechnologySelector
setupTechnology={setupTechnology}
onSetupTechnologyChange={setSetupTechnology}
/>
)}

{/* Defines the vars of the enabled input of the active policy template */}
<PolicyTemplateVarsForm
input={input}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';

import { SetupTechnology } from '@kbn/fleet-plugin/public';
import { FormattedMessage } from '@kbn/i18n-react';
import {
EuiAccordion,
EuiFormRow,
EuiLink,
EuiSpacer,
EuiSuperSelect,
EuiText,
useGeneratedHtmlId,
} from '@elastic/eui';

export const SetupTechnologySelector = ({
setupTechnology,
onSetupTechnologyChange,
}: {
setupTechnology: SetupTechnology;
onSetupTechnologyChange: (value: SetupTechnology) => void;
}) => {
const options = [
{
value: SetupTechnology.AGENTLESS,
inputDisplay: (
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.agentlessInputDisplay"
defaultMessage="Agentless"
/>
),
dropdownDisplay: (
<>
<strong>
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.agentlessDrowpownDisplay"
defaultMessage="Agentless"
/>
</strong>
<EuiText size="s" color="subdued">
<p>
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.agentlessDrowpownDescription"
defaultMessage="Set up the integration without an agent"
/>
</p>
</EuiText>
</>
),
},
{
value: SetupTechnology.AGENT_BASED,
inputDisplay: (
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.agentbasedInputDisplay"
defaultMessage="Agent-based"
/>
),
dropdownDisplay: (
<>
<strong>
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.agentbasedDrowpownDisplay"
defaultMessage="Agent-based"
/>
</strong>
<EuiText size="s" color="subdued">
<p>
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.agentbasedDrowpownDescription"
defaultMessage="Set up the integration with an agent"
/>
</p>
</EuiText>
</>
),
},
];

return (
<>
<EuiSpacer size="l" />
<EuiAccordion
id={useGeneratedHtmlId({ prefix: 'setup-type' })}
buttonContent={
<EuiLink>
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.advancedOptionsLabel"
defaultMessage="Advanced options"
/>
</EuiLink>
}
>
<EuiSpacer size="l" />
<EuiFormRow
fullWidth
label={
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.setupTechnologyLabel"
defaultMessage="Setup technology"
/>
}
>
<EuiSuperSelect
options={options}
valueOfSelected={setupTechnology}
placeholder={
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.setupTechnologyPlaceholder"
defaultMessage="Select the setup technology"
/>
}
onChange={onSetupTechnologyChange}
itemLayoutAlign="top"
hasDividers
fullWidth
/>
</EuiFormRow>
</EuiAccordion>
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { renderHook, act } from '@testing-library/react-hooks';

import { SetupTechnology } from '@kbn/fleet-plugin/public';
import { AgentPolicy, NewPackagePolicyInput } from '@kbn/fleet-plugin/common';

import { CLOUDBEAT_AWS } from '../../../../common/constants';
import { useSetupTechnology } from './use_setup_technology';

describe('useSetupTechnology', () => {
it('initializes with AGENT_BASED technology', () => {
const { result } = renderHook(() =>
useSetupTechnology({
input: { type: 'cloudbeat/no-agentless-support' } as NewPackagePolicyInput,
})
);
expect(result.current.setupTechnology).toBe(SetupTechnology.AGENT_BASED);
});

it('sets to AGENTLESS when agentless is available', () => {
const agentlessPolicy = { id: 'agentlessPolicyId' } as AgentPolicy;
const input = { type: CLOUDBEAT_AWS } as NewPackagePolicyInput;
const { result } = renderHook(() => useSetupTechnology({ input, agentlessPolicy }));
expect(result.current.isAgentlessAvailable).toBeTruthy();
expect(result.current.setupTechnology).toBe(SetupTechnology.AGENTLESS);
});

it('sets to AGENT_BASED when agentPolicyId differs from agentlessPolicyId', () => {
const input = { type: CLOUDBEAT_AWS } as NewPackagePolicyInput;
const agentPolicy = { id: 'agentPolicyId' } as AgentPolicy;
const agentlessPolicy = { id: 'agentlessPolicyId' } as AgentPolicy;
const { result } = renderHook(() =>
useSetupTechnology({ input, agentPolicy, agentlessPolicy })
);
expect(result.current.setupTechnology).toBe(SetupTechnology.AGENT_BASED);
});

it('calls handleSetupTechnologyChange when setupTechnology changes', () => {
const handleSetupTechnologyChangeMock = jest.fn();
const { result } = renderHook(() =>
useSetupTechnology({
input: { type: 'someType' } as NewPackagePolicyInput,
handleSetupTechnologyChange: handleSetupTechnologyChangeMock,
})
);

act(() => {
result.current.setSetupTechnology(SetupTechnology.AGENTLESS);
});

expect(handleSetupTechnologyChangeMock).toHaveBeenCalledWith(SetupTechnology.AGENTLESS);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useEffect, useMemo, useState } from 'react';

import { AgentPolicy, NewPackagePolicyInput } from '@kbn/fleet-plugin/common';
import { SetupTechnology } from '@kbn/fleet-plugin/public';
import { CLOUDBEAT_AWS } from '../../../../common/constants';

export const useSetupTechnology = ({
input,
agentPolicy,
agentlessPolicy,
handleSetupTechnologyChange,
}: {
input: NewPackagePolicyInput;
agentPolicy?: AgentPolicy;
agentlessPolicy?: AgentPolicy;
handleSetupTechnologyChange?: (value: SetupTechnology) => void;
}) => {
const [setupTechnology, setSetupTechnology] = useState<SetupTechnology>(
SetupTechnology.AGENT_BASED
);
const isCspmAws = input.type === CLOUDBEAT_AWS;
const isAgentlessAvailable = useMemo(
() => Boolean(isCspmAws && agentlessPolicy),
[isCspmAws, agentlessPolicy]
);
const agentPolicyId = useMemo(() => agentPolicy?.id, [agentPolicy]);
const agentlessPolicyId = useMemo(() => agentlessPolicy?.id, [agentlessPolicy]);

useEffect(() => {
if (agentPolicyId && agentPolicyId !== agentlessPolicyId) {
/*
handle case when agent policy is coming from outside,
e.g. from the get param or when coming to integration from a specific agent policy
*/
setSetupTechnology(SetupTechnology.AGENT_BASED);
} else if (isAgentlessAvailable) {
/*
preselecting agenteless when available
and resetting to agent-based when switching to another integration type, which doesn't support agentless
*/
setSetupTechnology(SetupTechnology.AGENTLESS);
} else {
setSetupTechnology(SetupTechnology.AGENT_BASED);
}
}, [agentPolicyId, agentlessPolicyId, isAgentlessAvailable]);

useEffect(() => {
if (handleSetupTechnologyChange) {
handleSetupTechnologyChange(setupTechnology);
}
}, [handleSetupTechnologyChange, setupTechnology]);

return {
isAgentlessAvailable,
setupTechnology,
setSetupTechnology,
};
};
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/experimental_features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const allowedExperimentalValues = Object.freeze<Record<string, boolean>>(
kafkaOutput: true,
outputSecretsStorage: true,
remoteESOutput: true,
agentless: false,
});

type ExperimentalConfigKeys = Array<keyof ExperimentalFeatures>;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from './download_sources';
export * from './fleet_server_policy_config';
export * from './fleet_proxy';
export * from './secret';
export * from './setup_technology';
11 changes: 11 additions & 0 deletions x-pack/plugins/fleet/common/types/models/setup_technology.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export enum SetupTechnology {
AGENTLESS = 'agentless',
AGENT_BASED = 'agent-based',
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export { useDevToolsRequest } from './devtools_request';
export { useOnSubmit } from './form';
export { useSetupTechnology } from './setup_technology';
Loading
Loading