From 9f797d8b80a01dc728beee25d2f21856ded14101 Mon Sep 17 00:00:00 2001 From: Adrian Stobbe Date: Wed, 20 Sep 2023 14:32:57 +0200 Subject: [PATCH] remove filter within time function --- internal/api/attestationconfigapi/cli/main.go | 13 ++--- .../api/attestationconfigapi/fetcher_test.go | 2 +- internal/api/attestationconfigapi/reporter.go | 26 +++------ .../api/attestationconfigapi/reporter_test.go | 53 ------------------- 4 files changed, 11 insertions(+), 83 deletions(-) diff --git a/internal/api/attestationconfigapi/cli/main.go b/internal/api/attestationconfigapi/cli/main.go index e2aa7f4c1e..aad439f658 100644 --- a/internal/api/attestationconfigapi/cli/main.go +++ b/internal/api/attestationconfigapi/cli/main.go @@ -15,8 +15,6 @@ Notice that there is no synchronization on API operations. // TODO(elchead): wha */ package main -// TODO: separate reporter and upload CLI to ease manual upload or use force flag? - import ( "encoding/json" "errors" @@ -70,13 +68,13 @@ func newRootCmd() *cobra.Command { } rootCmd.Flags().StringP("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.") + rootCmd.Flags().BoolP("force", "f", false, "Use force to manually push a new latest version."+ + " The version gets reported in the cache but the version selection logic is skipped.") + rootCmd.Flags().IntP("cache-window-size", "s", 0, "Number of versions to be considered for the latest version.") rootCmd.PersistentFlags().StringP("region", "r", awsRegion, "region of the targeted bucket.") rootCmd.PersistentFlags().StringP("bucket", "b", awsBucket, "bucket targeted by all operations.") rootCmd.PersistentFlags().StringP("distribution", "i", distributionID, "cloudflare distribution used.") must(rootCmd.MarkFlagRequired("maa-claims-path")) - rootCmd.LocalFlags().BoolP("force", "f", false, "Use force to manually push a new latest version."+ - " The version gets reported in the cache but the version selection logic is skipped.") - rootCmd.LocalFlags().IntP("cache-window-size", "s", 0, "Number of versions to be considered for the latest version.") rootCmd.AddCommand(newDeleteCmd()) return rootCmd } @@ -137,12 +135,9 @@ func runCmd(cmd *cobra.Command, _ []string) (retErr error) { if err != nil { return fmt.Errorf("creating client: %w", err) } - if err := client.UpdateLatestVersion(ctx, inputVersion, latestAPIVersion, flags.uploadDate, flags.force); err != nil { + if err := client.UpdateLatestAzureSEVSNPVersion(ctx, inputVersion, latestAPIVersion, flags.uploadDate, flags.force); err != nil { return fmt.Errorf("updating latest version: %w", err) } - // TODO move back in after refactor - // cmd.Printf("Successfully uploaded new Azure SEV-SNP version: %+v\n", inputVersion) - return nil } diff --git a/internal/api/attestationconfigapi/fetcher_test.go b/internal/api/attestationconfigapi/fetcher_test.go index 372ef34cdd..7338752882 100644 --- a/internal/api/attestationconfigapi/fetcher_test.go +++ b/internal/api/attestationconfigapi/fetcher_test.go @@ -28,7 +28,7 @@ func TestFetchLatestAzureSEVSNPVersion(t *testing.T) { wantErr bool want AzureSEVSNPVersionAPI }{ - "get latest version if older than 2 weeks": { + "get latest version": { fetcherVersions: []string{latestStr, olderStr}, want: latestVersion, }, diff --git a/internal/api/attestationconfigapi/reporter.go b/internal/api/attestationconfigapi/reporter.go index 2219468f02..cd252d2cae 100644 --- a/internal/api/attestationconfigapi/reporter.go +++ b/internal/api/attestationconfigapi/reporter.go @@ -33,10 +33,10 @@ const versionWindowSize = 15 var reportVersionDir = path.Join(attestationURLPath, variant.AzureSEVSNP{}.String(), cachedVersionsSubDir) -// UpdateLatestVersion reports the given version, checks the reported version values +// UpdateLatestAzureSEVSNPVersion reports the given version, checks the reported version values // and updates the latest version of the Azure SEVSNP in the API if there is an update . // force can be used to force an update of the latest API version regardless of the cache reporting. -func (c Client) UpdateLatestVersion(ctx context.Context, inputVersion, +func (c Client) UpdateLatestAzureSEVSNPVersion(ctx context.Context, inputVersion, latestAPIVersion AzureSEVSNPVersion, now time.Time, force bool, ) error { if err := c.reportAzureSEVSNPVersion(ctx, inputVersion, now); err != nil { @@ -52,8 +52,8 @@ func (c Client) UpdateLatestVersion(ctx context.Context, inputVersion, if err != nil { return fmt.Errorf("list reported versions: %w", err) } - if len(versionDates) < versionWindowSize { - c.s3Client.Logger.Infof("Skipping version update since found only %d out of expected reported versions.", len(versionDates), versionWindowSize) + if len(versionDates) < c.cacheWindowSize { + c.s3Client.Logger.Infof("Skipping version update since found only %d out of expected reported versions.", len(versionDates), c.cacheWindowSize) return nil } minVersion, minDate, err := c.findMinVersion(ctx, versionDates) @@ -71,6 +71,7 @@ func (c Client) UpdateLatestVersion(ctx context.Context, inputVersion, if err := c.uploadAzureSEVSNPVersion(ctx, *minVersion, t); err != nil { return fmt.Errorf("uploading version: %w", err) } + c.s3Client.Logger.Infof("Successfully uploaded new Azure SEV-SNP version: %+v", *minVersion) return nil } c.s3Client.Logger.Infof("Input version: %+v is not newer than latest API version: %+v", *minVersion, latestAPIVersion) @@ -114,7 +115,7 @@ func (c Client) findMinVersion(ctx context.Context, versionDates []string) (*Azu var minimalVersion *AzureSEVSNPVersion var minimalDate string sort.Strings(versionDates) // the oldest date with the minimal version should be taken - versionDates = versionDates[:versionWindowSize] + versionDates = versionDates[:c.cacheWindowSize] for _, date := range versionDates { obj, err := client.Fetch(ctx, c.s3Client, reportedAzureSEVSNPVersionAPI{Version: date + ".json"}) if err != nil { @@ -138,21 +139,6 @@ func (c Client) findMinVersion(ctx context.Context, versionDates []string) (*Azu return minimalVersion, minimalDate, nil } -func filterDatesWithinTime(dates []string, now time.Time, timeFrame time.Duration) []string { - var datesWithinTimeFrame []string - for _, date := range dates { - t, err := time.Parse(VersionFormat, date) - if err != nil { - continue - } - fmt.Println(now, " t ", t, " sub ", now.Sub(t)) - if now.Sub(t) >= 0 && now.Sub(t) <= timeFrame { - datesWithinTimeFrame = append(datesWithinTimeFrame, date) - } - } - return datesWithinTimeFrame -} - // isInputNewerThanOtherVersion compares all version fields and returns true if any input field is newer. func isInputNewerThanOtherVersion(input, other AzureSEVSNPVersion) (bool, error) { if input == other { diff --git a/internal/api/attestationconfigapi/reporter_test.go b/internal/api/attestationconfigapi/reporter_test.go index 17792dac8a..bfca2f6455 100644 --- a/internal/api/attestationconfigapi/reporter_test.go +++ b/internal/api/attestationconfigapi/reporter_test.go @@ -6,63 +6,10 @@ package attestationconfigapi import ( "testing" - "time" "github.com/stretchr/testify/assert" ) -func TestFilterDatesWithinTime(t *testing.T) { - dates := []string{ - "2022-01-01-00-00", - "2022-01-02-00-00", - "2022-01-03-00-00", - "2022-01-04-00-00", - "2022-01-05-00-00", - "2022-01-06-00-00", - "2022-01-07-00-00", - "2022-01-08-00-00", - } - now := time.Date(2022, 1, 9, 0, 0, 0, 0, time.UTC) - testCases := map[string]struct { - timeFrame time.Duration - expectedDates []string - customDates *[]string - }{ - "all dates within 3 days": { - timeFrame: time.Hour * 24 * 3, - expectedDates: []string{"2022-01-06-00-00", "2022-01-07-00-00", "2022-01-08-00-00"}, - }, - "ignore dates newer than now": { - timeFrame: time.Hour * 24 * 3, - customDates: toPtr(append(dates, "2023-01-09-00-00")), - expectedDates: []string{"2022-01-06-00-00", "2022-01-07-00-00", "2022-01-08-00-00"}, - }, - "no dates within time frame": { - timeFrame: time.Hour, - expectedDates: nil, - }, - "some dates within time frame": { - timeFrame: time.Hour * 24 * 4, - expectedDates: []string{"2022-01-05-00-00", "2022-01-06-00-00", "2022-01-07-00-00", "2022-01-08-00-00"}, - }, - } - - for name, tc := range testCases { - t.Run(name, func(t *testing.T) { - dates := dates - if tc.customDates != nil { - dates = *tc.customDates - } - filteredDates := filterDatesWithinTime(dates, now, tc.timeFrame) - assert.Equal(t, tc.expectedDates, filteredDates) - }) - } -} - -func toPtr[T any](v T) *T { - return &v -} - func TestIsInputNewerThanLatestAPI(t *testing.T) { newTestCfg := func() AzureSEVSNPVersion { return AzureSEVSNPVersion{