Skip to content

Commit

Permalink
ACC-532: set mq clean session to true (#2865)
Browse files Browse the repository at this point in the history
* set clean session

* delete emqx hosts creds api

* add emqx hosts del api to limited middleware controller

* add emqx hosts del api to limited middleware controller

* remove server creds from emqx
  • Loading branch information
abhishek9686 authored Mar 20, 2024
1 parent 3784efa commit c7e673f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
32 changes: 32 additions & 0 deletions controllers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func hostHandlers(r *mux.Router) {
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/emqx/hosts", logic.SecurityCheck(true, http.HandlerFunc(delEmqxHosts))).Methods(http.MethodDelete)
r.HandleFunc("/api/v1/auth-register/host", socketHandler)
}

Expand Down Expand Up @@ -749,3 +750,34 @@ func syncHost(w http.ResponseWriter, r *http.Request) {
slog.Info("requested host pull", "user", r.Header.Get("user"), "host", host.ID)
w.WriteHeader(http.StatusOK)
}

// swagger:route DELETE /api/emqx/hosts hosts delEmqxHosts
//
// Lists all hosts.
//
// Schemes: https
//
// Security:
// oauth
//
// Responses:
// 200: apiHostResponse
func delEmqxHosts(w http.ResponseWriter, r *http.Request) {
currentHosts, err := logic.GetAllHosts()
if err != nil {
logger.Log(0, r.Header.Get("user"), "failed to fetch hosts: ", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
for _, host := range currentHosts {
// delete EMQX credentials for host
if err := mq.GetEmqxHandler().DeleteEmqxUser(host.ID.String()); err != nil {
slog.Error("failed to remove host credentials from EMQX", "id", host.ID, "error", err)
}
}
err = mq.GetEmqxHandler().DeleteEmqxUser(servercfg.GetMqUserName())
if err != nil {
slog.Error("failed to remove server credentials from EMQX", "user", servercfg.GetMqUserName(), "error", err)
}
logic.ReturnSuccessResponse(w, r, "deleted hosts data on emqx")
}
3 changes: 2 additions & 1 deletion mq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ func setMqOptions(user, password string, opts *mqtt.ClientOptions) {
opts.SetPassword(password)
opts.SetAutoReconnect(true)
opts.SetConnectRetry(true)
opts.SetConnectRetryInterval(time.Second << 2)
opts.SetConnectRetryInterval(time.Second * 4)
opts.SetKeepAlive(time.Minute)
opts.SetCleanSession(true)
opts.SetWriteTimeout(time.Minute)
}

Expand Down
17 changes: 13 additions & 4 deletions pro/controllers/middleware.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package controllers

import (
"net/http"

"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/servercfg"
"net/http"
)

var limitedApis = map[string]struct{}{
"/api/server/status": {},
"/api/emqx/hosts": {},
"/api/users/adm/authenticate": {},
}

func OnlyServerAPIWhenUnlicensedMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if servercfg.ErrLicenseValidation != nil && request.URL.Path != "/api/server/status" {
logic.ReturnErrorResponse(writer, request, logic.FormatError(servercfg.ErrLicenseValidation, "forbidden"))
return
if servercfg.ErrLicenseValidation != nil {
if _, ok := limitedApis[request.URL.Path]; !ok {
logic.ReturnErrorResponse(writer, request, logic.FormatError(servercfg.ErrLicenseValidation, "forbidden"))
return
}
}
handler.ServeHTTP(writer, request)
})
Expand Down

0 comments on commit c7e673f

Please sign in to comment.