forked from monken/aws-ecr-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.js
109 lines (99 loc) · 2.61 KB
/
lambda.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const AWS = require("aws-sdk");
const ecr = new AWS.ECR();
const handledErrors = {
AccessDeniedException: {
statusCode: 403,
error: {
code: "DENIED",
message: "requested access to the resource is denied"
}
},
RepositoryNotFoundException: {
statusCode: 404,
error: {
code: "NAME_UNKNOWN",
message: "repository name not known to registry"
}
}
};
const actions = {
manifests: {
head: async (name, ref) => {
const res = await actions.manifests.get(name, ref);
return {
...res,
body: ""
};
},
get: async (name, ref) => {
// matching is done according to https://docs.docker.com/registry/spec/api/#content-digests
// however the algorithm regex seems broken in their docs
// matching to a word should be enough to catch it
if (ref.match(/^\w+:[A-Fa-f0-9]+$/)) {
selectorObj = { imageDigest: ref };
} else {
selectorObj = { imageTag: ref };
}
const { images } = await ecr
.batchGetImage({
imageIds: [selectorObj],
repositoryName: name
})
.promise();
const image = images[0];
if (!image) return { statusCode: 404, body: "{}" };
const manifest = JSON.parse(image.imageManifest);
return {
statusCode: 200,
body: image.imageManifest,
multiValueHeaders: {
"Content-Type": [manifest.mediaType]
}
};
}
},
blobs: {
get: async (name, digest) => {
const url = await ecr
.getDownloadUrlForLayer({
repositoryName: name,
layerDigest: digest
})
.promise();
return {
statusCode: 307,
headers: {
Location: url.downloadUrl,
"Docker-Content-Digest": url.layerDigest
}
};
}
}
};
exports.handler = async event => {
console.log(JSON.stringify(event));
const context = event.requestContext;
const segments = context.path.split(/\//);
const [action, reference] = segments.slice(segments.length - 2);
const name = segments.slice(2, segments.length - 2).join("/");
let fn;
try {
fn = actions[action][context.httpMethod.toLowerCase()];
} catch (e) {
return { statusCode: 404 };
}
return fn(name, reference).catch(e => {
if (Object.keys(handledErrors).includes(e.name)) {
const errResponse = handledErrors[e.name];
return {
statusCode: errResponse.statusCode,
body: JSON.stringify({
errors: [errResponse.error]
})
};
} else {
console.log(e.toString ? e.toString() : e);
throw e;
}
});
};