github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/utilccl/license_check.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Licensed as a CockroachDB Enterprise file under the Cockroach Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt 8 9 package utilccl 10 11 import ( 12 "time" 13 14 "github.com/cockroachdb/cockroach/pkg/base" 15 "github.com/cockroachdb/cockroach/pkg/ccl/utilccl/licenseccl" 16 "github.com/cockroachdb/cockroach/pkg/settings" 17 "github.com/cockroachdb/cockroach/pkg/settings/cluster" 18 "github.com/cockroachdb/cockroach/pkg/util/timeutil" 19 "github.com/cockroachdb/cockroach/pkg/util/uuid" 20 ) 21 22 var enterpriseLicense = func() *settings.StringSetting { 23 s := settings.RegisterValidatedStringSetting( 24 "enterprise.license", 25 "the encoded cluster license", 26 "", 27 func(sv *settings.Values, s string) error { 28 _, err := licenseccl.Decode(s) 29 return err 30 }, 31 ) 32 // Even though string settings are non-reportable by default, we 33 // still mark them explicitly in case a future code change flips the 34 // default. 35 s.SetReportable(false) 36 s.SetVisibility(settings.Public) 37 return s 38 }() 39 40 var testingEnterpriseEnabled = false 41 42 // TestingEnableEnterprise allows overriding the license check in tests. 43 func TestingEnableEnterprise() func() { 44 before := testingEnterpriseEnabled 45 testingEnterpriseEnabled = true 46 return func() { 47 testingEnterpriseEnabled = before 48 } 49 } 50 51 // TestingDisableEnterprise allows re-enabling the license check in tests. 52 func TestingDisableEnterprise() func() { 53 before := testingEnterpriseEnabled 54 testingEnterpriseEnabled = false 55 return func() { 56 testingEnterpriseEnabled = before 57 } 58 } 59 60 // CheckEnterpriseEnabled returns a non-nil error if the requested enterprise 61 // feature is not enabled, including information or a link explaining how to 62 // enable it. 63 func CheckEnterpriseEnabled(st *cluster.Settings, cluster uuid.UUID, org, feature string) error { 64 if testingEnterpriseEnabled { 65 return nil 66 } 67 return checkEnterpriseEnabledAt(st, timeutil.Now(), cluster, org, feature) 68 } 69 70 func init() { 71 base.CheckEnterpriseEnabled = CheckEnterpriseEnabled 72 base.LicenseType = getLicenseType 73 } 74 75 func checkEnterpriseEnabledAt( 76 st *cluster.Settings, at time.Time, cluster uuid.UUID, org, feature string, 77 ) error { 78 var lic *licenseccl.License 79 // FIXME(tschottdorf): see whether it makes sense to cache the decoded 80 // license. 81 if str := enterpriseLicense.Get(&st.SV); str != "" { 82 var err error 83 if lic, err = licenseccl.Decode(str); err != nil { 84 return err 85 } 86 } 87 return lic.Check(at, cluster, org, feature) 88 } 89 90 func getLicenseType(st *cluster.Settings) (string, error) { 91 str := enterpriseLicense.Get(&st.SV) 92 if str == "" { 93 return "None", nil 94 } 95 lic, err := licenseccl.Decode(str) 96 if err != nil { 97 return "", err 98 } 99 return lic.Type.String(), nil 100 }