diff --git a/vault/activity_log.go b/vault/activity_log.go index 7e334a2db83e..b8d999e69c33 100644 --- a/vault/activity_log.go +++ b/vault/activity_log.go @@ -1736,6 +1736,9 @@ func modifyResponseMonths(months []*ResponseMonth, start time.Time, end time.Tim return months } start = timeutil.StartOfMonth(start) + if timeutil.IsCurrentMonth(end, time.Now().UTC()) { + end = timeutil.StartOfMonth(end).AddDate(0, -1, 0) + } end = timeutil.EndOfMonth(end) modifiedResponseMonths := make([]*ResponseMonth, 0) firstMonth, err := time.Parse(time.RFC3339, months[0].Timestamp) @@ -1753,6 +1756,10 @@ func modifyResponseMonths(months []*ResponseMonth, start time.Time, end time.Tim lastMonth = timeutil.StartOfMonth(lastMonth.AddDate(0, 1, 0)) monthPlaceholder := &ResponseMonth{Timestamp: lastMonth.UTC().Format(time.RFC3339)} modifiedResponseMonths = append(modifiedResponseMonths, monthPlaceholder) + + // reset lastMonth to be the end of the month so we can make an apt comparison + // in the next loop iteration + lastMonth = timeutil.EndOfMonth(lastMonth) } return modifiedResponseMonths } diff --git a/vault/activity_log_test.go b/vault/activity_log_test.go index 53d69c9d219e..ff0b700e35f4 100644 --- a/vault/activity_log_test.go +++ b/vault/activity_log_test.go @@ -472,6 +472,36 @@ func TestActivityLog_SaveEntitiesToStorage(t *testing.T) { expectedEntityIDs(t, out, ids) } +func TestModifyResponseMonthsNilAppend(t *testing.T) { + end := time.Now().UTC() + start := timeutil.StartOfMonth(end).AddDate(0, -5, 0) + responseMonthTimestamp := timeutil.StartOfMonth(end).AddDate(0, -3, 0).Format(time.RFC3339) + responseMonths := []*ResponseMonth{{Timestamp: responseMonthTimestamp}} + months := modifyResponseMonths(responseMonths, start, end) + if len(months) != 5 { + t.Fatal("wrong number of months padded") + } + for _, m := range months { + ts, err := time.Parse(time.RFC3339, m.Timestamp) + if err != nil { + t.Fatal(err) + } + if !ts.Equal(start) { + t.Fatalf("incorrect time in month sequence timestamps: expected %+v, got %+v", start, ts) + } + start = timeutil.StartOfMonth(start).AddDate(0, 1, 0) + } + // The following is a redundant check, but for posterity and readability I've + // made it explicit. + lastMonth, err := time.Parse(time.RFC3339, months[4].Timestamp) + if err != nil { + t.Fatal(err) + } + if timeutil.IsCurrentMonth(lastMonth, time.Now().UTC()) { + t.Fatalf("do not include current month timestamp in nil padding for months") + } +} + func TestActivityLog_ReceivedFragment(t *testing.T) { core, _, _ := TestCoreUnsealed(t) a := core.activityLog