Skip to content

Commit

Permalink
Merge pull request #77 from chef/nikhil/license-validations
Browse files Browse the repository at this point in the history
Added implementation for license validation
  • Loading branch information
nikhil2611 authored Jun 22, 2023
2 parents 395e652 + 5e73148 commit 1608415
Show file tree
Hide file tree
Showing 27 changed files with 270 additions and 1,824 deletions.
23 changes: 22 additions & 1 deletion go.mod
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
)
340 changes: 4 additions & 336 deletions go.sum

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions licensing/client.go
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)
// }
87 changes: 87 additions & 0 deletions licensing/licensing.go
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)
}
}
7 changes: 0 additions & 7 deletions vendor/github.com/fsnotify/fsnotify/go.mod

This file was deleted.

2 changes: 0 additions & 2 deletions vendor/github.com/fsnotify/fsnotify/go.sum

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/github.com/hashicorp/hcl/go.mod

This file was deleted.

2 changes: 0 additions & 2 deletions vendor/github.com/hashicorp/hcl/go.sum

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/github.com/magiconair/properties/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/github.com/mitchellh/mapstructure/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/github.com/pelletier/go-toml/go.mod

This file was deleted.

5 changes: 0 additions & 5 deletions vendor/github.com/pelletier/go-toml/v2/go.mod

This file was deleted.

11 changes: 0 additions & 11 deletions vendor/github.com/pelletier/go-toml/v2/go.sum

This file was deleted.

13 changes: 0 additions & 13 deletions vendor/github.com/spf13/afero/go.mod

This file was deleted.

Loading

0 comments on commit 1608415

Please sign in to comment.