Skip to content

Commit

Permalink
Use pro module's function to get server usage on lic val
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielseibel1 committed Sep 22, 2023
1 parent 34a787e commit bf12675
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 17 additions & 9 deletions pro/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (
"encoding/json"
"errors"
"fmt"
"golang.org/x/exp/slog"
"io"
"net/http"
"time"

"golang.org/x/crypto/nacl/box"
"golang.org/x/exp/slog"

"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/ncutils"
"github.com/gravitl/netmaker/servercfg"
"golang.org/x/crypto/nacl/box"
)

const (
Expand All @@ -28,7 +29,7 @@ const (

type apiServerConf struct {
PrivateKey []byte `json:"private_key" binding:"required"`
PublicKey []byte `json:"public_key" binding:"required"`
PublicKey []byte `json:"public_key" binding:"required"`
}

// AddLicenseHooks - adds the validation and cache clear hooks
Expand Down Expand Up @@ -81,7 +82,7 @@ func ValidateLicense() (err error) {

licenseSecret := LicenseSecret{
AssociatedID: netmakerTenantID,
Usage: logic.GetCurrentServerUsage(),
Usage: getCurrentServerUsage(),
}

secretData, err := json.Marshal(&licenseSecret)
Expand Down Expand Up @@ -112,7 +113,11 @@ func ValidateLicense() (err error) {
return err
}

respData, err := ncutils.BoxDecrypt(base64decode(licenseResponse.EncryptedLicense), apiPublicKey, tempPrivKey)
respData, err := ncutils.BoxDecrypt(
base64decode(licenseResponse.EncryptedLicense),
apiPublicKey,
tempPrivKey,
)
if err != nil {
err = fmt.Errorf("failed to decrypt license: %w", err)
return err
Expand All @@ -132,7 +137,7 @@ func ValidateLicense() (err error) {
// as well as secure communication with API
// if none present, it generates a new pair
func FetchApiServerKeys() (pub *[32]byte, priv *[32]byte, err error) {
var returnData = apiServerConf{}
returnData := apiServerConf{}
currentData, err := database.FetchRecord(database.SERVERCONF_TABLE_NAME, db_license_key)
if err != nil && !database.IsEmptyRecord(err) {
return nil, nil, err
Expand Down Expand Up @@ -181,7 +186,6 @@ func getLicensePublicKey(licensePubKeyEncoded string) (*[32]byte, error) {
}

func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, error) {

publicKeyBytes, err := ncutils.ConvertKeyToBytes(publicKey)
if err != nil {
return nil, err
Expand All @@ -198,7 +202,11 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
return nil, err
}

req, err := http.NewRequest(http.MethodPost, getAccountsHost()+"/api/v1/license/validate", bytes.NewReader(requestBody))
req, err := http.NewRequest(
http.MethodPost,
getAccountsHost()+"/api/v1/license/validate",
bytes.NewReader(requestBody),
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -241,7 +249,7 @@ func getAccountsHost() string {
}

func cacheResponse(response []byte) error {
var lrc = licenseResponseCache{
lrc := licenseResponseCache{
Body: response,
}

Expand Down
2 changes: 0 additions & 2 deletions pro/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ func base64encode(input []byte) string {

// base64decode - base64 decode helper function
func base64decode(input string) []byte {

bytes, err := base64.StdEncoding.DecodeString(input)

if err != nil {
return nil
}
Expand Down

0 comments on commit bf12675

Please sign in to comment.