added error on attempt to update NetID

This commit is contained in:
Matthew R Kasun
2021-04-15 14:48:10 -04:00
parent b4afb373bf
commit 66fb590be2
2 changed files with 388 additions and 387 deletions

View File

@@ -1,22 +1,23 @@
package controller package controller
import ( import (
"gopkg.in/go-playground/validator.v9"
"github.com/gravitl/netmaker/models"
"errors"
"encoding/base64"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/mongoconn"
"time"
"strings"
"fmt"
"context" "context"
"encoding/base64"
"encoding/json" "encoding/json"
"errors"
"fmt"
"net/http" "net/http"
"strings"
"time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gravitl/netmaker/config"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mongoconn"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/gravitl/netmaker/config" "gopkg.in/go-playground/validator.v9"
) )
func networkHandlers(r *mux.Router) { func networkHandlers(r *mux.Router) {
@@ -79,6 +80,7 @@ func securityCheck(next http.Handler) http.HandlerFunc {
} }
} }
} }
//Consider a more secure way of setting master key //Consider a more secure way of setting master key
func authenticateMaster(tokenString string) bool { func authenticateMaster(tokenString string) bool {
if tokenString == config.Config.Server.MasterKey { if tokenString == config.Config.Server.MasterKey {
@@ -119,7 +121,9 @@ func validateNetwork(operation string, network models.Network) error {
_ = v.RegisterValidation("netid_valid", func(fl validator.FieldLevel) bool { _ = v.RegisterValidation("netid_valid", func(fl validator.FieldLevel) bool {
isFieldUnique := false isFieldUnique := false
inCharSet := false inCharSet := false
if operation == "update" { isFieldUnique = true } else{ if operation == "update" {
isFieldUnique = true
} else {
isFieldUnique, _ = functions.IsNetworkNameUnique(fl.Field().String()) isFieldUnique, _ = functions.IsNetworkNameUnique(fl.Field().String())
inCharSet = functions.NameInNetworkCharSet(fl.Field().String()) inCharSet = functions.NameInNetworkCharSet(fl.Field().String())
} }
@@ -182,7 +186,6 @@ func keyUpdate(w http.ResponseWriter, r *http.Request) {
return return
} }
network.KeyUpdateTimeStamp = time.Now().Unix() network.KeyUpdateTimeStamp = time.Now().Unix()
collection := mongoconn.Client.Database("netmaker").Collection("networks") collection := mongoconn.Client.Database("netmaker").Collection("networks")
@@ -229,7 +232,6 @@ func AlertNetwork(netid string) error{
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
filter := bson.M{"netid": netid} filter := bson.M{"netid": netid}
var network models.Network var network models.Network
network, err := functions.GetParentNetwork(netid) network, err := functions.GetParentNetwork(netid)
@@ -280,7 +282,6 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) {
networkChange.NetID = network.NetID networkChange.NetID = network.NetID
} }
//err = validateNetwork("update", networkChange) //err = validateNetwork("update", networkChange)
if err != nil { if err != nil {
returnErrorResponse(w, r, formatError(err, "internal")) returnErrorResponse(w, r, formatError(err, "internal"))
@@ -289,6 +290,10 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) {
//NOTE: Network.NetID is intentionally NOT editable. It acts as a static ID for the network. //NOTE: Network.NetID is intentionally NOT editable. It acts as a static ID for the network.
//DisplayName can be changed instead, which is what shows on the front end //DisplayName can be changed instead, which is what shows on the front end
if networkChange.NetID != network.NetID {
returnErrorResponse(w, r, formatError(errors.New("NetID is not editable"), "badrequest"))
return
}
if networkChange.AddressRange != "" { if networkChange.AddressRange != "" {
@@ -488,7 +493,6 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
collection := mongoconn.Client.Database("netmaker").Collection("networks") collection := mongoconn.Client.Database("netmaker").Collection("networks")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// insert our network into the network table // insert our network into the network table
result, err := collection.InsertOne(ctx, network) result, err := collection.InsertOne(ctx, network)
@@ -504,7 +508,6 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
// BEGIN KEY MANAGEMENT SECTION // BEGIN KEY MANAGEMENT SECTION
//TODO: Very little error handling //TODO: Very little error handling
//accesskey is created as a json string inside the Network collection item in mongo //accesskey is created as a json string inside the Network collection item in mongo
func createAccessKey(w http.ResponseWriter, r *http.Request) { func createAccessKey(w http.ResponseWriter, r *http.Request) {
@@ -549,7 +552,6 @@ func createAccessKey(w http.ResponseWriter, r *http.Request) {
privAddr = network.LocalRange privAddr = network.LocalRange
} }
netID := params["networkname"] netID := params["networkname"]
address := gconf.ServerGRPC + gconf.PortGRPC address := gconf.ServerGRPC + gconf.PortGRPC
@@ -625,7 +627,6 @@ func getAccessKeys(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(keys) json.NewEncoder(w).Encode(keys)
} }
//delete key. Has to do a little funky logic since it's not a collection item //delete key. Has to do a little funky logic since it's not a collection item
func deleteAccessKey(w http.ResponseWriter, r *http.Request) { func deleteAccessKey(w http.ResponseWriter, r *http.Request) {

View File

@@ -341,13 +341,13 @@ func TestUpdatenetwork(t *testing.T) {
network.NetID = "wirecat" network.NetID = "wirecat"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey") response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
assert.Nil(t, err, err) assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode) assert.Equal(t, http.StatusBadRequest, response.StatusCode)
defer response.Body.Close() defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&returnedNetwork) var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err) assert.Nil(t, err, err)
//returns previous value not the updated value assert.Equal(t, http.StatusBadRequest, message.Code)
// ----- needs fixing ----- assert.Equal(t, "NetID is not editable", message.message)
//assert.Equal(t, network.NetID, returnedNetwork.NetID)
}) })
t.Run("NetIDInvalidCredentials", func(t *testing.T) { t.Run("NetIDInvalidCredentials", func(t *testing.T) {
type Network struct { type Network struct {