Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent using future dates for LastSenAt and BackoffAt #1089

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,11 @@ func LoadAPIParams(v *viper.Viper) (API, error) {
backoffAtStr := vipertools.GetString(v, "internal.backoff_at")
if backoffAtStr != "" {
parsed, err := time.Parse(ini.DateFormat, backoffAtStr)
// nolint:gocritic
if err != nil {
log.Warnf("failed to parse backoff_at: %s", err)
} else if parsed.After(time.Now()) {
backoffAt = time.Now()
} else {
backoffAt = parsed
}
Expand Down Expand Up @@ -664,8 +667,11 @@ func LoadOfflineParams(v *viper.Viper) Offline {
lastSentAtStr := vipertools.GetString(v, "internal.heartbeats_last_sent_at")
if lastSentAtStr != "" {
parsed, err := time.Parse(ini.DateFormat, lastSentAtStr)
// nolint:gocritic
if err != nil {
log.Warnf("failed to parse heartbeats_last_sent_at: %s", err)
} else if parsed.After(time.Now()) {
lastSentAt = time.Now()
} else {
lastSentAt = parsed
}
Expand Down
26 changes: 26 additions & 0 deletions cmd/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,16 @@ func TestLoadOfflineParams_LastSentAt_Err(t *testing.T) {
assert.Zero(t, params.LastSentAt)
}

func TestLoadOfflineParams_LastSentAtFuture(t *testing.T) {
v := viper.New()
lastSentAt := time.Now().Add(time.Duration(2) * time.Hour)
v.Set("internal.heartbeats_last_sent_at", lastSentAt.Format(inipkg.DateFormat))

params := cmdparams.LoadOfflineParams(v)

assert.LessOrEqual(t, params.LastSentAt, time.Now())
}

func TestLoadOfflineParams_SyncMax(t *testing.T) {
v := viper.New()
v.Set("sync-offline-activity", 42)
Expand Down Expand Up @@ -2148,6 +2158,22 @@ func TestLoadAPIParams_BackoffAtErr(t *testing.T) {
}, params)
}

func TestLoadAPIParams_BackoffAtFuture(t *testing.T) {
v := viper.New()
backoff := time.Now().Add(time.Duration(2) * time.Hour)

v.Set("hostname", "my-computer")
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("internal.backoff_at", backoff.Format(inipkg.DateFormat))
v.Set("internal.backoff_retries", "3")

params, err := cmdparams.LoadAPIParams(v)
require.NoError(t, err)

assert.Equal(t, 3, params.BackoffRetries)
assert.LessOrEqual(t, params.BackoffAt, time.Now())
}

func TestLoadAPIParams_DisableSSLVerify_FlagTakesPrecedence(t *testing.T) {
v := viper.New()
v.Set("key", "00000000-0000-4000-8000-000000000000")
Expand Down
Loading