Skip to content

Commit

Permalink
server: move license check functions to base
Browse files Browse the repository at this point in the history
These license check functions have no dependencies and might as well be
accessible everywhere.

Release note: None
  • Loading branch information
benesch committed Feb 13, 2018
1 parent 2f73ab2 commit 853fe66
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
39 changes: 39 additions & 0 deletions pkg/base/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package base

import (
"errors"

"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)

// CheckEnterpriseEnabled returns a non-nil error if the requested enterprise
// feature is not enabled, including information or a link explaining how to
// enable it.
//
// This function is overridden by an init hook in CCL builds.
var CheckEnterpriseEnabled = func(_ *cluster.Settings, _ uuid.UUID, org, feature string) error {
return errors.New("OSS binaries do not include enterprise features")
}

// LicenseType returns what type of license the cluster is running with, or
// "OSS" if it is an OSS build.
//
// This function is overridden by an init hook in CCL builds.
var LicenseType = func(st *cluster.Settings) (string, error) {
return "OSS", nil
}
6 changes: 3 additions & 3 deletions pkg/ccl/utilccl/license_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ package utilccl
import (
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/ccl/utilccl/licenseccl"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
Expand Down Expand Up @@ -55,8 +55,8 @@ func CheckEnterpriseEnabled(st *cluster.Settings, cluster uuid.UUID, org, featur
}

func init() {
server.LicenseCheckFn = CheckEnterpriseEnabled
server.LicenseTypeFn = getLicenseType
base.CheckEnterpriseEnabled = CheckEnterpriseEnabled
base.LicenseType = getLicenseType
}

func checkEnterpriseEnabledAt(
Expand Down
7 changes: 1 addition & 6 deletions pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,13 +1015,8 @@ func (s *adminServer) Cluster(
// Check if enterprise features are enabled. We currently test for the
// feature "BACKUP", although enterprise licenses do not yet distinguish
// between different features.
enterpriseEnabled := false
organization := sql.ClusterOrganization.Get(&s.server.st.SV)
if err := LicenseCheckFn(
s.server.st, clusterID, organization, "BACKUP",
); err == nil {
enterpriseEnabled = true
}
enterpriseEnabled := base.CheckEnterpriseEnabled(s.server.st, clusterID, organization, "BACKUP") == nil

return &serverpb.ClusterResponse{
ClusterID: clusterID.String(),
Expand Down
8 changes: 3 additions & 5 deletions pkg/server/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,13 +1050,11 @@ func TestClusterAPI(t *testing.T) {
testutils.RunTrueAndFalse(t, "enterpriseOn", func(t *testing.T, enterpriseOn bool) {
// Override server license check.
if enterpriseOn {
oldLicenseCheck := LicenseCheckFn
LicenseCheckFn = func(_ *cluster.Settings, _ uuid.UUID, _, _ string) error {
old := base.CheckEnterpriseEnabled
base.CheckEnterpriseEnabled = func(_ *cluster.Settings, _ uuid.UUID, _, _ string) error {
return nil
}
defer func() {
LicenseCheckFn = oldLicenseCheck
}()
defer func() { base.CheckEnterpriseEnabled = old }()
}

if _, err := db.Exec(`SET CLUSTER SETTING diagnostics.reporting.enabled = $1`, reportingOn); err != nil {
Expand Down
16 changes: 0 additions & 16 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,6 @@ var (
"the amount of time subsystems wait for work to finish before shutting down",
10*time.Second,
)

// LicenseCheckFn is used to check if the current cluster has any enterprise
// features enabled. This function is overridden by an init hook in CCL
// builds.
LicenseCheckFn = func(
st *cluster.Settings, cluster uuid.UUID, org, feature string,
) error {
return errors.New("OSS build does not include Enterprise features")
}

// LicenseTypeFn returns what type of license the cluster is running with, or
// "OSS" if it is an OSS build. This function is overridden by an init hook in
// CCL builds.
LicenseTypeFn = func(st *cluster.Settings) (string, error) {
return "OSS", nil
}
)

// Server is the cockroach server node.
Expand Down
3 changes: 2 additions & 1 deletion pkg/server/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/mitchellh/reflectwalk"
"github.com/pkg/errors"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
Expand Down Expand Up @@ -169,7 +170,7 @@ func addInfoToURL(ctx context.Context, url *url.URL, s *Server, runningTime time
q.Set("internal",
strconv.FormatBool(strings.Contains(sql.ClusterOrganization.Get(&s.st.SV), "Cockroach Labs")))

licenseType, err := LicenseTypeFn(s.st)
licenseType, err := base.LicenseType(s.st)
if err == nil {
q.Set("licensetype", licenseType)
} else {
Expand Down

0 comments on commit 853fe66

Please sign in to comment.