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-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-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-08-23 10:39:49 -04:00
|
|
|
distribution, err := cmd.Flags().GetString("distribution")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting distribution: %w", err)
|
|
|
|
}
|
|
|
|
|
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-08-23 10:39:49 -04:00
|
|
|
client, clientClose, err := attestationconfigapi.NewClient(cmd.Context(), cfg, []byte(cosignPwd), []byte(privateKey), false, 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)
|
|
|
|
}
|