Skip to content

Commit

Permalink
use variant interface instead of fmt.Stringer
Browse files Browse the repository at this point in the history
  • Loading branch information
elchead committed Sep 10, 2024
1 parent 40a179a commit e4576a2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
30 changes: 26 additions & 4 deletions api/attestationconfigapi/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,31 @@ import (

const cosignPublicKey = constants.CosignPublicKeyReleases

var (
// AzureSEVSNP is the Azure SEV-SNP variant.
AzureSEVSNP Variant = attestationVariant{variant: variant.AzureSEVSNP{}}
// AWSSEVSNP is the AWS SEV-SNP variant.
AWSSEVSNP Variant = attestationVariant{variant: variant.AWSSEVSNP{}}
// GCPSEVSNP is the GCP SEV-SNP variant.
GCPSEVSNP Variant = attestationVariant{variant: variant.GCPSEVSNP{}}
)

type attestationVariant struct {
variant Variant
}

func (v attestationVariant) String() string {
return v.variant.String()
}

// Variant is a cloud provider specific attestation variant.
type Variant interface {
String() string
}

// Fetcher fetches config API resources without authentication.
type Fetcher interface {
FetchLatestVersion(ctx context.Context, attestation fmt.Stringer) (Entry, error)
FetchLatestVersion(ctx context.Context, attestation Variant) (Entry, error)
}

// fetcher fetches AttestationCfg API resources without authentication.
Expand Down Expand Up @@ -60,7 +82,7 @@ func newFetcherWithClientAndVerifier(client apifetcher.HTTPClient, cosignVerifie
}

// FetchLatestVersion returns the latest versions of the given type.
func (f *fetcher) FetchLatestVersion(ctx context.Context, variant fmt.Stringer) (Entry, error) {
func (f *fetcher) FetchLatestVersion(ctx context.Context, variant Variant) (Entry, error) {
list, err := f.fetchVersionList(ctx, variant)
if err != nil {
return Entry{}, err
Expand All @@ -71,7 +93,7 @@ func (f *fetcher) FetchLatestVersion(ctx context.Context, variant fmt.Stringer)
}

// fetchVersionList fetches the version list information from the config API.
func (f *fetcher) fetchVersionList(ctx context.Context, attestationVariant fmt.Stringer) (List, error) {
func (f *fetcher) fetchVersionList(ctx context.Context, attestationVariant Variant) (List, error) {
parsedVariant, err := variant.FromString(attestationVariant.String())
if err != nil {
return List{}, fmt.Errorf("parsing variant: %w", err)
Expand All @@ -88,7 +110,7 @@ func (f *fetcher) fetchVersionList(ctx context.Context, attestationVariant fmt.S
}

// fetchVersion fetches the version information from the config API.
func (f *fetcher) fetchVersion(ctx context.Context, version string, attestationVariant fmt.Stringer) (Entry, error) {
func (f *fetcher) fetchVersion(ctx context.Context, version string, attestationVariant Variant) (Entry, error) {
parsedVariant, err := variant.FromString(attestationVariant.String())
if err != nil {
return Entry{}, fmt.Errorf("parsing variant: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions cli/internal/cmd/configfetchmeasurements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd

import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -205,7 +204,7 @@ func (f stubVerifyFetcher) FetchAndVerifyMeasurements(_ context.Context, _ strin

type stubAttestationFetcher struct{}

func (f stubAttestationFetcher) FetchLatestVersion(_ context.Context, _ fmt.Stringer) (attestationconfigapi.Entry, error) {
func (f stubAttestationFetcher) FetchLatestVersion(_ context.Context, _ attestationconfigapi.Variant) (attestationconfigapi.Entry, error) {
return attestationconfigapi.Entry{
SEVSNPVersion: testCfg,
}, nil
Expand Down
3 changes: 1 addition & 2 deletions cli/internal/cmd/iamupgradeapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd

import (
"context"
"fmt"
"io"
"path/filepath"
"strings"
Expand Down Expand Up @@ -171,6 +170,6 @@ type stubConfigFetcher struct {
fetchLatestErr error
}

func (s *stubConfigFetcher) FetchLatestVersion(context.Context, fmt.Stringer) (attestationconfigapi.Entry, error) {
func (s *stubConfigFetcher) FetchLatestVersion(context.Context, attestationconfigapi.Variant) (attestationconfigapi.Entry, error) {
return attestationconfigapi.Entry{}, s.fetchLatestErr
}
3 changes: 1 addition & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package config
import (
"context"
"errors"
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -1052,7 +1051,7 @@ func getConfigAsMap(conf *Config, t *testing.T) (res configMap) {

type stubAttestationFetcher struct{}

func (f stubAttestationFetcher) FetchLatestVersion(_ context.Context, _ fmt.Stringer) (attestationconfigapi.Entry, error) {
func (f stubAttestationFetcher) FetchLatestVersion(_ context.Context, _ attestationconfigapi.Variant) (attestationconfigapi.Entry, error) {
return attestationconfigapi.Entry{
SEVSNPVersion: testCfg,
}, nil
Expand Down

0 comments on commit e4576a2

Please sign in to comment.