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
|
|
|
/*
|
2023-05-23 03:17:27 -04:00
|
|
|
Package imagefetcher provides helping wrappers around a versionsapi fetcher.
|
2023-01-19 09:57:50 -05:00
|
|
|
|
|
|
|
It also enables local image overrides and download of raw images.
|
|
|
|
*/
|
2023-05-23 03:17:27 -04:00
|
|
|
package imagefetcher
|
2022-11-22 12:47:08 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-25 12:43:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/fetcher"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/versionsapi"
|
2022-11-22 12:47:08 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2023-03-29 08:04:37 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/variant"
|
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-06-07 10:16:32 -04:00
|
|
|
fetcher: versionsapi.NewFetcher(),
|
2023-01-03 03:52:06 -05:00
|
|
|
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.
|
2023-05-23 03:17:27 -04:00
|
|
|
func (f *Fetcher) FetchReference(ctx context.Context,
|
|
|
|
provider cloudprovider.Provider, attestationVariant variant.Variant,
|
|
|
|
image, region string,
|
|
|
|
) (string, error) {
|
|
|
|
ver, err := versionsapi.NewVersionFromShortPath(image, versionsapi.VersionKindImage)
|
2022-12-09 05:51:38 -05:00
|
|
|
if err != nil {
|
2023-02-09 09:54:12 -05:00
|
|
|
return "", fmt.Errorf("parsing config image short path: %w", err)
|
2022-12-09 05:51:38 -05:00
|
|
|
}
|
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
|
|
|
|
2023-05-23 03:17:27 -04:00
|
|
|
return getReferenceFromImageInfo(provider, attestationVariant.String(), imgInfo, filters(provider, region)...)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
|
|
|
|
2023-05-23 03:17:27 -04:00
|
|
|
func filters(provider cloudprovider.Provider, region string) []filter {
|
|
|
|
var filters []filter
|
2022-11-22 12:47:08 -05:00
|
|
|
switch provider {
|
|
|
|
case cloudprovider.AWS:
|
2023-05-23 03:17:27 -04:00
|
|
|
filters = append(filters, func(i versionsapi.ImageInfoEntry) bool {
|
|
|
|
return i.Region == region
|
|
|
|
})
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-05-23 03:17:27 -04:00
|
|
|
return filters
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
|
|
|
|
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.
|
2023-05-23 03:17:27 -04:00
|
|
|
func getReferenceFromImageInfo(provider cloudprovider.Provider,
|
|
|
|
attestationVariant string, imgInfo versionsapi.ImageInfo,
|
|
|
|
filt ...filter,
|
2023-01-03 03:52:06 -05:00
|
|
|
) (string, error) {
|
2023-05-23 03:17:27 -04:00
|
|
|
var correctVariant versionsapi.ImageInfoEntry
|
|
|
|
var found bool
|
|
|
|
variantLoop:
|
|
|
|
for _, variant := range imgInfo.List {
|
|
|
|
gotCSP := cloudprovider.FromString(variant.CSP)
|
|
|
|
if gotCSP != provider || variant.AttestationVariant != attestationVariant {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, f := range filt {
|
|
|
|
if !f(variant) {
|
|
|
|
continue variantLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
correctVariant = variant
|
|
|
|
found = true
|
|
|
|
break
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-05-23 03:17:27 -04:00
|
|
|
if !found {
|
|
|
|
return "", fmt.Errorf("image not available in image info for CSP %q, variant %q and other filters", provider.String(), attestationVariant)
|
2022-11-22 12:47:08 -05:00
|
|
|
}
|
2023-01-03 03:52:06 -05:00
|
|
|
|
2023-05-23 03:17:27 -04:00
|
|
|
return correctVariant.Reference, 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
|
|
|
}
|
2023-05-23 03:17:27 -04:00
|
|
|
|
|
|
|
type filter func(versionsapi.ImageInfoEntry) bool
|