/
DockerRegistryNamedAccountCredentials.groovy
331 lines (294 loc) · 10.4 KB
/
DockerRegistryNamedAccountCredentials.groovy
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.clouddriver.docker.registry.security
import com.fasterxml.jackson.annotation.JsonIgnore
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client.DockerRegistryClient
import com.netflix.spinnaker.clouddriver.docker.registry.exception.DockerRegistryConfigException
import com.netflix.spinnaker.clouddriver.security.AccountCredentials
import retrofit.RetrofitError
import java.util.concurrent.TimeUnit
class DockerRegistryNamedAccountCredentials implements AccountCredentials<DockerRegistryCredentials> {
static class Builder {
String accountName
String environment
String accountType
String address
String username
String password
File passwordFile
File dockerconfigFile
String email
int cacheThreads
long clientTimeoutMillis
int paginateSize
boolean trackDigests
boolean sortTagsByDate
List<String> repositories
List<String> skip
Builder() {}
Builder accountName(String accountName) {
this.accountName = accountName
return this
}
Builder environment(String environment) {
this.environment = environment
return this
}
Builder accountType(String accountType) {
this.accountType = accountType
return this
}
Builder address(String address) {
this.address = address
return this
}
Builder username(String username) {
this.username = username
return this
}
Builder password(String address) {
this.password = address
return this
}
Builder passwordFile(String passwordFile) {
if (passwordFile) {
this.passwordFile = new File(passwordFile)
} else {
this.passwordFile = null
}
return this
}
Builder dockerconfigFile(String dockerconfigFile) {
if (dockerconfigFile) {
this.dockerconfigFile = new File(dockerconfigFile)
} else {
this.dockerconfigFile = null
}
return this
}
Builder email(String email) {
this.email = email
return this
}
Builder cacheThreads(int cacheThreads) {
this.cacheThreads = cacheThreads
return this
}
Builder clientTimeoutMillis(long clientTimeoutMillis) {
this.clientTimeoutMillis = clientTimeoutMillis
return this
}
Builder paginateSize(int paginateSize) {
this.paginateSize = paginateSize
return this
}
Builder trackDigests(boolean trackDigests) {
this.trackDigests = trackDigests
return this
}
Builder sortTagsByDate(boolean sortTagsByDate) {
this.sortTagsByDate = sortTagsByDate
return this
}
Builder repositories(List<String> repositories) {
this.repositories = repositories
return this
}
Builder skip(List<String> skip) {
this.skip = skip
return this
}
DockerRegistryNamedAccountCredentials build() {
return new DockerRegistryNamedAccountCredentials(accountName,
environment,
accountType,
address,
username,
password,
passwordFile,
dockerconfigFile,
email,
repositories,
skip,
cacheThreads,
clientTimeoutMillis,
paginateSize,
trackDigests,
sortTagsByDate)
}
}
DockerRegistryNamedAccountCredentials(String accountName,
String environment,
String accountType,
String address,
String username,
String password,
File passwordFile,
File dockerconfigFile,
String email,
List<String> repositories,
List<String> skip,
int cacheThreads,
long clientTimeoutMillis,
int paginateSize,
boolean trackDigests,
boolean sortTagsByDate) {
this(accountName,
environment,
accountType,
address,
username,
password,
passwordFile,
dockerconfigFile,
email,
repositories,
skip,
cacheThreads,
clientTimeoutMillis,
paginateSize,
trackDigests,
sortTagsByDate,
null)
}
DockerRegistryNamedAccountCredentials(String accountName,
String environment,
String accountType,
String address,
String username,
String password,
File passwordFile,
File dockerconfigFile,
String email,
List<String> repositories,
List<String> skip,
int cacheThreads,
long clientTimeoutMillis,
int paginateSize,
boolean trackDigests,
boolean sortTagsByDate,
List<String> requiredGroupMembership) {
if (!accountName) {
throw new IllegalArgumentException("Docker Registry account must be provided with a name.")
}
this.accountName = accountName
this.environment = environment
this.accountType = accountType
this.passwordFile = passwordFile
this.cacheThreads = cacheThreads ?: 1
this.paginateSize = paginateSize ?: 100
this.clientTimeoutMillis = clientTimeoutMillis ?: TimeUnit.MINUTES.toMillis(1)
if (!address) {
throw new IllegalArgumentException("Docker Registry account $accountName must provide an endpoint address.");
} else {
int addressLen = address.length();
if (address[addressLen - 1] == '/') {
address = address.substring(0, addressLen - 1)
addressLen -= 1
}
// Strip the v2 endpoint, as the Docker API assumes it's not present.
if (address.endsWith('/v2')) {
address = address.substring(0, addressLen - 3)
}
}
this.address = address
if (address.startsWith('https://')) {
this.registry = address.substring('https://'.length())
} else if (address.startsWith('http://')) {
this.registry = address.substring('http://'.length())
} else {
this.registry = address
}
this.username = username
this.password = password
this.email = email
this.trackDigests = trackDigests
this.sortTagsByDate = sortTagsByDate
this.skip = skip ?: []
this.requiredGroupMembership = requiredGroupMembership == null ? Collections.emptyList() : Collections.unmodifiableList(requiredGroupMembership)
this.credentials = buildCredentials(repositories)
}
@JsonIgnore
List<String> getRepositories() {
return credentials.repositories
}
@Override
String getName() {
return accountName
}
@JsonIgnore
String getBasicAuth() {
return this.credentials?.client?.basicAuth ?: ""
}
@JsonIgnore
List<String> getTags(String repository) {
def tags = credentials.client.getTags(repository).tags
if(sortTagsByDate){
tags = tags.sort{ tag ->
credentials.client.getCreationDate(repository, tag)
}.reverse()
}
tags
}
String getV2Endpoint() {
return "$address/v2"
}
@Override
String getCloudProvider() {
return CLOUD_PROVIDER
}
private DockerRegistryCredentials buildCredentials(List<String> repositories) {
try {
DockerRegistryClient client = (new DockerRegistryClient.Builder())
.address(address)
.email(email)
.username(username)
.password(password)
.passwordFile(passwordFile)
.clientTimeoutMillis(clientTimeoutMillis)
.paginateSize(paginateSize)
.build()
return new DockerRegistryCredentials(client, repositories, trackDigests, skip, sortTagsByDate)
} catch (RetrofitError e) {
if (e.response?.status == 404) {
throw new DockerRegistryConfigException("No repositories specified for ${name}, and the provided endpoint ${address} does not support /_catalog.")
} else {
throw e
}
}
}
private static final String CLOUD_PROVIDER = "dockerRegistry"
final String accountName
final String environment
final String accountType
final String address
final String registry
@JsonIgnore
final String username
@JsonIgnore
final String password
final File passwordFile
final String email
final boolean trackDigests
final boolean sortTagsByDate
final int cacheThreads
final long clientTimeoutMillis
final int paginateSize
@JsonIgnore
final DockerRegistryCredentials credentials
final List<String> requiredGroupMembership
final List<String> skip
}