-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathECR.js
More file actions
252 lines (237 loc) · 7.41 KB
/
ECR.js
File metadata and controls
252 lines (237 loc) · 7.41 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require('rubico/global')
const crypto = require('crypto')
const HTTP = require('./HTTP')
const AmzDate = require('./internal/AmzDate')
const AwsAuthorization = require('./internal/AwsAuthorization')
const AwsError = require('./internal/AwsError')
const userAgent = require('./userAgent')
const Readable = require('./Readable')
/**
* @name ECR
*
* @docs
* ```coffeescript [specscript]
* new ECR(options {
* accessKeyId: string,
* secretAccessKey: string,
* region: string,
* }) -> ecr ECR
* ```
* Presidium ECR client for [Amazon ECR](https://aws.amazon.com/ecr/).
*
* Arguments:
* * `options`
* * `accessKeyId` - long term credential (ID) of an [IAM](https://aws.amazon.com/iam/) user.
* * `secretAccessKey` - long term credential (secret) of an [IAM](https://aws.amazon.com/iam/) user.
* * `region` - geographic location of data center cluster, e.g. `us-east-1` or `us-west-2`. [Full list of AWS regions](https://docs.aws.amazon.com/global-infrastructure/latest/regions/aws-regions.html#available-regions)
*
* * Return:
* * `ecr` - an instance of the Presidium ECR client.
*
* ```javascript
* const awsCreds = AwsCredentials('my-profile')
* awsCreds.region = 'us-east-1'
*
* const ecr = new ECR({ ...awsCreds })
* ```
*/
class ECR {
constructor(options) {
this.accessKeyId = options.accessKeyId ?? ''
this.secretAccessKey = options.secretAccessKey ?? ''
this.region = options.region ?? ''
this.apiVersion = '2015-09-21'
this.endpoint = `ecr.${this.region}.amazonaws.com`
this.protocol = 'https'
this.http = new HTTP(`${this.protocol}://${this.endpoint}`)
}
/**
* @name _awsRequest
*
* @docs
* ```coffeescript [specscript]
* module http 'https://nodejs.org/api/http.html'
*
* _awsRequest(
* method string,
* url string,
* action string,
* payload string
* ) -> response Promise<http.ServerResponse>
* ```
*/
_awsRequest(method, url, action, payload) {
const amzDate = AmzDate()
const amzTarget = `AmazonEC2ContainerRegistry_V${this.apiVersion.replace(/-/g, '')}.${action}`
const headers = {
'Host': this.endpoint,
'Accept-Encoding': 'identity',
'Content-Length': Buffer.byteLength(payload, 'utf8'),
'User-Agent': userAgent,
'Content-Type': 'application/x-amz-json-1.1',
'Authorization': 'AUTHPARAMS',
'X-Amz-Date': amzDate,
'X-Amz-Target': amzTarget
}
const amzHeaders = {}
for (const key in headers) {
if (key.toLowerCase().startsWith('x-amz')) {
amzHeaders[key] = headers[key]
}
}
headers['Authorization'] = AwsAuthorization({
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey,
region: this.region,
method,
endpoint: this.endpoint,
protocol: this.protocol,
canonicalUri: url,
serviceName: 'ecr',
payloadHash:
crypto.createHash('sha256').update(payload, 'utf8').digest('hex'),
expires: 300,
queryParams: new URLSearchParams(),
headers: {
'Host': this.endpoint,
...amzHeaders
}
})
return this.http[method](url, { headers, body: payload })
}
/**
* @name createRepository
*
* @docs
* ```coffeescript [specscript]
* module AWSECRDocs 'https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_Types.html'
*
* createRepository(repositoryName string, options {
* tags: Array<{ Key: string, Value: string }>,
* imageTagMutability: 'MUTABLE'|'IMMUTABLE', # defaults to 'MUTABLE'
* encryptionConfiguration: AWSECRDocs.EncryptionConfiguration,
* }) -> data Promise<{
* repository: AWSECRDocs.Repository,
* }>
* ```
*
* Creates an ECR repository.
*
* Arguments:
* * `repositoryName` - the name of the ECR repository to create.
* * `options`
* * `tags` - user-defined metadata that will be applied to the ECR repository.
* * `imageTagMutability` - the tag mutability setting for the ECR repository.
* * `encryptionConfiguration` - [`AWSECRDocs.EncryptionConfiguration`](https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_EncryptionConfiguration.html) - the encryption configuration for the ECR repository.
*
* Return:
* * `data`
* * `repository` - [`AWSECRDocs.Repository`](https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_Repository.html) - the ECR repository data.
*
* `imageTagMutability` values:
* * `MUTABLE` - image tags may be overwritten.
* * `IMMUTABLE` - image tags cannot be overwritten.
*
* ```javascript
* const awsCreds = AwsCredentials('my-profile')
* awsCreds.region = 'us-east-1'
*
* const ecr = new ECR({ ...awsCreds })
*
* await ecr.createRepository('my-repository')
* ```
*/
async createRepository(repositoryName, options = {}) {
const payload = JSON.stringify({
repositoryName,
...options
})
const response = await this._awsRequest('POST', '/', 'CreateRepository', payload)
if (response.ok) {
const data = await Readable.JSON(response)
return data
}
throw new AwsError(await Readable.Text(response), response.status)
}
/**
* @name deleteRepository
*
* @docs
* ```coffeescript [specscript]
* module AWSECRDocs 'https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_Types.html'
*
* deleteRepository(repositoryName string, options {
* force: boolean
* }) -> data Promise<{
* repository: AWSECRDocs.Repository,
* }>
* ```
*
* Deletes an ECR repository.
*
* Arguments:
* * `repositoryName` - the name of the ECR repository to delete.
* * `options`
* * `force` - whether to force delete the contents of the ECR repository.
*
* Return:
* * `data`
* * `repository` - [`AWSECRDocs.Repository`](https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_Repository.html) - the deleted ECR repository data.
*
* ```javascript
* const awsCreds = AwsCredentials('my-profile')
* awsCreds.region = 'us-east-1'
*
* const ecr = new ECR({ ...awsCreds })
*
* await ecr.deleteRepository('my-repository')
* ```
*/
async deleteRepository(repositoryName, options = {}) {
const payload = JSON.stringify({
repositoryName,
...options
})
const response = await this._awsRequest('POST', '/', 'DeleteRepository', payload)
if (response.ok) {
const data = await Readable.JSON(response)
return data
}
throw new AwsError(await Readable.Text(response), response.status)
}
/**
* @name getAuthorizationToken
*
* @docs
* ```coffeescript [specscript]
* getAuthorizationToken() -> authToken Promise<string>
* ```
*
* Gets an authorization token from Amazon ECR.
*
* Arguments:
* * (none)
*
* Return:
* * `authToken` - the ECR authorization token
*
* ```javascript
* const awsCreds = AwsCredentials('my-profile')
* awsCreds.region = 'us-east-1'
*
* const ecr = new ECR({ ...awsCreds })
*
* const authToken = await ecr.getAuthorizationToken()
* ```
*/
async getAuthorizationToken() {
const response = await this._awsRequest('POST', '/', 'GetAuthorizationToken', '{}')
if (response.ok) {
const data = await Readable.JSON(response)
const authToken = data.authorizationData[0].authorizationToken
return authToken
}
throw new AwsError(await Readable.Text(response), response.status)
}
}
module.exports = ECR