mirror of
https://github.com/gravitl/netmaker.git
synced 2025-11-10 00:30:37 +08:00
NET-655 (#2670)
* NET-655 * Updated HostPull structure to include EgressRoutes and FirewallUpdate models. * added ServerVersion structure to hostpull model * added ServerVersion structure to hostpull model * removed ServerVersion structure * removed ServerVersion structure * added egressroute and fwupdate to hostpull handler * add host update fallback handler * set broker type on server cfg * use actual host password to create emqx user --------- Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com> Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>
This commit is contained in:
parent
61d6b2fa3f
commit
1f9ef50df7
5 changed files with 62 additions and 10 deletions
|
|
@ -31,6 +31,7 @@ func hostHandlers(r *mux.Router) {
|
||||||
r.HandleFunc("/api/hosts/adm/authenticate", authenticateHost).Methods(http.MethodPost)
|
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", 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/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)
|
r.HandleFunc("/api/v1/auth-register/host", socketHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -141,6 +142,8 @@ func pull(w http.ResponseWriter, r *http.Request) {
|
||||||
Peers: hPU.Peers,
|
Peers: hPU.Peers,
|
||||||
PeerIDs: hPU.PeerIDs,
|
PeerIDs: hPU.PeerIDs,
|
||||||
HostNetworkInfo: hPU.HostNetworkInfo,
|
HostNetworkInfo: hPU.HostNetworkInfo,
|
||||||
|
EgressRoutes: hPU.EgressRoutes,
|
||||||
|
FwUpdate: hPU.FwUpdate,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log(1, hostID, "completed a pull")
|
logger.Log(1, hostID, "completed a pull")
|
||||||
|
|
@ -208,6 +211,51 @@ func updateHost(w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(apiHostData)
|
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
|
// swagger:route DELETE /api/hosts/{hostid} hosts deleteHost
|
||||||
//
|
//
|
||||||
// Deletes a Netclient host from Netmaker server.
|
// Deletes a Netclient host from Netmaker server.
|
||||||
|
|
@ -497,7 +545,7 @@ func authenticateHost(response http.ResponseWriter, request *http.Request) {
|
||||||
|
|
||||||
// Create EMQX creds and ACLs if not found
|
// Create EMQX creds and ACLs if not found
|
||||||
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
|
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())
|
slog.Error("failed to create host credentials for EMQX: ", err.Error())
|
||||||
} else {
|
} else {
|
||||||
if err := mq.CreateHostACL(host.ID.String(), servercfg.GetServerInfo().Server); err != nil {
|
if err := mq.CreateHostACL(host.ID.String(), servercfg.GetServerInfo().Server); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -231,6 +231,8 @@ type HostPull struct {
|
||||||
ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
|
ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
|
||||||
PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
|
PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
|
||||||
HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,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
|
// NodeGet - struct for a single node get response
|
||||||
|
|
@ -261,6 +263,7 @@ type ServerConfig struct {
|
||||||
MQPort string `yaml:"mqport"`
|
MQPort string `yaml:"mqport"`
|
||||||
MQUserName string `yaml:"mq_username"`
|
MQUserName string `yaml:"mq_username"`
|
||||||
MQPassword string `yaml:"mq_password"`
|
MQPassword string `yaml:"mq_password"`
|
||||||
|
BrokerType string `yaml:"broker_type"`
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Broker string `yaml:"broker"`
|
Broker string `yaml:"broker"`
|
||||||
IsPro bool `yaml:"isee" json:"Is_EE"`
|
IsPro bool `yaml:"isee" json:"Is_EE"`
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
|
||||||
var sendPeerUpdate bool
|
var sendPeerUpdate bool
|
||||||
switch hostUpdate.Action {
|
switch hostUpdate.Action {
|
||||||
case models.CheckIn:
|
case models.CheckIn:
|
||||||
sendPeerUpdate = handleHostCheckin(&hostUpdate.Host, currentHost)
|
sendPeerUpdate = HandleHostCheckin(&hostUpdate.Host, currentHost)
|
||||||
case models.Acknowledgement:
|
case models.Acknowledgement:
|
||||||
hu := hostactions.GetAction(currentHost.ID.String())
|
hu := hostactions.GetAction(currentHost.ID.String())
|
||||||
if hu != nil {
|
if hu != nil {
|
||||||
|
|
@ -258,7 +258,7 @@ func ClientPeerUpdate(client mqtt.Client, msg mqtt.Message) {
|
||||||
slog.Info("sent peer updates after signal received from", "id", id)
|
slog.Info("sent peer updates after signal received from", "id", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleHostCheckin(h, currentHost *models.Host) bool {
|
func HandleHostCheckin(h, currentHost *models.Host) bool {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ func publish(host *models.Host, dest string, msg []byte) error {
|
||||||
if encryptErr != nil {
|
if encryptErr != nil {
|
||||||
return encryptErr
|
return encryptErr
|
||||||
}
|
}
|
||||||
if mqclient == nil {
|
if mqclient == nil || !mqclient.IsConnectionOpen() {
|
||||||
return errors.New("cannot publish ... mqclient not connected")
|
return errors.New("cannot publish ... mqclient not connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -119,6 +119,7 @@ func GetServerInfo() models.ServerConfig {
|
||||||
cfg.APIPort = GetAPIPort()
|
cfg.APIPort = GetAPIPort()
|
||||||
cfg.DNSMode = "off"
|
cfg.DNSMode = "off"
|
||||||
cfg.Broker = GetPublicBrokerEndpoint()
|
cfg.Broker = GetPublicBrokerEndpoint()
|
||||||
|
cfg.BrokerType = GetBrokerType()
|
||||||
if IsDNSMode() {
|
if IsDNSMode() {
|
||||||
cfg.DNSMode = "on"
|
cfg.DNSMode = "on"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue