Skip to content

Commit 1af651f

Browse files
author
Amir Moualem
authored
Merge pull request #298 from snyk/fix/ecr-image-determination
Fix/ecr image determination
2 parents f4e3c9e + e54b745 commit 1af651f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/scanner/images/credentials.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import * as aws from 'aws-sdk';
33
import logger = require('../../common/logger');
44

55
export async function getSourceCredentials(imageSource: string): Promise<string | undefined> {
6-
// TODO is this the best way we can determine the image's source?
7-
if (imageSource.indexOf('.ecr.') !== -1) {
6+
if (isEcrSource(imageSource)) {
87
const ecrRegion = ecrRegionFromFullImageName(imageSource);
98
return getEcrCredentials(ecrRegion);
109
}
1110
return undefined;
1211
}
1312

13+
export function isEcrSource(imageSource: string): boolean {
14+
// this regex tests the image source against the template:
15+
// <SOMETHING>.dkr.ecr.<SOMETHING>.amazonaws.com/<SOMETHING>
16+
const ecrImageRegex = new RegExp('\.dkr\.ecr\..*\.amazonaws\.com\/', 'i');
17+
return ecrImageRegex.test(imageSource);
18+
}
19+
1420
function getEcrCredentials(region: string): Promise<string> {
1521
return new Promise(async (resolve, reject) => {
1622
const ecr = new aws.ECR({region});

test/unit/scanner/image-registry-credentials.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ tap.test('ecrRegionFromFullImageName()', async (t) => {
1616
t.throws(() => {credentials.ecrRegionFromFullImageName('aws_account_id.dkr.ecr.amazonaws.com/my-web-app:latest');}, 'throws on badly formatted images');
1717
t.throws(() => {credentials.ecrRegionFromFullImageName('aws_account_id.dkr.ecr.region.amazonaws.com');}, 'throws on badly formatted images');
1818
});
19+
20+
tap.test('isEcrSource()', async (t) => {
21+
const sourceCredentialsForRandomImageName = credentials.isEcrSource('derka');
22+
t.equals(sourceCredentialsForRandomImageName, false, 'unidentified image source is not ECR');
23+
24+
const sourceCredentialsForInvalidEcrImage = credentials.isEcrSource('derka.ecr.derka');
25+
t.equals(sourceCredentialsForInvalidEcrImage, false, 'image just with .ecr. is not considered from ECR');
26+
27+
const sourceCredentialsForEcrImage = credentials.isEcrSource('aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app:latest');
28+
t.equals(sourceCredentialsForEcrImage, true, 'correct ECR template');
29+
30+
const sourceCredentialsForEcrImageWithRepo = credentials.isEcrSource('a291964488713.dkr.ecr.us-east-2.amazonaws.com/snyk/debian:10');
31+
t.equals(sourceCredentialsForEcrImageWithRepo, true, 'correct ECR template');
32+
33+
const sourceCredentialsForEcrImageMixedCase = credentials.isEcrSource('aws_account_id.dKr.ecR.region.amazonAWS.cOm/my-web-app:latest');
34+
t.equals(sourceCredentialsForEcrImageMixedCase, true, 'correct ECR template');
35+
});

0 commit comments

Comments
 (0)