mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 00:43:58 +08:00
fixing runtime panic on user delete
This commit is contained in:
BIN
controllers/.networkHttpController.go.swp
Normal file
BIN
controllers/.networkHttpController.go.swp
Normal file
Binary file not shown.
BIN
controllers/.userHttpController.go.swp
Normal file
BIN
controllers/.userHttpController.go.swp
Normal file
Binary file not shown.
@@ -538,7 +538,7 @@ func createAccessKey(w http.ResponseWriter, r *http.Request) {
|
|||||||
if accesskey.Uses == 0 {
|
if accesskey.Uses == 0 {
|
||||||
accesskey.Uses = 1
|
accesskey.Uses = 1
|
||||||
}
|
}
|
||||||
gconf, err := functions.GetGlobalConfig()
|
_, gconf, err := functions.GetGlobalConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnErrorResponse(w,r,formatError(err, "internal"))
|
returnErrorResponse(w,r,formatError(err, "internal"))
|
||||||
return
|
return
|
||||||
|
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/gravitl/netmaker/mongoconn"
|
"github.com/gravitl/netmaker/mongoconn"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"time"
|
"time"
|
||||||
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
"fmt"
|
"fmt"
|
||||||
"context"
|
"context"
|
||||||
@@ -444,12 +445,17 @@ func deleteUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
success, err := DeleteUser(params["username"])
|
success, err := DeleteUser(params["username"])
|
||||||
|
|
||||||
if err != nil || !success {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 400)
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
||||||
|
json.NewEncoder(w).Encode("Could not delete user " + params["username"])
|
||||||
|
return
|
||||||
|
} else if !success {
|
||||||
|
returnErrorResponse(w, r, formatError(errors.New("Delete unsuccessful."), "internal"))
|
||||||
json.NewEncoder(w).Encode("Could not delete user " + params["username"])
|
json.NewEncoder(w).Encode("Could not delete user " + params["username"])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(params["username"] + " deleted.")
|
json.NewEncoder(w).Encode(params["username"] + " deleted.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,7 +37,7 @@ func CreateServerToken(netID string) (string, error) {
|
|||||||
accesskey.Name = GenKeyName()
|
accesskey.Name = GenKeyName()
|
||||||
accesskey.Value = GenKey()
|
accesskey.Value = GenKey()
|
||||||
accesskey.Uses = 1
|
accesskey.Uses = 1
|
||||||
gconf, errG := GetGlobalConfig()
|
_, gconf, errG := GetGlobalConfig()
|
||||||
if errG != nil {
|
if errG != nil {
|
||||||
return "", errG
|
return "", errG
|
||||||
}
|
}
|
||||||
@@ -504,7 +504,9 @@ func UniqueAddress(networkName string) (string, error){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//pretty simple get
|
//pretty simple get
|
||||||
func GetGlobalConfig() ( models.GlobalConfig, error) {
|
func GetGlobalConfig() (bool, models.GlobalConfig, error) {
|
||||||
|
|
||||||
|
create := false
|
||||||
|
|
||||||
filter := bson.M{}
|
filter := bson.M{}
|
||||||
|
|
||||||
@@ -518,12 +520,16 @@ func GetGlobalConfig() ( models.GlobalConfig, error) {
|
|||||||
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err != nil {
|
if err == mongo.ErrNoDocuments {
|
||||||
|
fmt.Println("Global config does not exist. Need to create.")
|
||||||
|
create = true
|
||||||
|
return create, globalconf, err
|
||||||
|
} else if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
fmt.Println("Could not get global config")
|
fmt.Println("Could not get global config")
|
||||||
return globalconf, err
|
return create, globalconf, err
|
||||||
}
|
}
|
||||||
return globalconf, err
|
return create, globalconf, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -50,6 +50,10 @@ func CreateUserJWT(username string, isadmin bool) (response string, err error) {
|
|||||||
func VerifyUserToken(tokenString string) (username string, isadmin bool, err error) {
|
func VerifyUserToken(tokenString string) (username string, isadmin bool, err error) {
|
||||||
claims := &models.UserClaims{}
|
claims := &models.UserClaims{}
|
||||||
|
|
||||||
|
if tokenString == config.Config.Server.MasterKey {
|
||||||
|
return "masteradministrator", true, nil
|
||||||
|
}
|
||||||
|
|
||||||
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
||||||
return jwtSecretKey, nil
|
return jwtSecretKey, nil
|
||||||
})
|
})
|
||||||
|
24
main.go
24
main.go
@@ -16,6 +16,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@@ -25,6 +26,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
service "github.com/gravitl/netmaker/controllers"
|
service "github.com/gravitl/netmaker/controllers"
|
||||||
nodepb "github.com/gravitl/netmaker/grpc"
|
nodepb "github.com/gravitl/netmaker/grpc"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@@ -127,7 +129,7 @@ func runGRPC(wg *sync.WaitGroup, installserver bool) {
|
|||||||
gconf.Name = "netmaker"
|
gconf.Name = "netmaker"
|
||||||
err := setGlobalConfig(gconf)
|
err := setGlobalConfig(gconf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && err != mongo.ErrNoDocuments{
|
||||||
log.Fatalf("Unable to set global config: %v", err)
|
log.Fatalf("Unable to set global config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,11 +161,13 @@ func runGRPC(wg *sync.WaitGroup, installserver bool) {
|
|||||||
fmt.Println("Agent Server succesfully started on port " + grpcport + " (gRPC)")
|
fmt.Println("Agent Server succesfully started on port " + grpcport + " (gRPC)")
|
||||||
|
|
||||||
if installserver {
|
if installserver {
|
||||||
fmt.Println("Adding server to default network")
|
fmt.Println("Adding server to " + config.Config.Server.DefaultNetName)
|
||||||
success, err := serverctl.AddNetwork(config.Config.Server.DefaultNetName)
|
success, err := serverctl.AddNetwork(config.Config.Server.DefaultNetName)
|
||||||
if err != nil || !success {
|
if err != nil || !success {
|
||||||
fmt.Printf("Error adding to default network: %v", err)
|
fmt.Printf("Error adding to default network: %v", err)
|
||||||
|
fmt.Println("")
|
||||||
fmt.Println("Unable to add server to network. Continuing.")
|
fmt.Println("Unable to add server to network. Continuing.")
|
||||||
|
fmt.Println("Please investigate client installation on server.")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Server successfully added to default network.")
|
fmt.Println("Server successfully added to default network.")
|
||||||
}
|
}
|
||||||
@@ -198,13 +202,17 @@ func setGlobalConfig(globalconf models.GlobalConfig) (error) {
|
|||||||
collection := mongoconn.Client.Database("netmaker").Collection("config")
|
collection := mongoconn.Client.Database("netmaker").Collection("config")
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
|
||||||
_, err := functions.GetGlobalConfig()
|
create, _, err := functions.GetGlobalConfig()
|
||||||
if err != nil {
|
if create {
|
||||||
_, err := collection.InsertOne(ctx, globalconf)
|
_, err := collection.InsertOne(ctx, globalconf)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == mongo.ErrNoDocuments || strings.Contains(err.Error(), "no documents in result"){
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
filter := bson.M{"name": "netmaker"}
|
filter := bson.M{"name": "netmaker"}
|
||||||
update := bson.D{
|
update := bson.D{
|
||||||
@@ -213,10 +221,14 @@ func setGlobalConfig(globalconf models.GlobalConfig) (error) {
|
|||||||
{"portgrpc", globalconf.PortGRPC},
|
{"portgrpc", globalconf.PortGRPC},
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
err = collection.FindOneAndUpdate(ctx, filter, update).Decode(&globalconf)
|
err := collection.FindOneAndUpdate(ctx, filter, update).Decode(&globalconf)
|
||||||
}
|
if err == mongo.ErrNoDocuments {
|
||||||
|
//if err == mongo.ErrNoDocuments || strings.Contains(err.Error(), "no documents in result"){
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func createDefaultNetwork() (bool, error) {
|
func createDefaultNetwork() (bool, error) {
|
||||||
|
|
||||||
|
@@ -196,6 +196,8 @@ func Install(accesskey string, password string, server string, network string, n
|
|||||||
var privatekey wgtypes.Key
|
var privatekey wgtypes.Key
|
||||||
var privkeystring string
|
var privkeystring string
|
||||||
var endpoint string
|
var endpoint string
|
||||||
|
var postup string
|
||||||
|
var postdown string
|
||||||
var name string
|
var name string
|
||||||
var wginterface string
|
var wginterface string
|
||||||
|
|
||||||
@@ -274,6 +276,17 @@ func Install(accesskey string, password string, server string, network string, n
|
|||||||
}
|
}
|
||||||
fmt.Println(" Interface: " + wginterface)
|
fmt.Println(" Interface: " + wginterface)
|
||||||
|
|
||||||
|
if nodecfg.PostUp != "" {
|
||||||
|
postup = nodecfg.PostUp
|
||||||
|
}
|
||||||
|
fmt.Println(" PostUp: " + postup)
|
||||||
|
|
||||||
|
if nodecfg.PostDown!= "" {
|
||||||
|
postdown = nodecfg.PostDown
|
||||||
|
}
|
||||||
|
fmt.Println(" PostDown: " + postdown)
|
||||||
|
|
||||||
|
|
||||||
if nodecfg.KeepAlive != 0 {
|
if nodecfg.KeepAlive != 0 {
|
||||||
keepalive = nodecfg.KeepAlive
|
keepalive = nodecfg.KeepAlive
|
||||||
}
|
}
|
||||||
@@ -347,6 +360,8 @@ func Install(accesskey string, password string, server string, network string, n
|
|||||||
Accesskey: accesskey,
|
Accesskey: accesskey,
|
||||||
Nodenetwork: network,
|
Nodenetwork: network,
|
||||||
Listenport: listenport,
|
Listenport: listenport,
|
||||||
|
Postup: postup,
|
||||||
|
Postdown: postdown,
|
||||||
Keepalive: keepalive,
|
Keepalive: keepalive,
|
||||||
Localaddress: localaddress,
|
Localaddress: localaddress,
|
||||||
Interface: wginterface,
|
Interface: wginterface,
|
||||||
@@ -384,6 +399,8 @@ func Install(accesskey string, password string, server string, network string, n
|
|||||||
fmt.Println(" Local Address: " + node.Localaddress)
|
fmt.Println(" Local Address: " + node.Localaddress)
|
||||||
fmt.Println(" Name: " + node.Name)
|
fmt.Println(" Name: " + node.Name)
|
||||||
fmt.Println(" Interface: " + node.Interface)
|
fmt.Println(" Interface: " + node.Interface)
|
||||||
|
fmt.Println(" PostUp: " + node.Postup)
|
||||||
|
fmt.Println(" PostDown: " + node.Postdown)
|
||||||
fmt.Println(" Port: " + strconv.FormatInt(int64(node.Listenport), 10))
|
fmt.Println(" Port: " + strconv.FormatInt(int64(node.Listenport), 10))
|
||||||
fmt.Println(" KeepAlive: " + strconv.FormatInt(int64(node.Keepalive), 10))
|
fmt.Println(" KeepAlive: " + strconv.FormatInt(int64(node.Keepalive), 10))
|
||||||
fmt.Println(" Public Key: " + node.Publickey)
|
fmt.Println(" Public Key: " + node.Publickey)
|
||||||
@@ -483,6 +500,12 @@ func modConfig(node *nodepb.Node) error{
|
|||||||
if node.Localaddress != ""{
|
if node.Localaddress != ""{
|
||||||
nodecfg.LocalAddress = node.Localaddress
|
nodecfg.LocalAddress = node.Localaddress
|
||||||
}
|
}
|
||||||
|
if node.Postup != ""{
|
||||||
|
nodecfg.PostUp = node.Postup
|
||||||
|
}
|
||||||
|
if node.Postdown != ""{
|
||||||
|
nodecfg.PostDown = node.Postdown
|
||||||
|
}
|
||||||
if node.Listenport != 0{
|
if node.Listenport != 0{
|
||||||
nodecfg.Port = node.Listenport
|
nodecfg.Port = node.Listenport
|
||||||
}
|
}
|
||||||
|
@@ -15,6 +15,7 @@ func DownloadNetclient() error {
|
|||||||
// Get the data
|
// Get the data
|
||||||
resp, err := http.Get("https://github.com/gravitl/netmaker/releases/download/latest/netclient")
|
resp, err := http.Get("https://github.com/gravitl/netmaker/releases/download/latest/netclient")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("could not download netclient")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -22,6 +23,7 @@ func DownloadNetclient() error {
|
|||||||
// Create the file
|
// Create the file
|
||||||
out, err := os.Create("/etc/netclient/netclient")
|
out, err := os.Create("/etc/netclient/netclient")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("could not create /etc/netclient")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
@@ -33,6 +35,7 @@ func DownloadNetclient() error {
|
|||||||
func RemoveNetwork(network string) (bool, error) {
|
func RemoveNetwork(network string) (bool, error) {
|
||||||
_, err := os.Stat("/etc/netclient/netclient")
|
_, err := os.Stat("/etc/netclient/netclient")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("could not find /etc/netclient")
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
cmdoutput, err := exec.Command("/etc/netclient/netclient","-c","remove","-n",network).Output()
|
cmdoutput, err := exec.Command("/etc/netclient/netclient","-c","remove","-n",network).Output()
|
||||||
@@ -50,22 +53,25 @@ func AddNetwork(network string) (bool, error) {
|
|||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
os.Mkdir("/etc/netclient", 744)
|
os.Mkdir("/etc/netclient", 744)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
fmt.Println("couldnt find or create /etc/netclient")
|
fmt.Println("could not find or create /etc/netclient")
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
token, err := functions.CreateServerToken(network)
|
token, err := functions.CreateServerToken(network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("could not create server token for " + network)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
_, err = os.Stat("/etc/netclient/netclient")
|
_, err = os.Stat("/etc/netclient/netclient")
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = DownloadNetclient()
|
err = DownloadNetclient()
|
||||||
|
fmt.Println("could not download netclient")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = os.Chmod("/etc/netclient/netclient", 0755)
|
err = os.Chmod("/etc/netclient/netclient", 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("could not change netclient directory permissions")
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
cmdoutput, err := exec.Command("/etc/netclient/netclient","-c","install","-t",token,"-name","netmaker").Output()
|
cmdoutput, err := exec.Command("/etc/netclient/netclient","-c","install","-t",token,"-name","netmaker").Output()
|
||||||
@@ -73,8 +79,8 @@ func AddNetwork(network string) (bool, error) {
|
|||||||
fmt.Println(string(cmdoutput))
|
fmt.Println(string(cmdoutput))
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
fmt.Println(string(cmdoutput))
|
||||||
fmt.Println("Server added to network " + network)
|
fmt.Println("Server added to network " + network)
|
||||||
return true, err
|
return true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
7
test/restartmongo.sh
Normal file
7
test/restartmongo.sh
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
sudo docker kill mongodb
|
||||||
|
sudo docker rm mongodb
|
||||||
|
sudo docker volume rm mongovol
|
||||||
|
|
||||||
|
docker volume create mongovol && docker run -d --name mongodb -v mongovol:/data/db --network host -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=mongopass mongo --bind_ip 0.0.0.0
|
Reference in New Issue
Block a user