-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from chef/nikhil/license-validations
Added implementation for license validation
- Loading branch information
Showing
27 changed files
with
270 additions
and
1,824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,31 @@ | ||
module github.com/chef/go-libs | ||
|
||
go 1.13 | ||
go 1.18 | ||
|
||
require ( | ||
github.com/BurntSushi/toml v0.3.1 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/spf13/viper v1.11.0 | ||
github.com/stretchr/testify v1.7.1 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/fsnotify/fsnotify v1.5.1 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/magiconair/properties v1.8.6 // indirect | ||
github.com/mitchellh/mapstructure v1.4.3 // indirect | ||
github.com/pelletier/go-toml v1.9.4 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/afero v1.8.2 // indirect | ||
github.com/spf13/cast v1.4.1 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/subosito/gotenv v1.2.0 // indirect | ||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
gopkg.in/ini.v1 v1.66.4 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package licensing | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
type Response struct { | ||
Data interface{} `json:"data"` | ||
Message string `json:"message"` | ||
StatusCode int `json:"status_code"` | ||
} | ||
|
||
// ENDPOINTS CONSTANT | ||
const ( | ||
CLIENT = "v1/client" | ||
) | ||
|
||
func invokeGetAPI(opts map[string]string, URL string) { | ||
params := url.Values{} | ||
|
||
params.Add("licenseId", opts["licenseId"]) | ||
params.Add("entitlementId", opts["entitlementId"]) | ||
|
||
key, check := os.LookupEnv("CHEF_LICENSE_SERVER") | ||
if check { | ||
URL = key | ||
} | ||
res, err := http.Get(URL + "/" + CLIENT + "?" + params.Encode()) | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
defer res.Body.Close() | ||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var response Response | ||
if err := json.Unmarshal(body, &response); err != nil { // Parse []byte to go struct pointer | ||
log.Println("Can not unmarshal JSON") | ||
log.Fatal(err) | ||
} | ||
if response.StatusCode != 200 { | ||
log.Fatal(response.Message) | ||
} | ||
// fmt.Println("response is---", PrettyPrint(response)) | ||
if response.Data == false { | ||
log.Fatal("Error:", response.Message) | ||
} | ||
} | ||
|
||
// func PrettyPrint(i interface{}) string { | ||
// s, _ := json.MarshalIndent(i, "", "\t") | ||
// return string(s) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package licensing | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Key struct { | ||
Licenses []Licenses `yaml:":licenses"` | ||
FileFormatVersion string `yaml:":file_format_version"` | ||
} | ||
|
||
type Licenses struct { | ||
LicenseKey string `yaml:":license_key"` | ||
LicenseType string `yaml:":license_type"` | ||
UpdateTime string `yaml:":update_time"` | ||
} | ||
|
||
func CheckSoftwareEntitlement(softwareEntitlementID string, URL string) { | ||
var licenseKey []string | ||
home, _ := os.UserHomeDir() | ||
licenseFilePath := filepath.Join(home, ".chef/licenses.yaml") | ||
info, _ := os.Stat(licenseFilePath) | ||
if info != nil { | ||
licenseKey = licenseFileFetch(licenseFilePath) | ||
client(licenseKey, softwareEntitlementID, URL) | ||
return | ||
} | ||
key, check := os.LookupEnv("CHEF_LICENSE_KEY") | ||
if check { | ||
licenseKey = append(licenseKey, key) | ||
client(licenseKey, softwareEntitlementID, URL) | ||
return | ||
} | ||
args := os.Args | ||
for k, v := range args { | ||
if v == "--chef-license-key" { | ||
if len(args) > k+1 { | ||
licenseKey = append(licenseKey, args[k+1]) | ||
client(licenseKey, softwareEntitlementID, URL) | ||
return | ||
} | ||
} else if strings.HasPrefix(v, "--chef-license-key=") { | ||
split := strings.Split(v, "=") | ||
licenseKey = append(licenseKey, split[len(split)-1]) | ||
client(licenseKey, softwareEntitlementID, URL) | ||
return | ||
} | ||
} | ||
client(licenseKey, softwareEntitlementID, URL) | ||
} | ||
|
||
func licenseFileFetch(licenseFilePath string) []string { | ||
data, err := ioutil.ReadFile(licenseFilePath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var li Key | ||
err = yaml.Unmarshal(data, &li) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
licenseKey := []string{} | ||
|
||
for i := 0; i < len(li.Licenses); i++ { | ||
licenseKey = append(licenseKey, li.Licenses[i].LicenseKey) | ||
} | ||
|
||
return licenseKey | ||
|
||
} | ||
|
||
func client(licenseKey []string, softwareEntitlementID string, URL string) { | ||
if len(licenseKey) == 0 { | ||
log.Fatal("You dont have license key, Please generate by running chef license command") | ||
} else { | ||
var opts = make(map[string]string) | ||
opts["licenseId"] = strings.Join(licenseKey, ",") | ||
opts["entitlementId"] = softwareEntitlementID | ||
invokeGetAPI(opts, URL) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.