2023-06-02 03:19:23 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
package versionsapi
|
2023-06-02 03:19:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-02-08 09:20:01 -05:00
|
|
|
"log/slog"
|
2023-06-02 03:19:23 -04:00
|
|
|
"path"
|
|
|
|
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
|
|
|
|
apiclient "github.com/edgelesssys/constellation/v2/internal/api/client"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
)
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
// Client is a client for the versions API.
|
|
|
|
type Client struct {
|
2023-06-02 03:19:23 -04:00
|
|
|
*apiclient.Client
|
2023-06-02 05:20:01 -04:00
|
|
|
clientClose func(ctx context.Context) error
|
2023-06-02 03:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new client for the versions API.
|
|
|
|
func NewClient(ctx context.Context, region, bucket, distributionID string, dryRun bool,
|
2024-02-08 09:20:01 -05:00
|
|
|
log *slog.Logger,
|
2023-06-07 10:16:32 -04:00
|
|
|
) (*Client, CloseFunc, error) {
|
2023-06-02 05:20:01 -04:00
|
|
|
genericClient, genericClientClose, err := apiclient.NewClient(ctx, region, bucket, distributionID, dryRun, log)
|
2023-06-07 10:16:32 -04:00
|
|
|
versionsClient := &Client{
|
2023-06-02 05:20:01 -04:00
|
|
|
genericClient,
|
|
|
|
genericClientClose,
|
|
|
|
}
|
|
|
|
versionsClientClose := func(ctx context.Context) error {
|
|
|
|
return versionsClient.Close(ctx)
|
|
|
|
}
|
|
|
|
return versionsClient, versionsClientClose, err
|
2023-06-02 03:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReadOnlyClient creates a new read-only client.
|
|
|
|
// This client can be used to fetch objects but cannot write updates.
|
|
|
|
func NewReadOnlyClient(ctx context.Context, region, bucket, distributionID string,
|
2024-02-08 09:20:01 -05:00
|
|
|
log *slog.Logger,
|
2023-06-07 10:16:32 -04:00
|
|
|
) (*Client, CloseFunc, error) {
|
2023-06-02 05:20:01 -04:00
|
|
|
genericClient, genericClientClose, err := apiclient.NewReadOnlyClient(ctx, region, bucket, distributionID, log)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-06-07 10:16:32 -04:00
|
|
|
versionsClient := &Client{
|
2023-06-02 05:20:01 -04:00
|
|
|
genericClient,
|
|
|
|
genericClientClose,
|
|
|
|
}
|
|
|
|
versionsClientClose := func(ctx context.Context) error {
|
|
|
|
return versionsClient.Close(ctx)
|
|
|
|
}
|
|
|
|
return versionsClient, versionsClientClose, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the client.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) Close(ctx context.Context) error {
|
2023-06-02 05:20:01 -04:00
|
|
|
if c.clientClose == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.clientClose(ctx)
|
2023-06-02 03:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchVersionList fetches the given version list from the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) FetchVersionList(ctx context.Context, list List) (List, error) {
|
2023-06-02 03:19:23 -04:00
|
|
|
return apiclient.Fetch(ctx, c.Client, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateVersionList updates the given version list in the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) UpdateVersionList(ctx context.Context, list List) error {
|
2023-06-02 03:19:23 -04:00
|
|
|
semver.Sort(list.Versions)
|
|
|
|
return apiclient.Update(ctx, c.Client, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchVersionLatest fetches the latest version from the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) FetchVersionLatest(ctx context.Context, latest Latest) (Latest, error) {
|
2023-06-02 03:19:23 -04:00
|
|
|
return apiclient.Fetch(ctx, c.Client, latest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateVersionLatest updates the latest version in the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) UpdateVersionLatest(ctx context.Context, latest Latest) error {
|
2023-06-02 03:19:23 -04:00
|
|
|
return apiclient.Update(ctx, c.Client, latest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchImageInfo fetches the given image info from the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) FetchImageInfo(ctx context.Context, imageInfo ImageInfo) (ImageInfo, error) {
|
2023-06-02 03:19:23 -04:00
|
|
|
return apiclient.Fetch(ctx, c.Client, imageInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateImageInfo updates the given image info in the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) UpdateImageInfo(ctx context.Context, imageInfo ImageInfo) error {
|
2023-06-02 03:19:23 -04:00
|
|
|
return apiclient.Update(ctx, c.Client, imageInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchCLIInfo fetches the given CLI info from the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) FetchCLIInfo(ctx context.Context, cliInfo CLIInfo) (CLIInfo, error) {
|
2023-06-02 03:19:23 -04:00
|
|
|
return apiclient.Fetch(ctx, c.Client, cliInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCLIInfo updates the given CLI info in the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) UpdateCLIInfo(ctx context.Context, cliInfo CLIInfo) error {
|
2023-06-02 03:19:23 -04:00
|
|
|
return apiclient.Update(ctx, c.Client, cliInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRef deletes the given ref from the versions API.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) DeleteRef(ctx context.Context, ref string) error {
|
|
|
|
if err := ValidateRef(ref); err != nil {
|
2023-06-02 03:19:23 -04:00
|
|
|
return fmt.Errorf("validating ref: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
refPath := path.Join(constants.CDNAPIPrefix, "ref", ref)
|
|
|
|
if err := c.Client.DeletePath(ctx, refPath); err != nil {
|
|
|
|
return fmt.Errorf("deleting ref path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVersion deletes the given version from the versions API.
|
|
|
|
// The version will be removed from version lists and latest versions, and the versioned
|
|
|
|
// objects are deleted.
|
|
|
|
// Notice that the versions API can get into an inconsistent state if the version is the latest
|
|
|
|
// version but there is no older version of the same minor version available.
|
|
|
|
// Manual update of latest versions is required in this case.
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) DeleteVersion(ctx context.Context, ver Version) error {
|
2023-06-02 03:19:23 -04:00
|
|
|
var retErr error
|
|
|
|
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Deleting version %q from minor version list", ver.version))
|
2023-06-02 03:19:23 -04:00
|
|
|
possibleNewLatest, err := c.deleteVersionFromMinorVersionList(ctx, ver)
|
|
|
|
if err != nil {
|
|
|
|
retErr = errors.Join(retErr, fmt.Errorf("removing from minor version list: %w", err))
|
|
|
|
}
|
|
|
|
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Checking latest version for %q", ver.version))
|
2023-06-02 03:19:23 -04:00
|
|
|
if err := c.deleteVersionFromLatest(ctx, ver, possibleNewLatest); err != nil {
|
|
|
|
retErr = errors.Join(retErr, fmt.Errorf("updating latest version: %w", err))
|
|
|
|
}
|
|
|
|
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Deleting artifact path %q for %q", ver.ArtifactPath(APIV1), ver.version))
|
2023-06-07 10:16:32 -04:00
|
|
|
if err := c.Client.DeletePath(ctx, ver.ArtifactPath(APIV1)); err != nil {
|
2023-06-02 03:19:23 -04:00
|
|
|
retErr = errors.Join(retErr, fmt.Errorf("deleting artifact path: %w", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) deleteVersionFromMinorVersionList(ctx context.Context, ver Version,
|
|
|
|
) (*Latest, error) {
|
|
|
|
minorList := List{
|
2023-08-01 10:48:13 -04:00
|
|
|
Ref: ver.ref,
|
|
|
|
Stream: ver.stream,
|
2023-06-07 10:16:32 -04:00
|
|
|
Granularity: GranularityMinor,
|
|
|
|
Base: ver.WithGranularity(GranularityMinor),
|
|
|
|
Kind: VersionKindImage,
|
2023-06-02 03:19:23 -04:00
|
|
|
}
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Fetching minor version list for version %q", ver.version))
|
2023-06-02 03:19:23 -04:00
|
|
|
minorList, err := c.FetchVersionList(ctx, minorList)
|
2023-06-05 04:51:05 -04:00
|
|
|
var notFoundErr *apiclient.NotFoundError
|
2023-06-02 03:19:23 -04:00
|
|
|
if errors.As(err, ¬FoundErr) {
|
2024-02-08 09:20:01 -05:00
|
|
|
c.Client.Logger.Warn(fmt.Sprintf("Minor version list for version %s not found", ver.version))
|
|
|
|
c.Client.Logger.Warn("Skipping update of minor version list")
|
2023-06-02 03:19:23 -04:00
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
2023-08-01 10:48:13 -04:00
|
|
|
return nil, fmt.Errorf("fetching minor version list for version %s: %w", ver.version, err)
|
2023-06-02 03:19:23 -04:00
|
|
|
}
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
if !minorList.Contains(ver.version) {
|
2024-02-08 09:20:01 -05:00
|
|
|
c.Client.Logger.Warn(fmt.Sprintf("Version %s is not in minor version list %s", ver.version, minorList.JSONPath()))
|
|
|
|
c.Client.Logger.Warn("Skipping update of minor version list")
|
2023-06-02 03:19:23 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
semver.Sort(minorList.Versions)
|
|
|
|
for i, v := range minorList.Versions {
|
2023-08-01 10:48:13 -04:00
|
|
|
if v == ver.version {
|
2023-06-02 03:19:23 -04:00
|
|
|
minorList.Versions = append(minorList.Versions[:i], minorList.Versions[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
var latest *Latest
|
2023-06-02 03:19:23 -04:00
|
|
|
if len(minorList.Versions) != 0 {
|
2023-06-07 10:16:32 -04:00
|
|
|
latest = &Latest{
|
2023-08-01 10:48:13 -04:00
|
|
|
Ref: ver.ref,
|
|
|
|
Stream: ver.stream,
|
2023-06-07 10:16:32 -04:00
|
|
|
Kind: VersionKindImage,
|
2023-06-02 03:19:23 -04:00
|
|
|
Version: minorList.Versions[len(minorList.Versions)-1],
|
|
|
|
}
|
2024-02-08 09:20:01 -05:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Possible latest version replacement %q", latest.Version))
|
2023-06-02 03:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Client.DryRun {
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("DryRun: Updating minor version list %q to %v", minorList.JSONPath(), minorList))
|
2023-06-02 03:19:23 -04:00
|
|
|
return latest, nil
|
|
|
|
}
|
|
|
|
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Updating minor version list %q", minorList.JSONPath()))
|
2023-06-02 03:19:23 -04:00
|
|
|
if err := c.UpdateVersionList(ctx, minorList); err != nil {
|
|
|
|
return latest, fmt.Errorf("updating minor version list %s: %w", minorList.JSONPath(), err)
|
|
|
|
}
|
|
|
|
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Removed version %q from minor version list %q", ver.version, minorList.JSONPath()))
|
2023-06-02 03:19:23 -04:00
|
|
|
return latest, nil
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *Client) deleteVersionFromLatest(ctx context.Context, ver Version, possibleNewLatest *Latest,
|
2023-06-02 03:19:23 -04:00
|
|
|
) error {
|
2023-06-07 10:16:32 -04:00
|
|
|
latest := Latest{
|
2023-08-01 10:48:13 -04:00
|
|
|
Ref: ver.ref,
|
|
|
|
Stream: ver.stream,
|
2023-06-07 10:16:32 -04:00
|
|
|
Kind: VersionKindImage,
|
2023-06-02 03:19:23 -04:00
|
|
|
}
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Fetching latest version from %q", latest.JSONPath()))
|
2023-06-02 03:19:23 -04:00
|
|
|
latest, err := c.FetchVersionLatest(ctx, latest)
|
2023-06-05 04:51:05 -04:00
|
|
|
var notFoundErr *apiclient.NotFoundError
|
2023-06-02 03:19:23 -04:00
|
|
|
if errors.As(err, ¬FoundErr) {
|
2024-02-08 09:20:01 -05:00
|
|
|
c.Client.Logger.Warn(fmt.Sprintf("Latest version for %s not found", latest.JSONPath()))
|
2023-06-02 03:19:23 -04:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("fetching latest version: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
if latest.Version != ver.version {
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Latest version is %q, not the deleted version %q", latest.Version, ver.version))
|
2023-06-02 03:19:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if possibleNewLatest == nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
c.Client.Logger.Error(fmt.Sprintf("Latest version is %s, but no new latest version was found", latest.Version))
|
|
|
|
c.Client.Logger.Error(fmt.Sprintf("A manual update of latest at %s might be needed", latest.JSONPath()))
|
2023-06-02 03:19:23 -04:00
|
|
|
return fmt.Errorf("latest version is %s, but no new latest version was found", latest.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Client.DryRun {
|
2024-04-03 09:49:03 -04:00
|
|
|
c.Client.Logger.Debug(fmt.Sprintf("Would update latest version from %q to %q", latest.Version, possibleNewLatest.Version))
|
2023-06-02 03:19:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
c.Client.Logger.Info(fmt.Sprintf("Updating latest version from %s to %s", latest.Version, possibleNewLatest.Version))
|
2023-06-02 03:19:23 -04:00
|
|
|
if err := c.UpdateVersionLatest(ctx, *possibleNewLatest); err != nil {
|
|
|
|
return fmt.Errorf("updating latest version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-02 05:20:01 -04:00
|
|
|
|
|
|
|
// CloseFunc is a function that closes the client.
|
|
|
|
type CloseFunc func(ctx context.Context) error
|