From 1b5d5275215ea01c5cbe4c5d53c22ff5af16809d Mon Sep 17 00:00:00 2001 From: Mike Palmiotto Date: Wed, 12 Apr 2023 12:02:28 -0400 Subject: [PATCH] api: Add reporting fields to activitylog config endpoint (#20086) This PR adds the internal reporting state to the `internal/counters/config` read endpoint: * reporting_enabled * billing_start_timestamp --- changelog/20086.txt | 4 +++ vault/activity_log_test.go | 30 +++++++++++-------- vault/census.go | 6 +++- vault/core.go | 11 +++++++ vault/logical_system_activity.go | 10 ++++--- .../api-docs/system/internal-counters.mdx | 4 ++- 6 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 changelog/20086.txt diff --git a/changelog/20086.txt b/changelog/20086.txt new file mode 100644 index 000000000..9511c97b6 --- /dev/null +++ b/changelog/20086.txt @@ -0,0 +1,4 @@ +```release-note:improvement +api: `/sys/internal/counters/config` endpoint now contains read-only +`reporting_enabled` and `billing_start_timestamp` fields. +``` diff --git a/vault/activity_log_test.go b/vault/activity_log_test.go index 8f29113cd..5ffbbb18b 100644 --- a/vault/activity_log_test.go +++ b/vault/activity_log_test.go @@ -834,10 +834,12 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) { t.Fatalf("err: %v", err) } defaults := map[string]interface{}{ - "default_report_months": 12, - "retention_months": 24, - "enabled": activityLogEnabledDefaultValue, - "queries_available": false, + "default_report_months": 12, + "retention_months": 24, + "enabled": activityLogEnabledDefaultValue, + "queries_available": false, + "reporting_enabled": core.censusLicensingEnabled, + "billing_start_timestamp": core.GetBillingStart(), } if diff := deep.Equal(resp.Data, defaults); len(diff) > 0 { @@ -915,10 +917,12 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) { t.Fatalf("err: %v", err) } expected := map[string]interface{}{ - "default_report_months": 1, - "retention_months": 2, - "enabled": "enable", - "queries_available": false, + "default_report_months": 1, + "retention_months": 2, + "enabled": "enable", + "queries_available": false, + "reporting_enabled": false, + "billing_start_timestamp": core.GetBillingStart(), } if diff := deep.Equal(resp.Data, expected); len(diff) > 0 { @@ -951,10 +955,12 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) { } defaults := map[string]interface{}{ - "default_report_months": 12, - "retention_months": 24, - "enabled": activityLogEnabledDefaultValue, - "queries_available": false, + "default_report_months": 12, + "retention_months": 24, + "enabled": activityLogEnabledDefaultValue, + "queries_available": false, + "reporting_enabled": false, + "billing_start_timestamp": core.GetBillingStart(), } if diff := deep.Equal(resp.Data, defaults); len(diff) > 0 { diff --git a/vault/census.go b/vault/census.go index 2312b3b54..603fbf481 100644 --- a/vault/census.go +++ b/vault/census.go @@ -2,5 +2,9 @@ package vault +import "time" + // CensusAgent is a stub for OSS -type CensusAgent struct{} +type CensusAgent struct { + billingStart time.Time +} diff --git a/vault/core.go b/vault/core.go index 8f2503a16..ec4452499 100644 --- a/vault/core.go +++ b/vault/core.go @@ -4015,3 +4015,14 @@ func (c *Core) GetRaftAutopilotState(ctx context.Context) (*raft.AutopilotState, func (c *Core) Events() *eventbus.EventBus { return c.events } + +// GetBillingStart gets the billing start timestamp from the configured Census +// Agent, handling a nil agent. +func (c *Core) GetBillingStart() time.Time { + var billingStart time.Time + if c.censusAgent != nil { + billingStart = c.censusAgent.billingStart + } + + return billingStart +} diff --git a/vault/logical_system_activity.go b/vault/logical_system_activity.go index b4cdb0d66..fce5f0acf 100644 --- a/vault/logical_system_activity.go +++ b/vault/logical_system_activity.go @@ -267,10 +267,12 @@ func (b *SystemBackend) handleActivityConfigRead(ctx context.Context, req *logic return &logical.Response{ Data: map[string]interface{}{ - "default_report_months": config.DefaultReportMonths, - "retention_months": config.RetentionMonths, - "enabled": config.Enabled, - "queries_available": qa, + "default_report_months": config.DefaultReportMonths, + "retention_months": config.RetentionMonths, + "enabled": config.Enabled, + "queries_available": qa, + "reporting_enabled": b.Core.censusLicensingEnabled, + "billing_start_timestamp": b.Core.GetBillingStart(), }, }, nil } diff --git a/website/content/api-docs/system/internal-counters.mdx b/website/content/api-docs/system/internal-counters.mdx index 5ea156e31..6c1077c05 100644 --- a/website/content/api-docs/system/internal-counters.mdx +++ b/website/content/api-docs/system/internal-counters.mdx @@ -924,7 +924,9 @@ $ curl \ "default_report_months": 12, "enabled": "default-enabled", "queries_available": true, - "retention_months": 24 + "retention_months": 24, + "reporting_enabled": false, + "billing_start_timestamp": "2022-03-01T00:00:00Z", }, "warnings": null }