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 9f797d8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 83 deletions.
13 changes: 4 additions & 9 deletions internal/api/attestationconfigapi/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/attestationconfigapi/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
26 changes: 6 additions & 20 deletions internal/api/attestationconfigapi/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
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 9f797d8

Please sign in to comment.