diff --git a/changelog/27379.txt b/changelog/27379.txt new file mode 100644 index 000000000000..1744873c2839 --- /dev/null +++ b/changelog/27379.txt @@ -0,0 +1,5 @@ +```release-note:change +activity: The startTime will be set to the start of the current billing period by default. +The endTime will be set to the end of the current month. This applies to /sys/internal/counters/activity, +/sys/internal/counters/activity/export, and the vault operator usage command that utilizes /sys/internal/counters/activity. +``` diff --git a/command/operator_usage.go b/command/operator_usage.go index 079ddaba8fbc..117b3b78e018 100644 --- a/command/operator_usage.go +++ b/command/operator_usage.go @@ -56,7 +56,7 @@ func (c *OperatorUsageCommand) Flags() *FlagSets { f.TimeVar(&TimeVar{ Name: "start-time", - Usage: "Start of report period. Defaults to 'default_reporting_period' before end time.", + Usage: "Start of report period. Defaults to billing start time", Target: &c.flagStartTime, Completion: complete.PredictNothing, Default: time.Time{}, @@ -64,7 +64,7 @@ func (c *OperatorUsageCommand) Flags() *FlagSets { }) f.TimeVar(&TimeVar{ Name: "end-time", - Usage: "End of report period. Defaults to end of last month.", + Usage: "End of report period. Defaults to end of the current month.", Target: &c.flagEndTime, Completion: complete.PredictNothing, Default: time.Time{}, diff --git a/vault/external_tests/activity/activity_util.go b/vault/external_tests/activity/activity_util.go new file mode 100644 index 000000000000..499eb46ff33c --- /dev/null +++ b/vault/external_tests/activity/activity_util.go @@ -0,0 +1,48 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package activity + +import ( + "testing" + "time" + + "github.com/hashicorp/vault/api" + "github.com/hashicorp/vault/vault" + "github.com/mitchellh/mapstructure" +) + +type QueryResponse struct { + StartTime string `json:"start_time" mapstructure:"start_time"` + EndTime string `json:"end_time" mapstructure:"end_time"` + ByNamespace []*vault.ResponseNamespace `json:"by_namespace"` + Total *vault.ResponseCounts `json:"total"` +} + +func expectEndTime(t *testing.T, expected time.Time, resp *api.Secret) { + t.Helper() + + qr := QueryResponse{} + mapstructure.Decode(resp.Data, &qr) + parsedTime, err := time.Parse(time.RFC3339, qr.EndTime) + if err != nil { + t.Fatal(err) + } + if !expected.Equal(parsedTime) { + t.Errorf("wrong end time, expected %v actual %v", expected, parsedTime) + } +} + +func expectStartTime(t *testing.T, expected time.Time, resp *api.Secret) { + t.Helper() + + var qr QueryResponse + mapstructure.Decode(resp.Data, &qr) + parsedTime, err := time.Parse(time.RFC3339, qr.StartTime) + if err != nil { + t.Fatal(err) + } + if !expected.Equal(parsedTime) { + t.Errorf("wrong start time, expected %v actual %v", expected, parsedTime) + } +} diff --git a/vault/logical_system_activity.go b/vault/logical_system_activity.go index c286b572d07d..f7607a191cd0 100644 --- a/vault/logical_system_activity.go +++ b/vault/logical_system_activity.go @@ -13,7 +13,6 @@ import ( "time" "github.com/hashicorp/go-secure-stdlib/parseutil" - "github.com/hashicorp/vault/helper/timeutil" "github.com/hashicorp/vault/sdk/framework" "github.com/hashicorp/vault/sdk/logical" ) @@ -182,23 +181,25 @@ func (b *SystemBackend) rootActivityPaths() []*framework.Path { return paths } -func parseStartEndTimes(a *ActivityLog, d *framework.FieldData) (time.Time, time.Time, error) { +func parseStartEndTimes(a *ActivityLog, d *framework.FieldData, billingStartTime time.Time) (time.Time, time.Time, error) { startTime := d.Get("start_time").(time.Time) endTime := d.Get("end_time").(time.Time) // If a specific endTime is used, then respect that - // otherwise we want to give the latest N months, so go back to the start - // of the previous month + // otherwise we want to query up until the end of the current month. // // Also convert any user inputs to UTC to avoid // problems later. if endTime.IsZero() { - endTime = timeutil.EndOfMonth(timeutil.StartOfPreviousMonth(time.Now().UTC())) + endTime = time.Now().UTC() } else { endTime = endTime.UTC() } + + // If startTime is not specified, we would like to query + // from the beginning of the billing period if startTime.IsZero() { - startTime = a.DefaultStartTime(endTime) + startTime = billingStartTime } else { startTime = startTime.UTC() } @@ -218,7 +219,7 @@ func (b *SystemBackend) handleClientExport(ctx context.Context, req *logical.Req return logical.ErrorResponse("no activity log present"), nil } - startTime, endTime, err := parseStartEndTimes(a, d) + startTime, endTime, err := parseStartEndTimes(a, d, b.Core.BillingStart()) if err != nil { return logical.ErrorResponse(err.Error()), nil } @@ -257,7 +258,7 @@ func (b *SystemBackend) handleClientMetricQuery(ctx context.Context, req *logica endTime = time.Now().UTC() } else { var err error - startTime, endTime, err = parseStartEndTimes(a, d) + startTime, endTime, err = parseStartEndTimes(a, d, b.Core.BillingStart()) if err != nil { return logical.ErrorResponse(err.Error()), nil }