From 8001d76e28dcad76ac77d14b55da33b02f0914b5 Mon Sep 17 00:00:00 2001 From: Mike Palmiotto Date: Fri, 21 Apr 2023 15:29:37 -0400 Subject: [PATCH] Refactor reporter for unseal setup (#20296) --- vault/activity_log.go | 2 +- vault/activity_log_test.go | 6 +++--- vault/activity_log_util.go | 7 +++++-- vault/census.go | 9 ++++----- vault/core.go | 23 ++++++++++------------- vault/logical_system_activity.go | 2 +- vault/testing.go | 2 +- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/vault/activity_log.go b/vault/activity_log.go index aad726d34..4fa0a8068 100644 --- a/vault/activity_log.go +++ b/vault/activity_log.go @@ -1107,7 +1107,7 @@ func (c *Core) setupActivityLog(ctx context.Context, wg *sync.WaitGroup) error { }(manager.retentionMonths) manager.CensusReportDone = make(chan bool) - go c.activityLog.CensusReport(ctx, c.censusAgent) + go c.activityLog.CensusReport(ctx, c.censusAgent, c.billingStart) } return nil diff --git a/vault/activity_log_test.go b/vault/activity_log_test.go index 02d11e368..4416d78be 100644 --- a/vault/activity_log_test.go +++ b/vault/activity_log_test.go @@ -839,7 +839,7 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) { "enabled": activityLogEnabledDefaultValue, "queries_available": false, "reporting_enabled": core.censusLicensingEnabled, - "billing_start_timestamp": core.GetBillingStart(), + "billing_start_timestamp": core.billingStart, "minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths, } @@ -923,7 +923,7 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) { "enabled": "enable", "queries_available": false, "reporting_enabled": core.censusLicensingEnabled, - "billing_start_timestamp": core.GetBillingStart(), + "billing_start_timestamp": core.billingStart, "minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths, } @@ -962,7 +962,7 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) { "enabled": activityLogEnabledDefaultValue, "queries_available": false, "reporting_enabled": core.censusLicensingEnabled, - "billing_start_timestamp": core.GetBillingStart(), + "billing_start_timestamp": core.billingStart, "minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths, } diff --git a/vault/activity_log_util.go b/vault/activity_log_util.go index da19aaffa..4c1b7eda3 100644 --- a/vault/activity_log_util.go +++ b/vault/activity_log_util.go @@ -5,7 +5,10 @@ package vault -import "context" +import ( + "context" + "time" +) // sendCurrentFragment is a no-op on OSS func (a *ActivityLog) sendCurrentFragment(ctx context.Context) error { @@ -13,4 +16,4 @@ func (a *ActivityLog) sendCurrentFragment(ctx context.Context) error { } // CensusReport is a no-op on OSS -func (a *ActivityLog) CensusReport(_ctx context.Context, _ca *CensusAgent) {} +func (a *ActivityLog) CensusReport(context.Context, CensusReporter, time.Time) {} diff --git a/vault/census.go b/vault/census.go index 603fbf481..9d916dc14 100644 --- a/vault/census.go +++ b/vault/census.go @@ -2,9 +2,8 @@ package vault -import "time" - // CensusAgent is a stub for OSS -type CensusAgent struct { - billingStart time.Time -} +type CensusReporter struct{} + +// setupCensusAgent is a stub for OSS. +func (c *Core) setupCensusAgent() error { return nil } diff --git a/vault/core.go b/vault/core.go index 18715ece7..aca47398d 100644 --- a/vault/core.go +++ b/vault/core.go @@ -636,11 +636,14 @@ type Core struct { activityLogConfig ActivityLogCoreConfig // censusAgent is the mechanism used for reporting Vault's billing data. - censusAgent *CensusAgent + censusAgent CensusReporter // censusLicensingEnabled records whether Vault is exporting census metrics censusLicensingEnabled bool + // billingStart keeps track of the billing start time for exporting census metrics + billingStart time.Time + // activeTime is set on active nodes indicating the time at which this node // became active. activeTime time.Time @@ -812,7 +815,7 @@ type CoreConfig struct { LicensingConfig *LicensingConfig // Configured Census Agent - censusAgent *CensusAgent + CensusAgent CensusReporter DisablePerformanceStandby bool DisableIndexing bool @@ -2362,6 +2365,11 @@ func (s standardUnsealStrategy) unseal(ctx context.Context, logger log.Logger, c if err := c.setupAuditedHeadersConfig(ctx); err != nil { return err } + + if err := c.setupCensusAgent(); err != nil { + c.logger.Error("skipping reporting for nil agent", "error", err) + } + // not waiting on wg to avoid changing existing behavior var wg sync.WaitGroup if err := c.setupActivityLog(ctx, &wg); err != nil { @@ -4007,14 +4015,3 @@ 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 64f3cb693..cf06420a7 100644 --- a/vault/logical_system_activity.go +++ b/vault/logical_system_activity.go @@ -309,7 +309,7 @@ func (b *SystemBackend) handleActivityConfigRead(ctx context.Context, req *logic "enabled": config.Enabled, "queries_available": qa, "reporting_enabled": b.Core.censusLicensingEnabled, - "billing_start_timestamp": b.Core.GetBillingStart(), + "billing_start_timestamp": b.Core.billingStart, "minimum_retention_months": a.configOverrides.MinimumRetentionMonths, }, }, nil diff --git a/vault/testing.go b/vault/testing.go index 791a5994a..6a6151a77 100644 --- a/vault/testing.go +++ b/vault/testing.go @@ -217,7 +217,7 @@ func TestCoreWithSealAndUINoCleanup(t testing.T, opts *CoreConfig) *Core { conf.PluginDirectory = opts.PluginDirectory conf.DetectDeadlocks = opts.DetectDeadlocks conf.Experiments = []string{experiments.VaultExperimentEventsAlpha1} - conf.censusAgent = opts.censusAgent + conf.CensusAgent = opts.CensusAgent if opts.Logger != nil { conf.Logger = opts.Logger