forked from 2iq/aws-ecr-create-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (122 loc) · 3.86 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const core = require('@actions/core');
const github = require('@actions/github');
const {
ECR,
DescribeRepositoriesCommand,
CreateRepositoryCommand,
SetRepositoryPolicyCommand
} = require('@aws-sdk/client-ecr');
const ecr = new ECR();
const executeGitHubAction = async () => {
const ecrRepoName = core.getInput('ecr-name') || github.context.payload.repository.name;
const pullAccountIds = core.getInput('pull-account-ids') || '';
let wantedEcrRepo;
try {
core.info(`Checking ECR repo '${ecrRepoName}'.`);
wantedEcrRepo = await ecr.send(new DescribeRepositoriesCommand({ repositoryNames: [ecrRepoName] }))
wantedEcrRepo = wantedEcrRepo.repositories[0];
core.info(`ECR repo '${ecrRepoName}' already exists.`);
} catch (error) {
if (error.name !== 'RepositoryNotFoundException') {
throw error;
}
core.info(`ECR repo '${ecrRepoName}' does not exist.`);
const tags = [ { "Key": "c-service", "Value": ecrRepoName }];
wantedEcrRepo = await createNewEcrRepo(ecrRepoName, tags);
wantedEcrRepo = wantedEcrRepo.repository;
}
await updateRepositoryPolicyIfRequired(ecrRepoName, pullAccountIds);
core.setOutput('ecr-name', wantedEcrRepo.repositoryName);
core.setOutput('ecr-arn', wantedEcrRepo.repositoryArn);
core.setOutput('ecr-uri', wantedEcrRepo.repositoryUri);
};
const createNewEcrRepo = async (repoName, tags) => {
core.info(`Create new ECR repo '${repoName}'.`);
const params = {
"repositoryName": repoName,
"imageScanningConfiguration": {
"scanOnPush": false
},
"tags": tags
};
return await ecr.send(new CreateRepositoryCommand(params));
};
const updateRepositoryPolicyIfRequired = async (repoName, pullAccountIds) => {
const principals = pullAccountIds.split(',').map((id) => {
return `arn:aws:iam::${id.trim()}:root`;
});
principals.sort();
const lambdaSourceArns = pullAccountIds.split(',').map((id) => {
return `arn:aws:lambda:eu-west-1:${id.trim()}:function:*`
});
lambdaSourceArns.sort();
const policy = {
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowCrossAccountPull",
"Effect": "Allow",
"Principal": {
"AWS": principals,
},
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
},
{
"Sid": "LambdaECRImageRetrievalPolicy",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": [
"ecr:BatchGetImage",
"ecr:DeleteRepositoryPolicy",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:SetRepositoryPolicy"
],
"Condition": {
"StringLike": {
"aws:sourceArn": lambdaSourceArns
}
}
}
]
};
let newPolicyText = JSON.stringify(policy);
let updateRepositoryPolicy = false;
// Do we need to update repository policy?
try {
r = await ecr.getRepositoryPolicy({ repositoryName: repoName })
let oldPolicyText = JSON.stringify(JSON.parse(r.policyText))
// Update only if policy changed
if (oldPolicyText !== newPolicyText) {
updateRepositoryPolicy = true;
}
} catch (error) {
if (error.name !== 'RepositoryPolicyNotFoundException') {
throw error;
}
// OR if no Policy available yet
updateRepositoryPolicy = true;
}
if (updateRepositoryPolicy && principals.length > 0) {
core.info(`Set policy permission to ECR repo '${repoName}':`);
core.info(newPolicyText);
const params = {
"repositoryName": repoName,
"policyText": newPolicyText
};
await ecr.send(new SetRepositoryPolicyCommand(params));
}
}
module.exports = {
executeGitHubAction: executeGitHubAction
};
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'test') {
executeGitHubAction();
}