2022-11-29 05:39:07 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
package versionsapi
|
2022-11-29 05:39:07 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2022-12-05 09:15:03 -05:00
|
|
|
"regexp"
|
2022-11-29 05:39:07 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
)
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
// List represents a list of versions for a kind of resource.
|
2022-11-29 05:39:07 -05:00
|
|
|
// It has a granularity of either "major" or "minor".
|
|
|
|
//
|
2022-12-01 04:26:35 -05:00
|
|
|
// For example, a List with granularity "major" could contain
|
2022-11-29 05:39:07 -05:00
|
|
|
// the base version "v1" and a list of minor versions "v1.0", "v1.1", "v1.2" etc.
|
2022-12-01 04:26:35 -05:00
|
|
|
// A List with granularity "minor" could contain the base version
|
2022-11-29 05:39:07 -05:00
|
|
|
// "v1.0" and a list of patch versions "v1.0.0", "v1.0.1", "v1.0.2" etc.
|
2022-12-01 04:26:35 -05:00
|
|
|
type List struct {
|
2022-12-05 09:15:03 -05:00
|
|
|
// Ref is the branch name the list belongs to.
|
|
|
|
Ref string `json:"ref,omitempty"`
|
2022-11-29 05:39:07 -05:00
|
|
|
// Stream is the update stream of the list.
|
2022-12-05 09:15:03 -05:00
|
|
|
Stream string `json:"stream,omitempty"`
|
2022-11-29 05:39:07 -05:00
|
|
|
// Granularity is the granularity of the base version of this list.
|
|
|
|
// It can be either "major" or "minor".
|
2022-12-05 09:15:03 -05:00
|
|
|
Granularity string `json:"granularity,omitempty"`
|
2022-11-29 05:39:07 -05:00
|
|
|
// Base is the base version of the list.
|
|
|
|
// Every version in the list is a finer-grained version of this base version.
|
2022-12-05 09:15:03 -05:00
|
|
|
Base string `json:"base,omitempty"`
|
2022-11-29 05:39:07 -05:00
|
|
|
// Kind is the kind of resource this list is for.
|
2022-12-05 09:15:03 -05:00
|
|
|
Kind string `json:"kind,omitempty"`
|
2022-11-29 05:39:07 -05:00
|
|
|
// Versions is a list of all versions in this list.
|
2022-12-05 09:15:03 -05:00
|
|
|
Versions []string `json:"versions,omitempty"`
|
2022-11-29 05:39:07 -05:00
|
|
|
}
|
|
|
|
|
2022-11-29 10:58:22 -05:00
|
|
|
// Validate checks if the list is valid.
|
2022-11-29 05:39:07 -05:00
|
|
|
// This performs the following checks:
|
2022-12-05 09:15:03 -05:00
|
|
|
// - The ref is set.
|
2022-11-29 05:39:07 -05:00
|
|
|
// - The stream is supported.
|
|
|
|
// - The granularity is "major" or "minor".
|
|
|
|
// - The kind is supported.
|
|
|
|
// - The base version is a valid semantic version that matches the granularity.
|
|
|
|
// - All versions in the list are valid semantic versions that are finer-grained than the base version.
|
2022-12-01 04:26:35 -05:00
|
|
|
func (l *List) Validate() error {
|
2022-11-29 05:39:07 -05:00
|
|
|
var issues []string
|
2022-12-05 09:15:03 -05:00
|
|
|
if !IsValidRef(l.Ref) {
|
|
|
|
issues = append(issues, "ref is empty")
|
|
|
|
}
|
|
|
|
if !IsValidStream(l.Ref, l.Stream) {
|
|
|
|
issues = append(issues, fmt.Sprintf("stream %q is not supported on ref %q", l.Stream, l.Ref))
|
2022-11-29 05:39:07 -05:00
|
|
|
}
|
|
|
|
if l.Granularity != "major" && l.Granularity != "minor" {
|
|
|
|
issues = append(issues, fmt.Sprintf("granularity %q is not supported", l.Granularity))
|
|
|
|
}
|
|
|
|
if l.Kind != "image" {
|
|
|
|
issues = append(issues, fmt.Sprintf("kind %q is not supported", l.Kind))
|
|
|
|
}
|
|
|
|
if !semver.IsValid(l.Base) {
|
|
|
|
issues = append(issues, fmt.Sprintf("base version %q is not a valid semantic version", l.Base))
|
|
|
|
}
|
|
|
|
var normalizeFunc func(string) string
|
|
|
|
switch l.Granularity {
|
|
|
|
case "major":
|
|
|
|
normalizeFunc = semver.Major
|
|
|
|
case "minor":
|
|
|
|
normalizeFunc = semver.MajorMinor
|
|
|
|
default:
|
|
|
|
normalizeFunc = func(s string) string { return s }
|
|
|
|
}
|
|
|
|
if normalizeFunc(l.Base) != l.Base {
|
|
|
|
issues = append(issues, fmt.Sprintf("base version %q is not a %v version", l.Base, l.Granularity))
|
|
|
|
}
|
|
|
|
for _, ver := range l.Versions {
|
|
|
|
if !semver.IsValid(ver) {
|
|
|
|
issues = append(issues, fmt.Sprintf("version %q in list is not a valid semantic version", ver))
|
|
|
|
}
|
|
|
|
if normalizeFunc(ver) != l.Base {
|
|
|
|
issues = append(issues, fmt.Sprintf("version %q in list is not a finer-grained version of base version %q", ver, l.Base))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(issues) > 0 {
|
|
|
|
return fmt.Errorf("version list is invalid:\n%s", strings.Join(issues, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:35:25 -05:00
|
|
|
// JSONPath returns the S3 JSON path for this object.
|
|
|
|
func (l *List) JSONPath() string {
|
|
|
|
return path.Join(constants.CDNAPIPrefix, "ref", l.Ref, "stream", l.Stream, "versions", l.Granularity, l.Base, l.Kind+".json")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Latest is the latest version of a kind of resource.
|
|
|
|
type Latest struct {
|
|
|
|
// Ref is the branch name this latest version belongs to.
|
|
|
|
Ref string `json:"ref,omitempty"`
|
|
|
|
// Stream is stream name this latest version belongs to.
|
|
|
|
Stream string `json:"stream,omitempty"`
|
|
|
|
// Kind is the kind of resource this latest version is for.
|
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
// Version is the latest version for this ref, stream and kind.
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks if this latest version is valid.
|
|
|
|
func (l *Latest) Validate() error {
|
|
|
|
var issues []string
|
|
|
|
if !IsValidRef(l.Ref) {
|
|
|
|
issues = append(issues, "ref is empty")
|
|
|
|
}
|
|
|
|
if !IsValidStream(l.Ref, l.Stream) {
|
|
|
|
issues = append(issues, fmt.Sprintf("stream %q is not supported on ref %q", l.Stream, l.Ref))
|
|
|
|
}
|
|
|
|
if l.Kind != "image" {
|
|
|
|
issues = append(issues, fmt.Sprintf("kind %q is not supported", l.Kind))
|
|
|
|
}
|
|
|
|
if !semver.IsValid(l.Version) {
|
|
|
|
issues = append(issues, fmt.Sprintf("version %q is not a valid semantic version", l.Version))
|
|
|
|
}
|
|
|
|
if len(issues) > 0 {
|
|
|
|
return fmt.Errorf("latest version is invalid:\n%s", strings.Join(issues, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONPath returns the S3 JSON path for this object.
|
|
|
|
func (l *Latest) JSONPath() string {
|
|
|
|
return path.Join(constants.CDNAPIPrefix, "ref", l.Ref, "stream", l.Stream, "versions", "latest", l.Kind+".json")
|
|
|
|
}
|
|
|
|
|
2022-11-29 10:58:22 -05:00
|
|
|
// Contains returns true if the list contains the given version.
|
2022-12-01 04:26:35 -05:00
|
|
|
func (l *List) Contains(version string) bool {
|
2022-11-29 10:58:22 -05:00
|
|
|
for _, v := range l.Versions {
|
|
|
|
if v == version {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
// Fetcher fetches a list of versions.
|
|
|
|
type Fetcher struct {
|
2022-11-29 05:39:07 -05:00
|
|
|
httpc httpc
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new VersionsFetcher.
|
2022-12-01 04:26:35 -05:00
|
|
|
func New() *Fetcher {
|
|
|
|
return &Fetcher{
|
2022-11-29 05:39:07 -05:00
|
|
|
httpc: http.DefaultClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MinorVersionsOf fetches the list of minor versions for a given stream, major version and kind.
|
2022-12-05 09:15:03 -05:00
|
|
|
func (f *Fetcher) MinorVersionsOf(ctx context.Context, ref, stream, major, kind string) (*List, error) {
|
|
|
|
return f.list(ctx, stream, "major", major, ref, kind)
|
2022-11-29 05:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchVersionsOf fetches the list of patch versions for a given stream, minor version and kind.
|
2022-12-05 09:15:03 -05:00
|
|
|
func (f *Fetcher) PatchVersionsOf(ctx context.Context, ref, stream, minor, kind string) (*List, error) {
|
|
|
|
return f.list(ctx, stream, "minor", minor, ref, kind)
|
2022-11-29 05:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// list fetches the list of versions for a given stream, granularity, base and kind.
|
2022-12-05 09:15:03 -05:00
|
|
|
func (f *Fetcher) list(ctx context.Context, ref, stream, granularity, base, kind string) (*List, error) {
|
|
|
|
raw, err := getFromURL(ctx, f.httpc, ref, stream, granularity, base, kind)
|
2022-11-29 05:39:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("fetching versions list: %w", err)
|
|
|
|
}
|
2022-12-01 04:26:35 -05:00
|
|
|
list := &List{}
|
2022-11-29 05:39:07 -05:00
|
|
|
if err := json.Unmarshal(raw, &list); err != nil {
|
|
|
|
return nil, fmt.Errorf("decoding versions list: %w", err)
|
|
|
|
}
|
2022-11-29 10:58:22 -05:00
|
|
|
if err := list.Validate(); err != nil {
|
2022-11-29 05:39:07 -05:00
|
|
|
return nil, fmt.Errorf("validating versions list: %w", err)
|
|
|
|
}
|
|
|
|
if !f.listMatchesRequest(list, stream, granularity, base, kind) {
|
|
|
|
return nil, fmt.Errorf("versions list does not match request")
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
func (f *Fetcher) listMatchesRequest(list *List, stream, granularity, base, kind string) bool {
|
2022-11-29 05:39:07 -05:00
|
|
|
return list.Stream == stream && list.Granularity == granularity && list.Base == base && list.Kind == kind
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFromURL fetches the versions list from a URL.
|
2022-12-05 09:15:03 -05:00
|
|
|
func getFromURL(ctx context.Context, client httpc, ref, stream, granularity, base, kind string) ([]byte, error) {
|
2022-11-29 05:39:07 -05:00
|
|
|
url, err := url.Parse(constants.CDNRepositoryURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing image version repository URL: %w", err)
|
|
|
|
}
|
|
|
|
kindFilename := path.Base(kind) + ".json"
|
2022-12-05 09:15:03 -05:00
|
|
|
url.Path = path.Join(constants.CDNAPIPrefix, "ref", ref, "stream", stream, "versions", granularity, base, kindFilename)
|
2022-11-29 05:39:07 -05:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), http.NoBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return nil, fmt.Errorf("versions list %q does not exist", url.String())
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:59:09 -05:00
|
|
|
// IsValidStream returns true if the given stream is a valid stream.
|
2022-12-05 09:15:03 -05:00
|
|
|
func IsValidStream(ref, stream string) bool {
|
|
|
|
validReleaseStreams := []string{"stable", "console", "debug"}
|
|
|
|
validStreams := []string{"nightly", "console", "debug"}
|
|
|
|
|
|
|
|
if isReleaseRef(ref) {
|
|
|
|
validStreams = validReleaseStreams
|
|
|
|
}
|
2022-12-02 10:59:09 -05:00
|
|
|
|
|
|
|
for _, validStream := range validStreams {
|
|
|
|
if stream == validStream {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-12-05 09:15:03 -05:00
|
|
|
var notAZ09Regexp = regexp.MustCompile("[^a-zA-Z0-9-]+")
|
|
|
|
|
|
|
|
// CanonicalRef returns the canonicalized ref for the given ref.
|
|
|
|
func CanonicalRef(ref string) string {
|
|
|
|
return notAZ09Regexp.ReplaceAllString(ref, "-")
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidRef returns true if the given ref is a valid ref.
|
|
|
|
func IsValidRef(ref string) bool {
|
|
|
|
if ref == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if notAZ09Regexp.FindString(ref) != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(ref, "refs-heads") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func isReleaseRef(ref string) bool {
|
|
|
|
return ref == "-"
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
type httpc interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|