From 770c19302ef51bf6934c9a9e7936a799fc83efd2 Mon Sep 17 00:00:00 2001 From: Matthew R Kasun Date: Sun, 22 Jan 2023 16:31:43 -0500 Subject: [PATCH 1/2] reworked migrate function --- controllers/migrate.go | 79 ++++++++++++++++++++++++++++++++++++++++++ controllers/node.go | 50 ++------------------------ models/migrate.go | 8 +++++ 3 files changed, 89 insertions(+), 48 deletions(-) create mode 100644 controllers/migrate.go create mode 100644 models/migrate.go diff --git a/controllers/migrate.go b/controllers/migrate.go new file mode 100644 index 00000000..c7e68d5b --- /dev/null +++ b/controllers/migrate.go @@ -0,0 +1,79 @@ +package controller + +import ( + "encoding/json" + "io" + "net/http" + "strings" + + "github.com/gorilla/mux" + "github.com/gravitl/netmaker/database" + "github.com/gravitl/netmaker/logger" + "github.com/gravitl/netmaker/logic" + "github.com/gravitl/netmaker/models" + "golang.org/x/crypto/bcrypt" +) + +// swagger:route PUT /api/nodes/{network}/{nodeid}/migrate nodes migrateNode +// +// Used to migrate a legacy node. +// +// Schemes: https +// +// Security: +// oauth +// +// Responses: +// 200: nodeJoinResponse +func migrate(w http.ResponseWriter, r *http.Request) { + // we decode our body request params + data := models.MigrationData{} + err := json.NewDecoder(r.Body).Decode(&data) + if err != nil { + logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + params := mux.Vars(r) + //check authorization + record, err := database.FetchRecord(database.NODES_TABLE_NAME, data.LegacyNodeID) + if err != nil { + logger.Log(0, "no record for legacy node", data.LegacyNodeID, err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + var legacyNode models.LegacyNode + if err = json.Unmarshal([]byte(record), &legacyNode); err != nil { + logger.Log(0, "error decoding legacy node", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + logger.Log(0, "comparing passwords", legacyNode.Password, "pass: ", data.Password) + if err := bcrypt.CompareHashAndPassword([]byte(legacyNode.Password), []byte(data.Password)); err != nil { + logger.Log(0, "error decoding legacy password", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "unauthorized")) + return + } + network, err := logic.GetNetwork(params["network"]) + if err != nil { + logger.Log(0, "error retrieving network: ", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + key, err := logic.CreateAccessKey(models.AccessKey{}, network) + if err != nil { + logger.Log(0, "error creating key: ", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + data.JoinData.Key = key.Value + payload, err := json.Marshal(data.JoinData) + if err != nil { + logger.Log(0, "error encoding data: ", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + r.Body = io.NopCloser(strings.NewReader(string(payload))) + r.ContentLength = int64(len(string(payload))) + createNode(w, r) +} diff --git a/controllers/node.go b/controllers/node.go index fa47e42c..ab573596 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "net/http" "strings" @@ -26,7 +25,7 @@ func nodeHandlers(r *mux.Router) { r.HandleFunc("/api/nodes/{network}", authorize(false, true, "network", http.HandlerFunc(getNetworkNodes))).Methods(http.MethodGet) r.HandleFunc("/api/nodes/{network}/{nodeid}", authorize(true, true, "node", http.HandlerFunc(getNode))).Methods(http.MethodGet) r.HandleFunc("/api/nodes/{network}/{nodeid}", authorize(false, true, "node", http.HandlerFunc(updateNode))).Methods(http.MethodPut) - r.HandleFunc("/api/nodes/{network}/{nodeid}/migrate", authorize(true, true, "node", http.HandlerFunc(migrate))).Methods(http.MethodPut) + r.HandleFunc("/api/nodes/{network}/{nodeid}/migrate", migrate).Methods(http.MethodPost) r.HandleFunc("/api/nodes/{network}/{nodeid}", authorize(true, true, "node", http.HandlerFunc(deleteNode))).Methods(http.MethodDelete) r.HandleFunc("/api/nodes/{network}/{nodeid}/createrelay", authorize(false, true, "user", http.HandlerFunc(createRelay))).Methods(http.MethodPost) r.HandleFunc("/api/nodes/{network}/{nodeid}/deleterelay", authorize(false, true, "user", http.HandlerFunc(deleteRelay))).Methods(http.MethodDelete) @@ -176,7 +175,6 @@ func nodeauth(next http.Handler) http.HandlerFunc { for _, key := range network.AccessKeys { if key.Value == token { found = true - logic.DecrimentKey(network.NetID, key.Value) break } } @@ -552,6 +550,7 @@ func createNode(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, errorResponse) return } + logic.DecrimentKey(networkName, data.Key) user, err := pro.GetNetworkUser(networkName, promodels.NetworkUserID(keyName)) if err == nil { if user.ID != "" { @@ -833,51 +832,6 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) { runUpdates(&node, true) } -// swagger:route PUT /api/nodes/{network}/{nodeid}/migrate nodes migrateNode -// -// Used to migrate a legacy node. -// -// Schemes: https -// -// Security: -// oauth -// -// Responses: -// 200: nodeJoinResponse -func migrate(w http.ResponseWriter, r *http.Request) { - // we decode our body request params - data := models.JoinData{} - err := json.NewDecoder(r.Body).Decode(&data) - if err != nil { - logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error()) - logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) - return - } - params := mux.Vars(r) - network, err := logic.GetNetwork(params["network"]) - if err != nil { - logger.Log(0, "error retrieving network: ", err.Error()) - logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) - return - } - key, err := logic.CreateAccessKey(models.AccessKey{}, network) - if err != nil { - logger.Log(0, "error creating key: ", err.Error()) - logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) - return - } - data.Key = key.Value - payload, err := json.Marshal(data) - if err != nil { - logger.Log(0, "error encoding data: ", err.Error()) - logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) - return - } - r.Body = io.NopCloser(strings.NewReader(string(payload))) - r.ContentLength = int64(len(string(payload))) - createNode(w, r) -} - // swagger:route PUT /api/nodes/{network}/{nodeid} nodes updateNode // // Update an individual node. diff --git a/models/migrate.go b/models/migrate.go new file mode 100644 index 00000000..e9b7cbb5 --- /dev/null +++ b/models/migrate.go @@ -0,0 +1,8 @@ +package models + +// MigrationData struct needed to create new v0.18.0 node from v.0.17.X node +type MigrationData struct { + JoinData JoinData + LegacyNodeID string + Password string +} From 5e3162d9fb6444e09b3c6d940d015005455305c6 Mon Sep 17 00:00:00 2001 From: Matthew R Kasun Date: Sun, 22 Jan 2023 16:34:05 -0500 Subject: [PATCH 2/2] remove debug log --- controllers/migrate.go | 1 - 1 file changed, 1 deletion(-) diff --git a/controllers/migrate.go b/controllers/migrate.go index c7e68d5b..72a03084 100644 --- a/controllers/migrate.go +++ b/controllers/migrate.go @@ -48,7 +48,6 @@ func migrate(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) return } - logger.Log(0, "comparing passwords", legacyNode.Password, "pass: ", data.Password) if err := bcrypt.CompareHashAndPassword([]byte(legacyNode.Password), []byte(data.Password)); err != nil { logger.Log(0, "error decoding legacy password", err.Error()) logic.ReturnErrorResponse(w, r, logic.FormatError(err, "unauthorized"))