From 13f089409429bf24ce6c08360f8cb60cff239b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Mon, 3 Oct 2022 14:13:14 +0200 Subject: [PATCH] worker/aws: don't generate object key in worker There is a desire to make the worker as "dumb" as possible. Therefore it is not desired to generate the AWS object key names in the worker if it was not provided in the job. Modify the worker code to not generate the AWS object key in any case and instead set an error in case the object key was not provided. Modify Weldr API implementation to generate the object key, if it was not provided by the user. This is consistent with Cloud API implementation. --- cmd/osbuild-worker/jobimpl-osbuild.go | 15 ++++++++++----- internal/weldr/upload.go | 13 +++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index abaf05b53..206f23427 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -468,9 +468,9 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { break } - key := targetOptions.Key - if key == "" { - key = uuid.New().String() + if targetOptions.Key == "" { + targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, "No AWS object key provided", nil) + break } bucket := targetOptions.Bucket @@ -496,13 +496,13 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { } } - _, err = a.Upload(imagePath, bucket, key) + _, err = a.Upload(imagePath, bucket, targetOptions.Key) if err != nil { targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, err.Error(), nil) break } - ami, err := a.Register(jobTarget.ImageName, bucket, key, targetOptions.ShareWithAccounts, common.CurrentArch()) + ami, err := a.Register(jobTarget.ImageName, bucket, targetOptions.Key, targetOptions.ShareWithAccounts, common.CurrentArch()) if err != nil { targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorImportingImage, err.Error(), nil) break @@ -525,6 +525,11 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { break } + if targetOptions.Key == "" { + targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, "No AWS object key provided", nil) + break + } + url, targetError := uploadToS3(a, outputDirectory, jobTarget.OsbuildArtifact.ExportName, bucket, targetOptions.Key, jobTarget.OsbuildArtifact.ExportFilename, targetOptions.Public) if targetError != nil { targetResult.TargetError = targetError diff --git a/internal/weldr/upload.go b/internal/weldr/upload.go index df7e2a70b..04ab1626a 100644 --- a/internal/weldr/upload.go +++ b/internal/weldr/upload.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "encoding/json" "errors" + "fmt" "strings" "time" @@ -250,6 +251,10 @@ func uploadRequestToTarget(u uploadRequest, imageType distro.ImageType) *target. switch options := u.Settings.(type) { case *awsUploadSettings: + key := options.Key + if key == "" { + key = fmt.Sprintf("composer-api-%s", uuid.New().String()) + } t.Name = target.TargetNameAWS t.Options = &target.AWSTargetOptions{ Region: options.Region, @@ -257,9 +262,13 @@ func uploadRequestToTarget(u uploadRequest, imageType distro.ImageType) *target. SecretAccessKey: options.SecretAccessKey, SessionToken: options.SessionToken, Bucket: options.Bucket, - Key: options.Key, + Key: key, } case *awsS3UploadSettings: + key := options.Key + if key == "" { + key = fmt.Sprintf("composer-api-%s", uuid.New().String()) + } t.Name = target.TargetNameAWSS3 t.Options = &target.AWSS3TargetOptions{ Region: options.Region, @@ -267,7 +276,7 @@ func uploadRequestToTarget(u uploadRequest, imageType distro.ImageType) *target. SecretAccessKey: options.SecretAccessKey, SessionToken: options.SessionToken, Bucket: options.Bucket, - Key: options.Key, + Key: key, Endpoint: options.Endpoint, CABundle: options.CABundle, SkipSSLVerification: options.SkipSSLVerification,