Skip to content

Commit

Permalink
Merge branch 'develop' into fix_mem_prof
Browse files Browse the repository at this point in the history
  • Loading branch information
yabinma authored Nov 26, 2024
2 parents 85d8598 + a0747d4 commit b906ffe
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 34 deletions.
4 changes: 2 additions & 2 deletions controllers/ext_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
extclient.IngressGatewayID = nodeid
extclient.Network = node.Network
extclient.Tags = make(map[models.TagID]struct{})
extclient.Tags[models.TagID(fmt.Sprintf("%s.%s", extclient.Network,
models.RemoteAccessTagName))] = struct{}{}
// extclient.Tags[models.TagID(fmt.Sprintf("%s.%s", extclient.Network,
// models.RemoteAccessTagName))] = struct{}{}
// set extclient dns to ingressdns if extclient dns is not explicitly set
if (extclient.DNS == "") && (node.IngressDNS != "") {
extclient.DNS = node.IngressDNS
Expand Down
28 changes: 14 additions & 14 deletions controllers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,26 @@ func getStatus(w http.ResponseWriter, r *http.Request) {
if servercfg.ErrLicenseValidation != nil {
licenseErr = servercfg.ErrLicenseValidation.Error()
}
var trialEndDate time.Time
var err error
isOnTrial := false
if servercfg.IsPro &&
(servercfg.GetLicenseKey() == "" || servercfg.GetNetmakerTenantID() == "") {
trialEndDate, err = logic.GetTrialEndDate()
if err != nil {
slog.Error("failed to get trial end date", "error", err)
} else {
isOnTrial = true
}
}
//var trialEndDate time.Time
//var err error
// isOnTrial := false
// if servercfg.IsPro &&
// (servercfg.GetLicenseKey() == "" || servercfg.GetNetmakerTenantID() == "") {
// trialEndDate, err = logic.GetTrialEndDate()
// if err != nil {
// slog.Error("failed to get trial end date", "error", err)
// } else {
// isOnTrial = true
// }
// }
currentServerStatus := status{
DB: database.IsConnected(),
Broker: mq.IsConnected(),
IsBrokerConnOpen: mq.IsConnectionOpen(),
LicenseError: licenseErr,
IsPro: servercfg.IsPro,
TrialEndDate: trialEndDate,
IsOnTrialLicense: isOnTrial,
//TrialEndDate: trialEndDate,
//IsOnTrialLicense: isOnTrial,
}

w.Header().Set("Content-Type", "application/json")
Expand Down
5 changes: 5 additions & 0 deletions controllers/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ func deleteTag(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
// check if active policy is using the tag
if logic.CheckIfTagAsActivePolicy(tag.ID, tag.Network) {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("tag is currently in use by an active policy"), "badrequest"))
return
}
err = logic.DeleteTag(models.TagID(tagID), true)
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
Expand Down
19 changes: 19 additions & 0 deletions logic/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,25 @@ func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
}
}

func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
acls := listDevicePolicies(netID)
for _, acl := range acls {
for _, srcTagI := range acl.Src {
if srcTagI.ID == models.DeviceAclID {
if tagID.String() == srcTagI.Value {
return true
}
}
}
for _, dstTagI := range acl.Dst {
if dstTagI.ID == models.DeviceAclID {
return true
}
}
}
return false
}

// RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
acls := listDevicePolicies(netID)
Expand Down
3 changes: 3 additions & 0 deletions logic/acls/nodeacls/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func CreateNodeACL(networkID NetworkID, nodeID NodeID, defaultVal byte) (acls.AC
acls.AclMutex.Lock()
var newNodeACL = make(acls.ACL)
for existingNodeID := range currentNetworkACL {
if currentNetworkACL[existingNodeID] == nil {
currentNetworkACL[existingNodeID] = make(acls.ACL)
}
currentNetworkACL[existingNodeID][acls.AclID(nodeID)] = defaultVal // set the old nodes to default value for new node
newNodeACL[existingNodeID] = defaultVal // set the old nodes in new node ACL to default value
}
Expand Down
33 changes: 32 additions & 1 deletion logic/extpeers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package logic

import (
"encoding/json"
"errors"
"fmt"
"net"
"reflect"
"strings"
"sync"
"time"

"github.com/goombaio/namegenerator"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic/acls"
Expand Down Expand Up @@ -281,13 +283,41 @@ func CreateExtClient(extclient *models.ExtClient) error {
}

if extclient.ClientID == "" {
extclient.ClientID = models.GenerateNodeName()
extclient.ClientID, err = GenerateNodeName(extclient.Network)
if err != nil {
return err
}
}

extclient.LastModified = time.Now().Unix()
return SaveExtClient(extclient)
}

// GenerateNodeName - generates a random node name
func GenerateNodeName(network string) (string, error) {
seed := time.Now().UTC().UnixNano()
nameGenerator := namegenerator.NewNameGenerator(seed)
var name string
cnt := 0
for {
if cnt > 10 {
return "", errors.New("couldn't generate random name, try again")
}
cnt += 1
name = nameGenerator.Generate()
if len(name) > 15 {
continue
}
_, err := GetExtClient(name, network)
if err == nil {
// config exists with same name
continue
}
break
}
return name, nil
}

// SaveExtClient - saves an ext client to database
func SaveExtClient(extclient *models.ExtClient) error {
key, err := GetRecordKey(extclient.ClientID, extclient.Network)
Expand Down Expand Up @@ -802,6 +832,7 @@ func GetStaticNodesByNetwork(network models.NetworkID, onlyWg bool) (staticNode
if err != nil {
return
}
SortExtClient(extClients[:])
for _, extI := range extClients {
if extI.Network == network.String() {
if onlyWg && extI.RemoteAccessClientID != "" {
Expand Down
17 changes: 17 additions & 0 deletions logic/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N
} else if host.EndpointIPv6 != nil && peerHost.EndpointIPv6 != nil {
peerEndpoint = peerHost.EndpointIPv6
}
if host.EndpointIP == nil && peerEndpoint == nil {
if peerHost.EndpointIP != nil {
peerEndpoint = peerHost.EndpointIP
}
}
if host.EndpointIPv6 == nil && peerEndpoint == nil {
if peerHost.EndpointIPv6 != nil {
peerEndpoint = peerHost.EndpointIPv6
}
}
if node.IsRelay && peer.RelayedBy == node.ID.String() && !peer.IsStatic {
// don't set endpoint on relayed peer
peerEndpoint = nil
}
if isFailOverPeer && peer.FailedOverBy == node.ID && !peer.IsStatic {
peerEndpoint = nil
}

peerConfig.Endpoint = &net.UDPAddr{
IP: peerEndpoint,
Expand Down
12 changes: 0 additions & 12 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,6 @@ func updateNodes() {
}
}
}
extclients, _ := logic.GetAllExtClients()
for _, extclient := range extclients {
tagID := models.TagID(fmt.Sprintf("%s.%s", extclient.Network,
models.RemoteAccessTagName))
if extclient.Tags == nil {
extclient.Tags = make(map[models.TagID]struct{})
}
if _, ok := extclient.Tags[tagID]; !ok {
extclient.Tags[tagID] = struct{}{}
logic.SaveExtClient(&extclient)
}
}
}

func removeInterGw(egressRanges []string) ([]string, bool) {
Expand Down
3 changes: 2 additions & 1 deletion models/user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const (
AllUserRsrcID RsrcID = "all_user"
AllDnsRsrcID RsrcID = "all_dns"
AllFailOverRsrcID RsrcID = "all_fail_over"
AllAclsRsrcID RsrcID = "all_acls"
AllAclsRsrcID RsrcID = "all_acl"
AllTagsRsrcID RsrcID = "all_tag"
)

// Pre-Defined User Roles
Expand Down
8 changes: 8 additions & 0 deletions pro/controllers/failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ func failOverME(w http.ResponseWriter, r *http.Request) {
)
return
}
if peerNode.IsFailOver {
logic.ReturnErrorResponse(
w,
r,
logic.FormatError(errors.New("peer is acting as failover"), "badrequest"),
)
return
}
if node.IsRelayed && node.RelayedBy == peerNode.ID.String() {
logic.ReturnErrorResponse(
w,
Expand Down
4 changes: 2 additions & 2 deletions pro/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,8 @@ func getRemoteAccessGatewayConf(w http.ResponseWriter, r *http.Request) {
userConf.Enabled = parentNetwork.DefaultACL == "yes"
}
userConf.Tags = make(map[models.TagID]struct{})
userConf.Tags[models.TagID(fmt.Sprintf("%s.%s", userConf.Network,
models.RemoteAccessTagName))] = struct{}{}
// userConf.Tags[models.TagID(fmt.Sprintf("%s.%s", userConf.Network,
// models.RemoteAccessTagName))] = struct{}{}
if err = logic.CreateExtClient(&userConf); err != nil {
slog.Error(
"failed to create extclient",
Expand Down
6 changes: 5 additions & 1 deletion pro/logic/failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logic
import (
"errors"
"net"
"sync"

"github.com/google/uuid"
"github.com/gravitl/netmaker/logger"
Expand All @@ -11,8 +12,11 @@ import (
"golang.org/x/exp/slog"
)

func SetFailOverCtx(failOverNode, victimNode, peerNode models.Node) error {
var failOverCtxMutex = &sync.RWMutex{}

func SetFailOverCtx(failOverNode, victimNode, peerNode models.Node) error {
failOverCtxMutex.Lock()
defer failOverCtxMutex.Unlock()
if peerNode.FailOverPeers == nil {
peerNode.FailOverPeers = make(map[string]struct{})
}
Expand Down
2 changes: 1 addition & 1 deletion pro/logic/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var (
metricsCacheMutex = &sync.RWMutex{}
metricsCacheMap map[string]models.Metrics
metricsCacheMap = make(map[string]models.Metrics)
)

func getMetricsFromCache(key string) (metrics models.Metrics, ok bool) {
Expand Down
60 changes: 60 additions & 0 deletions pro/logic/user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ var NetworkUserAllPermissionTemplate = models.UserRolePermissionTemplate{
SelfOnly: true,
},
},
models.DnsRsrc: {
models.AllDnsRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.AclRsrc: {
models.AllAclsRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.EgressGwRsrc: {
models.AllEgressGwRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.InetGwRsrc: {
models.AllInetGwRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.RelayRsrc: {
models.AllRelayRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.TagRsrc: {
models.AllTagsRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
},
}

Expand Down Expand Up @@ -147,6 +177,36 @@ func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
SelfOnly: true,
},
},
models.DnsRsrc: {
models.AllDnsRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.AclRsrc: {
models.AllAclsRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.EgressGwRsrc: {
models.AllEgressGwRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.InetGwRsrc: {
models.AllInetGwRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.RelayRsrc: {
models.AllRelayRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
models.TagRsrc: {
models.AllTagsRsrcID: models.RsrcPermissionScope{
Read: true,
},
},
},
}
d, _ := json.Marshal(NetworkAdminPermissionTemplate)
Expand Down

0 comments on commit b906ffe

Please sign in to comment.