-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathspec.cpp
411 lines (317 loc) · 10.3 KB
/
spec.cpp
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include <stout/foreach.hpp>
#include <stout/json.hpp>
#include <stout/none.hpp>
#include <stout/protobuf.hpp>
#include <stout/strings.hpp>
#include <mesos/docker/spec.hpp>
using std::ostream;
using std::string;
using std::vector;
namespace docker {
namespace spec {
// TODO(jieyu): Use regex to parse and verify the reference.
Try<ImageReference> parseImageReference(const string& _s)
{
ImageReference reference;
string s(_s);
// Extract the digest.
if (strings::contains(s, "@")) {
vector<string> split = strings::split(s, "@");
if (split.size() != 2) {
return Error("Multiple '@' symbols found");
}
s = split[0];
reference.set_digest(split[1]);
}
// Remove the tag. We need to watch out for a
// host:port registry, which also contains ':'.
if (strings::contains(s, ":")) {
vector<string> split = strings::split(s, ":");
// The tag must be the last component. If a slash is
// present there is a registry port and no tag.
if (!strings::contains(split.back(), "/")) {
reference.set_tag(split.back());
split.pop_back();
s = strings::join(":", split);
}
}
// Extract the registry and repository. The first component can
// either be the registry, or the first part of the repository!
// We resolve this ambiguity using the same hacks used in the
// docker code ('.', ':', 'localhost' indicate a registry).
vector<string> split = strings::split(s, "/", 2);
if (split.size() == 1) {
reference.set_repository(s);
} else if (strings::contains(split[0], ".") ||
strings::contains(split[0], ":") ||
split[0] == "localhost") {
reference.set_registry(split[0]);
reference.set_repository(split[1]);
} else {
reference.set_repository(s);
}
return reference;
}
ostream& operator<<(ostream& stream, const ImageReference& reference)
{
if (reference.has_registry()) {
stream << reference.registry() << "/" << reference.repository();
} else {
stream << reference.repository();
}
if (reference.has_digest()) {
stream << "@" << reference.digest();
} else if (reference.has_tag()) {
stream << ":" << reference.tag();
}
return stream;
}
Result<int> getRegistryPort(const string& registry)
{
if (registry.empty()) {
return None();
}
Option<int> port;
vector<string> split = strings::split(registry, ":", 2);
if (split.size() != 1) {
Try<int> numified = numify<int>(split[1]);
if (numified.isError()) {
return Error("Failed to numify '" + split[1] + "'");
}
port = numified.get();
}
return port;
}
Try<string> getRegistryScheme(const string& registry)
{
Result<int> port = getRegistryPort(registry);
if (port.isError()) {
return Error("Failed to get registry port: " + port.error());
} else if (port.isSome()) {
if (port.get() == 443) {
return "https";
}
if (port.get() == 80) {
return "http";
}
// NOTE: For a local registry, it's typically a http server.
const string host = getRegistryHost(registry);
if (host == "localhost" || host == "127.0.0.1") {
return "http";
}
}
return "https";
}
string getRegistryHost(const string& registry)
{
if (registry.empty()) {
return "";
}
vector<string> split = strings::split(registry, ":", 2);
return split[0];
}
Try<hashmap<string, Config::Auth>> parseAuthConfig(
const JSON::Object& _config)
{
// This function handles both old and new docker config format,
// e.g., '~/.docker/config.json' or '~/.dockercfg'.
Result<JSON::Object> auths = _config.find<JSON::Object>("auths");
if (auths.isError()) {
return Error("Failed to find 'auths' in docker config file: " +
auths.error());
}
const JSON::Object& config = auths.isSome()
? auths.get()
: _config;
hashmap<string, Config::Auth> result;
foreachpair (const string& key, const JSON::Value& value, config.values) {
if (!value.is<JSON::Object>()) {
return Error("Invalid JSON object '" + stringify(value) + "'");
}
Try<Config::Auth> auth =
protobuf::parse<Config::Auth>(value.as<JSON::Object>());
if (auth.isError()) {
return Error("Protobuf parse failed: " + auth.error());
}
// Assuming no duplicate registry url in docker config file,
// if there exists, overwrite it.
result[key] = auth.get();
}
return result;
}
Try<hashmap<string, Config::Auth>> parseAuthConfig(const string& s)
{
Try<JSON::Object> json = JSON::parse<JSON::Object>(s);
if (json.isError()) {
return Error("JSON parse failed: " + json.error());
}
return parseAuthConfig(json.get());
}
string parseAuthUrl(const string& _url)
{
string url = _url;
if (strings::startsWith(_url, "http://")) {
url = strings::remove(_url, "http://", strings::PREFIX);
} else if (strings::startsWith(_url, "https://")) {
url = strings::remove(_url, "https://", strings::PREFIX);
}
vector<string> parts = strings::split(url, "/", 2);
return parts[0];
}
namespace v1 {
Option<Error> validate(const ImageManifest& manifest)
{
// TODO(gilbert): Add validations.
return None();
}
Try<ImageManifest> parse(const JSON::Object& json)
{
Try<ImageManifest> manifest = protobuf::parse<ImageManifest>(json);
if (manifest.isError()) {
return Error("Protobuf parse failed: " + manifest.error());
}
Option<Error> error = validate(manifest.get());
if (error.isSome()) {
return Error(
"Docker v1 image manifest validation failed: " + error->message);
}
return manifest.get();
}
Try<ImageManifest> parse(const string& s)
{
Try<JSON::Object> json = JSON::parse<JSON::Object>(s);
if (json.isError()) {
return Error("JSON parse failed: " + json.error());
}
return parse(json.get());
}
} // namespace v1 {
namespace v2 {
Option<Error> validate(const ImageManifest& manifest)
{
// Validate required fields are present,
// e.g., repeated fields that has to be >= 1.
if (manifest.fslayers_size() <= 0) {
return Error("'fsLayers' field size must be at least one");
}
if (manifest.history_size() <= 0) {
return Error("'history' field size must be at least one");
}
// Verify that blobSum and v1Compatibility numbers are equal.
if (manifest.fslayers_size() != manifest.history_size()) {
return Error("The size of 'fsLayers' should be equal "
"to the size of 'history'");
}
// Verify 'fsLayers' field.
foreach (const ImageManifest::FsLayer& fslayer, manifest.fslayers()) {
const string& blobSum = fslayer.blobsum();
if (!strings::contains(blobSum, ":")) {
return Error("Incorrect 'blobSum' format: " + blobSum);
}
}
return None();
}
Try<ImageManifest> parse(const JSON::Object& json)
{
Try<ImageManifest> manifest = protobuf::parse<ImageManifest>(json);
if (manifest.isError()) {
return Error("Protobuf parse failed: " + manifest.error());
}
for (int i = 0; i < manifest->history_size(); i++) {
Try<JSON::Object> v1Compatibility =
JSON::parse<JSON::Object>(manifest->history(i).v1compatibility());
if (v1Compatibility.isError()) {
return Error("Parsing v1Compatibility JSON failed: " +
v1Compatibility.error());
}
Try<v1::ImageManifest> v1 = v1::parse(v1Compatibility.get());
if (v1.isError()) {
return Error("Parsing v1Compatibility protobuf failed: " + v1.error());
}
CHECK(!manifest->history(i).has_v1());
manifest->mutable_history(i)->mutable_v1()->CopyFrom(v1.get());
}
Option<Error> error = validate(manifest.get());
if (error.isSome()) {
return Error(
"Docker v2 image manifest validation failed: " + error->message);
}
return manifest.get();
}
Try<ImageManifest> parse(const string& s)
{
Try<JSON::Object> json = JSON::parse<JSON::Object>(s);
if (json.isError()) {
return Error("JSON parse failed: " + json.error());
}
return parse(json.get());
}
} // namespace v2 {
namespace v2_2 {
Option<Error> validate(const ImageManifest& manifest)
{
// Validate required fields are present,
// e.g., repeated fields that has to be >= 1.
if (manifest.layers_size() <= 0) {
return Error("'layers' field size must be at least one");
}
// Verify 'config' field.
if (!strings::contains(manifest.config().digest(), ":")) {
return Error("Incorrect 'digest' format: " + manifest.config().digest());
}
// Verify 'layers' field.
for (int i = 0; i < manifest.layers_size(); ++i) {
if (!strings::contains(manifest.layers(i).digest(), ":")) {
return Error("Incorrect 'digest' format: " + manifest.layers(i).digest());
}
}
if (manifest.schemaversion() != 2) {
return Error("'schemaVersion' field must be 2");
}
if (manifest.mediatype() !=
"application/vnd.docker.distribution.manifest.v2+json") {
return Error(
"'mediaType' field must be "
"'application/vnd.docker.distribution.manifest.v2+json'");
}
return None();
}
Try<ImageManifest> parse(const JSON::Object& json)
{
Try<ImageManifest> manifest = protobuf::parse<ImageManifest>(json);
if (manifest.isError()) {
return Error("Protobuf parse failed: " + manifest.error());
}
Option<Error> error = validate(manifest.get());
if (error.isSome()) {
return Error(
"Docker v2 s2 image manifest validation failed: " + error->message);
}
return manifest.get();
}
Try<ImageManifest> parse(const string& s)
{
Try<JSON::Object> json = JSON::parse<JSON::Object>(s);
if (json.isError()) {
return Error("JSON parse failed: " + json.error());
}
return parse(json.get());
}
} // namespace v2_2 {
} // namespace spec {
} // namespace docker {