From d9a3d92f49a81310f1d0bcfd022c3b3b1e568f36 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 | 6 +-- internal/api/attestationconfigapi/reporter.go | 21 ++------ .../api/attestationconfigapi/reporter_test.go | 53 ------------------- 3 files changed, 6 insertions(+), 74 deletions(-) diff --git a/internal/api/attestationconfigapi/cli/main.go b/internal/api/attestationconfigapi/cli/main.go index e2aa7f4c1ee..75fd63b9fcb 100644 --- a/internal/api/attestationconfigapi/cli/main.go +++ b/internal/api/attestationconfigapi/cli/main.go @@ -70,13 +70,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 } diff --git a/internal/api/attestationconfigapi/reporter.go b/internal/api/attestationconfigapi/reporter.go index 2219468f029..106cbd1d110 100644 --- a/internal/api/attestationconfigapi/reporter.go +++ b/internal/api/attestationconfigapi/reporter.go @@ -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) @@ -114,7 +114,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 +138,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 17792dac8a0..bfca2f64555 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{