2023-04-21 04:47:07 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
// package archive is used to archive OS images in S3.
|
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/versionsapi"
|
2023-05-23 09:09:14 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-04-21 04:47:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-06-02 05:20:01 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/staticupload"
|
2023-04-21 04:47:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Archivist uploads OS images to S3.
|
|
|
|
type Archivist struct {
|
2023-06-02 05:20:01 -04:00
|
|
|
uploadClient uploadClient
|
|
|
|
uploadClientClose func(ctx context.Context) error
|
2023-04-21 04:47:07 -04:00
|
|
|
// bucket is the name of the S3 bucket to use.
|
|
|
|
bucket string
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Archivist.
|
2023-06-02 05:20:01 -04:00
|
|
|
func New(ctx context.Context, region, bucket, distributionID string, log *logger.Logger) (*Archivist, CloseFunc, error) {
|
|
|
|
staticUploadClient, staticUploadClientClose, err := staticupload.New(ctx, staticupload.Config{
|
|
|
|
Region: region,
|
|
|
|
Bucket: bucket,
|
|
|
|
DistributionID: distributionID,
|
|
|
|
CacheInvalidationStrategy: staticupload.CacheInvalidateBatchOnFlush,
|
|
|
|
})
|
2023-04-21 04:47:07 -04:00
|
|
|
if err != nil {
|
2023-06-02 05:20:01 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
archivist := &Archivist{
|
|
|
|
uploadClient: staticUploadClient,
|
|
|
|
uploadClientClose: staticUploadClientClose,
|
|
|
|
bucket: bucket,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
archivistClose := func(ctx context.Context) error {
|
|
|
|
return archivist.Close(ctx)
|
2023-04-21 04:47:07 -04:00
|
|
|
}
|
|
|
|
|
2023-06-02 05:20:01 -04:00
|
|
|
return archivist, archivistClose, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the uploader.
|
|
|
|
// It invalidates the CDN cache for all uploaded files.
|
|
|
|
func (a *Archivist) Close(ctx context.Context) error {
|
|
|
|
if a.uploadClientClose == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a.uploadClientClose(ctx)
|
2023-04-21 04:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Archive reads the OS image in img and uploads it as key.
|
2023-05-25 08:33:57 -04:00
|
|
|
func (a *Archivist) Archive(ctx context.Context, version versionsapi.Version, csp, attestationVariant string, img io.Reader) (string, error) {
|
2023-08-01 10:48:13 -04:00
|
|
|
key, err := url.JoinPath(version.ArtifactPath(versionsapi.APIV1), version.Kind().String(), "csp", csp, attestationVariant, "image.raw")
|
2023-04-21 04:47:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-05-23 03:17:27 -04:00
|
|
|
a.log.Debugf("Archiving OS image %s %s %v to s3://%v/%v", csp, attestationVariant, version.ShortPath(), a.bucket, key)
|
2023-04-21 04:47:07 -04:00
|
|
|
_, err = a.uploadClient.Upload(ctx, &s3.PutObjectInput{
|
|
|
|
Bucket: &a.bucket,
|
|
|
|
Key: &key,
|
|
|
|
Body: img,
|
|
|
|
ChecksumAlgorithm: s3types.ChecksumAlgorithmSha256,
|
|
|
|
})
|
2023-05-23 09:09:14 -04:00
|
|
|
return constants.CDNRepositoryURL + "/" + key, err
|
2023-04-21 04:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type uploadClient interface {
|
|
|
|
Upload(ctx context.Context, input *s3.PutObjectInput, opts ...func(*s3manager.Uploader)) (*s3manager.UploadOutput, error)
|
|
|
|
}
|
2023-06-02 05:20:01 -04:00
|
|
|
|
|
|
|
// CloseFunc is a function that closes the client.
|
|
|
|
type CloseFunc func(ctx context.Context) error
|