Skip to content

Commit

Permalink
fix(SystemCVEs): RHINENG-11464 inventory tab advisory filter (#2149)
Browse files Browse the repository at this point in the history
  • Loading branch information
xmicha82 authored Oct 17, 2024
1 parent 7deb190 commit 14e6ed2
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 53 deletions.
11 changes: 8 additions & 3 deletions src/Components/SmartComponents/SystemCves/SystemCves.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const SystemCVEs = ({
entity.id, systemCVEs, columns, linkToCustomerPortal
), [systemCVEs, systemCVEs.isLoading, entity.id, columns]);
const [urlParameters, setUrlParams] = useUrlParams(CVES_ALLOWED_PARAMS);
const { includesCvesWithoutErrata } = useContext(AccountStatContext);
const { includesCvesWithoutErrata, isFederated } = useContext(AccountStatContext);

const downloadReport = format => {
const params = { ...parameters, system: entity.id };
Expand Down Expand Up @@ -128,10 +128,15 @@ export const SystemCVEs = ({
dispatch(changeSystemCVEsParameters(params));
};

const paramsWithoutKeys = (params, keys) => Object.fromEntries(Object.entries(params).filter((p) => !keys.includes(p[0])));

useEffect(() => {
apply(
isEmpty(urlParameters) ?
{ advisory_available: 'true' }
isEmpty(paramsWithoutKeys(urlParameters, ['appName'])) ?
{
advisory_available: 'true',
...isFederated ? { appName: 'vulnerabilities' } : {}
}
: urlParameters
);

Expand Down
50 changes: 50 additions & 0 deletions src/Helpers/Hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ColumnManagementModal from '../Components/SmartComponents/Modals/ColumnMa
import useChrome from '@redhat-cloud-services/frontend-components/useChrome';
import { useFlag, useFlagsStatus } from '@unleash/proxy-client-react';
import { AccountStatContext } from '../Utilities/VulnerabilityRoutes';
import { getRhelSystems, checkEdgePresence, getCveListByAccount } from './APIHelper';

export const useNotification = (config = {}) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -279,3 +280,52 @@ export const useHybridSystemFilterFlag = () => {
const shouldUseHybridSystemFilter = isEdgeParityEnabled && hasEdgeDevices;
return shouldUseHybridSystemFilter;
};

export const useAccountStats = (isEdgeParityEnabled, setLoading, addNotification) => {
const [hasEdgeDevices, setHasEdgeDevices] = useState(true);
const [hasConventionalSystems, setHasConventionalSystems] = useState(true);
const [includesCvesWithoutErrata, setIncludesCvesWithoutErrata] = useState();
const [isAdvisoryAvailable, setAdvisoryAvailable] = useState();
const [hasAccess, setHasAccess] = useState(true);

useEffect(() => {
const fetchData = async () => {
try {
const rhelSystems = await getRhelSystems();
if (isEdgeParityEnabled) {
//if there is at least 1 edge device in the account level, not only in vulnerability
const edgeDevicePresent = await checkEdgePresence();
setHasEdgeDevices(edgeDevicePresent);
}

const { meta: {
cves_without_errata: cvesWithoutErrata, advisory_available: advisoryAvailable
} = {}
} = await getCveListByAccount({ limit: 1 });

setHasConventionalSystems(rhelSystems);
setIncludesCvesWithoutErrata(cvesWithoutErrata);
setAdvisoryAvailable(advisoryAvailable);
setLoading(false);
} catch (error) {
if (error.status === '403') {
setHasAccess(false);
}
else {
addNotification({
variant: 'danger',
autoDismiss: false,
msg: 'Failed to fetch systems',
description: error.detail
});
}

setLoading(false);
}
};

fetchData();
}, []);

return { hasEdgeDevices, hasConventionalSystems, includesCvesWithoutErrata, isAdvisoryAvailable, hasAccess };
};
35 changes: 35 additions & 0 deletions src/Utilities/AccountStatContextWrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import PropTypes from 'prop-types';
import useFeatureFlag from './useFeatureFlag';
import { useAccountStats, useNotification } from '../Helpers/Hooks';
import { AccountStatContext } from './VulnerabilityRoutes';

export const AccountStatContextWrapper = ({ children, setLoading, isFederated = false }) => {
const isEdgeParityEnabled = useFeatureFlag('vulnerability.edge_parity');
const [addNotification] = useNotification();

const {
hasConventionalSystems,
hasEdgeDevices,
includesCvesWithoutErrata,
isAdvisoryAvailable
} = useAccountStats(isEdgeParityEnabled, setLoading, addNotification);

return (
<AccountStatContext.Provider value={{
hasConventionalSystems,
hasEdgeDevices,
includesCvesWithoutErrata,
isAdvisoryAvailable,
isFederated
}}>
{ children }
</AccountStatContext.Provider>
);
};

AccountStatContextWrapper.propTypes = {
children: PropTypes.func,
setLoading: PropTypes.func,
isFederated: PropTypes.bool
};
62 changes: 13 additions & 49 deletions src/Utilities/VulnerabilityRoutes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useState, lazy, Suspense, createContext } from 'react';
import PropTypes from 'prop-types';
import { Navigate, Route, Routes, useLocation } from 'react-router-dom';
import { checkEdgePresence, getRhelSystems, getCveListByAccount } from '../Helpers/APIHelper';
import { PATHS } from '../Helpers/constants';
import { intl } from './IntlProvider';
import messages from '../Messages';
Expand All @@ -10,7 +9,7 @@ import ErrorState from '@redhat-cloud-services/frontend-components/ErrorState';
import { useChrome } from '@redhat-cloud-services/frontend-components/useChrome';
import useFeatureFlag from './useFeatureFlag';
import { NotAuthorized } from '@redhat-cloud-services/frontend-components/NotAuthorized';
import { useNotification } from '../Helpers/Hooks';
import { useAccountStats, useNotification } from '../Helpers/Hooks';
import PageLoading from '../Components/PresentationalComponents/Snippets/PageLoading';

const SystemsPage = lazy(() =>
Expand Down Expand Up @@ -54,64 +53,28 @@ export const checkForAccountSystems = (isEdgeParityEnabled, hasEdgeDevices, hasC
return isEdgeParityEnabled && (hasEdgeDevices || hasConventionalSystems) || hasConventionalSystems;
};

export const InsightsElement = ({ element: Element, title, globalFilterEnabled, ...elementProps }) => {
export const InsightsElement = ({ element: Element, title, globalFilterEnabled, ...elementProps }) => {
let location = useLocation();
const [isLoading, setLoading] = useState(true);
const [hasConventionalSystems, setHasConventionalSystems] = useState(true);
const [hasEdgeDevices, setHasEdgeDevices] = useState(true);
const [hasAccess, setHasAccess] = useState(true);
const [includesCvesWithoutErrata, setIncludesCvesWithoutErrata] = useState();
const [isAdvisoryAvailable, setAdvisoryAvailable] = useState();
const [addNotification] = useNotification();
const isEdgeParityEnabled = useFeatureFlag('vulnerability.edge_parity');
const [addNotification] = useNotification();
const chrome = useChrome();

useEffect(() => {
const fetchData = async () => {
try {
const rhelSystems = await getRhelSystems();
if (isEdgeParityEnabled) {
//if there is at least 1 edge device in the account level, not only in vulnerability
const edgeDevicePresent = await checkEdgePresence();
setHasEdgeDevices(edgeDevicePresent);
}

const { meta:
{
cves_without_errata: cvesWithoutErrata, advisory_available: advisoryAvailable
} = {}
} = await getCveListByAccount({ limit: 1 });

setHasConventionalSystems(rhelSystems);
setIncludesCvesWithoutErrata(cvesWithoutErrata);
setAdvisoryAvailable(advisoryAvailable);
setLoading(false);
} catch (error) {
if (error.status === '403') {
setHasAccess(false);
}
else {
addNotification({
variant: 'danger',
autoDismiss: false,
msg: 'Failed to fetch systems',
description: error.detail
});
}

setLoading(false);
}
};

fetchData();
}, []);
const {
hasEdgeDevices,
hasConventionalSystems,
includesCvesWithoutErrata,
isAdvisoryAvailable,
hasAccess
} = useAccountStats(isEdgeParityEnabled, setLoading, addNotification);

const subPath = location.pathname && location.pathname.split('/')[4];

useEffect(() => {
chrome.updateDocumentTitle(
`${subPath ? `${subPath} - ` : ''} ${title} - ${intl.formatMessage(messages.pageTitleSuffix)}`
);

}, [chrome, intl, subPath]);

useEffect(() => {
Expand Down Expand Up @@ -161,7 +124,8 @@ export const InsightsElement = ({ element: Element, title, globalFilterEnabled,
InsightsElement.propTypes = {
element: PropTypes.func,
title: PropTypes.string,
globalFilterEnabled: PropTypes.bool
globalFilterEnabled: PropTypes.bool,
changeTitle: PropTypes.bool
};

export const VulnerabilityRoutes = () => {
Expand Down
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Provider } from 'react-redux';
import { useRbac } from './Helpers/Hooks';
import { PERMISSIONS } from './Helpers/constants';
import PageLoading from './Components/PresentationalComponents/Snippets/PageLoading';
import { AccountStatContextWrapper } from './Utilities/AccountStatContextWrapper';

const WrappedSystemCves = ({ getRegistry, ...props }) => {
const [Wrapper, setWrapper] = useState();
const [isLoading, setLoading] = useState(true);

const [[canExport, canEditPairStatus]] = useRbac([PERMISSIONS.basicReporting, PERMISSIONS.setPairStatus]);

Expand All @@ -18,12 +20,23 @@ const WrappedSystemCves = ({ getRegistry, ...props }) => {
}

setWrapper(() => getRegistry ? Provider : Fragment);
setLoading(false);
}, []);

if (isLoading) {
return <PageLoading />;
}

return (
Wrapper ?
<Wrapper {...getRegistry && { store: getRegistry()?.getStore() }}>
<SystemCVEs canExport={canExport} canEditPairStatus={canEditPairStatus} {...props} />
<AccountStatContextWrapper setLoading={setLoading} isFederated>
<SystemCVEs
canExport={canExport}
canEditPairStatus={canEditPairStatus}
{...props}
/>
</AccountStatContextWrapper>
</Wrapper>
: <PageLoading />
);
Expand Down

0 comments on commit 14e6ed2

Please sign in to comment.