Skip to content

Commit

Permalink
fixup! fix delete + behavior without any version
Browse files Browse the repository at this point in the history
  • Loading branch information
elchead committed Sep 22, 2023
1 parent 7fd1c5b commit db3a25c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 7 additions & 1 deletion internal/api/attestationconfigapi/cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@ func runDelete(cmd *cobra.Command, _ []string) (retErr error) {
return fmt.Errorf("getting distribution: %w", err)
}

windowSize, err := cmd.Flags().GetInt("cache-window-size")
if err != nil {
return fmt.Errorf("getting cache window size: %w", err)
}

cfg := staticupload.Config{
Bucket: bucket,
Region: region,
DistributionID: distribution,
}
client, clientClose, err := attestationconfigapi.NewClient(cmd.Context(), cfg, []byte(cosignPwd), []byte(privateKey), false, log)
client, clientClose, err := attestationconfigapi.NewClient(cmd.Context(), cfg,
[]byte(cosignPwd), []byte(privateKey), false, windowSize, log)
if err != nil {
return fmt.Errorf("create attestation client: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/api/attestationconfigapi/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ func runCmd(cmd *cobra.Command, _ []string) (retErr error) {
}

url := "https://d33dzgxuwsgbpw.cloudfront.net"
latestAPIVersionAPI, err := attestationconfigapi.NewFetcherWithCustomCDN(url).FetchAzureSEVSNPVersionLatest(ctx)
latestAPIVersionAPI, err := attestationconfigapi.NewFetcherWithCustomCDNAndCosignKey(url, constants.CosignPublicKeyDev).FetchAzureSEVSNPVersionLatest(ctx)
if err != nil {
if errors.Is(err, attestationconfigapi.ErrNoVersionsFound) && flags.force {
log.Infof("No versions found in API, but assuming that we are uploading the first version.\n")
if errors.Is(err, attestationconfigapi.ErrNoVersionsFound) {
log.Infof("No versions found in API, but assuming that we are uploading the first version.")
} else {
return fmt.Errorf("fetching latest version: %w", err)
}
Expand Down
15 changes: 10 additions & 5 deletions internal/api/attestationconfigapi/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ func NewFetcher() Fetcher {
return NewFetcherWithClient(apifetcher.NewHTTPClient(), constants.CDNRepositoryURL)
}

// NewFetcherWithCustomCDN returns a new fetcher with custom CDN URL.
func NewFetcherWithCustomCDN(cdnURL string) Fetcher {
return NewFetcherWithClient(apifetcher.NewHTTPClient(), cdnURL)
// NewFetcherWithCustomCDNAndCosignKey returns a new fetcher with custom CDN URL.
func NewFetcherWithCustomCDNAndCosignKey(cdnURL, cosignKey string) Fetcher {
verifier, err := sigstore.NewCosignVerifier([]byte(cosignKey))
if err != nil {
// This relies on an embedded public key. If this key can not be validated, there is no way to recover from this.
panic(fmt.Errorf("creating cosign verifier: %w", err))
}
return newFetcherWithClientAndVerifier(apifetcher.NewHTTPClient(), verifier, cdnURL)
}

// NewFetcherWithClient returns a new fetcher with custom http client.
Expand All @@ -69,7 +74,7 @@ func (f *fetcher) FetchAzureSEVSNPVersionList(ctx context.Context, attestation A
func (f *fetcher) FetchAzureSEVSNPVersion(ctx context.Context, azureVersion AzureSEVSNPVersionAPI) (AzureSEVSNPVersionAPI, error) {
fetchedVersion, err := apifetcher.FetchAndVerify(ctx, f.HTTPClient, f.cdnURL, azureVersion, f.verifier)
if err != nil {
return fetchedVersion, fmt.Errorf("fetch version %s: %w", fetchedVersion.Version, err)
return fetchedVersion, fmt.Errorf("fetching version %s: %w", azureVersion.Version, err)
}
return fetchedVersion, nil
}
Expand All @@ -89,7 +94,7 @@ func (f *fetcher) FetchAzureSEVSNPVersionLatest(ctx context.Context) (res AzureS
}
res, err = f.FetchAzureSEVSNPVersion(ctx, getVersionRequest)
if err != nil {
return res, fmt.Errorf("fetching version: %w", err)
return res, err
}
return
}
3 changes: 1 addition & 2 deletions internal/api/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func FetchAndVerify[T apiObject](ctx context.Context, c HTTPClient, cdnURL strin
if err != nil {
return fetchedObj, fmt.Errorf("fetching signature: %w", err)
}

err = cosignVerifier.VerifySignature(marshalledObj, signature.Signature)
if err != nil {
return fetchedObj, fmt.Errorf("verifying signature: %w", err)
Expand Down Expand Up @@ -130,7 +129,7 @@ type apiObject interface {
// signature manages the signature of a object saved at location 'Signed'.
type signature struct {
// Signed is the object that is signed.
Signed string `json:"signed"`
Signed string `json:"-"`
// Signature is the signature of `Signed`.
Signature []byte `json:"signature"`
}
Expand Down

0 comments on commit db3a25c

Please sign in to comment.