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

Image Vulnerabilities Tab for Pod Detail View #5084

Merged
merged 1 commit into from
Apr 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import * as _ from 'lodash';
import * as classNames from 'classnames';
import { Tooltip } from '@patternfly/react-core';
import { sortable } from '@patternfly/react-table';
import { SecurityIcon } from '@patternfly/react-icons';
import {
Expand All @@ -12,7 +13,8 @@ import {
ListPage,
RowFunction,
} from '@console/internal/components/factory';
import { referenceForModel, PodKind } from '@console/internal/module/k8s';
import { GreenCheckCircleIcon } from '@console/shared/';
import { referenceForModel, PodKind, ContainerStatus } from '@console/internal/module/k8s';
import { match } from 'react-router';
import {
ResourceLink,
Expand All @@ -21,6 +23,9 @@ import {
SectionHeading,
ResourceSummary,
DetailsItem,
Firehose,
FirehoseResult,
Loading,
} from '@console/internal/components/utils';
import { ChartDonut } from '@patternfly/react-charts';
import { DefaultList } from '@console/internal/components/default-resource';
Expand All @@ -29,6 +34,7 @@ import { ImageManifestVuln, Feature, Vulnerability } from '../types';
import { ImageManifestVulnModel } from '../models';
import { quayURLFor } from './summary';
import './image-manifest-vuln.scss';
import { ContainerLink } from '@console/internal/components/pod';

const shortenImage = (img: string) =>
img
Expand Down Expand Up @@ -305,19 +311,136 @@ export const ImageManifestVulnPage: React.FC<ImageManifestVulnPageProps> = (prop
]}
flatten={(resources) => _.get(resources.imageManifestVuln, 'data', [])}
title="Image Manifest Vulnerabilities"
canCreate={false}
showTitle
hideNameFilter
hideLabelFilter
ListComponent={ImageManifestVulnList}
/>
);
};

const podKey = (pod: PodKind) => [pod.metadata.namespace, pod.metadata.name].join('/');

export const ContainerVulnerabilities: React.FC<ContainerVulnerabilitiesProps> = (props) => {
const vulnFor = (containerStatus: ContainerStatus) =>
_.get(props.imageManifestVuln, 'data', []).find(
(imv) =>
imv.status.affectedPods[podKey(props.pod)].some(
(id) => containerStatus.containerID === id,
) || containerStatus.imageID.includes(imv.spec.manifest),
);

const withVuln = (
vuln: ImageManifestVuln,
exists: (vuln: ImageManifestVuln) => JSX.Element,
absent: () => JSX.Element,
) => (vuln !== undefined ? exists(vuln) : absent());

return (
<div className="co-m-pane__body">
<div className="co-m-table-grid co-m-table-grid--bordered">
<div className="row co-m-table-grid__head">
<div className="col-md-3">Container</div>
<div className="col-md-4">Image</div>
<div className="col-md-2">
<Tooltip content="Results provided by Quay security scanner">
<span>Security Scan</span>
</Tooltip>
</div>
</div>
<div className="co-m-table-grid__body">
{props.pod.status.containerStatuses.map((status) => (
<div className="row" key={status.containerID}>
<div className="col-md-3">
<ContainerLink pod={props.pod} name={status.name} />
</div>
<div className="col-md-4 co-truncate co-nowrap co-select-to-copy">
{props.pod.spec.containers.find((c) => c.name === status.name).image}
</div>
<div className="col-md-3">
{props.loaded ? (
withVuln(
vulnFor(status),
(vuln) => (
<span style={{ display: 'flex', alignItems: 'center' }}>
<SecurityIcon
color={
vulnPriority.find(({ title }) => vuln.status.highestSeverity === title)
.color.value
}
/>
&nbsp;
<ResourceLink
kind={referenceForModel(ImageManifestVulnModel)}
name={vuln.metadata.name}
namespace={props.pod.metadata.namespace}
title={vuln.metadata.uid}
displayName={`${totalFor(
vulnPriority.findKey(
({ title }) => vuln.status.highestSeverity === title,
),
)(vuln)} ${vuln.status.highestSeverity}`}
hideIcon
/>
</span>
),
() => (
<span>
<GreenCheckCircleIcon />
&nbsp;No vulnerabilities found
</span>
),
)
) : (
<div>
<Loading />
</div>
)}
</div>
</div>
))}
</div>
</div>
</div>
);
};

export const ImageManifestVulnPodTab: React.FC<ImageManifestVulnPodTabProps> = (props) => {
return (
<Firehose
resources={[
{
isList: true,
kind: referenceForModel(ImageManifestVulnModel),
namespace: props.match.params.ns,
selector: {
matchLabels: { [podKey(props.obj)]: 'true' },
},
prop: 'imageManifestVuln',
},
]}
>
{/* FIXME(alecmerdler): Hack because `Firehose` injects props without TypeScript knowing about it */}
<ContainerVulnerabilities pod={props.obj} {...(props as any)} />
Copy link
Contributor

Choose a reason for hiding this comment

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

you could use watchK8sResource hook instead of Firehose and avoid props as any that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see a watchK8sResource hook, just an function that is passed as a prop by withDashboardResources, which doesn't apply to this scenario.

Copy link
Contributor

Choose a reason for hiding this comment

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

</Firehose>
);
};

