From eb28faf669a91229a4af0b3b365e11fbd99d062e Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Wed, 28 Feb 2024 17:57:25 +0700 Subject: [PATCH 01/18] add emqx migration func --- migrate/migrate.go | 21 +++++++++++++++ mq/migrate.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 mq/migrate.go diff --git a/migrate/migrate.go b/migrate/migrate.go index ce4cc8407..28c0e524a 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "os" "golang.org/x/exp/slog" @@ -12,6 +13,7 @@ import ( "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/logic/acls" "github.com/gravitl/netmaker/models" + "github.com/gravitl/netmaker/mq" "github.com/gravitl/netmaker/servercfg" ) @@ -22,6 +24,9 @@ func Run() { updateHosts() updateNodes() updateAcls() + if os.Getenv("MIGRATE_EMQX") == "true" { + migrateEmqx() + } } func assignSuperAdmin() { @@ -292,3 +297,19 @@ func updateAcls() { slog.Info(fmt.Sprintf("(migration) successfully saved new acls for network: %s", network.NetID)) } } + +func migrateEmqx() { + hosts, err := logic.GetAllHosts() + if err != nil { + slog.Error("failed to migrate emqx: ", "error", err) + return + } + clientIDs := []string{} + for _, host := range hosts { + clientIDs = append(clientIDs, host.ID.String()) + } + err = mq.KickOutClients(clientIDs) + if err != nil { + slog.Error("failed to migrate emqx: ", "kickout-error", err) + } +} diff --git a/mq/migrate.go b/mq/migrate.go new file mode 100644 index 000000000..1a29e25f0 --- /dev/null +++ b/mq/migrate.go @@ -0,0 +1,65 @@ +package mq + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + + "golang.org/x/exp/slog" +) + +func getEmqxAuthTokenOld() (string, error) { + payload, err := json.Marshal(&emqxLogin{ + Username: os.Getenv("OLD_MQ_USERNAME"), + Password: os.Getenv("OLD_MQ_PASSWORD"), + }) + if err != nil { + return "", err + } + resp, err := http.Post(os.Getenv("OLD_EMQX_REST_ENDPOINT")+"/api/v5/login", "application/json", bytes.NewReader(payload)) + if err != nil { + return "", err + } + msg, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("error during EMQX login %v", string(msg)) + } + var loginResp emqxLoginResponse + if err := json.Unmarshal(msg, &loginResp); err != nil { + return "", err + } + return loginResp.Token, nil +} + +func KickOutClients(clientIDs []string) error { + authToken, err := getEmqxAuthTokenOld() + if err != nil { + return err + } + for _, clientID := range clientIDs { + url := fmt.Sprintf("%s/api/v5/clients/%s", os.Getenv("OLD_EMQX_REST_ENDPOINT"), clientID) + client := &http.Client{} + req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + slog.Error("failed to kick out client:", "client", clientID, "error", err) + continue + } + req.Header.Add("Authorization", "Bearer "+authToken) + res, err := client.Do(req) + if err != nil { + slog.Error("failed to kick out client:", "client", clientID, "req-error", err) + continue + } + if res.StatusCode != http.StatusNoContent { + slog.Error("failed to kick out client:", "client", clientID, "status-code", res.StatusCode) + } + res.Body.Close() + } + return nil +} From 463092518208ebcff3bb92fb6aa69cc681d0ada5 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Thu, 29 Feb 2024 13:30:51 +0700 Subject: [PATCH 02/18] send pull syn over old mq for emqx migration --- migrate/migrate.go | 20 ++++++------ mq/migrate.go | 76 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/migrate/migrate.go b/migrate/migrate.go index 28c0e524a..d2aa038a6 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -299,17 +299,15 @@ func updateAcls() { } func migrateEmqx() { - hosts, err := logic.GetAllHosts() - if err != nil { - slog.Error("failed to migrate emqx: ", "error", err) - return - } - clientIDs := []string{} - for _, host := range hosts { - clientIDs = append(clientIDs, host.ID.String()) - } - err = mq.KickOutClients(clientIDs) + + err := mq.SendPullSYN() if err != nil { - slog.Error("failed to migrate emqx: ", "kickout-error", err) + slog.Error("failed to send pull syn to clients", "error", err) + slog.Info("proceeding to kicking out clients from emqx") + err := mq.KickOutClients() + if err != nil { + slog.Error("failed to migrate emqx: ", "kickout-error", err) + } } + } diff --git a/mq/migrate.go b/mq/migrate.go index 1a29e25f0..f518ecdf0 100644 --- a/mq/migrate.go +++ b/mq/migrate.go @@ -3,14 +3,47 @@ package mq import ( "bytes" "encoding/json" + "errors" "fmt" "io" "net/http" "os" + "time" + mqtt "github.com/eclipse/paho.mqtt.golang" + "github.com/gravitl/netmaker/logic" + "github.com/gravitl/netmaker/models" + "github.com/gravitl/netmaker/servercfg" "golang.org/x/exp/slog" ) +func setupmqtt_old() (mqtt.Client, error) { + + opts := mqtt.NewClientOptions() + opts.AddBroker(os.Getenv("OLD_BROKER_ENDPOINT")) + id := logic.RandomString(23) + opts.ClientID = id + opts.SetUsername(os.Getenv("OLD_MQ_USERNAME")) + opts.SetPassword(os.Getenv("OLD_MQ_PASSWORD")) + opts.SetAutoReconnect(true) + opts.SetConnectRetry(true) + opts.SetConnectRetryInterval(time.Second << 2) + opts.SetKeepAlive(time.Minute) + opts.SetWriteTimeout(time.Minute) + mqclient := mqtt.NewClient(opts) + + var connecterr error + if token := mqclient.Connect(); !token.WaitTimeout(30*time.Second) || token.Error() != nil { + if token.Error() == nil { + connecterr = errors.New("connect timeout") + } else { + connecterr = token.Error() + } + slog.Error("unable to connect to broker", "server", os.Getenv("OLD_BROKER_ENDPOINT"), "error", connecterr) + } + return mqclient, nil +} + func getEmqxAuthTokenOld() (string, error) { payload, err := json.Marshal(&emqxLogin{ Username: os.Getenv("OLD_MQ_USERNAME"), @@ -37,27 +70,58 @@ func getEmqxAuthTokenOld() (string, error) { return loginResp.Token, nil } -func KickOutClients(clientIDs []string) error { +func SendPullSYN() error { + mqclient, err := setupmqtt_old() + if err != nil { + return err + } + hosts, err := logic.GetAllHosts() + if err != nil { + return err + } + for _, host := range hosts { + host := host + hostUpdate := models.HostUpdate{ + Action: models.RequestPull, + Host: host, + } + msg, _ := json.Marshal(hostUpdate) + encrypted, encryptErr := encryptMsg(&host, msg) + if encryptErr != nil { + continue + } + mqclient.Publish(fmt.Sprintf("host/update/%s/%s", hostUpdate.Host.ID.String(), servercfg.GetServer()), 0, true, encrypted) + } + return nil +} + +func KickOutClients() error { authToken, err := getEmqxAuthTokenOld() if err != nil { return err } - for _, clientID := range clientIDs { - url := fmt.Sprintf("%s/api/v5/clients/%s", os.Getenv("OLD_EMQX_REST_ENDPOINT"), clientID) + hosts, err := logic.GetAllHosts() + if err != nil { + slog.Error("failed to migrate emqx: ", "error", err) + return err + } + + for _, host := range hosts { + url := fmt.Sprintf("%s/api/v5/clients/%s", os.Getenv("OLD_EMQX_REST_ENDPOINT"), host.ID.String()) client := &http.Client{} req, err := http.NewRequest(http.MethodDelete, url, nil) if err != nil { - slog.Error("failed to kick out client:", "client", clientID, "error", err) + slog.Error("failed to kick out client:", "client", host.ID.String(), "error", err) continue } req.Header.Add("Authorization", "Bearer "+authToken) res, err := client.Do(req) if err != nil { - slog.Error("failed to kick out client:", "client", clientID, "req-error", err) + slog.Error("failed to kick out client:", "client", host.ID.String(), "req-error", err) continue } if res.StatusCode != http.StatusNoContent { - slog.Error("failed to kick out client:", "client", clientID, "status-code", res.StatusCode) + slog.Error("failed to kick out client:", "client", host.ID.String(), "status-code", res.StatusCode) } res.Body.Close() } From 01592b2ecc2ef43f3c171d6611e31a631027f245 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Thu, 29 Feb 2024 19:58:18 +0700 Subject: [PATCH 03/18] add additional logging --- migrate/migrate.go | 5 +++-- mq/migrate.go | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/migrate/migrate.go b/migrate/migrate.go index d2aa038a6..f2a201f86 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -25,6 +25,7 @@ func Run() { updateNodes() updateAcls() if os.Getenv("MIGRATE_EMQX") == "true" { + logger.Log(0, "migrating emqx...") migrateEmqx() } } @@ -302,11 +303,11 @@ func migrateEmqx() { err := mq.SendPullSYN() if err != nil { - slog.Error("failed to send pull syn to clients", "error", err) + logger.Log(0, "failed to send pull syn to clients", "error", err.Error()) slog.Info("proceeding to kicking out clients from emqx") err := mq.KickOutClients() if err != nil { - slog.Error("failed to migrate emqx: ", "kickout-error", err) + logger.Log(0, "failed to migrate emqx: ", "kickout-error", err.Error()) } } diff --git a/mq/migrate.go b/mq/migrate.go index f518ecdf0..068d785f0 100644 --- a/mq/migrate.go +++ b/mq/migrate.go @@ -11,6 +11,7 @@ import ( "time" mqtt "github.com/eclipse/paho.mqtt.golang" + "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/servercfg" @@ -90,6 +91,7 @@ func SendPullSYN() error { if encryptErr != nil { continue } + logger.Log(0, "sending pull syn to", host.Name) mqclient.Publish(fmt.Sprintf("host/update/%s/%s", hostUpdate.Host.ID.String(), servercfg.GetServer()), 0, true, encrypted) } return nil From be0a5885ea4b3d5b7752c6ebfb7d5cc1cfbdcc1e Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Fri, 1 Mar 2024 12:58:00 +0700 Subject: [PATCH 04/18] use kick out clients --- migrate/migrate.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/migrate/migrate.go b/migrate/migrate.go index f2a201f86..02076638e 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -301,14 +301,14 @@ func updateAcls() { func migrateEmqx() { - err := mq.SendPullSYN() + // err := mq.SendPullSYN() + // if err != nil { + // logger.Log(0, "failed to send pull syn to clients", "error", err.Error()) + slog.Info("proceeding to kicking out clients from emqx") + err := mq.KickOutClients() if err != nil { - logger.Log(0, "failed to send pull syn to clients", "error", err.Error()) - slog.Info("proceeding to kicking out clients from emqx") - err := mq.KickOutClients() - if err != nil { - logger.Log(0, "failed to migrate emqx: ", "kickout-error", err.Error()) - } + logger.Log(0, "failed to migrate emqx: ", "kickout-error", err.Error()) } + //} } From 0259c87f874736fd201acd517e15f4cd927c99a0 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Fri, 1 Mar 2024 13:08:25 +0700 Subject: [PATCH 05/18] migrate emqx --- main.go | 4 ++++ migrate/migrate.go | 8 ++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 014711abf..83880beb3 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,10 @@ func main() { defer stop() var waitGroup sync.WaitGroup startControllers(&waitGroup, ctx) // start the api endpoint and mq and stun + if os.Getenv("MIGRATE_EMQX") == "true" { + logger.Log(0, "migrating emqx...") + migrate.MigrateEmqx() + } startHooks() <-ctx.Done() waitGroup.Wait() diff --git a/migrate/migrate.go b/migrate/migrate.go index 02076638e..e3b861bea 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "log" - "os" "golang.org/x/exp/slog" @@ -24,10 +23,7 @@ func Run() { updateHosts() updateNodes() updateAcls() - if os.Getenv("MIGRATE_EMQX") == "true" { - logger.Log(0, "migrating emqx...") - migrateEmqx() - } + } func assignSuperAdmin() { @@ -299,7 +295,7 @@ func updateAcls() { } } -func migrateEmqx() { +func MigrateEmqx() { // err := mq.SendPullSYN() // if err != nil { From 8e240ff701bc0ac782f4286da42956e5353064a4 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Fri, 1 Mar 2024 13:35:59 +0700 Subject: [PATCH 06/18] migrate emqx --- controllers/controller.go | 6 ++++++ main.go | 4 ---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/controllers/controller.go b/controllers/controller.go index d80093beb..798a61a02 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "os" "strings" "sync" "time" @@ -11,6 +12,7 @@ import ( "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/gravitl/netmaker/logger" + m "github.com/gravitl/netmaker/migrate" "github.com/gravitl/netmaker/servercfg" ) @@ -62,6 +64,10 @@ func HandleRESTRequests(wg *sync.WaitGroup, ctx context.Context) { logger.Log(0, err.Error()) } }() + if os.Getenv("MIGRATE_EMQX") == "true" { + logger.Log(0, "migrating emqx...") + m.MigrateEmqx() + } logger.Log(0, "REST Server successfully started on port ", port, " (REST)") // Block main routine until a signal is received diff --git a/main.go b/main.go index 83880beb3..014711abf 100644 --- a/main.go +++ b/main.go @@ -48,10 +48,6 @@ func main() { defer stop() var waitGroup sync.WaitGroup startControllers(&waitGroup, ctx) // start the api endpoint and mq and stun - if os.Getenv("MIGRATE_EMQX") == "true" { - logger.Log(0, "migrating emqx...") - migrate.MigrateEmqx() - } startHooks() <-ctx.Done() waitGroup.Wait() From 83cdc1abf656b295ca89e2b2123816bc5b48d72d Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Fri, 1 Mar 2024 14:26:07 +0700 Subject: [PATCH 07/18] migrate emqx --- controllers/controller.go | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/controller.go b/controllers/controller.go index 798a61a02..4ce41e47c 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -66,6 +66,7 @@ func HandleRESTRequests(wg *sync.WaitGroup, ctx context.Context) { }() if os.Getenv("MIGRATE_EMQX") == "true" { logger.Log(0, "migrating emqx...") + time.Sleep(time.Second * 2) m.MigrateEmqx() } logger.Log(0, "REST Server successfully started on port ", port, " (REST)") From 81af1d92c080c904605ffcd8854966797a96285f Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Fri, 1 Mar 2024 14:46:32 +0700 Subject: [PATCH 08/18] migrate emqx --- migrate/migrate.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/migrate/migrate.go b/migrate/migrate.go index e3b861bea..0fa5601a3 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -297,14 +297,14 @@ func updateAcls() { func MigrateEmqx() { - // err := mq.SendPullSYN() - // if err != nil { - // logger.Log(0, "failed to send pull syn to clients", "error", err.Error()) - slog.Info("proceeding to kicking out clients from emqx") - err := mq.KickOutClients() + err := mq.SendPullSYN() if err != nil { - logger.Log(0, "failed to migrate emqx: ", "kickout-error", err.Error()) + logger.Log(0, "failed to send pull syn to clients", "error", err.Error()) + slog.Info("proceeding to kicking out clients from emqx") + err := mq.KickOutClients() + if err != nil { + logger.Log(0, "failed to migrate emqx: ", "kickout-error", err.Error()) + } } - //} } From 2e3b640a9a37d5aa1024b31b586391a2d726914c Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Fri, 1 Mar 2024 14:59:47 +0700 Subject: [PATCH 09/18] migrate emqx --- migrate/migrate.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/migrate/migrate.go b/migrate/migrate.go index 0fa5601a3..0dd8ba5cb 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "time" "golang.org/x/exp/slog" @@ -300,11 +301,13 @@ func MigrateEmqx() { err := mq.SendPullSYN() if err != nil { logger.Log(0, "failed to send pull syn to clients", "error", err.Error()) - slog.Info("proceeding to kicking out clients from emqx") - err := mq.KickOutClients() - if err != nil { - logger.Log(0, "failed to migrate emqx: ", "kickout-error", err.Error()) - } + + } + time.Sleep(time.Second * 3) + slog.Info("proceeding to kicking out clients from emqx") + err = mq.KickOutClients() + if err != nil { + logger.Log(0, "failed to migrate emqx: ", "kickout-error", err.Error()) } } From d69d0ed2e281e427ad3d4e4849f252711cf36602 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Sat, 2 Mar 2024 12:42:04 +0700 Subject: [PATCH 10/18] validate relay req for inet gws --- controllers/node.go | 11 +++++++++++ logic/relay.go | 4 ++++ pro/initialize.go | 1 + pro/logic/relays.go | 3 +++ 4 files changed, 19 insertions(+) diff --git a/controllers/node.go b/controllers/node.go index d0a77ce75..2ff0456c9 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -645,6 +645,17 @@ func updateNode(w http.ResponseWriter, r *http.Request) { } relayUpdate := logic.RelayUpdates(¤tNode, newNode) + if relayUpdate && newNode.IsRelay { + err = logic.ValidateRelay(models.RelayRequest{ + NodeID: newNode.ID.String(), + NetID: newNode.Network, + RelayedNodes: newNode.RelayedNodes, + }) + if err != nil { + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + } _, err = logic.GetHost(newNode.HostID.String()) if err != nil { logger.Log(0, r.Header.Get("user"), diff --git a/logic/relay.go b/logic/relay.go index 181cf5114..225c77dc8 100644 --- a/logic/relay.go +++ b/logic/relay.go @@ -28,3 +28,7 @@ var SetRelayedNodes = func(setRelayed bool, relay string, relayed []string) []mo var RelayUpdates = func(currentNode, newNode *models.Node) bool { return false } + +var ValidateRelay = func(relay models.RelayRequest) error { + return nil +} diff --git a/pro/initialize.go b/pro/initialize.go index 2c06d3bb6..13e2bfaa6 100644 --- a/pro/initialize.go +++ b/pro/initialize.go @@ -101,6 +101,7 @@ func InitPro() { logic.UpdateRelayed = proLogic.UpdateRelayed logic.SetRelayedNodes = proLogic.SetRelayedNodes logic.RelayUpdates = proLogic.RelayUpdates + logic.ValidateRelay = proLogic.ValidateRelay logic.GetTrialEndDate = getTrialEndDate logic.SetDefaultGw = proLogic.SetDefaultGw logic.SetDefaultGwForRelayedUpdate = proLogic.SetDefaultGwForRelayedUpdate diff --git a/pro/logic/relays.go b/pro/logic/relays.go index 09e56dc0b..6be0c8a78 100644 --- a/pro/logic/relays.go +++ b/pro/logic/relays.go @@ -119,6 +119,9 @@ func ValidateRelay(relay models.RelayRequest) error { if relayedNode.IsIngressGateway { return errors.New("cannot relay an ingress gateway (" + relayedNodeID + ")") } + if relayedNode.IsInternetGateway { + return errors.New("cannot relay an internet gateway (" + relayedNodeID + ")") + } } return err } From b4f788f557a83f4452b95a84718760109245a46b Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 11:43:17 +0700 Subject: [PATCH 11/18] add debug logs to removing host from network --- controllers/hosts.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/controllers/hosts.go b/controllers/hosts.go index 4cee62f61..3f383809c 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -396,6 +396,7 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("hostid or network cannot be empty"), "badrequest")) return } + logger.Log(0, "Hereeeee-----> 1") // confirm host exists currHost, err := logic.GetHost(hostid) if err != nil { @@ -421,6 +422,7 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) return } + logger.Log(0, "Hereeeee-----> 2") node, err := logic.UpdateHostNetwork(currHost, network, false) if err != nil { @@ -445,6 +447,7 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) return } + logger.Log(0, "Hereeeee-----> 3") var gwClients []models.ExtClient if node.IsIngressGateway { gwClients = logic.GetGwExtclients(node.ID.String(), node.Network) @@ -454,12 +457,14 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("failed to delete node"), "internal")) return } + logger.Log(0, "Hereeeee-----> 4") go func() { mq.PublishMqUpdatesForDeletedNode(*node, true, gwClients) if servercfg.IsDNSMode() { logic.SetDNS() } }() + logger.Log(0, "Hereeeee-----> 5") logger.Log(2, r.Header.Get("user"), fmt.Sprintf("removed host %s from network %s", currHost.Name, network)) w.WriteHeader(http.StatusOK) } From e3b6b060c3d371d9fb8ea0609d8a04deb8bbe1eb Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 12:06:36 +0700 Subject: [PATCH 12/18] add debug logs to removing host from network --- controllers/hosts.go | 5 +++-- logic/nodes.go | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 3f383809c..1a038398f 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -452,19 +452,20 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { if node.IsIngressGateway { gwClients = logic.GetGwExtclients(node.ID.String(), node.Network) } + logger.Log(0, "Hereeeee-----> 4") logger.Log(1, "deleting node", node.ID.String(), "from host", currHost.Name) if err := logic.DeleteNode(node, forceDelete); err != nil { logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("failed to delete node"), "internal")) return } - logger.Log(0, "Hereeeee-----> 4") + logger.Log(0, "Hereeeee-----> 5") go func() { mq.PublishMqUpdatesForDeletedNode(*node, true, gwClients) if servercfg.IsDNSMode() { logic.SetDNS() } }() - logger.Log(0, "Hereeeee-----> 5") + logger.Log(0, "Hereeeee-----> 6") logger.Log(2, r.Header.Get("user"), fmt.Sprintf("removed host %s from network %s", currHost.Name, network)) w.WriteHeader(http.StatusOK) } diff --git a/logic/nodes.go b/logic/nodes.go index f5e7e1258..b38e028ea 100644 --- a/logic/nodes.go +++ b/logic/nodes.go @@ -189,13 +189,14 @@ func UpdateNode(currentNode *models.Node, newNode *models.Node) error { func DeleteNode(node *models.Node, purge bool) error { alreadyDeleted := node.PendingDelete || node.Action == models.NODE_DELETE node.Action = models.NODE_DELETE - + logger.Log(0, "Hereeeee-----> 4.1") //delete ext clients if node is ingress gw if node.IsIngressGateway { if err := DeleteGatewayExtClients(node.ID.String(), node.Network); err != nil { slog.Error("failed to delete ext clients", "nodeid", node.ID.String(), "error", err.Error()) } } + logger.Log(0, "Hereeeee-----> 4.2") if node.IsRelayed { // cleanup node from relayednodes on relay node relayNode, err := GetNodeByID(node.RelayedBy) @@ -211,6 +212,7 @@ func DeleteNode(node *models.Node, purge bool) error { UpsertNode(&relayNode) } } + logger.Log(0, "Hereeeee-----> 4.3") if node.FailedOverBy != uuid.Nil { ResetFailedOverPeer(node) } @@ -218,6 +220,7 @@ func DeleteNode(node *models.Node, purge bool) error { // unset all the relayed nodes SetRelayedNodes(false, node.ID.String(), node.RelayedNodes) } + logger.Log(0, "Hereeeee-----> 4.4") if node.InternetGwID != "" { inetNode, err := GetNodeByID(node.InternetGwID) if err == nil { @@ -232,10 +235,11 @@ func DeleteNode(node *models.Node, purge bool) error { UpsertNode(&inetNode) } } + logger.Log(0, "Hereeeee-----> 4.5") if node.IsInternetGateway { UnsetInternetGw(node) } - + logger.Log(0, "Hereeeee-----> 4.6") if !purge && !alreadyDeleted { newnode := *node newnode.PendingDelete = true @@ -256,9 +260,11 @@ func DeleteNode(node *models.Node, purge bool) error { } return err } + logger.Log(0, "Hereeeee-----> 4.7") if err := DissasociateNodeFromHost(node, host); err != nil { return err } + logger.Log(0, "Hereeeee-----> 4.8") return nil } From 1b4eb1b768edf8ad8fd57e9c285bb50bb1fcc604 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 12:25:44 +0700 Subject: [PATCH 13/18] add debug logs to removing host from network --- logic/hosts.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/logic/hosts.go b/logic/hosts.go index d3dd48bb9..e883f1916 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -386,6 +386,7 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { if len(h.ID.String()) == 0 || h.ID == uuid.Nil { return ErrInvalidHostID } + logger.Log(0, "Hereeeee-----> 4.7.1") if n.HostID != h.ID { // check if node actually belongs to host return fmt.Errorf("node is not associated with host") } @@ -399,6 +400,7 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { break } } + logger.Log(0, "Hereeeee-----> 4.7.2") if index < 0 { if len(h.Nodes) == 0 { return fmt.Errorf("node %s, not found in host, %s", n.ID.String(), h.ID.String()) @@ -406,15 +408,23 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { } else { h.Nodes = RemoveStringSlice(h.Nodes, index) } + logger.Log(0, "Hereeeee-----> 4.7.3") go func() { if servercfg.IsPro { + logger.Log(0, "Hereeeee-----> 4.7.3.1") if clients, err := GetNetworkExtClients(n.Network); err != nil { + logger.Log(0, "Hereeeee-----> 4.7.3.2") for i := range clients { AllowClientNodeAccess(&clients[i], n.ID.String()) + logger.Log(0, "Hereeeee-----> 4.7.3.3") } + logger.Log(0, "Hereeeee-----> 4.7.3.4") } + logger.Log(0, "Hereeeee-----> 4.7.3.5") + } }() + logger.Log(0, "Hereeeee-----> 4.7.4") if err := DeleteNodeByID(n); err != nil { return err } From 0dae81438fda1a6a49ec77e8d690ac9ec6c01357 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 12:38:21 +0700 Subject: [PATCH 14/18] add debug logs to removing host from network --- logic/hosts.go | 12 ++++++------ logic/nodes.go | 7 ++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/logic/hosts.go b/logic/hosts.go index e883f1916..d84c434a5 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -411,16 +411,16 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { logger.Log(0, "Hereeeee-----> 4.7.3") go func() { if servercfg.IsPro { - logger.Log(0, "Hereeeee-----> 4.7.3.1") + //logger.Log(0, "Hereeeee-----> 4.7.3.1") if clients, err := GetNetworkExtClients(n.Network); err != nil { - logger.Log(0, "Hereeeee-----> 4.7.3.2") + //logger.Log(0, "Hereeeee-----> 4.7.3.2") for i := range clients { AllowClientNodeAccess(&clients[i], n.ID.String()) - logger.Log(0, "Hereeeee-----> 4.7.3.3") + //logger.Log(0, "Hereeeee-----> 4.7.3.3") } - logger.Log(0, "Hereeeee-----> 4.7.3.4") + //logger.Log(0, "Hereeeee-----> 4.7.3.4") } - logger.Log(0, "Hereeeee-----> 4.7.3.5") + //logger.Log(0, "Hereeeee-----> 4.7.3.5") } }() @@ -428,7 +428,7 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { if err := DeleteNodeByID(n); err != nil { return err } - + logger.Log(0, "Hereeeee-----> 4.7.5") return UpsertHost(h) } diff --git a/logic/nodes.go b/logic/nodes.go index b38e028ea..7d6a2db70 100644 --- a/logic/nodes.go +++ b/logic/nodes.go @@ -287,27 +287,32 @@ func GetNodeByHostRef(hostid, network string) (node models.Node, err error) { func DeleteNodeByID(node *models.Node) error { var err error var key = node.ID.String() - + logger.Log(0, "Hereeeee-----> 4.7.4.1") if err = database.DeleteRecord(database.NODES_TABLE_NAME, key); err != nil { if !database.IsEmptyRecord(err) { return err } } + logger.Log(0, "Hereeeee-----> 4.7.4.2") if servercfg.CacheEnabled() { deleteNodeFromCache(node.ID.String()) } + logger.Log(0, "Hereeeee-----> 4.7.4.3") if servercfg.IsDNSMode() { SetDNS() } + logger.Log(0, "Hereeeee-----> 4.7.4.4") _, err = nodeacls.RemoveNodeACL(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String())) if err != nil { // ignoring for now, could hit a nil pointer if delete called twice logger.Log(2, "attempted to remove node ACL for node", node.ID.String()) } + logger.Log(0, "Hereeeee-----> 4.7.4.5") // removeZombie <- node.ID if err = DeleteMetrics(node.ID.String()); err != nil { logger.Log(1, "unable to remove metrics from DB for node", node.ID.String(), err.Error()) } + logger.Log(0, "Hereeeee-----> 4.7.4.6") return nil } From f165f5fcf414a4f89c9563971b54892050a3d05f Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 12:51:37 +0700 Subject: [PATCH 15/18] add debug logs to removing host from network --- logic/acls/nodeacls/modify.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/logic/acls/nodeacls/modify.go b/logic/acls/nodeacls/modify.go index 54c898b79..5710dd612 100644 --- a/logic/acls/nodeacls/modify.go +++ b/logic/acls/nodeacls/modify.go @@ -77,14 +77,12 @@ func RemoveNodeACL(networkID NetworkID, nodeID NodeID) (acls.ACLContainer, error if err != nil { return nil, err } - acls.AclMutex.Lock() for currentNodeID := range currentNetworkACL { if NodeID(currentNodeID) != nodeID { currentNetworkACL[currentNodeID].Remove(acls.AclID(nodeID)) } } delete(currentNetworkACL, acls.AclID(nodeID)) - acls.AclMutex.Unlock() return currentNetworkACL.Save(acls.ContainerID(networkID)) } From c25a73201566ecae36ec50613dfc95ca38f308d0 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 13:11:51 +0700 Subject: [PATCH 16/18] fix acl mutex lock --- logic/acls/nodeacls/retrieve.go | 4 +++- logic/hosts.go | 11 ----------- logic/nodes.go | 6 ------ 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/logic/acls/nodeacls/retrieve.go b/logic/acls/nodeacls/retrieve.go index 70870e4d6..15397c24c 100644 --- a/logic/acls/nodeacls/retrieve.go +++ b/logic/acls/nodeacls/retrieve.go @@ -15,8 +15,10 @@ func AreNodesAllowed(networkID NetworkID, node1, node2 NodeID) bool { } var allowed bool acls.AclMutex.RLock() - allowed = currentNetworkACL[acls.AclID(node1)].IsAllowed(acls.AclID(node2)) && currentNetworkACL[acls.AclID(node2)].IsAllowed(acls.AclID(node1)) + currNetworkACLNode1 := currentNetworkACL[acls.AclID(node1)] + currNetworkACLNode2 := currentNetworkACL[acls.AclID(node2)] acls.AclMutex.RUnlock() + allowed = currNetworkACLNode1.IsAllowed(acls.AclID(node2)) && currNetworkACLNode2.IsAllowed(acls.AclID(node1)) return allowed } diff --git a/logic/hosts.go b/logic/hosts.go index d84c434a5..6d8c3b281 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -386,7 +386,6 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { if len(h.ID.String()) == 0 || h.ID == uuid.Nil { return ErrInvalidHostID } - logger.Log(0, "Hereeeee-----> 4.7.1") if n.HostID != h.ID { // check if node actually belongs to host return fmt.Errorf("node is not associated with host") } @@ -400,7 +399,6 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { break } } - logger.Log(0, "Hereeeee-----> 4.7.2") if index < 0 { if len(h.Nodes) == 0 { return fmt.Errorf("node %s, not found in host, %s", n.ID.String(), h.ID.String()) @@ -408,27 +406,18 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error { } else { h.Nodes = RemoveStringSlice(h.Nodes, index) } - logger.Log(0, "Hereeeee-----> 4.7.3") go func() { if servercfg.IsPro { - //logger.Log(0, "Hereeeee-----> 4.7.3.1") if clients, err := GetNetworkExtClients(n.Network); err != nil { - //logger.Log(0, "Hereeeee-----> 4.7.3.2") for i := range clients { AllowClientNodeAccess(&clients[i], n.ID.String()) - //logger.Log(0, "Hereeeee-----> 4.7.3.3") } - //logger.Log(0, "Hereeeee-----> 4.7.3.4") } - //logger.Log(0, "Hereeeee-----> 4.7.3.5") - } }() - logger.Log(0, "Hereeeee-----> 4.7.4") if err := DeleteNodeByID(n); err != nil { return err } - logger.Log(0, "Hereeeee-----> 4.7.5") return UpsertHost(h) } diff --git a/logic/nodes.go b/logic/nodes.go index 7d6a2db70..7304ef54c 100644 --- a/logic/nodes.go +++ b/logic/nodes.go @@ -287,32 +287,26 @@ func GetNodeByHostRef(hostid, network string) (node models.Node, err error) { func DeleteNodeByID(node *models.Node) error { var err error var key = node.ID.String() - logger.Log(0, "Hereeeee-----> 4.7.4.1") if err = database.DeleteRecord(database.NODES_TABLE_NAME, key); err != nil { if !database.IsEmptyRecord(err) { return err } } - logger.Log(0, "Hereeeee-----> 4.7.4.2") if servercfg.CacheEnabled() { deleteNodeFromCache(node.ID.String()) } - logger.Log(0, "Hereeeee-----> 4.7.4.3") if servercfg.IsDNSMode() { SetDNS() } - logger.Log(0, "Hereeeee-----> 4.7.4.4") _, err = nodeacls.RemoveNodeACL(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String())) if err != nil { // ignoring for now, could hit a nil pointer if delete called twice logger.Log(2, "attempted to remove node ACL for node", node.ID.String()) } - logger.Log(0, "Hereeeee-----> 4.7.4.5") // removeZombie <- node.ID if err = DeleteMetrics(node.ID.String()); err != nil { logger.Log(1, "unable to remove metrics from DB for node", node.ID.String(), err.Error()) } - logger.Log(0, "Hereeeee-----> 4.7.4.6") return nil } From 8e0580764bb7097f9406f04b120e5b4e10e60c1e Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 13:14:51 +0700 Subject: [PATCH 17/18] remove debug logs --- controllers/hosts.go | 6 ------ logic/nodes.go | 8 -------- 2 files changed, 14 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 1a038398f..4cee62f61 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -396,7 +396,6 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("hostid or network cannot be empty"), "badrequest")) return } - logger.Log(0, "Hereeeee-----> 1") // confirm host exists currHost, err := logic.GetHost(hostid) if err != nil { @@ -422,7 +421,6 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) return } - logger.Log(0, "Hereeeee-----> 2") node, err := logic.UpdateHostNetwork(currHost, network, false) if err != nil { @@ -447,25 +445,21 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) return } - logger.Log(0, "Hereeeee-----> 3") var gwClients []models.ExtClient if node.IsIngressGateway { gwClients = logic.GetGwExtclients(node.ID.String(), node.Network) } - logger.Log(0, "Hereeeee-----> 4") logger.Log(1, "deleting node", node.ID.String(), "from host", currHost.Name) if err := logic.DeleteNode(node, forceDelete); err != nil { logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("failed to delete node"), "internal")) return } - logger.Log(0, "Hereeeee-----> 5") go func() { mq.PublishMqUpdatesForDeletedNode(*node, true, gwClients) if servercfg.IsDNSMode() { logic.SetDNS() } }() - logger.Log(0, "Hereeeee-----> 6") logger.Log(2, r.Header.Get("user"), fmt.Sprintf("removed host %s from network %s", currHost.Name, network)) w.WriteHeader(http.StatusOK) } diff --git a/logic/nodes.go b/logic/nodes.go index 7304ef54c..353a33f16 100644 --- a/logic/nodes.go +++ b/logic/nodes.go @@ -189,14 +189,12 @@ func UpdateNode(currentNode *models.Node, newNode *models.Node) error { func DeleteNode(node *models.Node, purge bool) error { alreadyDeleted := node.PendingDelete || node.Action == models.NODE_DELETE node.Action = models.NODE_DELETE - logger.Log(0, "Hereeeee-----> 4.1") //delete ext clients if node is ingress gw if node.IsIngressGateway { if err := DeleteGatewayExtClients(node.ID.String(), node.Network); err != nil { slog.Error("failed to delete ext clients", "nodeid", node.ID.String(), "error", err.Error()) } } - logger.Log(0, "Hereeeee-----> 4.2") if node.IsRelayed { // cleanup node from relayednodes on relay node relayNode, err := GetNodeByID(node.RelayedBy) @@ -212,7 +210,6 @@ func DeleteNode(node *models.Node, purge bool) error { UpsertNode(&relayNode) } } - logger.Log(0, "Hereeeee-----> 4.3") if node.FailedOverBy != uuid.Nil { ResetFailedOverPeer(node) } @@ -220,7 +217,6 @@ func DeleteNode(node *models.Node, purge bool) error { // unset all the relayed nodes SetRelayedNodes(false, node.ID.String(), node.RelayedNodes) } - logger.Log(0, "Hereeeee-----> 4.4") if node.InternetGwID != "" { inetNode, err := GetNodeByID(node.InternetGwID) if err == nil { @@ -235,11 +231,9 @@ func DeleteNode(node *models.Node, purge bool) error { UpsertNode(&inetNode) } } - logger.Log(0, "Hereeeee-----> 4.5") if node.IsInternetGateway { UnsetInternetGw(node) } - logger.Log(0, "Hereeeee-----> 4.6") if !purge && !alreadyDeleted { newnode := *node newnode.PendingDelete = true @@ -260,11 +254,9 @@ func DeleteNode(node *models.Node, purge bool) error { } return err } - logger.Log(0, "Hereeeee-----> 4.7") if err := DissasociateNodeFromHost(node, host); err != nil { return err } - logger.Log(0, "Hereeeee-----> 4.8") return nil } From 8ac864fec88676313783b5dda5b4eb65217f3a0c Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 13:39:33 +0700 Subject: [PATCH 18/18] add update check for validate relay --- controllers/node.go | 2 +- logic/relay.go | 2 +- pro/logic/relays.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/controllers/node.go b/controllers/node.go index 2ff0456c9..2c2ce0431 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -650,7 +650,7 @@ func updateNode(w http.ResponseWriter, r *http.Request) { NodeID: newNode.ID.String(), NetID: newNode.Network, RelayedNodes: newNode.RelayedNodes, - }) + }, true) if err != nil { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) return diff --git a/logic/relay.go b/logic/relay.go index 225c77dc8..bd3c80bb9 100644 --- a/logic/relay.go +++ b/logic/relay.go @@ -29,6 +29,6 @@ var RelayUpdates = func(currentNode, newNode *models.Node) bool { return false } -var ValidateRelay = func(relay models.RelayRequest) error { +var ValidateRelay = func(relay models.RelayRequest, update bool) error { return nil } diff --git a/pro/logic/relays.go b/pro/logic/relays.go index 6be0c8a78..6c06f8c0f 100644 --- a/pro/logic/relays.go +++ b/pro/logic/relays.go @@ -44,7 +44,7 @@ func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error) if host.OS != "linux" { return returnnodes, models.Node{}, fmt.Errorf("only linux machines can be relay nodes") } - err = ValidateRelay(relay) + err = ValidateRelay(relay, false) if err != nil { return returnnodes, models.Node{}, err } @@ -101,14 +101,14 @@ func SetRelayedNodes(setRelayed bool, relay string, relayed []string) []models.N // } // ValidateRelay - checks if relay is valid -func ValidateRelay(relay models.RelayRequest) error { +func ValidateRelay(relay models.RelayRequest, update bool) error { var err error node, err := logic.GetNodeByID(relay.NodeID) if err != nil { return err } - if node.IsRelay { + if !update && node.IsRelay { return errors.New("node is already acting as a relay") } for _, relayedNodeID := range relay.RelayedNodes {