From 514221d6a9b2c690a3a8b2d700fb3f551e63e481 Mon Sep 17 00:00:00 2001 From: Farukh Khan Date: Wed, 15 Nov 2023 15:43:15 +0800 Subject: [PATCH 1/7] NET-655 * Updated HostPull structure to include EgressRoutes and FirewallUpdate models. --- models/structs.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/models/structs.go b/models/structs.go index 906d16cf8..476b750c2 100644 --- a/models/structs.go +++ b/models/structs.go @@ -223,12 +223,14 @@ type TrafficKeys struct { // HostPull - response of a host's pull type HostPull struct { - Host Host `json:"host" yaml:"host"` - Nodes []Node `json:"nodes" yaml:"nodes"` - Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"` - ServerConfig ServerConfig `json:"server_config" yaml:"server_config"` - PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"` - HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"` + Host Host `json:"host" yaml:"host"` + Nodes []Node `json:"nodes" yaml:"nodes"` + Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"` + ServerConfig ServerConfig `json:"server_config" yaml:"server_config"` + PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"` + HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"` + EgressRoutes []EgressNetworkRoutes `json:"egress_network_routes"` + FwUpdate FwUpdate `json:"fw_update"` } // NodeGet - struct for a single node get response From d9a70884060218cb4ae211c311f3d2af7377a05f Mon Sep 17 00:00:00 2001 From: Farukh Khan Date: Wed, 15 Nov 2023 16:00:52 +0800 Subject: [PATCH 2/7] added ServerVersion structure to hostpull model * added ServerVersion structure to hostpull model --- models/structs.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/structs.go b/models/structs.go index 476b750c2..00dad7d9b 100644 --- a/models/structs.go +++ b/models/structs.go @@ -227,6 +227,7 @@ type HostPull struct { Nodes []Node `json:"nodes" yaml:"nodes"` Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"` ServerConfig ServerConfig `json:"server_config" yaml:"server_config"` + ServerVersion string `json:"serverversion" bson:"serverversion" yaml:"serverversion"` PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"` HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"` EgressRoutes []EgressNetworkRoutes `json:"egress_network_routes"` From 9c7e3dfec85e6cab80440460cdd737c325777d58 Mon Sep 17 00:00:00 2001 From: Farukh Khan Date: Thu, 16 Nov 2023 18:59:00 +0800 Subject: [PATCH 3/7] removed ServerVersion structure * removed ServerVersion structure --- models/structs.go | 1 - 1 file changed, 1 deletion(-) diff --git a/models/structs.go b/models/structs.go index 00dad7d9b..476b750c2 100644 --- a/models/structs.go +++ b/models/structs.go @@ -227,7 +227,6 @@ type HostPull struct { Nodes []Node `json:"nodes" yaml:"nodes"` Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"` ServerConfig ServerConfig `json:"server_config" yaml:"server_config"` - ServerVersion string `json:"serverversion" bson:"serverversion" yaml:"serverversion"` PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"` HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"` EgressRoutes []EgressNetworkRoutes `json:"egress_network_routes"` From b17aeff3ef3d0c8349f0c95e18c89dc5531db5e0 Mon Sep 17 00:00:00 2001 From: Farukh Khan Date: Wed, 22 Nov 2023 20:53:26 +0800 Subject: [PATCH 4/7] added egressroute and fwupdate to hostpull handler --- controllers/hosts.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controllers/hosts.go b/controllers/hosts.go index b79fbb813..a56ca5592 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -129,6 +129,8 @@ func pull(w http.ResponseWriter, r *http.Request) { Peers: hPU.Peers, PeerIDs: hPU.PeerIDs, HostNetworkInfo: hPU.HostNetworkInfo, + EgressRoutes: hPU.EgressRoutes, + FwUpdate: hPU.FwUpdate, } logger.Log(1, hostID, "completed a pull") From c1287254c8dba85d42d1e93cefbbd64a90d07b79 Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Thu, 14 Dec 2023 16:36:24 +0400 Subject: [PATCH 5/7] add host update fallback handler --- controllers/hosts.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ mq/handlers.go | 4 ++-- mq/util.go | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 261f07246..5a825f9f2 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -31,6 +31,7 @@ func hostHandlers(r *mux.Router) { r.HandleFunc("/api/hosts/adm/authenticate", authenticateHost).Methods(http.MethodPost) r.HandleFunc("/api/v1/host", Authorize(true, false, "host", http.HandlerFunc(pull))).Methods(http.MethodGet) r.HandleFunc("/api/v1/host/{hostid}/signalpeer", Authorize(true, false, "host", http.HandlerFunc(signalPeer))).Methods(http.MethodPost) + r.HandleFunc("/api/v1/fallback/host/{hostid}", Authorize(true, false, "host", http.HandlerFunc(hostUpdateFallback))).Methods(http.MethodPut) r.HandleFunc("/api/v1/auth-register/host", socketHandler) } @@ -218,6 +219,51 @@ func updateHost(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(apiHostData) } +// swagger:route PUT /api/v1/fallback/host/{hostid} hosts hostUpdateFallback +// +// Updates a Netclient host on Netmaker server. +// +// Schemes: https +// +// Security: +// oauth +// +// Responses: +// 200: apiHostResponse +func hostUpdateFallback(w http.ResponseWriter, r *http.Request) { + var params = mux.Vars(r) + hostid := params["hostid"] + currentHost, err := logic.GetHost(hostid) + if err != nil { + slog.Error("error getting host", "id", hostid, "error", err) + return + } + + var hostUpdate models.HostUpdate + err = json.NewDecoder(r.Body).Decode(&hostUpdate) + if err != nil { + logger.Log(0, r.Header.Get("user"), "failed to update a host:", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) + return + } + slog.Info("recieved host update", "name", hostUpdate.Host.Name, "id", hostUpdate.Host.ID) + switch hostUpdate.Action { + case models.CheckIn: + _ = mq.HandleHostCheckin(&hostUpdate.Host, currentHost) + + case models.UpdateHost: + + _ = logic.UpdateHostFromClient(&hostUpdate.Host, currentHost) + err := logic.UpsertHost(currentHost) + if err != nil { + slog.Error("failed to update host", "id", currentHost.ID, "error", err) + return + } + + } + +} + // swagger:route DELETE /api/hosts/{hostid} hosts deleteHost // // Deletes a Netclient host from Netmaker server. diff --git a/mq/handlers.go b/mq/handlers.go index a6b67f3f8..0acba3a3e 100644 --- a/mq/handlers.go +++ b/mq/handlers.go @@ -104,7 +104,7 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) { var sendPeerUpdate bool switch hostUpdate.Action { case models.CheckIn: - sendPeerUpdate = handleHostCheckin(&hostUpdate.Host, currentHost) + sendPeerUpdate = HandleHostCheckin(&hostUpdate.Host, currentHost) case models.Acknowledgement: hu := hostactions.GetAction(currentHost.ID.String()) if hu != nil { @@ -282,7 +282,7 @@ func HandleNewNodeDNS(host *models.Host, node *models.Node) error { return nil } -func handleHostCheckin(h, currentHost *models.Host) bool { +func HandleHostCheckin(h, currentHost *models.Host) bool { if h == nil { return false } diff --git a/mq/util.go b/mq/util.go index 6ecee1414..d99891098 100644 --- a/mq/util.go +++ b/mq/util.go @@ -78,7 +78,7 @@ func publish(host *models.Host, dest string, msg []byte) error { if encryptErr != nil { return encryptErr } - if mqclient == nil { + if mqclient == nil || !mqclient.IsConnectionOpen() { return errors.New("cannot publish ... mqclient not connected") } From 16b0e41ea3dc0cf257fc46486c92d1cf0edc3699 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Tue, 19 Dec 2023 21:53:14 +0400 Subject: [PATCH 6/7] set broker type on server cfg --- models/structs.go | 1 + servercfg/serverconf.go | 1 + 2 files changed, 2 insertions(+) diff --git a/models/structs.go b/models/structs.go index fcdeffefb..fc03cc957 100644 --- a/models/structs.go +++ b/models/structs.go @@ -263,6 +263,7 @@ type ServerConfig struct { MQPort string `yaml:"mqport"` MQUserName string `yaml:"mq_username"` MQPassword string `yaml:"mq_password"` + BrokerType string `yaml:"broker_type"` Server string `yaml:"server"` Broker string `yaml:"broker"` IsPro bool `yaml:"isee" json:"Is_EE"` diff --git a/servercfg/serverconf.go b/servercfg/serverconf.go index 46b4178f5..dcf4fd8c4 100644 --- a/servercfg/serverconf.go +++ b/servercfg/serverconf.go @@ -119,6 +119,7 @@ func GetServerInfo() models.ServerConfig { cfg.APIPort = GetAPIPort() cfg.DNSMode = "off" cfg.Broker = GetPublicBrokerEndpoint() + cfg.BrokerType = GetBrokerType() if IsDNSMode() { cfg.DNSMode = "on" } From e68bbbdb0927d133988e1003ce41f1cf23e30c34 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Wed, 20 Dec 2023 08:42:48 +0400 Subject: [PATCH 7/7] use actual host password to create emqx user --- controllers/hosts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 5a825f9f2..b54149792 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -540,7 +540,7 @@ func authenticateHost(response http.ResponseWriter, request *http.Request) { // Create EMQX creds and ACLs if not found if servercfg.GetBrokerType() == servercfg.EmqxBrokerType { - if err := mq.CreateEmqxUser(host.ID.String(), host.HostPass, false); err != nil { + if err := mq.CreateEmqxUser(host.ID.String(), authRequest.Password, false); err != nil { slog.Error("failed to create host credentials for EMQX: ", err.Error()) } else { if err := mq.CreateHostACL(host.ID.String(), servercfg.GetServerInfo().Server); err != nil {