瀏覽代碼

fix license validation, replace node limit with hosts

Abhishek Kondur 2 年之前
父節點
當前提交
d0333fb3c1
共有 8 個文件被更改,包括 57 次插入71 次删除
  1. 5 6
      config/config.go
  2. 1 9
      controllers/limits.go
  3. 8 1
      ee/initialize.go
  4. 4 5
      ee/license.go
  5. 16 15
      ee/types.go
  6. 2 2
      ee/util.go
  7. 1 3
      logic/serverconf.go
  8. 20 30
      servercfg/serverconf.go

+ 5 - 6
config/config.go

@@ -72,13 +72,12 @@ type ServerConfig struct {
 	NetmakerAccountID    string `yaml:"netmaker_account_id"`
 	IsEE                 string `yaml:"is_ee"`
 	StunPort             int    `yaml:"stun_port"`
-	NodeLimit            int    `yaml:"node_limit"`
-	UserLimit            int    `yaml:"user_limit"`
-	ClientLimit          int    `yaml:"client_limit"`
-	NetworkLimit         int    `yaml:"network_limit"`
-	HostLimit            int    `yaml:"host_limit"`
-	DeployedByOperator   bool   `yaml:"deployed_by_operator"`
 	StunList             string `yaml:"stun_list"`
+	UsersLimit           int    `yaml:"user_limit"`
+	ClientsLimit         int    `yaml:"client_limit"`
+	NetworksLimit        int    `yaml:"network_limit"`
+	HostsLimit           int    `yaml:"host_limit"`
+	DeployedByOperator   bool   `yaml:"deployed_by_operator"`
 }
 
 // SQLConfig - Generic SQL Config

+ 1 - 9
controllers/limits.go

@@ -6,7 +6,6 @@ import (
 	"github.com/gravitl/netmaker/database"
 	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/models"
-	"github.com/gravitl/netmaker/servercfg"
 )
 
 // limit consts
@@ -23,20 +22,13 @@ func checkFreeTierLimits(limit_choice int, next http.Handler) http.HandlerFunc {
 			Code: http.StatusUnauthorized, Message: "free tier limits exceeded on networks",
 		}
 
-		if logic.Free_Tier && servercfg.Is_EE { // check that free tier limits not exceeded
+		if logic.Free_Tier { // check that free tier limits not exceeded
 			if limit_choice == networks_l {
 				currentNetworks, err := logic.GetNetworks()
 				if (err != nil && !database.IsEmptyRecord(err)) || len(currentNetworks) >= logic.Networks_Limit {
 					logic.ReturnErrorResponse(w, r, errorResponse)
 					return
 				}
-			} else if limit_choice == node_l {
-				nodes, err := logic.GetAllNodes()
-				if (err != nil && !database.IsEmptyRecord(err)) || len(nodes) >= logic.Node_Limit {
-					errorResponse.Message = "free tier limits exceeded on nodes"
-					logic.ReturnErrorResponse(w, r, errorResponse)
-					return
-				}
 			} else if limit_choice == users_l {
 				users, err := logic.GetUsers()
 				if (err != nil && !database.IsEmptyRecord(err)) || len(users) >= logic.Users_Limit {

+ 8 - 1
ee/initialize.go

@@ -16,7 +16,6 @@ import (
 // InitEE - Initialize EE Logic
 func InitEE() {
 	setIsEnterprise()
-	servercfg.Is_EE = true
 	models.SetLogo(retrieveEELogo())
 	controller.HttpHandlers = append(
 		controller.HttpHandlers,
@@ -38,6 +37,14 @@ func InitEE() {
 	logic.EnterpriseResetAllPeersFailovers = eelogic.WipeAffectedFailoversOnly
 }
 
+func setControllerLimits() {
+	logic.Hosts_Limit = Limits.Hosts
+	logic.Users_Limit = Limits.Users
+	logic.Clients_Limit = Limits.Clients
+	logic.Networks_Limit = Limits.Networks
+	servercfg.Is_EE = true
+}
+
 func resetFailover() {
 	nets, err := logic.GetNetworks()
 	if err == nil {

+ 4 - 5
ee/license.go

@@ -9,7 +9,6 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
-	"math"
 	"net/http"
 
 	"github.com/gravitl/netmaker/database"
@@ -58,8 +57,8 @@ func ValidateLicense() error {
 	}
 
 	licenseSecret := LicenseSecret{
-		UserID: netmakerAccountID,
-		Limits: getCurrentServerLimit(),
+		AssociatedID: netmakerAccountID,
+		Limits:       getCurrentServerLimit(),
 	}
 
 	secretData, err := json.Marshal(&licenseSecret)
@@ -92,9 +91,9 @@ func ValidateLicense() error {
 		logger.FatalLog(errValidation.Error())
 	}
 
-	Limits.Networks = math.MaxInt
+	Limits.Networks = license.LimitNetworks
 	Limits.Clients = license.LimitClients
-	Limits.Nodes = license.LimitNodes
+	Limits.Hosts = license.LimitHosts
 	Limits.Servers = license.LimitServers
 	Limits.Users = license.LimitUsers
 	setControllerLimits()

+ 16 - 15
ee/types.go

@@ -3,7 +3,7 @@ package ee
 import "fmt"
 
 const (
-	api_endpoint               = "https://api.controller.netmaker.io/api/v1/license/validate"
+	api_endpoint               = "https://afbf-94-207-127-12.in.ngrok.io/api/v1/license/validate"
 	license_cache_key          = "license_response_cache"
 	license_validation_err_msg = "invalid license"
 	server_id_key              = "nm-server-id"
@@ -15,7 +15,7 @@ var errValidation = fmt.Errorf(license_validation_err_msg)
 var Limits = GlobalLimits{
 	Servers:  0,
 	Users:    0,
-	Nodes:    0,
+	Hosts:    0,
 	Clients:  0,
 	Networks: 0,
 }
@@ -24,21 +24,22 @@ var Limits = GlobalLimits{
 type GlobalLimits struct {
 	Servers  int
 	Users    int
-	Nodes    int
+	Hosts    int
 	Clients  int
 	Networks int
 }
 
 // LicenseKey - the license key struct representation with associated data
 type LicenseKey struct {
-	LicenseValue string `json:"license_value"` // actual (public) key and the unique value for the key
-	Expiration   int64  `json:"expiration"`
-	LimitServers int    `json:"limit_servers"`
-	LimitUsers   int    `json:"limit_users"`
-	LimitNodes   int    `json:"limit_nodes"`
-	LimitClients int    `json:"limit_clients"`
-	Metadata     string `json:"metadata"`
-	IsActive     string `json:"is_active"` // yes if active
+	LicenseValue  string `json:"license_value"` // actual (public) key and the unique value for the key
+	Expiration    int64  `json:"expiration"`
+	LimitServers  int    `json:"limit_servers"`
+	LimitUsers    int    `json:"limit_users"`
+	LimitHosts    int    `json:"limit_hosts"`
+	LimitNetworks int    ` json:"limit_networks"`
+	LimitClients  int    `json:"limit_clients"`
+	Metadata      string `json:"metadata"`
+	IsActive      bool   `json:"is_active"` // yes if active
 }
 
 // ValidatedLicense - the validated license struct
@@ -49,15 +50,15 @@ type ValidatedLicense struct {
 
 // LicenseSecret - the encrypted struct for sending user-id
 type LicenseSecret struct {
-	UserID string        `json:"user_id" binding:"required"` // UUID for user foreign key to User table
-	Limits LicenseLimits `json:"limits" binding:"required"`
+	AssociatedID string        `json:"associated_id" binding:"required"` // UUID for user foreign key to User table
+	Limits       LicenseLimits `json:"limits" binding:"required"`
 }
 
 // LicenseLimits - struct license limits
 type LicenseLimits struct {
 	Servers int `json:"servers" binding:"required"`
 	Users   int `json:"users" binding:"required"`
-	Nodes   int `json:"nodes" binding:"required"`
+	Hosts   int `json:"hosts" binding:"required"`
 	Clients int `json:"clients" binding:"required"`
 }
 
@@ -65,7 +66,7 @@ type LicenseLimits struct {
 func (l *LicenseLimits) SetDefaults() {
 	l.Clients = 0
 	l.Servers = 1
-	l.Nodes = 0
+	l.Hosts = 0
 	l.Users = 1
 }
 

+ 2 - 2
ee/util.go

@@ -33,9 +33,9 @@ func base64decode(input string) []byte {
 
 func getCurrentServerLimit() (limits LicenseLimits) {
 	limits.SetDefaults()
-	nodes, err := logic.GetAllNodes()
+	hosts, err := logic.GetAllHosts()
 	if err == nil {
-		limits.Nodes = len(nodes)
+		limits.Hosts = len(hosts)
 	}
 	clients, err := logic.GetAllExtClients()
 	if err == nil {

+ 1 - 3
logic/serverconf.go

@@ -8,8 +8,6 @@ import (
 )
 
 var (
-	// Node_Limit - dummy var for community
-	Node_Limit = 1000000000
 	// Networks_Limit - dummy var for community
 	Networks_Limit = 1000000000
 	// Users_Limit - dummy var for community
@@ -90,7 +88,7 @@ func StoreJWTSecret(privateKey string) error {
 }
 
 func SetFreeTierLimits() {
-	Node_Limit = servercfg.GetNodeLimit()
+	Free_Tier = true
 	Users_Limit = servercfg.GetUserLimit()
 	Clients_Limit = servercfg.GetClientLimit()
 	Networks_Limit = servercfg.GetNetworkLimit()

+ 20 - 30
servercfg/serverconf.go

@@ -626,54 +626,44 @@ func GetStunPort() int {
 	return port
 }
 
-func GetNodeLimit() int {
-	var nodelimit int
-	if os.Getenv("NODE_LIMIT") != "" {
-		nodelimit, _ = strconv.Atoi(os.Getenv("Node_LIMIT"))
-	} else {
-		nodelimit = config.Config.Server.NodeLimit
-	}
-	return nodelimit
-}
-
 func GetUserLimit() int {
-	var userlimit int
-	if os.Getenv("USER_LIMIT") != "" {
-		userlimit, _ = strconv.Atoi(os.Getenv("USER_LIMIT"))
+	var userslimit int
+	if os.Getenv("USERS_LIMIT") != "" {
+		userslimit, _ = strconv.Atoi(os.Getenv("USER_LIMIT"))
 	} else {
-		userlimit = config.Config.Server.UserLimit
+		userslimit = config.Config.Server.UsersLimit
 	}
-	return userlimit
+	return userslimit
 }
 
 func GetNetworkLimit() int {
-	var networklimit int
-	if os.Getenv("NETWORK_LIMIT") != "" {
-		networklimit, _ = strconv.Atoi(os.Getenv("NETWORK_LIMIT"))
+	var networkslimit int
+	if os.Getenv("NETWORKS_LIMIT") != "" {
+		networkslimit, _ = strconv.Atoi(os.Getenv("NETWORKS_LIMIT"))
 	} else {
-		networklimit = config.Config.Server.NetworkLimit
+		networkslimit = config.Config.Server.NetworksLimit
 	}
-	return networklimit
+	return networkslimit
 }
 
 func GetClientLimit() int {
-	var clientLimit int
-	if os.Getenv("CLIENT_LIMIT") != "" {
-		clientLimit, _ = strconv.Atoi(os.Getenv("CLIENT_LIMIT"))
+	var clientsLimit int
+	if os.Getenv("CLIENTS_LIMIT") != "" {
+		clientsLimit, _ = strconv.Atoi(os.Getenv("CLIENTS_LIMIT"))
 	} else {
-		clientLimit = config.Config.Server.ClientLimit
+		clientsLimit = config.Config.Server.ClientsLimit
 	}
-	return clientLimit
+	return clientsLimit
 }
 
 func GetHostLimit() int {
-	var hostLimit int
-	if os.Getenv("HOST_LIMIT") != "" {
-		hostLimit, _ = strconv.Atoi(os.Getenv("HOST_LIMIT"))
+	var hostsLimit int
+	if os.Getenv("HOSTS_LIMIT") != "" {
+		hostsLimit, _ = strconv.Atoi(os.Getenv("HOSTS_LIMIT"))
 	} else {
-		hostLimit = config.Config.Server.HostLimit
+		hostsLimit = config.Config.Server.HostsLimit
 	}
-	return hostLimit
+	return hostsLimit
 }
 
 func DeployedByOperator() bool {