-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPacker.cs
317 lines (283 loc) · 13 KB
/
Packer.cs
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
// Copyright The ORAS Authors.
// 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.
using OrasProject.Oras.Oci;
using OrasProject.Oras.Content;
using OrasProject.Oras.Exceptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace OrasProject.Oras;
public static class Packer
{
/// <summary>
/// ErrInvalidDateTimeFormat is returned
/// when "org.opencontainers.artifact.created" or "org.opencontainers.image.created" is provided,
/// but its value is not in RFC 3339 format.
/// Reference: https://www.rfc-editor.org/rfc/rfc3339#section-5.6
/// </summary>
private const string _errInvalidDateTimeFormat = "invalid date and time format";
/// <summary>
/// ErrMissingArtifactType is returned
/// when ManifestVersion is Version1_1 and artifactType is empty
/// and the config media type is set to "application/vnd.oci.empty.v1+json".
/// </summary>
private const string _errMissingArtifactType = "missing artifact type";
public const string MediaTypeUnknownConfig = "application/vnd.unknown.config.v1+json";
public const string MediaTypeUnknownArtifact = "application/vnd.unknown.artifact.v1";
/// <summary>
/// ManifestVersion represents the manifest version used for PackManifest
/// </summary>
public enum ManifestVersion
{
// Version1_0 represents the OCI Image Manifest defined in image-spec v1.0.2.
// Reference: https://github.com/opencontainers/image-spec/blob/v1.0.2/manifest.md
Version1_0 = 1,
// Version1_1 represents the OCI Image Manifest defined in image-spec v1.1.0.
// Reference: https://github.com/opencontainers/image-spec/blob/v1.1.0/manifest.md
Version1_1 = 2
}
/// <summary>
/// mediaTypeRegex checks the format of media types.
/// References:
/// - https://github.com/opencontainers/image-spec/blob/v1.1.0/schema/defs-descriptor.json#L7
/// - https://datatracker.ietf.org/doc/html/rfc6838#section-4.2
/// </summary>
private static readonly Regex _mediaTypeRegex = new Regex(@"^[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}/[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}(\+json)?$", RegexOptions.Compiled);
/// <summary>
/// PackManifest generates an OCI Image Manifestbased on the given parameters
/// and pushes the packed manifest to a content storage/registry using pusher.
/// The version of the manifest to be packed is determined by manifestVersion
/// (Recommended value: Version1_1).
/// - If manifestVersion is <b><c>Version1_1</c></b>
/// artifactType MUST NOT be empty unless PackManifestOptions.ConfigDescriptor is specified.
/// - If manifestVersion is <b><c>Version1_0</c></b>
/// if PackManifestOptions.ConfigDescriptor is null, artifactType will be used as the
/// config media type; if artifactType is empty,
/// "application/vnd.unknown.config.v1+json" will be used.
/// if PackManifestOptions.ConfigDescriptor is NOT null, artifactType will be ignored.
///
/// artifactType and PackManifestOptions.ConfigDescriptor.MediaType MUST comply with RFC 6838.
///
/// Each time when PackManifest is called, if a time stamp is not specified, a new time
/// stamp is generated in the manifest annotations with the key ocispec.AnnotationCreated
/// (i.e. "org.opencontainers.image.created"). To make PackManifest reproducible,
/// set the key ocispec.AnnotationCreated to a fixed value in
/// opts.Annotations. The value MUST conform to RFC 3339.
///
/// If succeeded, returns a descriptor of the packed manifest.
/// </summary>
/// <param name="pusher"></param>
/// <param name="version"></param>
/// <param name="artifactType"></param>
/// <param name="options"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static async Task<Descriptor> PackManifestAsync(
IPushable pusher,
ManifestVersion version,
string? artifactType,
PackManifestOptions options = default,
CancellationToken cancellationToken = default)
{
switch (version)
{
case ManifestVersion.Version1_0:
return await PackManifestV1_0Async(pusher, artifactType, options, cancellationToken).ConfigureAwait(false);
case ManifestVersion.Version1_1:
return await PackManifestV1_1Async(pusher, artifactType, options, cancellationToken).ConfigureAwait(false);
default:
throw new NotSupportedException($"ManifestVersion({version}) is not supported");
}
}
/// <summary>
/// Pack version 1.0 manifest
/// </summary>
/// <param name="pusher"></param>
/// <param name="artifactType"></param>
/// <param name="options"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
private static async Task<Descriptor> PackManifestV1_0Async(IPushable pusher, string? artifactType, PackManifestOptions options = default, CancellationToken cancellationToken = default)
{
if (options.Subject != null)
{
throw new NotSupportedException("Subject is not supported for manifest version 1.0.");
}
Descriptor configDescriptor;
if (options.Config != null)
{
ValidateMediaType(options.Config.MediaType);
configDescriptor = options.Config;
}
else
{
if (string.IsNullOrEmpty(artifactType))
{
artifactType = MediaTypeUnknownConfig;
}
ValidateMediaType(artifactType);
configDescriptor = await PushCustomEmptyConfigAsync(pusher, artifactType, options.ConfigAnnotations, cancellationToken).ConfigureAwait(false);
}
var annotations = EnsureAnnotationCreated(options.ManifestAnnotations, "org.opencontainers.image.created");
var manifest = new Manifest
{
SchemaVersion = 2,
MediaType = Oci.MediaType.ImageManifest,
Config = configDescriptor,
Layers = options.Layers ?? new List<Descriptor>(),
Annotations = annotations
};
return await PushManifestAsync(pusher, manifest, manifest.MediaType, manifest.Config.MediaType, manifest.Annotations, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Pack version 1.1 manifest
/// </summary>
/// <param name="pusher"></param>
/// <param name="artifactType"></param>
/// <param name="options"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="MissingArtifactTypeException"></exception>
private static async Task<Descriptor> PackManifestV1_1Async(IPushable pusher, string? artifactType, PackManifestOptions options = default, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(artifactType) && (options.Config == null || options.Config.MediaType == MediaType.EmptyJson))
{
throw new MissingArtifactTypeException(_errMissingArtifactType);
} else if (!string.IsNullOrEmpty(artifactType)) {
ValidateMediaType(artifactType);
}
Descriptor configDescriptor;
if (options.Config != null)
{
ValidateMediaType(options.Config.MediaType);
configDescriptor = options.Config;
}
else
{
configDescriptor = Descriptor.Empty;
options.Config = configDescriptor;
var configBytes = new byte[] { 0x7B, 0x7D };
await PushIfNotExistAsync(pusher, configDescriptor, configBytes, cancellationToken).ConfigureAwait(false);
}
if (options.Layers == null || options.Layers.Count == 0)
{
options.Layers ??= new List<Descriptor>();
// use the empty descriptor as the single layer
options.Layers.Add(Descriptor.Empty);
}
var annotations = EnsureAnnotationCreated(options.ManifestAnnotations, "org.opencontainers.image.created");
var manifest = new Manifest
{
SchemaVersion = 2,
MediaType = MediaType.ImageManifest,
ArtifactType = artifactType,
Subject = options.Subject,
Config = options.Config,
Layers = options.Layers,
Annotations = annotations
};
return await PushManifestAsync(pusher, manifest, manifest.MediaType, manifest.ArtifactType, manifest.Annotations, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Save manifest to local or remote storage
/// </summary>
/// <param name="pusher"></param>
/// <param name="manifest"></param>
/// <param name="mediaType"></param>
/// <param name="artifactType"></param>
/// <param name="annotations"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private static async Task<Descriptor> PushManifestAsync(IPushable pusher, object manifest, string mediaType, string? artifactType, IDictionary<string, string>? annotations, CancellationToken cancellationToken = default)
{
var manifestJson = JsonSerializer.SerializeToUtf8Bytes(manifest);
var manifestDesc = Descriptor.Create(manifestJson, mediaType);
manifestDesc.ArtifactType = artifactType;
manifestDesc.Annotations = annotations;
await pusher.PushAsync(manifestDesc, new MemoryStream(manifestJson), cancellationToken).ConfigureAwait(false);
return manifestDesc;
}
/// <summary>
/// Validate manifest media type
/// </summary>
/// <param name="mediaType"></param>
/// <exception cref="InvalidMediaTypeException"></exception>
private static void ValidateMediaType(string mediaType)
{
if (!_mediaTypeRegex.IsMatch(mediaType))
{
throw new InvalidMediaTypeException($"{mediaType} is an invalid media type");
}
}
/// <summary>
/// Push an empty configure with unknown media type to storage
/// </summary>
/// <param name="pusher"></param>
/// <param name="mediaType"></param>
/// <param name="annotations"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private static async Task<Descriptor> PushCustomEmptyConfigAsync(IPushable pusher, string mediaType, IDictionary<string, string>? annotations, CancellationToken cancellationToken = default)
{
var configBytes = JsonSerializer.SerializeToUtf8Bytes(new { });
var configDescriptor = Descriptor.Create(configBytes, mediaType);
configDescriptor.Annotations = annotations;
await PushIfNotExistAsync(pusher, configDescriptor, configBytes, cancellationToken).ConfigureAwait(false);
return configDescriptor;
}
/// <summary>
/// Push data to local or remote storage
/// </summary>
/// <param name="pusher"></param>
/// <param name="descriptor"></param>
/// <param name="data"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private static async Task PushIfNotExistAsync(IPushable pusher, Descriptor descriptor, byte[] data, CancellationToken cancellationToken = default)
{
await pusher.PushAsync(descriptor, new MemoryStream(data), cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Validate the value of the key in annotations should have correct timestamp format.
/// If the key is missed, the key and current timestamp is added to the annotations
/// </summary>
/// <param name="annotations"></param>
/// <param name="key"></param>
/// <returns></returns>
/// <exception cref="InvalidDateTimeFormatException"></exception>
private static IDictionary<string, string> EnsureAnnotationCreated(IDictionary<string, string>? annotations, string key)
{
if (annotations == null)
{
annotations = new Dictionary<string, string>();
}
string? value;
if (annotations.TryGetValue(key, out value))
{
if (!DateTime.TryParse(value, out _))
{
throw new InvalidDateTimeFormatException(_errInvalidDateTimeFormat);
}
return annotations;
}
var copiedAnnotations = new Dictionary<string, string>(annotations);
copiedAnnotations[key] = DateTime.UtcNow.ToString("o");
return copiedAnnotations;
}
}