2022-11-22 12:47:08 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
/*
|
|
|
|
Package image provides helping wrappers around a versionsapi fetcher.
|
|
|
|
|
|
|
|
It also enables local image overrides and download of raw images.
|
|
|
|
*/
|
2022-11-22 12:47:08 -05:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-01-03 03:52:06 -05:00
|
|
|
"errors"
|
2022-11-22 12:47:08 -05:00
|
|
|
"fmt"
|
2023-01-03 03:52:06 -05:00
|
|
|
"io/fs"
|
|
|
|
"regexp"
|
2022-11-22 12:47:08 -05:00
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2023-01-03 03:52:06 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versionsapi"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versionsapi/fetcher"
|
2022-11-22 12:47:08 -05:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Fetcher fetches image references using a lookup table.
|
|
|
|
type Fetcher struct {
|
2023-01-03 03:52:06 -05:00
|
|
|
fetcher versionsAPIImageInfoFetcher
|
|
|
|
fs *afero.Afero
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new image fetcher.
|
|
|
|
func New() *Fetcher {
|
|
|
|
return &Fetcher{
|
2023-01-03 03:52:06 -05:00
|
|
|
fetcher: fetcher.NewFetcher(),
|
|
|
|
fs: &afero.Afero{Fs: afero.NewOsFs()},
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchReference fetches the image reference for a given image version uid, CSP and image variant.
|
|
|
|
func (f *Fetcher) FetchReference(ctx context.Context, config *config.Config) (string, error) {
|
|
|
|
provider := config.GetProvider()
|
|
|
|
variant, err := variant(provider, config)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
|
|
|
ver, err := versionsapi.NewVersionFromShortPath(config.Image, versionsapi.VersionKindImage)
|
2022-12-09 05:51:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-11-22 12:47:08 -05:00
|
|
|
|
2023-01-03 03:52:06 -05:00
|
|
|
imgInfoReq := versionsapi.ImageInfo{
|
|
|
|
Ref: ver.Ref,
|
|
|
|
Stream: ver.Stream,
|
|
|
|
Version: ver.Version,
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:15:49 -05:00
|
|
|
url, err := imgInfoReq.URL()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-01-03 03:52:06 -05:00
|
|
|
imgInfo, err := getFromFile(f.fs, imgInfoReq)
|
|
|
|
if err != nil && errors.Is(err, fs.ErrNotExist) {
|
|
|
|
imgInfo, err = f.fetcher.FetchImageInfo(ctx, imgInfoReq)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-13 04:15:49 -05:00
|
|
|
|
|
|
|
var notFoundErr *fetcher.NotFoundError
|
|
|
|
switch {
|
|
|
|
case errors.As(err, ¬FoundErr):
|
|
|
|
overridePath := imageInfoFilename(imgInfoReq)
|
|
|
|
return "", fmt.Errorf("image info file not found locally at %q or remotely at %s", overridePath, url)
|
|
|
|
case err != nil:
|
2023-01-03 03:52:06 -05:00
|
|
|
return "", err
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
|
|
|
if err := imgInfo.Validate(); err != nil {
|
|
|
|
return "", fmt.Errorf("validating image info file: %w", err)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
|
|
|
return getReferenceFromImageInfo(provider, variant, imgInfo)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// variant returns the image variant for a given CSP and configuration.
|
|
|
|
func variant(provider cloudprovider.Provider, config *config.Config) (string, error) {
|
|
|
|
switch provider {
|
|
|
|
case cloudprovider.AWS:
|
|
|
|
return config.Provider.AWS.Region, nil
|
|
|
|
case cloudprovider.Azure:
|
|
|
|
if *config.Provider.Azure.ConfidentialVM {
|
|
|
|
return "cvm", nil
|
|
|
|
}
|
|
|
|
return "trustedlaunch", nil
|
|
|
|
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
return "sev-es", nil
|
|
|
|
case cloudprovider.QEMU:
|
|
|
|
return "default", nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unsupported provider: %s", provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 03:52:06 -05:00
|
|
|
func getFromFile(fs *afero.Afero, imgInfo versionsapi.ImageInfo) (versionsapi.ImageInfo, error) {
|
|
|
|
fileName := imageInfoFilename(imgInfo)
|
2022-11-22 12:47:08 -05:00
|
|
|
|
2023-01-03 03:52:06 -05:00
|
|
|
raw, err := fs.ReadFile(fileName)
|
2022-11-22 12:47:08 -05:00
|
|
|
if err != nil {
|
2023-01-03 03:52:06 -05:00
|
|
|
return versionsapi.ImageInfo{}, err
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
|
|
|
var newInfo versionsapi.ImageInfo
|
|
|
|
if err := json.Unmarshal(raw, &newInfo); err != nil {
|
|
|
|
return versionsapi.ImageInfo{}, fmt.Errorf("decoding image info file: %w", err)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
|
|
|
|
2023-01-03 03:52:06 -05:00
|
|
|
return newInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var filenameReplaceRegexp = regexp.MustCompile(`([^a-zA-Z0-9.-])`)
|
|
|
|
|
|
|
|
func imageInfoFilename(imgInfo versionsapi.ImageInfo) string {
|
|
|
|
path := imgInfo.JSONPath()
|
|
|
|
return filenameReplaceRegexp.ReplaceAllString(path, "_")
|
|
|
|
}
|
|
|
|
|
|
|
|
// getReferenceFromImageInfo returns the image reference for a given CSP and image variant.
|
|
|
|
func getReferenceFromImageInfo(provider cloudprovider.Provider, variant string, imgInfo versionsapi.ImageInfo,
|
|
|
|
) (string, error) {
|
|
|
|
var providerList map[string]string
|
|
|
|
switch provider {
|
|
|
|
case cloudprovider.AWS:
|
|
|
|
providerList = imgInfo.AWS
|
|
|
|
case cloudprovider.Azure:
|
|
|
|
providerList = imgInfo.Azure
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
providerList = imgInfo.GCP
|
|
|
|
case cloudprovider.QEMU:
|
|
|
|
providerList = imgInfo.QEMU
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("image not available in image info for CSP %q", provider.String())
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
|
|
|
if providerList == nil {
|
|
|
|
return "", fmt.Errorf("image not available in image info for CSP %q", provider.String())
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
|
|
|
ref, ok := providerList[variant]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("image not available in image info for variant %q", variant)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
|
|
|
return ref, nil
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
|
|
|
|
2023-01-03 03:52:06 -05:00
|
|
|
type versionsAPIImageInfoFetcher interface {
|
|
|
|
FetchImageInfo(ctx context.Context, imageInfo versionsapi.ImageInfo) (versionsapi.ImageInfo, error)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|