From 9ae2bdfae67e78f74cd2b74364a49f85f7723038 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Tue, 30 May 2023 18:20:49 -0400 Subject: [PATCH] feat: license util config (#3248) * feat: license util config * address comments * introduce census job * add job * fix import groups * remove pointers --- internal/census/census.go | 41 ++++++++++ internal/census/census_job.go | 76 +++++++++++++++++++ internal/cmd/config/config.go | 11 +++ internal/cmd/config/config_load_test.go | 5 ++ .../testdata/TestLoad/SingleFile/config.hcl | 6 ++ internal/daemon/controller/controller.go | 4 + 6 files changed, 143 insertions(+) create mode 100644 internal/census/census.go create mode 100644 internal/census/census_job.go diff --git a/internal/census/census.go b/internal/census/census.go new file mode 100644 index 000000000..d86c91103 --- /dev/null +++ b/internal/census/census.go @@ -0,0 +1,41 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package census + +import ( + "context" + "fmt" + + "github.com/hashicorp/boundary/internal/db" + "github.com/hashicorp/boundary/internal/errors" + "github.com/hashicorp/boundary/internal/scheduler" + "github.com/hashicorp/boundary/internal/util" +) + +// RegisterJob registers the census job with the provided scheduler. +func RegisterJob(ctx context.Context, s *scheduler.Scheduler, optOut bool, r db.Reader, w db.Writer) error { + const op = "census.RegisterJob" + if s == nil { + return errors.New(ctx, errors.InvalidParameter, "nil scheduler", op, errors.WithoutEvent()) + } + if util.IsNil(r) { + return errors.New(ctx, errors.Internal, "nil DB reader", op, errors.WithoutEvent()) + } + if util.IsNil(w) { + return errors.New(ctx, errors.Internal, "nil DB writer", op, errors.WithoutEvent()) + } + if util.IsNil(optOut) { + return errors.New(ctx, errors.Internal, "nil opt out value", op, errors.WithoutEvent()) + } + + censusJob, err := NewCensusJobFn(ctx, optOut, r, w) + if err != nil { + return fmt.Errorf("error creating census job: %w", err) + } + if err := s.RegisterJob(ctx, censusJob); err != nil { + return errors.Wrap(ctx, err, op) + } + + return nil +} diff --git a/internal/census/census_job.go b/internal/census/census_job.go new file mode 100644 index 000000000..edaae70a6 --- /dev/null +++ b/internal/census/census_job.go @@ -0,0 +1,76 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package census + +import ( + "context" + "time" + + "github.com/hashicorp/boundary/internal/db" + "github.com/hashicorp/boundary/internal/errors" + "github.com/hashicorp/boundary/internal/scheduler" +) + +var ( + NewCensusJobFn = newCensusJob + RunFn = runInternal +) + +type censusJob struct { + r db.Reader + w db.Writer + optOut bool + agent any +} + +func newCensusJob(ctx context.Context, optOut bool, r db.Reader, w db.Writer) (*censusJob, error) { + const op = "censusJob.newCensusJob" + switch { + case r == nil: + return nil, errors.New(ctx, errors.InvalidParameter, op, "missing db.Reader") + case w == nil: + return nil, errors.New(ctx, errors.InvalidParameter, op, "missing db.Writer") + } + + return &censusJob{ + r: r, + w: w, + optOut: optOut, + agent: nil, + }, nil +} + +// Status reports the job’s current status. +func (c *censusJob) Status() scheduler.JobStatus { + return scheduler.JobStatus{} +} + +// Run performs the required work depending on the implementation. +// The context is used to notify the job that it should exit early. +func (c *censusJob) Run(ctx context.Context) error { + const op = "census.(censusJob).Run" + err := RunFn(ctx, c) + return err +} + +func runInternal(ctx context.Context, c *censusJob) error { + return nil +} + +// NextRunIn returns the duration until the next job run should be scheduled. +// We report as ready immediately after a successful run. This doesn't mean that +// this job will run immediately, only about as often as the configured scheduler interval. +func (c *censusJob) NextRunIn(_ context.Context) (time.Duration, error) { + return 0, nil +} + +// Name is the unique name of the job. +func (c *censusJob) Name() string { + return "job_run_census" +} + +// Description is the human-readable description of the job. +func (c *censusJob) Description() string { + return "Gathers and exports session usage metrics" +} diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index 01c589510..91b977746 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -148,6 +148,9 @@ type Config struct { // sensitive. This should only be enabled for debugging purposes, and can be // toggled with SIGHUP. EnableWorkerAuthDebugging bool `hcl:"enable_worker_auth_debugging"` + + // For opting out of license utilization reporting + Reporting Reporting `hcl:"reporting"` } type Controller struct { @@ -308,6 +311,14 @@ type Plugins struct { ExecutionDir string `hcl:"execution_dir"` } +type Reporting struct { + License License `hcl:"license"` +} + +type License struct { + Enabled bool `hcl:"enabled"` +} + // DevWorker is a Config that is used for dev mode of Boundary // workers. Supported options: WithObservationsEnabled, WithSysEventsEnabled, // WithAuditEventsEnabled, TestWithErrorEventsEnabled diff --git a/internal/cmd/config/config_load_test.go b/internal/cmd/config/config_load_test.go index 8acfee575..b931efa52 100644 --- a/internal/cmd/config/config_load_test.go +++ b/internal/cmd/config/config_load_test.go @@ -451,6 +451,11 @@ func TestLoad(t *testing.T) { ExecutionDir: "", }, HcpbClusterId: "", + Reporting: &config.Reporting{ + License: &config.License{ + Enabled: false, + }, + }, }, nil, }, diff --git a/internal/cmd/config/testdata/TestLoad/SingleFile/config.hcl b/internal/cmd/config/testdata/TestLoad/SingleFile/config.hcl index 7e4ee2f72..f18520114 100644 --- a/internal/cmd/config/testdata/TestLoad/SingleFile/config.hcl +++ b/internal/cmd/config/testdata/TestLoad/SingleFile/config.hcl @@ -63,3 +63,9 @@ kms "aead" { key = "8fZBjCUfN0TzjEGLQldGY4+iE9AkOvCfjh7+p0GtRBQ=" key_id = "global_recovery" } + +reporting { + license { + enabled = false + } +} diff --git a/internal/daemon/controller/controller.go b/internal/daemon/controller/controller.go index 8d0864877..ba73d9fb5 100644 --- a/internal/daemon/controller/controller.go +++ b/internal/daemon/controller/controller.go @@ -15,6 +15,7 @@ import ( "github.com/hashicorp/boundary/internal/auth/oidc" "github.com/hashicorp/boundary/internal/auth/password" "github.com/hashicorp/boundary/internal/authtoken" + "github.com/hashicorp/boundary/internal/census" "github.com/hashicorp/boundary/internal/cmd/base" "github.com/hashicorp/boundary/internal/cmd/config" credstatic "github.com/hashicorp/boundary/internal/credential/static" @@ -536,6 +537,9 @@ func (c *Controller) registerJobs() error { if err := cleaner.RegisterJob(c.baseContext, c.scheduler, rw); err != nil { return err } + if err := census.RegisterJob(c.baseContext, c.scheduler, c.conf.RawConfig.Reporting.License.Enabled, rw, rw); err != nil { + return err + } return nil }