-
Notifications
You must be signed in to change notification settings - Fork 47
/
version.go
458 lines (392 loc) · 12.2 KB
/
version.go
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package versionsapi
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"path"
"regexp"
"strings"
"golang.org/x/mod/semver"
"github.com/edgelesssys/constellation/v2/internal/constants"
)
// ReleaseRef is the ref used for release versions.
const ReleaseRef = "-"
// Version represents a version. A version has a ref, stream, version string and kind.
//
// Notice that version is a meta object to the versions API and there isn't an
// actual corresponding object in the S3 bucket.
// Therefore, the version object doesn't have a URL or JSON path.
//
// Versions fields are private so the type can be used in other packages by
// defining private interfaces.
type Version struct {
ref string
stream string
version string
kind VersionKind
}
// NewVersion creates a new Version object and validates it.
func NewVersion(ref, stream, version string, kind VersionKind) (Version, error) {
ver := Version{
ref: ref,
stream: stream,
version: version,
kind: kind,
}
if err := ver.Validate(); err != nil {
return Version{}, err
}
return ver, nil
}
// NewVersionFromShortPath creates a new Version from a version short path.
func NewVersionFromShortPath(shortPath string, kind VersionKind) (Version, error) {
ref, stream, version, err := parseShortPath(shortPath)
if err != nil {
return Version{}, err
}
ver := Version{
ref: ref,
stream: stream,
version: version,
kind: kind,
}
if err := ver.Validate(); err != nil {
return Version{}, err
}
return ver, nil
}
// Ref returns the ref of the version.
func (v Version) Ref() string {
return v.ref
}
// Stream returns the stream of the version.
func (v Version) Stream() string {
return v.stream
}
// Version returns the version string of the version.
func (v Version) Version() string {
return v.version
}
// Kind returns the kind of the version.
func (v Version) Kind() VersionKind {
return v.kind
}
// ShortPath returns the short path of the version.
func (v Version) ShortPath() string {
return shortPath(v.ref, v.stream, v.version)
}
// Validate validates the version.
func (v Version) Validate() error {
var retErr error
if err := ValidateRef(v.ref); err != nil {
retErr = errors.Join(retErr, err)
}
if err := ValidateStream(v.ref, v.stream); err != nil {
retErr = errors.Join(retErr, err)
}
if !semver.IsValid(v.version) {
retErr = errors.Join(retErr, fmt.Errorf("version %q is not a valid semantic version", v.version))
}
if semver.Canonical(v.version) != v.version {
retErr = errors.Join(retErr, fmt.Errorf("version %q is not a canonical semantic version", v.version))
}
if v.kind == VersionKindUnknown {
retErr = errors.Join(retErr, errors.New("version kind is unknown"))
}
return retErr
}
// Equal returns true if the versions are equal.
func (v Version) Equal(other Version) bool {
return v.ref == other.ref &&
v.stream == other.stream &&
v.version == other.version &&
v.kind == other.kind
}
// Major returns the major version corresponding to the version.
// For example, if the version is "v1.2.3", the major version is "v1".
func (v Version) Major() string {
return semver.Major(v.version)
}
// MajorMinor returns the major and minor version corresponding to the version.
// For example, if the version is "v1.2.3", the major and minor version is "v1.2".
func (v Version) MajorMinor() string {
return semver.MajorMinor(v.version)
}
// WithGranularity returns the version with the given granularity.
//
// For example, if the version is "v1.2.3" and the granularity is GranularityMajor,
// the returned version is "v1".
// This is a helper function for Major() and MajorMinor() and v.version.
// In case of an unknown granularity, an empty string is returned.
func (v Version) WithGranularity(gran Granularity) string {
switch gran {
case GranularityMajor:
return v.Major()
case GranularityMinor:
return v.MajorMinor()
case GranularityPatch:
return v.version
default:
return ""
}
}
// ListURL returns the URL of the list with the given granularity,
// this version is listed in.
// For example, assing GranularityMajor returns the URL of the versions list
// that maps the major version of this version to its minor version.
// In case of an unknown granularity, an empty string is returned.
func (v Version) ListURL(gran Granularity) string {
if gran == GranularityUnknown || gran == GranularityPatch {
return ""
}
return constants.CDNRepositoryURL + "/" + v.ListPath(gran)
}
// ListPath returns the path of the list with the given granularity,
// this version is listed in.
// For example, assing GranularityMajor returns the path of the versions list
// that maps the major version of this version to its minor version.
// In case of an unknown granularity, an empty string is returned.
func (v Version) ListPath(gran Granularity) string {
if gran == GranularityUnknown || gran == GranularityPatch {
return ""
}
return path.Join(
constants.CDNAPIPrefix,
"ref", v.ref,
"stream", v.stream,
"versions",
gran.String(), v.WithGranularity(gran),
v.kind.String()+".json",
)
}
// ArtifactsURL returns the URL to the artifacts stored for this version.
// The URL points to a directory.
func (v Version) ArtifactsURL(apiVer apiVersion) string {
return constants.CDNRepositoryURL + "/" + v.ArtifactPath(apiVer)
}
// ArtifactPath returns the path to the artifacts stored for this version.
// The path points to a directory.
func (v Version) ArtifactPath(apiVer apiVersion) string {
return path.Join(
constants.CDNAPIBase,
apiVer.String(),
"ref", v.ref,
"stream", v.stream,
v.version,
)
}
// VersionKind represents the kind of resource the version versions.
type VersionKind int
const (
// VersionKindUnknown is the default value for VersionKind.
VersionKindUnknown VersionKind = iota
// VersionKindImage is the kind for image versions.
VersionKindImage
// VersionKindCLI is the kind for CLI versions.
VersionKindCLI
)
// MarshalJSON marshals the VersionKind to JSON.
func (k VersionKind) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
}
// UnmarshalJSON unmarshals the VersionKind from JSON.
func (k *VersionKind) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*k = VersionKindFromString(s)
return nil
}
// String returns the string representation of the VersionKind.
func (k VersionKind) String() string {
switch k {
case VersionKindImage:
return "image"
case VersionKindCLI:
return "cli"
default:
return "unknown"
}
}
// VersionKindFromString returns the VersionKind for the given string.
func VersionKindFromString(s string) VersionKind {
switch strings.ToLower(s) {
case "image":
return VersionKindImage
case "cli":
return VersionKindCLI
default:
return VersionKindUnknown
}
}
// Granularity represents the granularity of a semantic version.
type Granularity int
const (
// GranularityUnknown is the default granularity.
GranularityUnknown Granularity = iota
// GranularityMajor is the granularity of a major version, e.g. "v1".
// Lists with granularity "major" map from a major version to a list of minor versions.
GranularityMajor
// GranularityMinor is the granularity of a minor version, e.g. "v1.0".
// Lists with granularity "minor" map from a minor version to a list of patch versions.
GranularityMinor
// GranularityPatch is the granularity of a patch version, e.g. "v1.0.0".
// There are no lists with granularity "patch".
GranularityPatch
)
// MarshalJSON marshals the granularity to JSON.
func (g Granularity) MarshalJSON() ([]byte, error) {
return json.Marshal(g.String())
}
// UnmarshalJSON unmarshals the granularity from JSON.
func (g *Granularity) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*g = GranularityFromString(s)
return nil
}
// String returns the string representation of the granularity.
func (g Granularity) String() string {
switch g {
case GranularityMajor:
return "major"
case GranularityMinor:
return "minor"
case GranularityPatch:
return "patch"
default:
return "unknown"
}
}
// GranularityFromString returns the granularity for the given string.
func GranularityFromString(s string) Granularity {
switch strings.ToLower(s) {
case "major":
return GranularityMajor
case "minor":
return GranularityMinor
case "patch":
return GranularityPatch
default:
return GranularityUnknown
}
}
var notAZ09Regexp = regexp.MustCompile("[^a-zA-Z0-9-]")
// CanonicalizeRef returns the canonicalized ref for the given ref.
func CanonicalizeRef(ref string) string {
if ref == ReleaseRef {
return ref
}
canRef := notAZ09Regexp.ReplaceAllString(ref, "-")
if canRef == ReleaseRef {
return "" // No ref should be cannonicalized to the release ref.
}
return canRef
}
// ValidateRef checks if the given ref is a valid ref.
// Canonicalize the ref before checking if it is valid.
func ValidateRef(ref string) error {
if ref == "" {
return errors.New("ref must not be empty")
}
if notAZ09Regexp.FindString(ref) != "" {
return errors.New("ref must only contain alphanumeric characters and dashes")
}
if strings.HasPrefix(ref, "refs-heads") {
return errors.New("ref must not start with 'refs-heads'")
}
return nil
}
// ValidateStream checks if the given stream is a valid stream for the given ref.
func ValidateStream(ref, stream string) error {
validReleaseStreams := []string{"stable", "console", "debug"}
validStreams := []string{"nightly", "console", "debug"}
if ref == ReleaseRef {
validStreams = validReleaseStreams
}
for _, validStream := range validStreams {
if stream == validStream {
return nil
}
}
return fmt.Errorf("stream %q is unknown or not supported on ref %q", stream, ref)
}
// MeasurementURL builds the measurement and signature URLs for the given version.
func MeasurementURL(version Version) (measurementURL, signatureURL *url.URL, err error) {
if version.kind != VersionKindImage {
return &url.URL{}, &url.URL{}, fmt.Errorf("kind %q is not supported", version.kind)
}
measurementPath, err := url.JoinPath(version.ArtifactsURL(APIV2), "image", constants.CDNMeasurementsFile)
if err != nil {
return &url.URL{}, &url.URL{}, fmt.Errorf("joining path for measurement: %w", err)
}
signaturePath, err := url.JoinPath(version.ArtifactsURL(APIV2), "image", constants.CDNMeasurementsSignature)
if err != nil {
return &url.URL{}, &url.URL{}, fmt.Errorf("joining path for signature: %w", err)
}
measurementURL, err = url.Parse(measurementPath)
if err != nil {
return &url.URL{}, &url.URL{}, fmt.Errorf("parsing path for measurement: %w", err)
}
signatureURL, err = url.Parse(signaturePath)
if err != nil {
return &url.URL{}, &url.URL{}, fmt.Errorf("parsing path for signature: %w", err)
}
return measurementURL, signatureURL, nil
}
var (
shortPathRegex = regexp.MustCompile(`^ref/([a-zA-Z0-9-]+)/stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
shortPathReleaseRegex = regexp.MustCompile(`^stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
)
func shortPath(ref, stream, version string) string {
var sp string
if ref != ReleaseRef {
return path.Join("ref", ref, "stream", stream, version)
}
if stream != "stable" {
return path.Join(sp, "stream", stream, version)
}
return version
}
func parseShortPath(shortPath string) (ref, stream, version string, err error) {
if shortPathRegex.MatchString(shortPath) {
matches := shortPathRegex.FindStringSubmatch(shortPath)
ref := matches[1]
if err := ValidateRef(ref); err != nil {
return "", "", "", err
}
stream := matches[2]
if err := ValidateStream(ref, stream); err != nil {
return "", "", "", err
}
version := matches[3]
if !semver.IsValid(version) {
return "", "", "", fmt.Errorf("invalid version %q", version)
}
return ref, stream, version, nil
}
if shortPathReleaseRegex.MatchString(shortPath) {
matches := shortPathReleaseRegex.FindStringSubmatch(shortPath)
stream := matches[1]
if err := ValidateStream(ref, stream); err != nil {
return "", "", "", err
}
version := matches[2]
if !semver.IsValid(version) {
return "", "", "", fmt.Errorf("invalid version %q", version)
}
return ReleaseRef, stream, version, nil
}
if semver.IsValid(shortPath) {
return ReleaseRef, "stable", shortPath, nil
}
return "", "", "", fmt.Errorf("invalid short path %q", shortPath)
}