github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/cliccl/demo.go (about) 1 // Copyright 2019 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 cliccl 10 11 import ( 12 gosql "database/sql" 13 "fmt" 14 "io/ioutil" 15 "net/http" 16 "time" 17 18 "github.com/cockroachdb/cockroach/pkg/build" 19 "github.com/cockroachdb/cockroach/pkg/cli" 20 "github.com/cockroachdb/cockroach/pkg/util/envutil" 21 "github.com/cockroachdb/cockroach/pkg/util/log" 22 "github.com/cockroachdb/cockroach/pkg/util/uuid" 23 "github.com/cockroachdb/errors" 24 ) 25 26 // This URL grants a license that is valid for 24 hours. 27 const licenseDefaultURL = "https://register.cockroachdb.com/api/license" 28 29 // We make licenseURL configurable for use in tests. 30 var licenseURL = envutil.EnvOrDefaultString("COCKROACH_DEMO_LICENSE_URL", licenseDefaultURL) 31 32 func getLicense(clusterID uuid.UUID) (string, error) { 33 client := &http.Client{ 34 Timeout: 5 * time.Second, 35 } 36 req, err := http.NewRequest("GET", licenseURL, nil) 37 if err != nil { 38 return "", err 39 } 40 // Send some extra information to the endpoint. 41 q := req.URL.Query() 42 // Let the endpoint know we are requesting a demo license. 43 q.Add("kind", "demo") 44 q.Add("version", build.VersionPrefix()) 45 q.Add("clusterid", clusterID.String()) 46 req.URL.RawQuery = q.Encode() 47 48 resp, err := client.Do(req) 49 if err != nil { 50 return "", err 51 } 52 defer resp.Body.Close() 53 54 if resp.StatusCode != http.StatusOK { 55 return "", errors.New("unable to connect to licensing endpoint") 56 } 57 bodyBytes, err := ioutil.ReadAll(resp.Body) 58 if err != nil { 59 return "", err 60 } 61 return string(bodyBytes), nil 62 } 63 64 func getAndApplyLicense(db *gosql.DB, clusterID uuid.UUID, org string) (bool, error) { 65 license, err := getLicense(clusterID) 66 if err != nil { 67 fmt.Fprintf(log.OrigStderr, "\nerror while contacting licensing server:\n%+v\n", err) 68 return false, nil 69 } 70 if _, err := db.Exec(`SET CLUSTER SETTING cluster.organization = $1`, org); err != nil { 71 return false, err 72 } 73 if _, err := db.Exec(`SET CLUSTER SETTING enterprise.license = $1`, license); err != nil { 74 return false, err 75 } 76 return true, nil 77 } 78 79 func init() { 80 // Set the GetAndApplyLicense function within cockroach demo. 81 // This separation is done to avoid using enterprise features in an OSS/BSL build. 82 cli.GetAndApplyLicense = getAndApplyLicense 83 }