fleet/server/contexts/license/license.go
Dante Catalfamo 437c8114b1
Allow certain licenses to disable telemetry (#29093)
#28220
---------

Co-authored-by: Scott Gress <scott@fleetdm.com>
2025-05-22 14:27:07 -04:00

42 lines
1.1 KiB
Go

// Package license provides an API to create a context with the current license
// stored in it, and to retrieve the license from the context.
package license
import (
"context"
"github.com/fleetdm/fleet/v4/server/fleet"
)
type key int
const licenseKey key = 0
// NewContext creates a new context.Context with the license.
func NewContext(ctx context.Context, lic *fleet.LicenseInfo) context.Context {
return context.WithValue(ctx, licenseKey, lic)
}
// FromContext returns the license from the context and true, or nil and false
// if there is no license.
func FromContext(ctx context.Context) (*fleet.LicenseInfo, bool) {
v, ok := ctx.Value(licenseKey).(*fleet.LicenseInfo)
return v, ok
}
// IsPremium is a convenience function that returns true if the license stored
// in the context is for a premium tier, false otherwise (including if there
// is no license in the context).
func IsPremium(ctx context.Context) bool {
if lic, ok := FromContext(ctx); ok {
return lic.IsPremium()
}
return false
}
func IsAllowDisableTelemetry(ctx context.Context) bool {
if lic, ok := FromContext(ctx); ok {
return lic.IsAllowDisableTelemetry()
}
return false
}