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

Set default reporting start time to billing start date #27379

Merged
merged 2 commits into from
Jun 6, 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
5 changes: 5 additions & 0 deletions changelog/27379.txt
Original file line number Diff line number Diff line change
@@ -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.
```
4 changes: 2 additions & 2 deletions command/operator_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ 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{},
Formats: TimeVar_TimeOrDay | TimeVar_Month,
})
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{},
Expand Down
48 changes: 48 additions & 0 deletions vault/external_tests/activity/activity_util.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 9 additions & 8 deletions vault/logical_system_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
}
Expand All @@ -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())
mpalmi marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading