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] User Name Misconfiguration Table and Preview Contextual Flyout #192946

Merged
merged 8 commits into from
Sep 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ export const MisconfigurationFindingsDetailsTable = memo(

const navToFindings = useNavigateFindings();

const navToFindingsByHostName = (hostName: string) => {
navToFindings({ 'host.name': hostName }, ['rule.name']);
};

const navToFindingsByRuleAndResourceId = (ruleId: string, resourceId: string) => {
navToFindings({ 'rule.id': ruleId, 'resource.id': resourceId });
};

const navToFindingsByName = (name: string, queryField: 'host.name' | 'user.name') => {
navToFindings({ [queryField]: name }, ['rule.name']);
};

const columns: Array<EuiBasicTableColumn<MisconfigurationFindingDetailFields>> = [
{
field: 'rule',
Expand Down Expand Up @@ -154,7 +154,7 @@ export const MisconfigurationFindingsDetailsTable = memo(
<EuiPanel hasShadow={false}>
<EuiLink
onClick={() => {
navToFindingsByHostName(queryName);
navToFindingsByName(queryName, fieldName);
}}
>
{i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ import { FormattedMessage } from '@kbn/i18n-react';
import { useCspSetupStatusApi } from '@kbn/cloud-security-posture/src/hooks/use_csp_setup_status_api';
import { MisconfigurationsPreview } from './misconfiguration/misconfiguration_preview';

export const EntityInsight = <T,>({ hostName }: { hostName: string }) => {
export const EntityInsight = <T,>({
name,
fieldName,
isPreviewMode,
}: {
name: string;
fieldName: 'host.name' | 'user.name';
isPreviewMode?: boolean;
}) => {
const { euiTheme } = useEuiTheme();
const getSetupStatus = useCspSetupStatusApi();
const hasMisconfigurationFindings = getSetupStatus.data?.hasMisconfigurationsFindings;
Expand All @@ -22,7 +30,6 @@ export const EntityInsight = <T,>({ hostName }: { hostName: string }) => {
<>
{hasMisconfigurationFindings && (
<>
<EuiHorizontalRule />
<EuiAccordion
initialIsOpen={true}
id="entityInsight-accordion"
Expand All @@ -45,9 +52,14 @@ export const EntityInsight = <T,>({ hostName }: { hostName: string }) => {
}
>
<EuiSpacer size="m" />
<MisconfigurationsPreview hostName={hostName} />
<MisconfigurationsPreview
name={name}
fieldName={fieldName}
isPreviewMode={isPreviewMode}
/>
<EuiSpacer size="m" />
</EuiAccordion>
<EuiHorizontalRule />
</>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { render } from '@testing-library/react';
import React from 'react';
import { MisconfigurationsPreview } from './misconfiguration_preview';

const mockProps = {
hostName: 'testContextID',
const mockProps: { name: string; fieldName: 'host.name' | 'user.name' } = {
name: 'testContextID',
fieldName: 'host.name',
};

describe('MisconfigurationsPreview', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import { i18n } from '@kbn/i18n';
import { ExpandablePanel } from '@kbn/security-solution-common';
import { buildEntityFlyoutPreviewQuery } from '@kbn/cloud-security-posture-common';
import { useExpandableFlyoutApi } from '@kbn/expandable-flyout';
import { UserDetailsPanelKey } from '../../../flyout/entity_details/user_details_left';
import { HostDetailsPanelKey } from '../../../flyout/entity_details/host_details_left';
import { useRiskScore } from '../../../entity_analytics/api/hooks/use_risk_score';
import { RiskScoreEntity } from '../../../../common/entity_analytics/risk_engine';
import { buildHostNamesFilter } from '../../../../common/search_strategy';
import type { HostRiskScore, UserRiskScore } from '../../../../common/search_strategy';
import { buildHostNamesFilter, buildUserNamesFilter } from '../../../../common/search_strategy';

const FIRST_RECORD_PAGINATION = {
cursorStart: 0,
Expand Down Expand Up @@ -120,9 +122,17 @@ const MisconfigurationPreviewScore = ({
);
};

export const MisconfigurationsPreview = ({ hostName }: { hostName: string }) => {
export const MisconfigurationsPreview = ({
name,
fieldName,
isPreviewMode,
}: {
name: string;
fieldName: 'host.name' | 'user.name';
isPreviewMode?: boolean;
}) => {
const { data } = useMisconfigurationPreview({
query: buildEntityFlyoutPreviewQuery('host.name', hostName),
query: buildEntityFlyoutPreviewQuery(fieldName, name),
sort: [],
enabled: true,
pageSize: 1,
Expand All @@ -133,33 +143,44 @@ export const MisconfigurationsPreview = ({ hostName }: { hostName: string }) =>

const { euiTheme } = useEuiTheme();
const hasMisconfigurationFindings = passedFindings > 0 || failedFindings > 0;
const hostNameFilterQuery = useMemo(
() => (hostName ? buildHostNamesFilter([hostName]) : undefined),
[hostName]

const buildFilterQuery = useMemo(
() => (fieldName === 'host.name' ? buildHostNamesFilter([name]) : buildUserNamesFilter([name])),
[fieldName, name]
);

const riskScoreState = useRiskScore({
riskEntity: RiskScoreEntity.host,
filterQuery: hostNameFilterQuery,
riskEntity: fieldName === 'host.name' ? RiskScoreEntity.host : RiskScoreEntity.user,
filterQuery: buildFilterQuery,
onlyLatest: false,
pagination: FIRST_RECORD_PAGINATION,
});
const { data: hostRisk } = riskScoreState;
const hostRiskData = hostRisk && hostRisk.length > 0 ? hostRisk[0] : undefined;
const isRiskScoreExist = !!hostRiskData?.host.risk;
const riskData = hostRisk && hostRisk.length > 0 ? hostRisk[0] : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const riskData = hostRisk && hostRisk.length > 0 ? hostRisk[0] : undefined;
const riskData = hostRisk?.[0];

This should do the same thing

const isRiskScoreExist =
fieldName === 'host.name'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, but consider consolidating this check to a const (e.g. isUsingHostName / fieldIsHostName) since its repeating a few times

? !!(riskData as HostRiskScore)?.host.risk
: !!(riskData as UserRiskScore)?.user.risk;
const { openLeftPanel } = useExpandableFlyoutApi();
const isPreviewMode = false;
const goToEntityInsightTab = useCallback(() => {
openLeftPanel({
id: HostDetailsPanelKey,
params: {
name: hostName,
isRiskScoreExist,
hasMisconfigurationFindings,
path: { tab: 'csp_insights' },
},
id: fieldName === 'host.name' ? HostDetailsPanelKey : UserDetailsPanelKey,
params:
fieldName === 'host.name'
? {
name,
isRiskScoreExist,
hasMisconfigurationFindings,
path: { tab: 'csp_insights' },
}
: {
user: { name },
isRiskScoreExist,
hasMisconfigurationFindings,
path: { tab: 'csp_insights' },
},
});
}, [hasMisconfigurationFindings, hostName, isRiskScoreExist, openLeftPanel]);
}, [fieldName, hasMisconfigurationFindings, isRiskScoreExist, name, openLeftPanel]);
const link = useMemo(
() =>
!isPreviewMode
Expand All @@ -178,7 +199,7 @@ export const MisconfigurationsPreview = ({ hostName }: { hostName: string }) =>
return (
<ExpandablePanel
header={{
iconType: hasMisconfigurationFindings ? 'arrowStart' : '',
iconType: !isPreviewMode && hasMisconfigurationFindings ? 'arrowStart' : '',
title: (
<EuiText
size="xs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const HostPanelContent = ({
entity={{ name: hostName, type: 'host' }}
onChange={onAssetCriticalityChange}
/>
<EntityInsight name={hostName} fieldName={'host.name'} isPreviewMode={isPreviewMode} />
<ObservedEntity
observedData={observedHost}
contextID={contextID}
Expand All @@ -72,7 +73,6 @@ export const HostPanelContent = ({
observedFields={observedFields}
queryId={HOST_PANEL_OBSERVED_HOST_QUERY_ID}
/>
<EntityInsight hostName={hostName} />
</FlyoutBody>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,46 @@ describe('LeftPanel', () => {

expect(tabElement).not.toBeInTheDocument();
});

it("doesn't render insights panel when there no misconfiguration findings", () => {
const { queryByText } = render(
<UserDetailsPanel
path={{
tab: EntityDetailsLeftPanelTab.RISK_INPUTS,
}}
isRiskScoreExist
user={{ name: 'test user', email: [] }}
scopeId={'scopeId'}
hasMisconfigurationFindings={false}
/>,
{
wrapper: TestProviders,
}
);

const tabElement = queryByText('Insights');

expect(tabElement).not.toBeInTheDocument();
});

it('render insights panel when there are misconfiguration findings', () => {
const { queryByText } = render(
<UserDetailsPanel
path={{
tab: EntityDetailsLeftPanelTab.CSP_INSIGHTS,
}}
isRiskScoreExist
user={{ name: 'test user', email: [] }}
scopeId={'scopeId'}
hasMisconfigurationFindings={true}
/>,
{
wrapper: TestProviders,
}
);

const tabElement = queryByText('Insights');

expect(tabElement).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface UserDetailsPanelProps extends Record<string, unknown> {
user: UserParam;
path?: PanelPath;
scopeId: string;
hasMisconfigurationFindings?: boolean;
}
export interface UserDetailsExpandableFlyoutProps extends FlyoutPanelProps {
key: 'user_details';
Expand All @@ -40,10 +41,24 @@ export const UserDetailsPanel = ({
user,
path,
scopeId,
hasMisconfigurationFindings,
}: UserDetailsPanelProps) => {
const managedUser = useManagedUser(user.name, user.email);
const tabs = useTabs(managedUser.data, user.name, isRiskScoreExist, scopeId);
const { selectedTabId, setSelectedTabId } = useSelectedTab(isRiskScoreExist, user, tabs, path);
const tabs = useTabs(
managedUser.data,
user.name,
isRiskScoreExist,
scopeId,
hasMisconfigurationFindings
);

const { selectedTabId, setSelectedTabId } = useSelectedTab(
isRiskScoreExist,
user,
tabs,
path,
hasMisconfigurationFindings
);

if (managedUser.isLoading) return <FlyoutLoading />;

Expand All @@ -67,7 +82,8 @@ const useSelectedTab = (
isRiskScoreExist: boolean,
user: UserParam,
tabs: LeftPanelTabsType,
path: PanelPath | undefined
path: PanelPath | undefined,
hasMisconfigurationFindings?: boolean
) => {
const { openLeftPanel } = useExpandableFlyoutApi();

Expand All @@ -81,12 +97,13 @@ const useSelectedTab = (
const setSelectedTabId = (tabId: EntityDetailsLeftPanelTab) => {
openLeftPanel({
id: UserDetailsPanelKey,
path: {
tab: tabId,
},
params: {
user,
isRiskScoreExist,
hasMisconfigurationFindings,
path: {
tab: tabId,
},
},
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import React, { useMemo } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';

import { getRiskInputTab } from '../../../entity_analytics/components/entity_details_flyout';
import {
getInsightsInputTab,
getRiskInputTab,
} from '../../../entity_analytics/components/entity_details_flyout';
import { UserAssetTableType } from '../../../explore/users/store/model';
import { ManagedUserDatasetKey } from '../../../../common/search_strategy/security_solution/users/managed_details';
import type {
Expand All @@ -26,7 +29,8 @@ export const useTabs = (
managedUser: ManagedUserHits,
name: string,
isRiskScoreExist: boolean,
scopeId: string
scopeId: string,
hasMisconfigurationFindings?: boolean
): LeftPanelTabsType =>
useMemo(() => {
const tabs: LeftPanelTabsType = [];
Expand All @@ -51,8 +55,12 @@ export const useTabs = (
tabs.push(getEntraTab(entraManagedUser));
}

if (hasMisconfigurationFindings) {
tabs.push(getInsightsInputTab({ name, fieldName: 'user.name' }));
}

return tabs;
}, [isRiskScoreExist, managedUser, name, scopeId]);
}, [hasMisconfigurationFindings, isRiskScoreExist, managedUser, name, scopeId]);

const getOktaTab = (oktaManagedUser: ManagedUserHit) => ({
id: EntityDetailsLeftPanelTab.OKTA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ObservedEntity } from '../shared/components/observed_entity';
import type { ObservedEntityData } from '../shared/components/observed_entity/types';
import { useObservedUserItems } from './hooks/use_observed_user_items';
import type { EntityDetailsLeftPanelTab } from '../shared/components/left_panel/left_panel_header';
import { EntityInsight } from '../../../cloud_security_posture/components';

interface UserPanelContentProps {
userName: string;
Expand Down Expand Up @@ -72,6 +73,7 @@ export const UserPanelContent = ({
entity={{ name: userName, type: 'user' }}
onChange={onAssetCriticalityChange}
/>
<EntityInsight name={userName} fieldName={'user.name'} isPreviewMode={isPreviewMode} />
<ObservedEntity
observedData={observedUser}
contextID={contextID}
Expand Down
Loading