-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ecr.ts
54 lines (50 loc) · 1.88 KB
/
ecr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { ECR, ECRClientConfig } from '@aws-sdk/client-ecr';
import { logger } from '../../../logger';
import type { HostRule } from '../../../types';
import type { HttpError } from '../../../util/http';
import type { HttpResponse } from '../../../util/http/types';
import { regEx } from '../../../util/regex';
import { addSecretForSanitizing } from '../../../util/sanitize';
export const ecrRegex = regEx(/\d+\.dkr\.ecr(?:-fips)?\.([-a-z0-9]+)\.amazonaws\.com/);
export const ecrPublicRegex = regEx(/public\.ecr\.aws/);
export async function getECRAuthToken(
region: string,
opts: HostRule,
): Promise<string | null> {
const config: ECRClientConfig = { region };
if (opts.username && opts.password) {
config.credentials = {
accessKeyId: opts.username,
secretAccessKey: opts.password,
...(opts.token && { sessionToken: opts.token }),
};
}
const ecr = new ECR(config);
try {
const data = await ecr.getAuthorizationToken({});
const authorizationToken = data?.authorizationData?.[0]?.authorizationToken;
if (authorizationToken) {
// sanitize token
addSecretForSanitizing(authorizationToken);
return authorizationToken;
}
logger.warn(
'Could not extract authorizationToken from ECR getAuthorizationToken response',
);
} catch (err) {
logger.trace({ err }, 'err');
logger.warn('ECR getAuthorizationToken error');
}
return null;
}
export function isECRMaxResultsError(err: HttpError): boolean {
const resp = err.response as HttpResponse<any> | undefined;
return !!(
resp?.statusCode === 405 &&
resp.headers?.['docker-distribution-api-version'] &&
// https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_DescribeRepositories.html#ECR-DescribeRepositories-request-maxResults
resp.body?.['errors']?.[0]?.message?.includes(
'Member must have value less than or equal to 1000',
)
);
}