@@ -4,7 +4,7 @@ import { DepGraph, legacy } from '@snyk/dep-graph';
44
55import { logger } from '../../common/logger' ;
66import { pull as skopeoCopy , getDestinationForImage } from './skopeo' ;
7- import { IPullableImage , IScanImage } from './types' ;
7+ import { IPullableImage , IScanImage , SkopeoRepositoryType } from './types' ;
88import { IScanResult } from '../types' ;
99import {
1010 buildDockerPropertiesOnDepTree ,
@@ -13,30 +13,43 @@ import {
1313 LegacyPluginResponse ,
1414} from './docker-plugin-shim' ;
1515
16- export async function pullImages ( images : IPullableImage [ ] ) : Promise < IPullableImage [ ] > {
17- const pulledImages : IPullableImage [ ] = [ ] ;
18-
19- for ( const image of images ) {
20- const { imageName, imageWithDigest, fileSystemPath } = image ;
21- if ( ! fileSystemPath ) {
22- continue ;
23- }
24-
16+ /*
17+ pulled images by skopeo archive repo type:
18+ 1st try to pull by docker archive image if it fail try to pull by oci archive
19+ */
20+ async function pullImageBySkopeoRepo ( imageToPull : IPullableImage ) : Promise < IPullableImage > {
21+ // Scan image by digest if exists, other way fallback tag
22+ const scanId = imageToPull . imageWithDigest ?? imageToPull . imageName ;
23+ imageToPull . skopeoRepoType = SkopeoRepositoryType . DockerArchive ;
2524 try {
26- // Scan image by digest if exists, other way fallback tag
27- const scanId = imageWithDigest ?? imageName ;
28- await skopeoCopy ( scanId , fileSystemPath ) ;
29- pulledImages . push ( image ) ;
30- } catch ( error ) {
31- logger . error ( { error , image : imageWithDigest } , 'failed to pull image' ) ;
25+ // copy docker archive image
26+ await skopeoCopy ( scanId , imageToPull . fileSystemPath , imageToPull . skopeoRepoType ) ;
27+ } catch ( dockerError ) {
28+ imageToPull . skopeoRepoType = SkopeoRepositoryType . OciArchive ;
29+ // copy oci archive image
30+ await skopeoCopy ( scanId , imageToPull . fileSystemPath , imageToPull . skopeoRepoType ) ;
3231 }
33- }
32+ return imageToPull ;
33+ }
3434
35- return pulledImages ;
35+ export async function pullImages ( images : IPullableImage [ ] ) : Promise < IPullableImage [ ] > {
36+ const pulledImages : IPullableImage [ ] = [ ] ;
37+ for ( const image of images ) {
38+ if ( ! image . fileSystemPath ) {
39+ continue ;
40+ }
41+ try {
42+ const pulledImage = await pullImageBySkopeoRepo ( image ) ;
43+ pulledImages . push ( pulledImage ) ;
44+ } catch ( error ) {
45+ logger . error ( { error, image : image . imageWithDigest ?? image . imageName } , 'failed to pull image docker/oci archive image' ) ;
46+ }
47+ }
48+ return pulledImages ;
3649}
3750
38- export function getImagesWithFileSystemPath ( images : IScanImage [ ] ) : IPullableImage [ ] {
39- return images . map ( ( image ) => ( { ...image , fileSystemPath : getDestinationForImage ( image . imageName ) } ) ) ;
51+ export function getImagesWithFileSystemPath ( images : IScanImage [ ] ) : { imageName : string ; skopeoRepoType : SkopeoRepositoryType ; fileSystemPath : string ; imageWithDigest ?: string } [ ] {
52+ return images . map ( ( image ) => ( { ...image , fileSystemPath : getDestinationForImage ( image . imageName ) } ) ) ;
4053}
4154
4255export async function removePulledImages ( images : IPullableImage [ ] ) : Promise < void > {
@@ -75,10 +88,11 @@ export function getImageParts(imageWithTag: string) : {imageName: string, imageT
7588export async function scanImages ( images : IPullableImage [ ] ) : Promise < IScanResult [ ] > {
7689 const scannedImages : IScanResult [ ] = [ ] ;
7790
78- for ( const { imageName, fileSystemPath, imageWithDigest } of images ) {
91+ for ( const { imageName, fileSystemPath, imageWithDigest, skopeoRepoType } of images ) {
7992 try {
8093 const shouldIncludeAppVulns = true ;
81- const dockerArchivePath = `docker-archive:${ fileSystemPath } ` ;
94+ const archiveType = skopeoRepoType == SkopeoRepositoryType . DockerArchive ?"docker-archive" :"oci-archive" ;
95+ const dockerArchivePath = `${ archiveType } :${ fileSystemPath } ` ;
8296
8397 const pluginResponse = await scan ( {
8498 path : dockerArchivePath ,
0 commit comments