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 (
|
2024-04-16 12:13:47 -04:00
|
|
|
"context"
|
2023-08-23 10:39:49 -04:00
|
|
|
"errors"
|
2023-06-05 06:33:22 -04:00
|
|
|
"fmt"
|
2024-02-08 09:20:01 -05:00
|
|
|
"log/slog"
|
2023-11-14 07:24:25 -05:00
|
|
|
"path"
|
2023-06-05 06:33:22 -04:00
|
|
|
|
2024-04-16 12:13:47 -04:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"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/attestationconfigapi"
|
2023-11-14 07:24:25 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2023-11-09 03:59:19 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// newDeleteCmd creates the delete command.
|
|
|
|
func newDeleteCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2024-04-16 12:13:47 -04:00
|
|
|
Use: "delete {aws|azure|gcp} {snp-report|guest-firmware} <version>",
|
2023-11-14 07:24:25 -05:00
|
|
|
Short: "Delete an object from the attestationconfig API",
|
2023-11-09 03:59:19 -05:00
|
|
|
Long: "Delete a specific object version from the config api. <version> is the name of the object to delete (without .json suffix)",
|
2023-11-14 07:24:25 -05:00
|
|
|
Example: "COSIGN_PASSWORD=$CPW COSIGN_PRIVATE_KEY=$CKEY cli delete azure snp-report 1.0.0",
|
2023-11-09 03:59:19 -05:00
|
|
|
Args: cobra.MatchAll(cobra.ExactArgs(3), isCloudProvider(0), isValidKind(1)),
|
2023-08-25 06:40:47 -04:00
|
|
|
PreRunE: envCheck,
|
|
|
|
RunE: runDelete,
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
2023-09-25 05:53:02 -04:00
|
|
|
|
|
|
|
recursivelyCmd := &cobra.Command{
|
2024-04-16 12:13:47 -04:00
|
|
|
Use: "recursive {aws|azure|gcp}",
|
2023-11-14 07:24:25 -05:00
|
|
|
Short: "delete all objects from the API path constellation/v1/attestation/<csp>",
|
|
|
|
Long: "Delete all objects from the API path constellation/v1/attestation/<csp>",
|
|
|
|
Example: "COSIGN_PASSWORD=$CPW COSIGN_PRIVATE_KEY=$CKEY cli delete recursive azure",
|
|
|
|
Args: cobra.MatchAll(cobra.ExactArgs(1), isCloudProvider(0)),
|
|
|
|
RunE: runRecursiveDelete,
|
2023-09-25 05:53:02 -04:00
|
|
|
}
|
2023-06-05 06:33:22 -04:00
|
|
|
|
2023-11-09 03:59:19 -05:00
|
|
|
cmd.AddCommand(recursivelyCmd)
|
2023-06-05 06:33:22 -04:00
|
|
|
|
2023-11-09 03:59:19 -05:00
|
|
|
return cmd
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
|
|
|
|
2023-11-09 03:59:19 -05:00
|
|
|
func runDelete(cmd *cobra.Command, args []string) (retErr error) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log := logger.NewTextLogger(slog.LevelDebug).WithGroup("attestationconfigapi")
|
2023-11-09 03:59:19 -05:00
|
|
|
|
|
|
|
deleteCfg, err := newDeleteConfig(cmd, ([3]string)(args[:3]))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating delete config: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-06-05 06:33:22 -04:00
|
|
|
cfg := staticupload.Config{
|
2023-11-09 03:59:19 -05:00
|
|
|
Bucket: deleteCfg.bucket,
|
|
|
|
Region: deleteCfg.region,
|
|
|
|
DistributionID: deleteCfg.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-11-09 03:59:19 -05:00
|
|
|
switch deleteCfg.provider {
|
|
|
|
case cloudprovider.AWS:
|
2024-04-16 12:13:47 -04:00
|
|
|
return deleteEntry(cmd.Context(), variant.AWSSEVSNP{}, client, deleteCfg)
|
2023-11-09 03:59:19 -05:00
|
|
|
case cloudprovider.Azure:
|
2024-04-16 12:13:47 -04:00
|
|
|
return deleteEntry(cmd.Context(), variant.AzureSEVSNP{}, client, deleteCfg)
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
return deleteEntry(cmd.Context(), variant.GCPSEVSNP{}, client, deleteCfg)
|
2023-11-09 03:59:19 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported cloud provider: %s", deleteCfg.provider)
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 05:53:02 -04:00
|
|
|
|
2023-11-09 03:59:19 -05:00
|
|
|
func runRecursiveDelete(cmd *cobra.Command, args []string) (retErr error) {
|
2023-11-14 07:24:25 -05:00
|
|
|
// newDeleteConfig expects 3 args, so we pass "all" for the version argument and "snp-report" as kind.
|
|
|
|
args = append(args, "snp-report")
|
2023-11-09 03:59:19 -05:00
|
|
|
args = append(args, "all")
|
|
|
|
deleteCfg, err := newDeleteConfig(cmd, ([3]string)(args[:3]))
|
2023-09-25 05:53:02 -04:00
|
|
|
if err != nil {
|
2023-11-09 03:59:19 -05:00
|
|
|
return fmt.Errorf("creating delete config: %w", err)
|
2023-09-25 05:53:02 -04:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
log := logger.NewTextLogger(slog.LevelDebug).WithGroup("attestationconfigapi")
|
2023-09-25 05:53:02 -04:00
|
|
|
client, closeFn, err := staticupload.New(cmd.Context(), staticupload.Config{
|
2023-11-09 03:59:19 -05:00
|
|
|
Bucket: deleteCfg.bucket,
|
|
|
|
Region: deleteCfg.region,
|
|
|
|
DistributionID: deleteCfg.distribution,
|
2023-09-25 05:53:02 -04:00
|
|
|
}, 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))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-11-14 07:24:25 -05:00
|
|
|
var deletePath string
|
|
|
|
switch deleteCfg.provider {
|
|
|
|
case cloudprovider.AWS:
|
|
|
|
deletePath = path.Join(attestationconfigapi.AttestationURLPath, variant.AWSSEVSNP{}.String())
|
|
|
|
case cloudprovider.Azure:
|
|
|
|
deletePath = path.Join(attestationconfigapi.AttestationURLPath, variant.AzureSEVSNP{}.String())
|
2024-04-16 12:13:47 -04:00
|
|
|
case cloudprovider.GCP:
|
|
|
|
deletePath = path.Join(attestationconfigapi.AttestationURLPath, variant.GCPSEVSNP{}.String())
|
2023-11-14 07:24:25 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported cloud provider: %s", deleteCfg.provider)
|
|
|
|
}
|
|
|
|
|
2024-04-16 12:13:47 -04:00
|
|
|
return deleteEntryRecursive(cmd.Context(), deletePath, client, deleteCfg)
|
2023-11-14 07:24:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type deleteConfig struct {
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
kind objectKind
|
|
|
|
version string
|
|
|
|
region string
|
|
|
|
bucket string
|
|
|
|
url string
|
|
|
|
distribution string
|
|
|
|
cosignPublicKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDeleteConfig(cmd *cobra.Command, args [3]string) (deleteConfig, error) {
|
|
|
|
region, err := cmd.Flags().GetString("region")
|
|
|
|
if err != nil {
|
|
|
|
return deleteConfig{}, fmt.Errorf("getting region: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket, err := cmd.Flags().GetString("bucket")
|
|
|
|
if err != nil {
|
|
|
|
return deleteConfig{}, fmt.Errorf("getting bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testing, err := cmd.Flags().GetBool("testing")
|
|
|
|
if err != nil {
|
|
|
|
return deleteConfig{}, fmt.Errorf("getting testing flag: %w", err)
|
2023-09-25 05:53:02 -04:00
|
|
|
}
|
2023-11-14 07:24:25 -05:00
|
|
|
apiCfg := getAPIEnvironment(testing)
|
2023-11-09 03:59:19 -05:00
|
|
|
|
2023-11-14 07:24:25 -05:00
|
|
|
provider := cloudprovider.FromString(args[0])
|
|
|
|
kind := kindFromString(args[1])
|
|
|
|
version := args[2]
|
|
|
|
|
|
|
|
return deleteConfig{
|
|
|
|
provider: provider,
|
|
|
|
kind: kind,
|
|
|
|
version: version,
|
|
|
|
region: region,
|
|
|
|
bucket: bucket,
|
|
|
|
url: apiCfg.url,
|
|
|
|
distribution: apiCfg.distribution,
|
|
|
|
cosignPublicKey: apiCfg.cosignPublicKey,
|
|
|
|
}, nil
|
2023-09-25 05:53:02 -04:00
|
|
|
}
|
2024-04-16 12:13:47 -04:00
|
|
|
|
|
|
|
func deleteEntry(ctx context.Context, attvar variant.Variant, client *attestationconfigapi.Client, cfg deleteConfig) error {
|
|
|
|
if cfg.kind != snpReport {
|
|
|
|
return fmt.Errorf("kind %s not supported", cfg.kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.DeleteSEVSNPVersion(ctx, attvar, cfg.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteEntryRecursive(ctx context.Context, path string, client *staticupload.Client, cfg deleteConfig) error {
|
|
|
|
resp, err := client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
|
|
|
|
Bucket: aws.String(cfg.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(ctx, &s3.DeleteObjectsInput{
|
|
|
|
Bucket: aws.String(cfg.bucket),
|
|
|
|
Delete: &s3types.Delete{
|
|
|
|
Objects: objIDs,
|
|
|
|
Quiet: toPtr(true),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toPtr[T any](v T) *T {
|
|
|
|
return &v
|
|
|
|
}
|