Skip to content

Commit

Permalink
remove filter within time function
Browse files Browse the repository at this point in the history
  • Loading branch information
elchead committed Sep 20, 2023
1 parent 25a556b commit d9a3d92
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 74 deletions.
6 changes: 3 additions & 3 deletions internal/api/attestationconfigapi/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 3 additions & 18 deletions internal/api/attestationconfigapi/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
53 changes: 0 additions & 53 deletions internal/api/attestationconfigapi/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d9a3d92

Please sign in to comment.