2023-06-05 06:33:22 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2023-08-10 03:45:46 -04:00
|
|
|
package main
|
2023-06-05 06:33:22 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-23 10:39:49 -04:00
|
|
|
"errors"
|
2023-06-05 06:33:22 -04:00
|
|
|
"fmt"
|
|
|
|
|
2023-09-25 05:53:02 -04:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
2023-08-09 12:58:46 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-06-05 06:33:22 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/staticupload"
|
|
|
|
"github.com/spf13/cobra"
|
2023-08-09 12:58:46 -04:00
|
|
|
"go.uber.org/zap"
|
2023-06-05 06:33:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// newDeleteCmd creates the delete command.
|
|
|
|
func newDeleteCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2023-08-25 06:40:47 -04:00
|
|
|
Use: "delete",
|
|
|
|
Short: "delete a specific version from the config api",
|
|
|
|
PreRunE: envCheck,
|
|
|
|
RunE: runDelete,
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
|
|
|
cmd.Flags().StringP("version", "v", "", "Name of the version to delete (without .json suffix)")
|
2023-08-09 12:58:46 -04:00
|
|
|
must(cmd.MarkFlagRequired("version"))
|
2023-09-25 05:53:02 -04:00
|
|
|
|
|
|
|
recursivelyCmd := &cobra.Command{
|
|
|
|
Use: "recursive",
|
|
|
|
Short: "delete all objects from the API path",
|
|
|
|
RunE: runRecursiveDelete,
|
|
|
|
}
|
|
|
|
cmd.AddCommand(recursivelyCmd)
|
2023-06-05 06:33:22 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteCmd struct {
|
|
|
|
attestationClient deleteClient
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteClient interface {
|
|
|
|
DeleteAzureSEVSNPVersion(ctx context.Context, versionStr string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d deleteCmd) delete(cmd *cobra.Command) error {
|
|
|
|
version, err := cmd.Flags().GetString("version")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.attestationClient.DeleteAzureSEVSNPVersion(cmd.Context(), version)
|
|
|
|
}
|
|
|
|
|
2023-08-23 10:39:49 -04:00
|
|
|
func runDelete(cmd *cobra.Command, _ []string) (retErr error) {
|
2023-08-09 12:58:46 -04:00
|
|
|
log := logger.New(logger.PlainLog, zap.DebugLevel).Named("attestationconfigapi")
|
2023-08-23 09:10:20 -04:00
|
|
|
|
|
|
|
region, err := cmd.Flags().GetString("region")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting region: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket, err := cmd.Flags().GetString("bucket")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-25 05:53:02 -04:00
|
|
|
testing, err := cmd.Flags().GetBool("testing")
|
2023-08-23 10:39:49 -04:00
|
|
|
if err != nil {
|
2023-09-25 05:53:02 -04:00
|
|
|
return fmt.Errorf("getting testing flag: %w", err)
|
2023-08-23 10:39:49 -04:00
|
|
|
}
|
2023-09-25 05:53:02 -04:00
|
|
|
_, distribution := getEnvironment(testing)
|
2023-08-23 10:39:49 -04:00
|
|
|
|
2023-06-05 06:33:22 -04:00
|
|
|
cfg := staticupload.Config{
|
2023-08-23 10:39:49 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Region: region,
|
|
|
|
DistributionID: distribution,
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
2023-09-25 05:53:02 -04:00
|
|
|
client, clientClose, err := attestationconfigapi.NewClient(cmd.Context(), cfg,
|
|
|
|
[]byte(cosignPwd), []byte(privateKey), false, 1, log)
|
2023-06-05 06:33:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create attestation client: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2023-08-23 10:39:49 -04:00
|
|
|
err := clientClose(cmd.Context())
|
|
|
|
if err != nil {
|
|
|
|
retErr = errors.Join(retErr, fmt.Errorf("failed to invalidate cache: %w", err))
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
|
|
|
}()
|
2023-08-23 10:39:49 -04:00
|
|
|
|
2023-06-05 06:33:22 -04:00
|
|
|
deleteCmd := deleteCmd{
|
2023-08-09 12:58:46 -04:00
|
|
|
attestationClient: client,
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
|
|
|
return deleteCmd.delete(cmd)
|
|
|
|
}
|
2023-09-25 05:53:02 -04:00
|
|
|
|
|
|
|
func runRecursiveDelete(cmd *cobra.Command, _ []string) (retErr error) {
|
|
|
|
region, err := cmd.Flags().GetString("region")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting region: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket, err := cmd.Flags().GetString("bucket")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testing, err := cmd.Flags().GetBool("testing")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting testing flag: %w", err)
|
|
|
|
}
|
|
|
|
_, distribution := getEnvironment(testing)
|
|
|
|
|
|
|
|
log := logger.New(logger.PlainLog, zap.DebugLevel).Named("attestationconfigapi")
|
|
|
|
client, closeFn, err := staticupload.New(cmd.Context(), staticupload.Config{
|
|
|
|
Bucket: bucket,
|
|
|
|
Region: region,
|
|
|
|
DistributionID: distribution,
|
|
|
|
}, log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create static upload client: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := closeFn(cmd.Context())
|
|
|
|
if err != nil {
|
|
|
|
retErr = errors.Join(retErr, fmt.Errorf("failed to close client: %w", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
path := "constellation/v1/attestation/azure-sev-snp"
|
|
|
|
resp, err := client.ListObjectsV2(cmd.Context(), &s3.ListObjectsV2Input{
|
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
Prefix: aws.String(path),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all objects in the path.
|
|
|
|
objIDs := make([]s3types.ObjectIdentifier, len(resp.Contents))
|
|
|
|
for i, obj := range resp.Contents {
|
|
|
|
objIDs[i] = s3types.ObjectIdentifier{Key: obj.Key}
|
|
|
|
}
|
|
|
|
if len(objIDs) > 0 {
|
|
|
|
_, err = client.DeleteObjects(cmd.Context(), &s3.DeleteObjectsInput{
|
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
Delete: &s3types.Delete{
|
|
|
|
Objects: objIDs,
|
|
|
|
Quiet: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|