123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- //TODO: Consider restructuring this file/folder "github.com/gorilla/handlers"
- //It may make more sense to split into different files and not call it "helpers"
- package functions
- import (
- "context"
- "encoding/base64"
- "errors"
- "fmt"
- "math/rand"
- "net"
- "strings"
- "time"
- "github.com/gravitl/netmaker/models"
- "github.com/gravitl/netmaker/mongoconn"
- "github.com/gravitl/netmaker/servercfg"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo"
- "go.mongodb.org/mongo-driver/mongo/options"
- )
- //Takes in an arbitrary field and value for field and checks to see if any other
- //node has that value for the same field within the network
- func CreateServerToken(netID string) (string, error) {
- fmt.Println("Creating token.")
- var network models.Network
- var accesskey models.AccessKey
- network, err := GetParentNetwork(netID)
- if err != nil {
- return "", err
- }
- accesskey.Name = GenKeyName()
- accesskey.Value = GenKey()
- accesskey.Uses = 1
- address := "127.0.0.1:" + servercfg.GetGRPCPort()
- privAddr := ""
- if *network.IsLocal {
- privAddr = network.LocalRange
- }
- accessstringdec := address + "|" + netID + "|" + accesskey.Value + "|" + privAddr
- accesskey.AccessString = base64.StdEncoding.EncodeToString([]byte(accessstringdec))
- fmt.Println(" access string: " + accesskey.AccessString)
- network.AccessKeys = append(network.AccessKeys, accesskey)
- collection := mongoconn.Client.Database("netmaker").Collection("networks")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- // Create filter
- filter := bson.M{"netid": netID}
- // Read update model from body request
- fmt.Println("Adding key to " + network.NetID)
- // prepare update model.
- update := bson.D{
- {"$set", bson.D{
- {"accesskeys", network.AccessKeys},
- }},
- }
- errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&network)
- defer cancel()
- if errN != nil {
- return "", errN
- }
- return accesskey.AccessString, nil
- }
- func IsFieldUnique(network string, field string, value string) bool {
- var node models.Node
- isunique := true
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{field: value, "network": network}
- err := collection.FindOne(ctx, filter).Decode(&node)
- defer cancel()
- if err != nil {
- return isunique
- }
- if node.Name != "" {
- isunique = false
- }
- return isunique
- }
- func NetworkExists(name string) (bool, error) {
- collection := mongoconn.Client.Database("netmaker").Collection("networks")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"netid": name}
- var result bson.M
- err := collection.FindOne(ctx, filter).Decode(&result)
- defer cancel()
- if err != nil {
- if err == mongo.ErrNoDocuments {
- return false, nil
- }
- }
- return true, err
- }
- //TODO: This is very inefficient (N-squared). Need to find a better way.
- //Takes a list of nodes in a network and iterates through
- //for each node, it gets a unique address. That requires checking against all other nodes once more
- func UpdateNetworkNodeAddresses(networkName string) error {
- //Connection mongoDB with mongoconn class
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"network": networkName}
- cur, err := collection.Find(ctx, filter)
- if err != nil {
- return err
- }
- defer cancel()
- for cur.Next(context.TODO()) {
- var node models.Node
- err := cur.Decode(&node)
- if err != nil {
- fmt.Println("error in node address assignment!")
- return err
- }
- ipaddr, iperr := UniqueAddress(networkName)
- if iperr != nil {
- fmt.Println("error in node address assignment!")
- return iperr
- }
- filter := bson.M{"macaddress": node.MacAddress}
- update := bson.D{{"$set", bson.D{{"address", ipaddr}}}}
- errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&node)
- defer cancel()
- if errN != nil {
- return errN
- }
- }
- return err
- }
- //TODO TODO TODO!!!!!
- func UpdateNetworkPrivateAddresses(networkName string) error {
- //Connection mongoDB with mongoconn class
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"network": networkName}
- cur, err := collection.Find(ctx, filter)
- if err != nil {
- return err
- }
- defer cancel()
- for cur.Next(context.TODO()) {
- var node models.Node
- err := cur.Decode(&node)
- if err != nil {
- fmt.Println("error in node address assignment!")
- return err
- }
- ipaddr, iperr := UniqueAddress(networkName)
- if iperr != nil {
- fmt.Println("error in node address assignment!")
- return iperr
- }
- filter := bson.M{"macaddress": node.MacAddress}
- update := bson.D{{"$set", bson.D{{"address", ipaddr}}}}
- errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&node)
- defer cancel()
- if errN != nil {
- return errN
- }
- }
- return err
- }
- //Checks to see if any other networks have the same name (id)
- func IsNetworkNameUnique(name string) (bool, error) {
- isunique := true
- dbs, err := ListNetworks()
- if err != nil {
- return false, err
- }
- for i := 0; i < len(dbs); i++ {
- if name == dbs[i].NetID {
- isunique = false
- }
- }
- return isunique, nil
- }
- func IsNetworkDisplayNameUnique(name string) (bool, error) {
- isunique := true
- dbs, err := ListNetworks()
- if err != nil {
- return false, err
- }
- for i := 0; i < len(dbs); i++ {
- if name == dbs[i].DisplayName {
- isunique = false
- }
- }
- return isunique, nil
- }
- func GetNetworkNodeNumber(networkName string) (int, error) {
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"network": networkName}
- count, err := collection.CountDocuments(ctx, filter)
- returncount := int(count)
- //not sure if this is the right way of handling this error...
- if err != nil {
- return 9999, err
- }
- defer cancel()
- return returncount, err
- }
- //Kind of a weird name. Should just be GetNetworks I think. Consider changing.
- //Anyway, returns all the networks
- func ListNetworks() ([]models.Network, error) {
- var networks []models.Network
- collection := mongoconn.Client.Database("netmaker").Collection("networks")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- cur, err := collection.Find(ctx, bson.M{}, options.Find().SetProjection(bson.M{"_id": 0}))
- if err != nil {
- return networks, err
- }
- defer cancel()
- for cur.Next(context.TODO()) {
- var network models.Network
- err := cur.Decode(&network)
- if err != nil {
- return networks, err
- }
- // add network our array
- networks = append(networks, network)
- }
- if err := cur.Err(); err != nil {
- return networks, err
- }
- return networks, err
- }
- //Checks to see if access key is valid
- //Does so by checking against all keys and seeing if any have the same value
- //may want to hash values before comparing...consider this
- //TODO: No error handling!!!!
- func IsKeyValid(networkname string, keyvalue string) bool {
- network, _ := GetParentNetwork(networkname)
- var key models.AccessKey
- foundkey := false
- isvalid := false
- for i := len(network.AccessKeys) - 1; i >= 0; i-- {
- currentkey := network.AccessKeys[i]
- if currentkey.Value == keyvalue {
- key = currentkey
- foundkey = true
- }
- }
- if foundkey {
- if key.Uses > 0 {
- isvalid = true
- }
- }
- return isvalid
- }
- //TODO: Contains a fatal error return. Need to change
- //This just gets a network object from a network name
- //Should probably just be GetNetwork. kind of a dumb name.
- //Used in contexts where it's not the Parent network.
- func GetParentNetwork(networkname string) (models.Network, error) {
- var network models.Network
- collection := mongoconn.Client.Database("netmaker").Collection("networks")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"netid": networkname}
- err := collection.FindOne(ctx, filter).Decode(&network)
- defer cancel()
- if err != nil {
- return network, err
- }
- return network, nil
- }
- func IsIpNet(host string) bool {
- return net.ParseIP(host) != nil
- }
- //Similar to above but checks if Cidr range is valid
- //At least this guy's got some print statements
- //still not good error handling
- func IsIpCIDR(host string) bool {
- ip, ipnet, err := net.ParseCIDR(host)
- if err != nil {
- fmt.Println(err)
- fmt.Println("Address Range is not valid!")
- return false
- }
- return ip != nil && ipnet != nil
- }
- //This is used to validate public keys (make sure they're base64 encoded like all public keys should be).
- func IsBase64(s string) bool {
- _, err := base64.StdEncoding.DecodeString(s)
- return err == nil
- }
- //This should probably just be called GetNode
- //It returns a node based on the ID of the node.
- //Why do we need this?
- //TODO: Check references. This seems unnecessary.
- func GetNodeObj(id primitive.ObjectID) models.Node {
- var node models.Node
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"_id": id}
- err := collection.FindOne(ctx, filter).Decode(&node)
- defer cancel()
- if err != nil {
- fmt.Println(err)
- fmt.Println("Did not get the node...")
- return node
- }
- fmt.Println("Got node " + node.Name)
- return node
- }
- //This checks to make sure a network name is valid.
- //Switch to REGEX?
- func NameInNetworkCharSet(name string) bool {
- charset := "abcdefghijklmnopqrstuvwxyz1234567890-_"
- for _, char := range name {
- if !strings.Contains(charset, strings.ToLower(string(char))) {
- return false
- }
- }
- return true
- }
- func NameInDNSCharSet(name string) bool {
- charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
- for _, char := range name {
- if !strings.Contains(charset, strings.ToLower(string(char))) {
- return false
- }
- }
- return true
- }
- func NameInNodeCharSet(name string) bool {
- charset := "abcdefghijklmnopqrstuvwxyz1234567890-"
- for _, char := range name {
- if !strings.Contains(charset, strings.ToLower(string(char))) {
- return false
- }
- }
- return true
- }
- //This returns a node based on its mac address.
- //The mac address acts as the Unique ID for nodes.
- //Is this a dumb thing to do? I thought it was cool but maybe it's dumb.
- //It doesn't really provide a tangible benefit over a random ID
- func GetNodeByMacAddress(network string, macaddress string) (models.Node, error) {
- var node models.Node
- filter := bson.M{"macaddress": macaddress, "network": network}
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- err := collection.FindOne(ctx, filter).Decode(&node)
- defer cancel()
- if err != nil {
- return node, err
- }
- return node, nil
- }
- //This returns a unique address for a node to use
- //it iterates through the list of IP's in the subnet
- //and checks against all nodes to see if it's taken, until it finds one.
- //TODO: We do not handle a case where we run out of addresses.
- //We will need to handle that eventually
- func UniqueAddress(networkName string) (string, error) {
- var network models.Network
- network, err := GetParentNetwork(networkName)
- if err != nil {
- fmt.Println("UniqueAddress encountered an error")
- return "666", err
- }
- offset := true
- ip, ipnet, err := net.ParseCIDR(network.AddressRange)
- if err != nil {
- fmt.Println("UniqueAddress encountered an error")
- return "666", err
- }
- for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); Inc(ip) {
- if offset {
- offset = false
- continue
- }
- if IsIPUnique(networkName, ip.String()) {
- return ip.String(), err
- }
- }
- //TODO
- err1 := errors.New("ERROR: No unique addresses available. Check network subnet.")
- return "W1R3: NO UNIQUE ADDRESSES AVAILABLE", err1
- }
- func UniqueAddress6(networkName string) (string, error) {
- var network models.Network
- network, err := GetParentNetwork(networkName)
- if err != nil {
- fmt.Println("Network Not Found")
- return "", err
- }
- if network.IsDualStack == nil || *network.IsDualStack == false {
- return "", nil
- }
- offset := true
- ip, ipnet, err := net.ParseCIDR(network.AddressRange6)
- if err != nil {
- fmt.Println("UniqueAddress6 encountered an error")
- return "666", err
- }
- for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); Inc(ip) {
- if offset {
- offset = false
- continue
- }
- if IsIP6Unique(networkName, ip.String()) {
- return ip.String(), err
- }
- }
- //TODO
- err1 := errors.New("ERROR: No unique addresses available. Check network subnet.")
- return "W1R3: NO UNIQUE ADDRESSES AVAILABLE", err1
- }
- //generate an access key value
- func GenKey() string {
- var seededRand *rand.Rand = rand.New(
- rand.NewSource(time.Now().UnixNano()))
- length := 16
- charset := "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- b := make([]byte, length)
- for i := range b {
- b[i] = charset[seededRand.Intn(len(charset))]
- }
- return string(b)
- }
- //generate a key value
- //we should probably just have 1 random string generator
- //that can be used across all functions
- //have a "base string" a "length" and a "charset"
- func GenKeyName() string {
- var seededRand *rand.Rand = rand.New(
- rand.NewSource(time.Now().UnixNano()))
- length := 5
- charset := "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- b := make([]byte, length)
- for i := range b {
- b[i] = charset[seededRand.Intn(len(charset))]
- }
- return "key-" + string(b)
- }
- //checks if IP is unique in the address range
- //used by UniqueAddress
- func IsIPUnique(network string, ip string) bool {
- var node models.Node
- isunique := true
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"address": ip, "network": network}
- err := collection.FindOne(ctx, filter).Decode(&node)
- defer cancel()
- if err != nil {
- fmt.Println(err)
- return isunique
- }
- if node.Address == ip {
- isunique = false
- }
- return isunique
- }
- //checks if IP is unique in the address range
- //used by UniqueAddress
- func IsIP6Unique(network string, ip string) bool {
- var node models.Node
- isunique := true
- collection := mongoconn.Client.Database("netmaker").Collection("nodes")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"address6": ip, "network": network}
- err := collection.FindOne(ctx, filter).Decode(&node)
- defer cancel()
- if err != nil {
- fmt.Println(err)
- return isunique
- }
- if node.Address6 == ip {
- isunique = false
- }
- return isunique
- }
- //called once key has been used by createNode
- //reduces value by one and deletes if necessary
- func DecrimentKey(networkName string, keyvalue string) {
- var network models.Network
- network, err := GetParentNetwork(networkName)
- if err != nil {
- return
- }
- for i := len(network.AccessKeys) - 1; i >= 0; i-- {
- currentkey := network.AccessKeys[i]
- if currentkey.Value == keyvalue {
- network.AccessKeys[i].Uses--
- if network.AccessKeys[i].Uses < 1 {
- //this is the part where it will call the delete
- //not sure if there's edge cases I'm missing
- DeleteKey(network, i)
- return
- }
- }
- }
- collection := mongoconn.Client.Database("netmaker").Collection("networks")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- filter := bson.M{"netid": network.NetID}
- update := bson.D{
- {"$set", bson.D{
- {"accesskeys", network.AccessKeys},
- }},
- }
- errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&network)
- defer cancel()
- if errN != nil {
- return
- }
- }
- //takes the logic from controllers.deleteKey
- func DeleteKey(network models.Network, i int) {
- network.AccessKeys = append(network.AccessKeys[:i],
- network.AccessKeys[i+1:]...)
- collection := mongoconn.Client.Database("netmaker").Collection("networks")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- // Create filter
- filter := bson.M{"netid": network.NetID}
- // prepare update model.
- update := bson.D{
- {"$set", bson.D{
- {"accesskeys", network.AccessKeys},
- }},
- }
- errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&network)
- defer cancel()
- if errN != nil {
- return
- }
- }
- //increments an IP over the previous
- func Inc(ip net.IP) {
- for j := len(ip) - 1; j >= 0; j-- {
- ip[j]++
- if ip[j] > 0 {
- break
- }
- }
- }
|