2023-01-23 05:31:43 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
|
|
|
"github.com/gravitl/netmaker/logic"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
2023-03-14 07:51:12 +08:00
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
2023-01-23 05:31:43 +08:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2023-03-14 07:51:12 +08:00
|
|
|
// swagger:route PUT /api/v1/nodes/migrate nodes migrateNode
|
2023-01-23 05:31:43 +08:00
|
|
|
//
|
|
|
|
// Used to migrate a legacy node.
|
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: nodeJoinResponse
|
|
|
|
func migrate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
2023-03-14 07:51:12 +08:00
|
|
|
|
|
|
|
var networksToAdd = []string{}
|
|
|
|
for i := range data.LegacyNodes {
|
|
|
|
legacyNode := data.LegacyNodes[i]
|
|
|
|
record, err := database.FetchRecord(database.NODES_TABLE_NAME, legacyNode.ID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, "no record for legacy node", legacyNode.ID, err.Error())
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
var oldLegacyNode models.LegacyNode
|
|
|
|
if err = json.Unmarshal([]byte(record), &oldLegacyNode); err != nil {
|
|
|
|
logger.Log(0, "error decoding legacy node", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(oldLegacyNode.Password), []byte(legacyNode.Password)); err != nil {
|
|
|
|
logger.Log(0, "error decoding legacy password", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "unauthorized"))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
networksToAdd = append(networksToAdd, oldLegacyNode.Network)
|
|
|
|
_ = database.DeleteRecord(database.NODES_TABLE_NAME, oldLegacyNode.ID)
|
|
|
|
}
|
2023-01-23 05:31:43 +08:00
|
|
|
}
|
2023-03-14 07:51:12 +08:00
|
|
|
if len(networksToAdd) == 0 {
|
|
|
|
logger.Log(0, "no valid networks to migrate for host", data.NewHost.Name)
|
2023-01-23 05:31:43 +08:00
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "unauthorized"))
|
|
|
|
return
|
|
|
|
}
|
2023-03-14 07:51:12 +08:00
|
|
|
if !logic.HostExists(&data.NewHost) {
|
2023-03-22 09:47:20 +08:00
|
|
|
logic.CheckHostPorts(&data.NewHost)
|
2023-03-14 07:51:12 +08:00
|
|
|
if err = logic.CreateHost(&data.NewHost); err != nil {
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
|
|
|
|
return
|
2023-02-17 02:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-14 07:51:12 +08:00
|
|
|
key, keyErr := logic.RetrievePublicTrafficKey()
|
|
|
|
if keyErr != nil {
|
|
|
|
logger.Log(0, "error retrieving key:", keyErr.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
|
|
return
|
2023-02-17 02:56:19 +08:00
|
|
|
}
|
2023-03-14 07:51:12 +08:00
|
|
|
server := servercfg.GetServerInfo()
|
|
|
|
server.TrafficKey = key
|
|
|
|
response := models.RegisterResponse{
|
|
|
|
ServerConf: server,
|
|
|
|
RequestedHost: data.NewHost,
|
2023-02-17 02:56:19 +08:00
|
|
|
}
|
2023-03-14 07:51:12 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(&response)
|
|
|
|
logger.Log(0, "successfully migrated host", data.NewHost.Name, data.NewHost.ID.String())
|
|
|
|
// notify host of changes, peer and node updates
|
|
|
|
go checkNetRegAndHostUpdate(networksToAdd, &data.NewHost)
|
2023-01-23 05:31:43 +08:00
|
|
|
}
|