From bf1267527465923423847802fd4c6bbde18300de Mon Sep 17 00:00:00 2001 From: gabrielseibel1 Date: Fri, 22 Sep 2023 14:29:13 -0300 Subject: [PATCH] Use pro module's function to get server usage on lic val --- pro/license.go | 26 +++++++++++++++++--------- pro/util.go | 2 -- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pro/license.go b/pro/license.go index e0229e7c9..c76505348 100644 --- a/pro/license.go +++ b/pro/license.go @@ -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 ( @@ -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 @@ -81,7 +82,7 @@ func ValidateLicense() (err error) { licenseSecret := LicenseSecret{ AssociatedID: netmakerTenantID, - Usage: logic.GetCurrentServerUsage(), + Usage: getCurrentServerUsage(), } secretData, err := json.Marshal(&licenseSecret) @@ -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 @@ -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 @@ -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 @@ -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 } @@ -241,7 +249,7 @@ func getAccountsHost() string { } func cacheResponse(response []byte) error { - var lrc = licenseResponseCache{ + lrc := licenseResponseCache{ Body: response, } diff --git a/pro/util.go b/pro/util.go index d7490c8fe..7fc2d4cd9 100644 --- a/pro/util.go +++ b/pro/util.go @@ -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 }