MCPcopy Create free account
hub / github.com/coder/coder / getBestLicenseJWT

Method getBestLicenseJWT

enterprise/coderd/usage/publisher.go:342–388  ·  view source on GitHub ↗

getBestLicenseJWT returns the best license JWT to use for the request. The criteria is as follows: - The license must be valid and active (after nbf, before exp) - The license must have usage publishing enabled The most recently issued (iat) license is chosen. If no licenses are found or none have

(ctx context.Context)

Source from the content-addressed store, hash-verified

340// If no licenses are found or none have usage publishing enabled,
341// errUsagePublishingDisabled is returned.
342func (p *tallymanPublisher) getBestLicenseJWT(ctx context.Context) (string, error) {
343 licenses, err := p.db.GetUnexpiredLicenses(ctx)
344 if err != nil {
345 return "", xerrors.Errorf("get unexpired licenses: %w", err)
346 }
347 if len(licenses) == 0 {
348 return "", errUsagePublishingDisabled
349 }
350
351 type licenseJWTWithClaims struct {
352 Claims *license.Claims
353 Raw string
354 }
355
356 var bestLicense licenseJWTWithClaims
357 for _, dbLicense := range licenses {
358 claims, err := license.ParseClaims(dbLicense.JWT, p.licenseKeys)
359 if err != nil {
360 p.log.Warn(ctx, "failed to parse license claims", slog.F("license_id", dbLicense.ID), slog.Error(err))
361 continue
362 }
363 if claims.AccountType != license.AccountTypeSalesforce {
364 // Non-Salesforce accounts cannot be tracked as they do not have a
365 // trusted Salesforce opportunity ID encoded in the license.
366 continue
367 }
368 if !claims.PublishUsageData {
369 // Publishing is disabled.
370 continue
371 }
372
373 // Otherwise, if it's issued more recently, it's the best license.
374 // IssuedAt is verified to be non-nil in license.ParseClaims.
375 if bestLicense.Claims == nil || claims.IssuedAt.Time.After(bestLicense.Claims.IssuedAt.Time) {
376 bestLicense = licenseJWTWithClaims{
377 Claims: claims,
378 Raw: dbLicense.JWT,
379 }
380 }
381 }
382
383 if bestLicense.Raw == "" {
384 return "", errUsagePublishingDisabled
385 }
386
387 return bestLicense.Raw, nil
388}
389
390func (p *tallymanPublisher) sendPublishRequest(ctx context.Context, deploymentID uuid.UUID, licenseJwt string, req usagetypes.TallymanV1IngestRequest) (usagetypes.TallymanV1IngestResponse, error) {
391 body, err := json.Marshal(req)

Callers 1

publishOnceMethod · 0.95

Calls 4

ParseClaimsFunction · 0.92
GetUnexpiredLicensesMethod · 0.65
ErrorfMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected