Skip to content

Commit 4672700

Browse files
authored
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.
1 parent 06394a5 commit 4672700

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

cli/server.go

+16
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,22 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
788788
Prometheus: vals.Prometheus.Enable.Value(),
789789
STUN: len(vals.DERP.Server.STUNAddresses) != 0,
790790
Tunnel: tunnel != nil,
791+
ParseLicenseJWT: func(lic *telemetry.License) error {
792+
// This will be nil when running in AGPL-only mode.
793+
if options.ParseLicenseClaims == nil {
794+
return nil
795+
}
796+
797+
email, trial, err := options.ParseLicenseClaims(lic.JWT)
798+
if err != nil {
799+
return err
800+
}
801+
if email != "" {
802+
lic.Email = &email
803+
}
804+
lic.Trial = &trial
805+
return nil
806+
},
791807
})
792808
if err != nil {
793809
return xerrors.Errorf("create telemetry reporter: %w", err)

coderd/coderd.go

+5
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ type Options struct {
174174
StatsBatcher *batchstats.Batcher
175175

176176
WorkspaceAppsStatsCollectorOptions workspaceapps.StatsCollectorOptions
177+
178+
// This janky function is used in telemetry to parse fields out of the raw
179+
// JWT. It needs to be passed through like this because license parsing is
180+
// under the enterprise license, and can't be imported into AGPL.
181+
ParseLicenseClaims func(rawJWT string) (email string, trial bool, err error)
177182
}
178183

179184
// @title Coder API

coderd/telemetry/telemetry.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type Options struct {
5252
STUN bool
5353
SnapshotFrequency time.Duration
5454
Tunnel bool
55+
ParseLicenseJWT func(lic *License) error
5556
}
5657

5758
// New constructs a reporter for telemetry data.
@@ -446,7 +447,13 @@ func (r *remoteReporter) createSnapshot() (*Snapshot, error) {
446447
}
447448
snapshot.Licenses = make([]License, 0, len(licenses))
448449
for _, license := range licenses {
449-
snapshot.Licenses = append(snapshot.Licenses, ConvertLicense(license))
450+
tl := ConvertLicense(license)
451+
if r.options.ParseLicenseJWT != nil {
452+
if err := r.options.ParseLicenseJWT(&tl); err != nil {
453+
r.options.Logger.Warn(ctx, "parse license JWT", slog.Error(err))
454+
}
455+
}
456+
snapshot.Licenses = append(snapshot.Licenses, tl)
450457
}
451458
return nil
452459
})
@@ -904,6 +911,10 @@ type License struct {
904911
UploadedAt time.Time `json:"uploaded_at"`
905912
Exp time.Time `json:"exp"`
906913
UUID uuid.UUID `json:"uuid"`
914+
// These two fields are set by decoding the JWT. If the signing keys aren't
915+
// passed in, these will always be nil.
916+
Email *string `json:"email"`
917+
Trial *bool `json:"trial"`
907918
}
908919

909920
type WorkspaceProxy struct {

enterprise/coderd/coderd.go

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
109109
}
110110
}()
111111

112+
api.AGPL.Options.ParseLicenseClaims = func(rawJWT string) (email string, trial bool, err error) {
113+
c, err := license.ParseClaims(rawJWT, Keys)
114+
if err != nil {
115+
return "", false, err
116+
}
117+
return c.Subject, c.Trial, nil
118+
}
112119
api.AGPL.Options.SetUserGroups = api.setUserGroups
113120
api.AGPL.Options.SetUserSiteRoles = api.setUserSiteRoles
114121
api.AGPL.SiteHandler.AppearanceFetcher = api.fetchAppearanceConfig

enterprise/trialer/trialer.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"net/http"
1010
"time"
1111

12-
"golang.org/x/xerrors"
13-
1412
"github.com/google/uuid"
13+
"golang.org/x/xerrors"
1514

1615
"github.com/coder/coder/v2/coderd/database"
1716
"github.com/coder/coder/v2/coderd/database/dbtime"

0 commit comments

Comments
 (0)