2023-06-01 07:55:46 -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-01 07:55:46 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
2023-06-05 06:33:22 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"go.uber.org/zap"
|
2023-06-02 06:10:22 -04:00
|
|
|
|
2023-06-01 07:55:46 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/staticupload"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-06-02 06:10:22 -04:00
|
|
|
awsRegion = "eu-central-1"
|
|
|
|
awsBucket = "cdn-constellation-backend"
|
|
|
|
envCosignPwd = "COSIGN_PASSWORD"
|
|
|
|
envCosignPrivateKey = "COSIGN_PRIVATE_KEY"
|
2023-06-01 07:55:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-08-09 12:58:46 -04:00
|
|
|
maaFilePath string
|
2023-06-01 07:55:46 -04:00
|
|
|
// Cosign credentials.
|
2023-06-02 06:10:22 -04:00
|
|
|
cosignPwd string
|
|
|
|
privateKey string
|
2023-06-01 07:55:46 -04:00
|
|
|
)
|
|
|
|
|
2023-08-10 03:45:46 -04:00
|
|
|
func main() {
|
|
|
|
if err := newRootCmd().Execute(); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// newRootCmd creates the root command.
|
|
|
|
func newRootCmd() *cobra.Command {
|
|
|
|
rootCmd := &cobra.Command{
|
2023-08-09 12:58:46 -04:00
|
|
|
Use: "COSIGN_PASSWORD=$CPWD COSIGN_PRIVATE_KEY=$CKEY upload --version-file $FILE",
|
2023-06-01 07:55:46 -04:00
|
|
|
Short: "Upload a set of versions specific to the azure-sev-snp attestation variant to the config api.",
|
|
|
|
|
2023-08-09 12:58:46 -04:00
|
|
|
Long: fmt.Sprintf("Upload a set of versions specific to the azure-sev-snp attestation variant to the config api."+
|
|
|
|
"Please authenticate with AWS through your preferred method (e.g. environment variables, CLI)"+
|
|
|
|
"to be able to upload to S3. Set the %s and %s environment variables to authenticate with cosign.",
|
|
|
|
envCosignPrivateKey, envCosignPwd,
|
|
|
|
),
|
2023-06-02 06:10:22 -04:00
|
|
|
PreRunE: envCheck,
|
|
|
|
RunE: runCmd,
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
rootCmd.Flags().StringVarP(&maaFilePath, "maa-claims-path", "t", "", "File path to a json file containing the MAA claims.")
|
|
|
|
rootCmd.Flags().StringP("upload-date", "d", "", "upload a version with this date as version name.")
|
|
|
|
must(rootCmd.MarkFlagRequired("maa-claims-path"))
|
2023-06-05 06:33:22 -04:00
|
|
|
rootCmd.AddCommand(newDeleteCmd())
|
2023-06-01 07:55:46 -04:00
|
|
|
return rootCmd
|
|
|
|
}
|
|
|
|
|
2023-06-02 06:10:22 -04:00
|
|
|
func envCheck(_ *cobra.Command, _ []string) error {
|
|
|
|
if os.Getenv(envCosignPrivateKey) == "" || os.Getenv(envCosignPwd) == "" {
|
|
|
|
return fmt.Errorf("please set both %s and %s environment variables", envCosignPrivateKey, envCosignPwd)
|
|
|
|
}
|
|
|
|
cosignPwd = os.Getenv(envCosignPwd)
|
|
|
|
privateKey = os.Getenv(envCosignPrivateKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-01 07:55:46 -04:00
|
|
|
func runCmd(cmd *cobra.Command, _ []string) error {
|
|
|
|
ctx := cmd.Context()
|
2023-08-09 12:58:46 -04:00
|
|
|
log := logger.New(logger.PlainLog, zap.DebugLevel).Named("attestationconfigapi")
|
2023-06-01 07:55:46 -04:00
|
|
|
cfg := staticupload.Config{
|
|
|
|
Bucket: awsBucket,
|
|
|
|
Region: awsRegion,
|
|
|
|
}
|
2023-08-10 03:46:34 -04:00
|
|
|
log.Infof("Reading MAA claims from file: %s", maaFilePath)
|
2023-08-09 12:58:46 -04:00
|
|
|
maaClaimsBytes, err := os.ReadFile(maaFilePath)
|
2023-06-01 07:55:46 -04:00
|
|
|
if err != nil {
|
2023-08-09 12:58:46 -04:00
|
|
|
return fmt.Errorf("reading MAA claims file: %w", err)
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
var maaTCB maaTokenTCBClaims
|
|
|
|
if err = json.Unmarshal(maaClaimsBytes, &maaTCB); err != nil {
|
|
|
|
return fmt.Errorf("unmarshalling MAA claims file: %w", err)
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
inputVersion := maaTCB.ToAzureSEVSNPVersion()
|
2023-08-10 03:46:34 -04:00
|
|
|
log.Infof("Input version: %+v", inputVersion)
|
2023-06-01 07:55:46 -04:00
|
|
|
|
2023-06-09 06:48:12 -04:00
|
|
|
dateStr, err := cmd.Flags().GetString("upload-date")
|
2023-06-01 07:55:46 -04:00
|
|
|
if err != nil {
|
2023-06-09 06:48:12 -04:00
|
|
|
return fmt.Errorf("getting upload date: %w", err)
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
uploadDate := time.Now()
|
2023-06-09 06:48:12 -04:00
|
|
|
if dateStr != "" {
|
2023-06-12 10:04:54 -04:00
|
|
|
uploadDate, err = time.Parse(attestationconfigapi.VersionFormat, dateStr)
|
2023-06-09 06:48:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing date: %w", err)
|
|
|
|
}
|
2023-06-02 06:10:22 -04:00
|
|
|
}
|
2023-06-09 06:48:12 -04:00
|
|
|
|
2023-08-10 03:46:34 -04:00
|
|
|
latestAPIVersionAPI, err := attestationconfigapi.NewFetcher().FetchAzureSEVSNPVersionLatest(ctx, uploadDate)
|
2023-08-09 12:58:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fetching latest version: %w", err)
|
|
|
|
}
|
2023-08-10 03:46:34 -04:00
|
|
|
latestAPIVersion := latestAPIVersionAPI.AzureSEVSNPVersion
|
2023-06-09 06:48:12 -04:00
|
|
|
|
2023-08-10 03:46:34 -04:00
|
|
|
isNewer, err := isInputNewerThanLatestAPI(inputVersion, latestAPIVersion)
|
2023-08-09 12:58:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("comparing versions: %w", err)
|
|
|
|
}
|
|
|
|
if !isNewer {
|
2023-08-10 03:46:34 -04:00
|
|
|
log.Infof("Input version: %+v is not newer than latest API version: %+v", inputVersion, latestAPIVersion)
|
2023-08-09 12:58:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-10 03:46:34 -04:00
|
|
|
log.Infof("Input version: %+v is newer than latest API version: %+v", inputVersion, latestAPIVersion)
|
2023-06-01 07:55:46 -04:00
|
|
|
|
2023-08-09 12:58:46 -04:00
|
|
|
client, stop, err := attestationconfigapi.NewClient(ctx, cfg, []byte(cosignPwd), []byte(privateKey), false, log)
|
|
|
|
defer func() {
|
|
|
|
if err := stop(ctx); err != nil {
|
|
|
|
cmd.Printf("stopping client: %v\n", err)
|
2023-06-02 06:10:22 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating client: %w", err)
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
|
|
|
|
if err := client.UploadAzureSEVSNP(ctx, inputVersion, uploadDate); err != nil {
|
|
|
|
return fmt.Errorf("uploading version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("Successfully uploaded new Azure SEV-SNP version: %+v\n", inputVersion)
|
2023-06-01 07:55:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-09 12:58:46 -04:00
|
|
|
// maaTokenTCBClaims describes the TCB information in a MAA token.
|
|
|
|
type maaTokenTCBClaims struct {
|
2023-08-10 03:46:34 -04:00
|
|
|
IsolationTEE struct {
|
|
|
|
TEESvn uint8 `json:"x-ms-sevsnpvm-tee-svn"`
|
|
|
|
SNPFwSvn uint8 `json:"x-ms-sevsnpvm-snpfw-svn"`
|
|
|
|
MicrocodeSvn uint8 `json:"x-ms-sevsnpvm-microcode-svn"`
|
|
|
|
BootloaderSvn uint8 `json:"x-ms-sevsnpvm-bootloader-svn"`
|
|
|
|
} `json:"x-ms-isolation-tee"`
|
2023-08-09 12:58:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c maaTokenTCBClaims) ToAzureSEVSNPVersion() attestationconfigapi.AzureSEVSNPVersion {
|
|
|
|
return attestationconfigapi.AzureSEVSNPVersion{
|
2023-08-10 03:46:34 -04:00
|
|
|
TEE: c.IsolationTEE.TEESvn,
|
|
|
|
SNP: c.IsolationTEE.SNPFwSvn,
|
|
|
|
Microcode: c.IsolationTEE.MicrocodeSvn,
|
|
|
|
Bootloader: c.IsolationTEE.BootloaderSvn,
|
2023-06-09 06:48:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 06:10:22 -04:00
|
|
|
// isInputNewerThanLatestAPI compares all version fields with the latest API version and returns true if any input field is newer.
|
2023-06-07 10:16:32 -04:00
|
|
|
func isInputNewerThanLatestAPI(input, latest attestationconfigapi.AzureSEVSNPVersion) (bool, error) {
|
2023-08-09 12:58:46 -04:00
|
|
|
if input == latest {
|
|
|
|
return false, nil
|
2023-06-02 06:10:22 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
if input.TEE < latest.TEE {
|
|
|
|
return false, fmt.Errorf("input TEE version: %d is older than latest API version: %d", input.TEE, latest.TEE)
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
2023-08-09 12:58:46 -04:00
|
|
|
if input.SNP < latest.SNP {
|
|
|
|
return false, fmt.Errorf("input SNP version: %d is older than latest API version: %d", input.SNP, latest.SNP)
|
|
|
|
}
|
|
|
|
if input.Microcode < latest.Microcode {
|
|
|
|
return false, fmt.Errorf("input Microcode version: %d is older than latest API version: %d", input.Microcode, latest.Microcode)
|
|
|
|
}
|
|
|
|
if input.Bootloader < latest.Bootloader {
|
|
|
|
return false, fmt.Errorf("input Bootloader version: %d is older than latest API version: %d", input.Bootloader, latest.Bootloader)
|
|
|
|
}
|
|
|
|
return true, nil
|
2023-06-05 06:33:22 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 07:55:46 -04:00
|
|
|
func must(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|