From 4672700ef658472b5755ab6433f9ae44bf9b66a6 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 14 Dec 2023 15:52:52 -0600 Subject: [PATCH] chore: add additional fields to license telemetry (#11173) This sends the email the license was issued to, and whether or not it's a trial in the telemetry payload. It's a bit janky since the license parsing is all enterprise licensed. --- cli/server.go | 16 ++++++++++++++++ coderd/coderd.go | 5 +++++ coderd/telemetry/telemetry.go | 13 ++++++++++++- enterprise/coderd/coderd.go | 7 +++++++ enterprise/trialer/trialer.go | 3 +-- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/cli/server.go b/cli/server.go index 11a979d112..844e6f1ef4 100644 --- a/cli/server.go +++ b/cli/server.go @@ -788,6 +788,22 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. Prometheus: vals.Prometheus.Enable.Value(), STUN: len(vals.DERP.Server.STUNAddresses) != 0, Tunnel: tunnel != nil, + ParseLicenseJWT: func(lic *telemetry.License) error { + // This will be nil when running in AGPL-only mode. + if options.ParseLicenseClaims == nil { + return nil + } + + email, trial, err := options.ParseLicenseClaims(lic.JWT) + if err != nil { + return err + } + if email != "" { + lic.Email = &email + } + lic.Trial = &trial + return nil + }, }) if err != nil { return xerrors.Errorf("create telemetry reporter: %w", err) diff --git a/coderd/coderd.go b/coderd/coderd.go index 9713d23a03..581264df51 100644 --- a/coderd/coderd.go +++ b/coderd/coderd.go @@ -174,6 +174,11 @@ type Options struct { StatsBatcher *batchstats.Batcher WorkspaceAppsStatsCollectorOptions workspaceapps.StatsCollectorOptions + + // This janky function is used in telemetry to parse fields out of the raw + // JWT. It needs to be passed through like this because license parsing is + // under the enterprise license, and can't be imported into AGPL. + ParseLicenseClaims func(rawJWT string) (email string, trial bool, err error) } // @title Coder API diff --git a/coderd/telemetry/telemetry.go b/coderd/telemetry/telemetry.go index 39f3b892c2..71dce3a77c 100644 --- a/coderd/telemetry/telemetry.go +++ b/coderd/telemetry/telemetry.go @@ -52,6 +52,7 @@ type Options struct { STUN bool SnapshotFrequency time.Duration Tunnel bool + ParseLicenseJWT func(lic *License) error } // New constructs a reporter for telemetry data. @@ -446,7 +447,13 @@ func (r *remoteReporter) createSnapshot() (*Snapshot, error) { } snapshot.Licenses = make([]License, 0, len(licenses)) for _, license := range licenses { - snapshot.Licenses = append(snapshot.Licenses, ConvertLicense(license)) + tl := ConvertLicense(license) + if r.options.ParseLicenseJWT != nil { + if err := r.options.ParseLicenseJWT(&tl); err != nil { + r.options.Logger.Warn(ctx, "parse license JWT", slog.Error(err)) + } + } + snapshot.Licenses = append(snapshot.Licenses, tl) } return nil }) @@ -904,6 +911,10 @@ type License struct { UploadedAt time.Time `json:"uploaded_at"` Exp time.Time `json:"exp"` UUID uuid.UUID `json:"uuid"` + // These two fields are set by decoding the JWT. If the signing keys aren't + // passed in, these will always be nil. + Email *string `json:"email"` + Trial *bool `json:"trial"` } type WorkspaceProxy struct { diff --git a/enterprise/coderd/coderd.go b/enterprise/coderd/coderd.go index 028ae5a676..32e96ec259 100644 --- a/enterprise/coderd/coderd.go +++ b/enterprise/coderd/coderd.go @@ -109,6 +109,13 @@ func New(ctx context.Context, options *Options) (_ *API, err error) { } }() + api.AGPL.Options.ParseLicenseClaims = func(rawJWT string) (email string, trial bool, err error) { + c, err := license.ParseClaims(rawJWT, Keys) + if err != nil { + return "", false, err + } + return c.Subject, c.Trial, nil + } api.AGPL.Options.SetUserGroups = api.setUserGroups api.AGPL.Options.SetUserSiteRoles = api.setUserSiteRoles api.AGPL.SiteHandler.AppearanceFetcher = api.fetchAppearanceConfig diff --git a/enterprise/trialer/trialer.go b/enterprise/trialer/trialer.go index 14a8fa7b50..e143225b88 100644 --- a/enterprise/trialer/trialer.go +++ b/enterprise/trialer/trialer.go @@ -9,9 +9,8 @@ import ( "net/http" "time" - "golang.org/x/xerrors" - "github.com/google/uuid" + "golang.org/x/xerrors" "github.com/coder/coder/v2/coderd/database" "github.com/coder/coder/v2/coderd/database/dbtime"