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
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"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"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/mongo/options"
"github.com/gravitl/netmaker/config"
"gopkg.in/go-playground/validator.v9"
)
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
func authenticateMaster(tokenString string) bool {
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 {
isFieldUnique := false
inCharSet := false
if operation == "update" { isFieldUnique = true } else{
if operation == "update" {
isFieldUnique = true
} else {
isFieldUnique, _ = functions.IsNetworkNameUnique(fl.Field().String())
inCharSet = functions.NameInNetworkCharSet(fl.Field().String())
}
@@ -182,7 +186,6 @@ func keyUpdate(w http.ResponseWriter, r *http.Request) {
return
}
network.KeyUpdateTimeStamp = time.Now().Unix()
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)
filter := bson.M{"netid": netid}
var network models.Network
network, err := functions.GetParentNetwork(netid)
@@ -280,7 +282,6 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) {
networkChange.NetID = network.NetID
}
//err = validateNetwork("update", networkChange)
if err != nil {
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.
//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 != "" {
@@ -488,7 +493,6 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
collection := mongoconn.Client.Database("netmaker").Collection("networks")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// insert our network into the network table
result, err := collection.InsertOne(ctx, network)
@@ -504,7 +508,6 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
// BEGIN KEY MANAGEMENT SECTION
//TODO: Very little error handling
//accesskey is created as a json string inside the Network collection item in mongo
func createAccessKey(w http.ResponseWriter, r *http.Request) {
@@ -549,7 +552,6 @@ func createAccessKey(w http.ResponseWriter, r *http.Request) {
privAddr = network.LocalRange
}
netID := params["networkname"]
address := gconf.ServerGRPC + gconf.PortGRPC
@@ -625,7 +627,6 @@ func getAccessKeys(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(keys)
}
//delete key. Has to do a little funky logic since it's not a collection item
func deleteAccessKey(w http.ResponseWriter, r *http.Request) {

View File

@@ -341,13 +341,13 @@ func TestUpdatenetwork(t *testing.T) {
network.NetID = "wirecat"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
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)
//returns previous value not the updated value
// ----- needs fixing -----
//assert.Equal(t, network.NetID, returnedNetwork.NetID)
assert.Equal(t, http.StatusBadRequest, message.Code)
assert.Equal(t, "NetID is not editable", message.message)
})
t.Run("NetIDInvalidCredentials", func(t *testing.T) {
type Network struct {