Refactor reporter for unseal setup (#20296)

This commit is contained in:
Mike Palmiotto 2023-04-21 15:29:37 -04:00 committed by GitHub
parent d7f67b8856
commit 8001d76e28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 26 deletions

View file

@ -1107,7 +1107,7 @@ func (c *Core) setupActivityLog(ctx context.Context, wg *sync.WaitGroup) error {
}(manager.retentionMonths) }(manager.retentionMonths)
manager.CensusReportDone = make(chan bool) manager.CensusReportDone = make(chan bool)
go c.activityLog.CensusReport(ctx, c.censusAgent) go c.activityLog.CensusReport(ctx, c.censusAgent, c.billingStart)
} }
return nil return nil

View file

@ -839,7 +839,7 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) {
"enabled": activityLogEnabledDefaultValue, "enabled": activityLogEnabledDefaultValue,
"queries_available": false, "queries_available": false,
"reporting_enabled": core.censusLicensingEnabled, "reporting_enabled": core.censusLicensingEnabled,
"billing_start_timestamp": core.GetBillingStart(), "billing_start_timestamp": core.billingStart,
"minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths, "minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths,
} }
@ -923,7 +923,7 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) {
"enabled": "enable", "enabled": "enable",
"queries_available": false, "queries_available": false,
"reporting_enabled": core.censusLicensingEnabled, "reporting_enabled": core.censusLicensingEnabled,
"billing_start_timestamp": core.GetBillingStart(), "billing_start_timestamp": core.billingStart,
"minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths, "minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths,
} }
@ -962,7 +962,7 @@ func TestActivityLog_API_ConfigCRUD(t *testing.T) {
"enabled": activityLogEnabledDefaultValue, "enabled": activityLogEnabledDefaultValue,
"queries_available": false, "queries_available": false,
"reporting_enabled": core.censusLicensingEnabled, "reporting_enabled": core.censusLicensingEnabled,
"billing_start_timestamp": core.GetBillingStart(), "billing_start_timestamp": core.billingStart,
"minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths, "minimum_retention_months": core.activityLog.configOverrides.MinimumRetentionMonths,
} }

View file

@ -5,7 +5,10 @@
package vault package vault
import "context" import (
"context"
"time"
)
// sendCurrentFragment is a no-op on OSS // sendCurrentFragment is a no-op on OSS
func (a *ActivityLog) sendCurrentFragment(ctx context.Context) error { 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 // 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) {}

View file

@ -2,9 +2,8 @@
package vault package vault
import "time"
// CensusAgent is a stub for OSS // CensusAgent is a stub for OSS
type CensusAgent struct { type CensusReporter struct{}
billingStart time.Time
} // setupCensusAgent is a stub for OSS.
func (c *Core) setupCensusAgent() error { return nil }

View file

@ -636,11 +636,14 @@ type Core struct {
activityLogConfig ActivityLogCoreConfig activityLogConfig ActivityLogCoreConfig
// censusAgent is the mechanism used for reporting Vault's billing data. // censusAgent is the mechanism used for reporting Vault's billing data.
censusAgent *CensusAgent censusAgent CensusReporter
// censusLicensingEnabled records whether Vault is exporting census metrics // censusLicensingEnabled records whether Vault is exporting census metrics
censusLicensingEnabled bool 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 // activeTime is set on active nodes indicating the time at which this node
// became active. // became active.
activeTime time.Time activeTime time.Time
@ -812,7 +815,7 @@ type CoreConfig struct {
LicensingConfig *LicensingConfig LicensingConfig *LicensingConfig
// Configured Census Agent // Configured Census Agent
censusAgent *CensusAgent CensusAgent CensusReporter
DisablePerformanceStandby bool DisablePerformanceStandby bool
DisableIndexing 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 { if err := c.setupAuditedHeadersConfig(ctx); err != nil {
return err 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 // not waiting on wg to avoid changing existing behavior
var wg sync.WaitGroup var wg sync.WaitGroup
if err := c.setupActivityLog(ctx, &wg); err != nil { 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 { func (c *Core) Events() *eventbus.EventBus {
return c.events 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
}

View file

@ -309,7 +309,7 @@ func (b *SystemBackend) handleActivityConfigRead(ctx context.Context, req *logic
"enabled": config.Enabled, "enabled": config.Enabled,
"queries_available": qa, "queries_available": qa,
"reporting_enabled": b.Core.censusLicensingEnabled, "reporting_enabled": b.Core.censusLicensingEnabled,
"billing_start_timestamp": b.Core.GetBillingStart(), "billing_start_timestamp": b.Core.billingStart,
"minimum_retention_months": a.configOverrides.MinimumRetentionMonths, "minimum_retention_months": a.configOverrides.MinimumRetentionMonths,
}, },
}, nil }, nil

View file

@ -217,7 +217,7 @@ func TestCoreWithSealAndUINoCleanup(t testing.T, opts *CoreConfig) *Core {
conf.PluginDirectory = opts.PluginDirectory conf.PluginDirectory = opts.PluginDirectory
conf.DetectDeadlocks = opts.DetectDeadlocks conf.DetectDeadlocks = opts.DetectDeadlocks
conf.Experiments = []string{experiments.VaultExperimentEventsAlpha1} conf.Experiments = []string{experiments.VaultExperimentEventsAlpha1}
conf.censusAgent = opts.censusAgent conf.CensusAgent = opts.CensusAgent
if opts.Logger != nil { if opts.Logger != nil {
conf.Logger = opts.Logger conf.Logger = opts.Logger