export type ContainerVulnerabilitiesProps = {
loaded: boolean;
pod: PodKind;
imageManifestVuln: FirehoseResult<ImageManifestVuln[]>;
};

export type ImageManifestVulnDetailsPageProps = {
match: match<{ ns: string; name: string }>;
};

export type ImageManifestVulnPageProps = {
namespace?: string;
match?: match<{ ns?: string }>;
selector?: { [key: string]: string };
};

export type ImageManifestVulnListProps = {
Expand All @@ -344,8 +467,15 @@ export type ImageVulnerabilityRowProps = {
packageName: string;
};

export type ImageManifestVulnPodTabProps = {
match: match<{ ns: string; name: string }>;
obj: PodKind;
};

ImageManifestVulnPage.displayName = 'ImageManifestVulnPage';
ImageManifestVulnList.displayName = 'ImageManifestVulnList';
AffectedPods.displayName = 'AffectedPods';
ImageVulnerabilitiesTable.displayName = 'ImageVulnerabilitiesTable';
ImageVulnerabilityRow.displayName = 'ImageVulnerabilityRow';
ImageManifestVulnPodTab.displayName = 'ImageManifestVulnPodTab';
ContainerVulnerabilities.displayName = 'ContainerVulnerabilities';
2 changes: 1 addition & 1 deletion frontend/packages/container-security/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { Map as ImmutableMap } from 'immutable';
import { ImageManifestVuln } from './types';

export const SecurityLabellerFlag = 'SECURITY_LABELLER';
export const ContainerSecurityFlag = 'SECURITY_LABELLER';

export enum Priority {
Defcon1 = 'Defcon1',
Expand Down
30 changes: 25 additions & 5 deletions frontend/packages/container-security/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {
RoutePage,
ResourceDetailsPage,
ResourceNSNavItem,
HorizontalNavTab,
} from '@console/plugin-sdk';
import { ClusterServiceVersionModel } from '@console/operator-lifecycle-manager';
import { ImageManifestVulnModel } from './models';
import { SecurityLabellerFlag } from './const';
import { ContainerSecurityFlag } from './const';
import { securityHealthHandler } from './components/summary';
import { getKebabActionsForKind } from './kebab-actions';
import { WatchImageVuln } from './types';
import { PodModel } from '@console/internal/models';

type ConsumedExtensions =
| ModelDefinition
Expand All @@ -27,7 +29,8 @@ type ConsumedExtensions =
| DashboardsOverviewHealthResourceSubsystem<WatchImageVuln>
| RoutePage
| KebabActions
| ResourceNSNavItem;
| ResourceNSNavItem
| HorizontalNavTab;

const plugin: Plugin<ConsumedExtensions> = [
{
Expand All @@ -40,7 +43,7 @@ const plugin: Plugin<ConsumedExtensions> = [
type: 'FeatureFlag/Model',
properties: {
model: ImageManifestVulnModel,
flag: SecurityLabellerFlag,
flag: ContainerSecurityFlag,
},
},
{
Expand Down Expand Up @@ -110,7 +113,7 @@ const plugin: Plugin<ConsumedExtensions> = [
),
},
flags: {
required: [SecurityLabellerFlag],
required: [ContainerSecurityFlag],
},
},
{
Expand All @@ -132,7 +135,24 @@ const plugin: Plugin<ConsumedExtensions> = [
},
},
flags: {
required: [SecurityLabellerFlag],
required: [ContainerSecurityFlag],
},
},
{
type: 'HorizontalNavTab',
properties: {
model: PodModel,
page: {
name: 'Vulnerabilities',
href: 'vulnerabilities',
},
loader: () =>
import(
'./components/image-manifest-vuln' /* webpackChunkName: "container-security" */
).then((m) => m.ImageManifestVulnPodTab),
},
flags: {
required: [ContainerSecurityFlag],
},
},
];
Expand Down
3 changes: 2 additions & 1 deletion frontend/public/components/pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,15 @@ const PodTableHeader = () => {
};
PodTableHeader.displayName = 'PodTableHeader';

const ContainerLink: React.FC<ContainerLinkProps> = ({ pod, name }) => (
export const ContainerLink: React.FC<ContainerLinkProps> = ({ pod, name }) => (
<span className="co-resource-item co-resource-item--inline">
<ResourceIcon kind="Container" />
<Link to={`/k8s/ns/${pod.metadata.namespace}/pods/${pod.metadata.name}/containers/${name}`}>
{name}
</Link>
</span>
);
ContainerLink.displayName = 'ContainerLink';

export const ContainerRow: React.FC<ContainerRowProps> = ({ pod, container }) => {
const cstatus = getContainerStatus(pod, container.name);
Expand Down