@@ -3,31 +3,31 @@ import * as plugin from 'snyk-docker-plugin';
33
44import logger = require( '../../common/logger' ) ;
55import { pull as skopeoCopy , getDestinationForImage } from './skopeo' ;
6- import { IPullableImage , IScanImage } from './types' ;
6+ import { IPullableImage } from './types' ;
77import { IStaticAnalysisOptions , StaticAnalysisImageType , IScanResult , IPluginOptions } from '../types' ;
88
99export async function pullImages ( images : IPullableImage [ ] ) : Promise < IPullableImage [ ] > {
1010 const pulledImages : IPullableImage [ ] = [ ] ;
1111
1212 for ( const image of images ) {
13- const { imageWithDigest , fileSystemPath} = image ;
13+ const { imageName , fileSystemPath} = image ;
1414 if ( ! fileSystemPath ) {
1515 continue ;
1616 }
1717
1818 try {
19- await skopeoCopy ( imageWithDigest , fileSystemPath ) ;
19+ await skopeoCopy ( imageName , fileSystemPath ) ;
2020 pulledImages . push ( image ) ;
2121 } catch ( error ) {
22- logger . error ( { error, image : imageWithDigest } , 'failed to pull image' ) ;
22+ logger . error ( { error, image : imageName } , 'failed to pull image' ) ;
2323 }
2424 }
2525
2626 return pulledImages ;
2727}
2828
29- export function getImagesWithFileSystemPath ( images : IScanImage [ ] ) : IPullableImage [ ] {
30- return images . map ( ( image ) => ( { ... image , fileSystemPath : getDestinationForImage ( image . imageName ) } ) ) ;
29+ export function getImagesWithFileSystemPath ( images : string [ ] ) : IPullableImage [ ] {
30+ return images . map ( ( image ) => ( { imageName : image , fileSystemPath : getDestinationForImage ( image ) } ) ) ;
3131}
3232
3333export async function removePulledImages ( images : IPullableImage [ ] ) : Promise < void > {
@@ -41,15 +41,15 @@ export async function removePulledImages(images: IPullableImage[]): Promise<void
4141}
4242
4343// Exported for testing
44- export function getImageParts ( imageWithTag : string ) : { imageName : string , imageTag : string , imageDigest : string } {
44+ export function getImageParts ( imageWithTag : string ) : { imageName : string , imageTag : string } {
4545 // we're matching pattern: <registry:port_number>(optional)/<image_name>(mandatory):<image_tag>(optional)@<tag_identifier>(optional)
4646 // extracted from https://github.com/docker/distribution/blob/master/reference/regexp.go
4747 const regex = / ^ ( (?: (?: [ a - z A - Z 0 - 9 ] | [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 - ] * [ a - z A - Z 0 - 9 ] ) (?: (?: \. (?: [ a - z A - Z 0 - 9 ] | [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 - ] * [ a - z A - Z 0 - 9 ] ) ) + ) ? (?: : [ 0 - 9 ] + ) ? \/ ) ? [ a - z 0 - 9 ] + (?: (?: (?: [ . _ ] | _ _ | [ - ] * ) [ a - z 0 - 9 ] + ) + ) ? (?: (?: \/ [ a - z 0 - 9 ] + (?: (?: (?: [ . _ ] | _ _ | [ - ] * ) [ a - z 0 - 9 ] + ) + ) ? ) + ) ? ) (?: : ( [ \w ] [ \w . - ] { 0 , 127 } ) ) ? (?: @ ( [ A - Z a - z ] [ A - Z a - z 0 - 9 ] * (?: [ - _ + . ] [ A - Z a - z ] [ A - Z a - z 0 - 9 ] * ) * [: ] [ A - F a - f 0 - 9 ] { 32 , } ) ) ? $ / ig;
4848 const groups = regex . exec ( imageWithTag ) ;
49-
49+
5050 if ( ! groups ) {
5151 logger . error ( { image : imageWithTag } , 'Image with tag is malformed, cannot extract valid parts' ) ;
52- return { imageName : imageWithTag , imageTag : '' , imageDigest : '' } ;
52+ return { imageName : imageWithTag , imageTag : '' } ;
5353 }
5454
5555 const IMAGE_NAME_GROUP = 1 ;
@@ -58,8 +58,8 @@ export function getImageParts(imageWithTag: string) : {imageName: string, imageT
5858
5959 return {
6060 imageName : groups [ IMAGE_NAME_GROUP ] ,
61- imageTag : groups [ IMAGE_TAG_GROUP ] || '' ,
62- imageDigest : groups [ IMAGE_DIGEST_GROUP ] || '' ,
61+ // prefer tag over digest
62+ imageTag : groups [ IMAGE_TAG_GROUP ] || groups [ IMAGE_DIGEST_GROUP ] || '' ,
6363 } ;
6464}
6565
@@ -78,7 +78,7 @@ export async function scanImages(images: IPullableImage[]): Promise<IScanResult[
7878
7979 const dockerfile = undefined ;
8080
81- for ( const { imageName, fileSystemPath, imageWithDigest } of images ) {
81+ for ( const { imageName, fileSystemPath} of images ) {
8282 try {
8383 const staticAnalysisOptions = constructStaticAnalysisOptions ( fileSystemPath ) ;
8484 const options : IPluginOptions = {
@@ -92,18 +92,16 @@ export async function scanImages(images: IPullableImage[]): Promise<IScanResult[
9292 throw Error ( 'Unexpected empty result from docker-plugin' ) ;
9393 }
9494
95- const imageParts = getImageParts ( imageName ) ;
95+ const imageParts : { imageName : string , imageTag : string } = getImageParts ( imageName ) ;
9696
9797 result . imageMetadata = {
9898 image : imageParts . imageName ,
9999 imageTag : imageParts . imageTag ,
100- imageDigest : getImageParts ( imageWithDigest ) . imageDigest ,
101100 } ;
102101
103102 scannedImages . push ( {
104103 image : imageParts . imageName ,
105104 imageWithTag : imageName ,
106- imageWithDigest : imageWithDigest ,
107105 pluginResult : result ,
108106 } ) ;
109107 } catch ( error ) {
0 commit comments