Skip to content

Commit

Permalink
Refactor reporter for unseal setup (#20296)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalmi committed Apr 21, 2023
1 parent 4c971e0 commit 77f83d9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion vault/activity_log.go
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions vault/activity_log_test.go
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}

Expand Down
7 changes: 5 additions & 2 deletions vault/activity_log_util.go
Expand Up @@ -5,12 +5,15 @@

package vault

import "context"
import (
"context"
"time"
)

// sendCurrentFragment is a no-op on OSS
func (a *ActivityLog) sendCurrentFragment(ctx context.Context) error {
return nil
}

// 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) {}
9 changes: 4 additions & 5 deletions vault/census.go
Expand Up @@ -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 }
23 changes: 10 additions & 13 deletions vault/core.go
Expand Up @@ -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
Expand Down Expand Up @@ -812,7 +815,7 @@ type CoreConfig struct {
LicensingConfig *LicensingConfig

// Configured Census Agent
censusAgent *CensusAgent
CensusAgent CensusReporter

DisablePerformanceStandby bool
DisableIndexing bool
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion vault/logical_system_activity.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vault/testing.go
Expand Up @@ -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
Expand Down

0 comments on commit 77f83d9

Please sign in to comment.