netmaker/controllers/nodeHttpController.go

740 lines
24 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package controller
import (
"github.com/gravitl/netmaker/models"
2021-04-13 11:19:01 +08:00
"errors"
2021-03-26 00:17:52 +08:00
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/mongoconn"
"golang.org/x/crypto/bcrypt"
"time"
"strings"
"fmt"
"context"
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
func nodeHandlers(r *mux.Router) {
r.HandleFunc("/api/nodes", authorize(false, "master", http.HandlerFunc(getAllNodes))).Methods("GET")
r.HandleFunc("/api/nodes/{network}", authorize(true, "network", http.HandlerFunc(getNetworkNodes))).Methods("GET")
r.HandleFunc("/api/nodes/{network}/{macaddress}", authorize(true, "node", http.HandlerFunc(getNode))).Methods("GET")
r.HandleFunc("/api/nodes/{network}/{macaddress}", authorize(true, "node", http.HandlerFunc(updateNode))).Methods("PUT")
r.HandleFunc("/api/nodes/{network}/{macaddress}", authorize(true, "node", http.HandlerFunc(deleteNode))).Methods("DELETE")
r.HandleFunc("/api/nodes/{network}/{macaddress}/checkin", authorize(true, "node", http.HandlerFunc(checkIn))).Methods("POST")
2021-04-13 14:55:49 +08:00
r.HandleFunc("/api/nodes/{network}/{macaddress}/creategateway", authorize(true, "master", http.HandlerFunc(createGateway))).Methods("POST")
r.HandleFunc("/api/nodes/{network}/{macaddress}/deletegateway", authorize(true, "master", http.HandlerFunc(deleteGateway))).Methods("POST")
r.HandleFunc("/api/nodes/{network}/{macaddress}/uncordon", authorize(true, "master", http.HandlerFunc(uncordonNode))).Methods("POST")
r.HandleFunc("/api/nodes/{network}/nodes", createNode).Methods("POST")
r.HandleFunc("/api/nodes/adm/{network}/lastmodified", authorize(true, "network", http.HandlerFunc(getLastModified))).Methods("GET")
r.HandleFunc("/api/nodes/adm/{network}/authenticate", authenticate).Methods("POST")
2021-03-26 00:17:52 +08:00
}
//Node authenticates using its password and retrieves a JWT for authorization.
func authenticate(response http.ResponseWriter, request *http.Request) {
//Auth request consists of Mac Address and Password (from node that is authorizing
//in case of Master, auth is ignored and mac is set to "mastermac"
var authRequest models.AuthParams
var result models.Node
var errorResponse = models.ErrorResponse{
Code: http.StatusInternalServerError, Message: "W1R3: It's not you it's me.",
}
//Get password fnd mac rom request
decoder := json.NewDecoder(request.Body)
decoderErr := decoder.Decode(&authRequest)
defer request.Body.Close()
if decoderErr != nil {
returnErrorResponse(response, request, errorResponse)
return
} else {
2021-03-26 00:17:52 +08:00
errorResponse.Code = http.StatusBadRequest
if authRequest.MacAddress == "" {
errorResponse.Message = "W1R3: MacAddress can't be empty"
returnErrorResponse(response, request, errorResponse)
return
2021-03-26 00:17:52 +08:00
} else if authRequest.Password == "" {
errorResponse.Message = "W1R3: Password can't be empty"
returnErrorResponse(response, request, errorResponse)
return
} else {
2021-03-26 00:17:52 +08:00
//Search DB for node with Mac Address. Ignore pending nodes (they should not be able to authenticate with API untill approved).
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
2021-03-26 00:17:52 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var err = collection.FindOne(ctx, bson.M{ "macaddress": authRequest.MacAddress, "ispending": false }).Decode(&result)
defer cancel()
if err != nil {
returnErrorResponse(response, request, errorResponse)
return
2021-03-26 00:17:52 +08:00
}
//compare password from request to stored password in database
//might be able to have a common hash (certificates?) and compare those so that a password isn't passed in in plain text...
//TODO: Consider a way of hashing the password client side before sending, or using certificates
err = bcrypt.CompareHashAndPassword([]byte(result.Password), []byte(authRequest.Password))
if err != nil {
returnErrorResponse(response, request, errorResponse)
return
2021-03-26 00:17:52 +08:00
} else {
//Create a new JWT for the node
tokenString, _ := functions.CreateJWT(authRequest.MacAddress, result.Network)
2021-03-26 00:17:52 +08:00
if tokenString == "" {
returnErrorResponse(response, request, errorResponse)
return
2021-03-26 00:17:52 +08:00
}
var successResponse = models.SuccessResponse{
Code: http.StatusOK,
Message: "W1R3: Device " + authRequest.MacAddress + " Authorized",
Response: models.SuccessfulLoginResponse{
AuthToken: tokenString,
MacAddress: authRequest.MacAddress,
},
}
//Send back the JWT
successJSONResponse, jsonError := json.Marshal(successResponse)
if jsonError != nil {
returnErrorResponse(response, request, errorResponse)
return
2021-03-26 00:17:52 +08:00
}
2021-04-13 11:19:01 +08:00
response.WriteHeader(http.StatusOK)
2021-03-26 00:17:52 +08:00
response.Header().Set("Content-Type", "application/json")
response.Write(successJSONResponse)
}
}
}
}
//The middleware for most requests to the API
//They all pass through here first
//This will validate the JWT (or check for master token)
//This will also check against the authNetwork and make sure the node should be accessing that endpoint,
2021-03-26 00:17:52 +08:00
//even if it's technically ok
//This is kind of a poor man's RBAC. There's probably a better/smarter way.
//TODO: Consider better RBAC implementations
func authorize(networkCheck bool, authNetwork string, next http.Handler) http.HandlerFunc {
2021-03-26 00:17:52 +08:00
return func(w http.ResponseWriter, r *http.Request) {
var errorResponse = models.ErrorResponse{
Code: http.StatusInternalServerError, Message: "W1R3: It's not you it's me.",
}
var params = mux.Vars(r)
networkexists, _ := functions.NetworkExists(params["network"])
2021-03-26 00:17:52 +08:00
//check that the request is for a valid network
//if (networkCheck && !networkexists) || err != nil {
if (networkCheck && !networkexists) {
2021-03-26 00:17:52 +08:00
errorResponse = models.ErrorResponse{
Code: http.StatusNotFound, Message: "W1R3: This network does not exist. ",
2021-03-26 00:17:52 +08:00
}
returnErrorResponse(w, r, errorResponse)
return
2021-03-26 00:17:52 +08:00
} else {
w.Header().Set("Content-Type", "application/json")
//get the auth token
bearerToken := r.Header.Get("Authorization")
var tokenSplit = strings.Split(bearerToken, " ")
//I put this in in case the user doesn't put in a token at all (in which case it's empty)
//There's probably a smarter way of handling this.
var authToken = "928rt238tghgwe@TY@$Y@#WQAEGB2FC#@HG#@$Hddd"
if len(tokenSplit) > 1 {
authToken = tokenSplit[1]
2021-03-26 10:29:36 +08:00
} else {
errorResponse = models.ErrorResponse{
Code: http.StatusUnauthorized, Message: "W1R3: Missing Auth Token.",
}
returnErrorResponse(w, r, errorResponse)
return
}
2021-03-26 00:17:52 +08:00
2021-03-26 10:29:36 +08:00
2021-03-26 00:17:52 +08:00
//This checks if
//A: the token is the master password
//B: the token corresponds to a mac address, and if so, which one
//TODO: There's probably a better way of dealing with the "master token"/master password. Plz Halp.
macaddress, _, err := functions.VerifyToken(authToken)
if err != nil {
2021-03-26 10:29:36 +08:00
errorResponse = models.ErrorResponse{
Code: http.StatusUnauthorized, Message: "W1R3: Error Verifying Auth Token.",
}
returnErrorResponse(w, r, errorResponse)
return
2021-03-26 00:17:52 +08:00
}
var isAuthorized = false
//The mastermac (login with masterkey from config) can do everything!! May be dangerous.
if macaddress == "mastermac" {
isAuthorized = true
//for everyone else, there's poor man's RBAC. The "cases" are defined in the routes in the handlers
//So each route defines which access network should be allowed to access it
2021-03-26 00:17:52 +08:00
} else {
switch authNetwork {
2021-03-26 00:17:52 +08:00
case "all":
isAuthorized = true
case "nodes":
isAuthorized = (macaddress != "")
case "network":
node, err := functions.GetNodeByMacAddress(params["network"], macaddress)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-03-26 10:29:36 +08:00
errorResponse = models.ErrorResponse{
Code: http.StatusUnauthorized, Message: "W1R3: Missing Auth Token.",
}
returnErrorResponse(w, r, errorResponse)
return
2021-03-26 00:17:52 +08:00
}
isAuthorized = (node.Network == params["network"])
2021-03-26 00:17:52 +08:00
case "node":
isAuthorized = (macaddress == params["macaddress"])
case "master":
isAuthorized = (macaddress == "mastermac")
default:
isAuthorized = false
}
}
if !isAuthorized {
errorResponse = models.ErrorResponse{
Code: http.StatusUnauthorized, Message: "W1R3: You are unauthorized to access this endpoint.",
}
returnErrorResponse(w, r, errorResponse)
return
2021-03-26 00:17:52 +08:00
} else {
//If authorized, this function passes along it's request and output to the appropriate route function.
next.ServeHTTP(w, r)
}
}
}
}
//Gets all nodes associated with network, including pending nodes
func getNetworkNodes(w http.ResponseWriter, r *http.Request) {
2021-03-26 00:17:52 +08:00
w.Header().Set("Content-Type", "application/json")
var nodes []models.ReturnNode
var params = mux.Vars(r)
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
2021-03-26 00:17:52 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
filter := bson.M{"network": params["network"]}
2021-03-26 00:17:52 +08:00
//Filtering out the ID field cuz Dillon doesn't like it. May want to filter out other fields in the future
cur, err := collection.Find(ctx, filter, options.Find().SetProjection(bson.M{"_id": 0}))
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-03-26 00:17:52 +08:00
defer cancel()
for cur.Next(context.TODO()) {
//Using a different model for the ReturnNode (other than regular node).
//Either we should do this for ALL structs (so Networks and Keys)
2021-03-26 00:17:52 +08:00
//OR we should just use the original struct
//My preference is to make some new return structs
//TODO: Think about this. Not an immediate concern. Just need to get some consistency eventually
var node models.ReturnNode
err := cur.Decode(&node)
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
2021-03-26 00:17:52 +08:00
}
// add item our array of nodes
nodes = append(nodes, node)
}
//TODO: Another fatal error we should take care of.
if err := cur.Err(); err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
return
2021-03-26 00:17:52 +08:00
}
//Returns all the nodes in JSON format
2021-04-13 11:19:01 +08:00
w.WriteHeader(http.StatusOK)
2021-03-26 00:17:52 +08:00
json.NewEncoder(w).Encode(nodes)
}
//A separate function to get all nodes, not just nodes for a particular network.
2021-03-26 00:17:52 +08:00
//Not quite sure if this is necessary. Probably necessary based on front end but may want to review after iteration 1 if it's being used or not
func getAllNodes(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var nodes []models.ReturnNode
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
2021-03-26 00:17:52 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// Filter out them ID's again
cur, err := collection.Find(ctx, bson.M{}, options.Find().SetProjection(bson.M{"_id": 0}))
if err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
2021-03-26 00:17:52 +08:00
return
}
defer cancel()
for cur.Next(context.TODO()) {
var node models.ReturnNode
err := cur.Decode(&node)
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-03-26 00:17:52 +08:00
// add node to our array
nodes = append(nodes, node)
}
//TODO: Fatal error
if err := cur.Err(); err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
return
2021-03-26 00:17:52 +08:00
}
2021-04-13 11:19:01 +08:00
2021-03-26 00:17:52 +08:00
//Return all the nodes in JSON format
2021-04-13 11:19:01 +08:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(nodes)
2021-03-26 00:17:52 +08:00
}
//This function get's called when a node "checks in" at check in interval
//Honestly I'm not sure what all it should be doing
//TODO: Implement the necessary stuff, including the below
//Check the last modified of the network
2021-03-26 00:17:52 +08:00
//Check the last modified of the nodes
//Write functions for responding to these two thingies
func checkIn(w http.ResponseWriter, r *http.Request) {
//TODO: Current thoughts:
//Dont bother with a networklastmodified
2021-03-26 00:17:52 +08:00
//Instead, implement a "configupdate" boolean on nodes
//when there is a network update that requrires a config update, then the node will pull its new config
2021-03-26 00:17:52 +08:00
// set header.
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
var node models.Node
//Retrieves node with DB Call which is inefficient. Let's just get the time and set it.
//node = functions.GetNodeByMacAddress(params["network"], params["macaddress"])
2021-03-26 00:17:52 +08:00
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
2021-03-26 00:17:52 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
filter := bson.M{"macaddress": params["macaddress"], "network": params["network"]}
2021-03-26 00:17:52 +08:00
//old code was inefficient, this is all we need.
time := time.Now().String()
//node.SetLastCheckIn()
// prepare update model with new time
update := bson.D{
{"$set", bson.D{
{"lastcheckin", time},
}},
}
2021-04-13 11:19:01 +08:00
err := collection.FindOneAndUpdate(ctx, filter, update).Decode(&node)
2021-03-26 00:17:52 +08:00
defer cancel()
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
2021-03-26 00:17:52 +08:00
return
}
2021-04-13 11:19:01 +08:00
//TODO: check node last modified vs network last modified
2021-04-13 11:19:01 +08:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
2021-03-26 00:17:52 +08:00
}
//Get an individual node. Nothin fancy here folks.
func getNode(w http.ResponseWriter, r *http.Request) {
// set header.
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
node, err := GetNode(params["macaddress"], params["network"])
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
w.WriteHeader(http.StatusOK)
2021-03-26 00:17:52 +08:00
json.NewEncoder(w).Encode(node)
}
//Get the time that a network of nodes was last modified.
2021-03-26 00:17:52 +08:00
//TODO: This needs to be refactored
//Potential way to do this: On UpdateNode, set a new field for "LastModified"
//If we go with the existing way, we need to at least set network.NodesLastModified on UpdateNode
2021-03-26 00:17:52 +08:00
func getLastModified(w http.ResponseWriter, r *http.Request) {
// set header.
w.Header().Set("Content-Type", "application/json")
var network models.Network
2021-03-26 00:17:52 +08:00
var params = mux.Vars(r)
collection := mongoconn.Client.Database("netmaker").Collection("networks")
2021-03-26 00:17:52 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
filter := bson.M{"netid": params["network"]}
err := collection.FindOne(ctx, filter).Decode(&network)
2021-03-26 00:17:52 +08:00
defer cancel()
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-03-26 00:17:52 +08:00
2021-04-13 11:19:01 +08:00
w.WriteHeader(http.StatusOK)
w.Write([]byte(string(network.NodesLastModified)))
2021-03-26 00:17:52 +08:00
}
//This one's a doozy
//To create a node
//Must have valid key and be unique
func createNode(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
var errorResponse = models.ErrorResponse{
Code: http.StatusInternalServerError, Message: "W1R3: It's not you it's me.",
}
networkName := params["network"]
2021-03-26 00:17:52 +08:00
//Check if network exists first
2021-03-26 00:17:52 +08:00
//TODO: This is inefficient. Let's find a better way.
//Just a few rows down we grab the network anyway
networkexists, err := functions.NetworkExists(networkName)
2021-03-26 00:17:52 +08:00
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
} else if !networkexists {
2021-03-26 00:17:52 +08:00
errorResponse = models.ErrorResponse{
Code: http.StatusNotFound, Message: "W1R3: Network does not exist! ",
2021-03-26 00:17:52 +08:00
}
returnErrorResponse(w, r, errorResponse)
return
}
var node models.Node
//get node from body of request
2021-04-13 11:19:01 +08:00
err = json.NewDecoder(r.Body).Decode(&node)
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-03-26 00:17:52 +08:00
node.Network = networkName
2021-03-26 00:17:52 +08:00
network, err := node.GetNetwork()
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-03-26 00:17:52 +08:00
//Check to see if key is valid
//TODO: Triple inefficient!!! This is the third call to the DB we make for networks
validKey := functions.IsKeyValid(networkName, node.AccessKey)
2021-03-26 00:17:52 +08:00
if !validKey {
//Check to see if network will allow manual sign up
2021-03-26 00:17:52 +08:00
//may want to switch this up with the valid key check and avoid a DB call that way.
if *network.AllowManualSignUp {
2021-03-26 00:17:52 +08:00
node.IsPending = true
} else {
errorResponse = models.ErrorResponse{
Code: http.StatusUnauthorized, Message: "W1R3: Key invalid, or none provided.",
}
returnErrorResponse(w, r, errorResponse)
return
}
}
err = ValidateNode("create", networkName, node)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
return
2021-03-26 00:17:52 +08:00
}
node, err = CreateNode(node, networkName)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
2021-03-26 00:17:52 +08:00
return
}
2021-04-13 11:19:01 +08:00
w.WriteHeader(http.StatusOK)
2021-03-26 00:17:52 +08:00
json.NewEncoder(w).Encode(node)
}
//Takes node out of pending state
//TODO: May want to use cordon/uncordon terminology instead of "ispending".
func uncordonNode(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
var node models.Node
node, err := functions.GetNodeByMacAddress(params["network"], params["macaddress"])
2021-03-26 00:17:52 +08:00
if err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
return
2021-03-26 00:17:52 +08:00
}
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
2021-03-26 00:17:52 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// Create filter
filter := bson.M{"macaddress": params["macaddress"], "network": params["network"]}
2021-03-26 00:17:52 +08:00
node.SetLastModified()
fmt.Println("Uncordoning node " + node.Name)
// prepare update model.
update := bson.D{
{"$set", bson.D{
{"ispending", false},
}},
}
2021-04-13 11:19:01 +08:00
err = collection.FindOneAndUpdate(ctx, filter, update).Decode(&node)
2021-03-26 00:17:52 +08:00
defer cancel()
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
2021-03-26 00:17:52 +08:00
return
}
2021-04-13 11:19:01 +08:00
2021-03-26 10:29:36 +08:00
fmt.Println("Node " + node.Name + " uncordoned.")
2021-04-13 11:19:01 +08:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("SUCCESS")
}
func createGateway(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
2021-04-13 14:55:49 +08:00
var gateway models.GatewayRequest
err := json.NewDecoder(r.Body).Decode(&gateway)
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
gateway.NetID = params["network"]
gateway.NodeID = params["macaddress"]
2021-04-13 11:19:01 +08:00
node, err := functions.GetNodeByMacAddress(params["network"], params["macaddress"])
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-04-13 14:55:49 +08:00
err = validateGateway(gateway)
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-04-13 11:19:01 +08:00
2021-04-13 14:55:49 +08:00
var nodechange models.Node
2021-04-13 11:19:01 +08:00
2021-04-13 15:27:24 +08:00
isgateway := true
nodechange.IsGateway = &isgateway
2021-04-13 14:55:49 +08:00
nodechange.GatewayRange = gateway.RangeString
if gateway.PostUp == "" {
nodechange.PostUp = "iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
} else {
nodechange.PostUp = gateway.PostUp
}
if gateway.PostDown == "" {
nodechange.PostDown = "iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
} else {
nodechange.PostDown = gateway.PostDown
}
2021-04-13 11:19:01 +08:00
2021-04-13 14:55:49 +08:00
node, err = UpdateNode(nodechange, node)
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-04-13 11:19:01 +08:00
2021-04-13 14:55:49 +08:00
err = AlertNetwork(params["networkname"])
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-03-26 10:29:36 +08:00
2021-04-13 14:55:49 +08:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
}
func validateGateway(gateway models.GatewayRequest) error {
var err error
2021-04-13 15:27:24 +08:00
isIpv4 := functions.IsIpv4CIDR(gateway.RangeString)
2021-04-13 14:55:49 +08:00
empty := gateway.RangeString == ""
if empty || !isIpv4 {
err = errors.New("IP Range Not Valid")
}
return err
}
func deleteGateway(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
node, err := functions.GetNodeByMacAddress(params["network"], params["macaddress"])
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
2021-04-13 11:19:01 +08:00
}
2021-03-26 00:17:52 +08:00
2021-04-13 14:55:49 +08:00
var nodechange models.Node
2021-04-13 11:19:01 +08:00
2021-04-13 15:27:24 +08:00
isgateway := false
nodechange.IsGateway = &isgateway
2021-04-13 14:55:49 +08:00
nodechange.GatewayRange = ""
nodechange.PostUp = ""
nodechange.PostDown = ""
2021-04-13 11:19:01 +08:00
2021-04-13 14:55:49 +08:00
node, err = UpdateNode(nodechange, node)
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-04-13 14:55:49 +08:00
err = AlertNetwork(params["networkname"])
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
2021-03-26 00:17:52 +08:00
2021-04-13 14:55:49 +08:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
}
2021-03-26 00:17:52 +08:00
2021-04-13 11:19:01 +08:00
2021-03-26 00:17:52 +08:00
func updateNode(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
//Get id from parameters
//id, _ := primitive.ObjectIDFromHex(params["id"])
var node models.Node
//start here
node, err := functions.GetNodeByMacAddress(params["network"], params["macaddress"])
2021-03-26 00:17:52 +08:00
if err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
2021-03-26 00:17:52 +08:00
return
}
2021-04-13 11:19:01 +08:00
2021-03-26 00:17:52 +08:00
var nodechange models.Node
// we decode our body request params
_ = json.NewDecoder(r.Body).Decode(&nodechange)
if nodechange.Network == "" {
nodechange.Network = node.Network
2021-03-26 00:17:52 +08:00
}
if nodechange.MacAddress == "" {
nodechange.MacAddress = node.MacAddress
}
err = ValidateNode("update", params["network"], nodechange)
2021-03-26 00:17:52 +08:00
if err != nil {
2021-04-13 11:19:01 +08:00
returnErrorResponse(w,r,formatError(err, "internal"))
2021-03-26 00:17:52 +08:00
return
}
node, err = UpdateNode(nodechange, node)
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
}
w.WriteHeader(http.StatusOK)
2021-03-26 00:17:52 +08:00
json.NewEncoder(w).Encode(node)
}
//Delete a node
//Pretty straightforward
func deleteNode(w http.ResponseWriter, r *http.Request) {
// Set header
w.Header().Set("Content-Type", "application/json")
// get params
var params = mux.Vars(r)
success, err := DeleteNode(params["macaddress"], params["network"])
2021-03-26 00:17:52 +08:00
2021-04-13 11:19:01 +08:00
if err != nil {
returnErrorResponse(w,r,formatError(err, "internal"))
return
} else if !success {
err = errors.New("Could not delete node " + params["macaddress"])
returnErrorResponse(w,r,formatError(err, "internal"))
2021-03-26 00:17:52 +08:00
return
}
2021-04-13 11:19:01 +08:00
w.WriteHeader(http.StatusOK)
2021-03-26 00:17:52 +08:00
json.NewEncoder(w).Encode(params["macaddress"] + " deleted.")
}