Set default reporting start time to billing start date (#27379)

* Apply oss patch

* Added changelog
This commit is contained in:
divyaac
2024-06-06 10:13:04 -07:00
committed by GitHub
parent 566aaa661a
commit f3eedc4aa2
4 changed files with 64 additions and 10 deletions

5
changelog/27379.txt Normal file
View File

@@ -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.
```

View File

@@ -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{},

View File

@@ -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)
}
}

View File

@@ -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
}