2023-04-21 04:47:07 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-05-22 08:05:34 -04:00
|
|
|
"strings"
|
2023-04-21 04:47:07 -04:00
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/versionsapi"
|
2024-01-05 03:49:11 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2023-04-21 04:47:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/osimage"
|
|
|
|
)
|
|
|
|
|
|
|
|
func uploadImage(ctx context.Context, archiveC archivist, uploadC uploader, req *osimage.UploadRequest, out io.Writer) error {
|
|
|
|
// upload to S3 archive
|
2024-01-04 10:59:31 -05:00
|
|
|
imageReader, err := req.ImageReader()
|
2023-04-21 04:47:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-04 10:59:31 -05:00
|
|
|
defer imageReader.Close()
|
2024-01-05 03:49:11 -05:00
|
|
|
|
|
|
|
var archiveURL string
|
|
|
|
if needsArchival(req.Provider, req.Version) {
|
|
|
|
var err error
|
|
|
|
archiveURL, err = archiveC.Archive(ctx, req.Version, strings.ToLower(req.Provider.String()), req.AttestationVariant, imageReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-21 04:47:07 -04:00
|
|
|
}
|
2024-01-05 03:49:11 -05:00
|
|
|
|
2023-04-21 04:47:07 -04:00
|
|
|
// upload to CSP
|
|
|
|
imageReferences, err := uploadC.Upload(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(imageReferences) == 0 {
|
2023-05-23 03:17:27 -04:00
|
|
|
imageReferences = []versionsapi.ImageInfoEntry{
|
|
|
|
{
|
|
|
|
CSP: req.Provider.String(),
|
|
|
|
AttestationVariant: req.AttestationVariant,
|
|
|
|
Reference: archiveURL,
|
|
|
|
},
|
2023-04-21 04:47:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
imageInfo := versionsapi.ImageInfo{
|
2023-08-01 10:48:13 -04:00
|
|
|
Ref: req.Version.Ref(),
|
|
|
|
Stream: req.Version.Stream(),
|
|
|
|
Version: req.Version.Version(),
|
2023-05-23 03:17:27 -04:00
|
|
|
List: imageReferences,
|
2023-04-21 04:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(out).Encode(imageInfo); err != nil {
|
|
|
|
return fmt.Errorf("uploading image: marshaling output: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-05 03:49:11 -05:00
|
|
|
|
|
|
|
func needsArchival(provider cloudprovider.Provider, version versionsapi.Version) bool {
|
|
|
|
switch provider {
|
|
|
|
case cloudprovider.OpenStack, cloudprovider.QEMU: // image upload for some CSPs only consists of this archival step
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return version.Stream() == "stable" || version.Ref() == "-"
|
|
|
|
}
|