2021-12-08 01:46:55 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-08-08 10:22:54 +08:00
|
|
|
"fmt"
|
2021-12-08 01:46:55 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
2022-09-29 02:59:21 +08:00
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
2021-12-08 01:46:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateEgressGateway - creates an egress gateway
|
2022-12-21 04:29:09 +08:00
|
|
|
func CreateEgressGateway(gateway models.EgressGatewayRequest) (models.Node, error) {
|
|
|
|
node, err := GetNodeByID(gateway.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
host, err := GetHost(node.HostID.String())
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
2023-02-07 00:14:30 +08:00
|
|
|
if host.OS != "linux" { // support for other OS to be added
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, errors.New(host.OS + " is unsupported for egress gateways")
|
2022-01-26 22:04:30 +08:00
|
|
|
}
|
2023-02-07 00:04:47 +08:00
|
|
|
for i := len(gateway.Ranges) - 1; i >= 0; i-- {
|
2023-04-08 00:26:42 +08:00
|
|
|
if gateway.Ranges[i] == "::/0" {
|
|
|
|
logger.Log(0, "currently IPv6 internet gateways are not supported", gateway.Ranges[i])
|
2023-02-07 00:04:47 +08:00
|
|
|
gateway.Ranges = append(gateway.Ranges[:i], gateway.Ranges[i+1:]...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
normalized, err := NormalizeCIDR(gateway.Ranges[i])
|
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
|
|
|
}
|
|
|
|
gateway.Ranges[i] = normalized
|
|
|
|
|
|
|
|
}
|
2022-07-20 19:01:28 +08:00
|
|
|
if gateway.NatEnabled == "" {
|
|
|
|
gateway.NatEnabled = "yes"
|
|
|
|
}
|
2021-12-08 01:46:55 +08:00
|
|
|
err = ValidateEgressGateway(gateway)
|
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
node.IsEgressGateway = true
|
2021-12-08 01:46:55 +08:00
|
|
|
node.EgressGatewayRanges = gateway.Ranges
|
2022-12-22 19:42:33 +08:00
|
|
|
node.EgressGatewayNatEnabled = models.ParseBool(gateway.NatEnabled)
|
2022-08-08 10:22:54 +08:00
|
|
|
node.EgressGatewayRequest = gateway // store entire request for use when preserving the egress gateway
|
2021-12-08 01:46:55 +08:00
|
|
|
node.SetLastModified()
|
|
|
|
nodeData, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
if err = database.Insert(node.ID.String(), string(nodeData), database.NODES_TABLE_NAME); err != nil {
|
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:58:20 +08:00
|
|
|
// ValidateEgressGateway - validates the egress gateway model
|
2021-12-08 01:46:55 +08:00
|
|
|
func ValidateEgressGateway(gateway models.EgressGatewayRequest) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
empty := len(gateway.Ranges) == 0
|
|
|
|
if empty {
|
|
|
|
err = errors.New("IP Ranges Cannot Be Empty")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteEgressGateway - deletes egress from node
|
2022-12-21 04:29:09 +08:00
|
|
|
func DeleteEgressGateway(network, nodeid string) (models.Node, error) {
|
2022-01-11 06:52:21 +08:00
|
|
|
node, err := GetNodeByID(nodeid)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
node.IsEgressGateway = false
|
2021-12-08 01:46:55 +08:00
|
|
|
node.EgressGatewayRanges = []string{}
|
2022-08-08 10:22:54 +08:00
|
|
|
node.EgressGatewayRequest = models.EgressGatewayRequest{} // remove preserved request as the egress gateway is gone
|
2021-12-08 01:46:55 +08:00
|
|
|
node.SetLastModified()
|
2022-01-12 00:59:26 +08:00
|
|
|
|
2021-12-08 01:46:55 +08:00
|
|
|
data, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
if err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME); err != nil {
|
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateIngressGateway - creates an ingress gateway
|
2022-12-21 04:29:09 +08:00
|
|
|
func CreateIngressGateway(netid string, nodeid string, failover bool) (models.Node, error) {
|
2021-12-08 01:46:55 +08:00
|
|
|
|
2022-01-11 06:52:21 +08:00
|
|
|
node, err := GetNodeByID(nodeid)
|
2022-12-21 04:29:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return models.Node{}, err
|
2022-12-13 20:06:36 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
host, err := GetHost(node.HostID.String())
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, err
|
|
|
|
}
|
2023-01-25 19:35:45 +08:00
|
|
|
if host.OS != "linux" {
|
|
|
|
return models.Node{}, errors.New("ingress can only be created on linux based node")
|
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
if host.FirewallInUse == models.FIREWALL_NONE {
|
|
|
|
return models.Node{}, errors.New("firewall is not supported for ingress gateways")
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
network, err := GetParentNetwork(netid)
|
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
node.IsIngressGateway = true
|
2021-12-08 01:46:55 +08:00
|
|
|
node.IngressGatewayRange = network.AddressRange
|
2022-09-02 23:01:19 +08:00
|
|
|
node.IngressGatewayRange6 = network.AddressRange6
|
2021-12-08 01:46:55 +08:00
|
|
|
node.SetLastModified()
|
2022-09-29 02:59:21 +08:00
|
|
|
if failover && servercfg.Is_EE {
|
2022-12-21 04:29:09 +08:00
|
|
|
node.Failover = true
|
2022-09-29 02:59:21 +08:00
|
|
|
}
|
2021-12-08 01:46:55 +08:00
|
|
|
data, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2022-12-21 04:29:09 +08:00
|
|
|
return models.Node{}, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
|
|
|
err = SetNetworkNodesLastModified(netid)
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteIngressGateway - deletes an ingress gateway
|
2023-04-07 00:27:33 +08:00
|
|
|
func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, []models.ExtClient, error) {
|
|
|
|
removedClients := []models.ExtClient{}
|
2022-01-12 02:05:22 +08:00
|
|
|
node, err := GetNodeByID(nodeid)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2023-04-07 00:27:33 +08:00
|
|
|
return models.Node{}, false, removedClients, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2023-04-07 00:27:33 +08:00
|
|
|
clients, err := GetExtClientsByID(nodeid, networkName)
|
2023-04-12 22:16:18 +08:00
|
|
|
if err != nil && !database.IsEmptyRecord(err) {
|
2023-04-07 00:27:33 +08:00
|
|
|
return models.Node{}, false, removedClients, err
|
|
|
|
}
|
|
|
|
|
|
|
|
removedClients = clients
|
|
|
|
|
2021-12-08 01:46:55 +08:00
|
|
|
// delete ext clients belonging to ingress gateway
|
2022-12-21 04:29:09 +08:00
|
|
|
if err = DeleteGatewayExtClients(node.ID.String(), networkName); err != nil {
|
2023-04-07 00:27:33 +08:00
|
|
|
return models.Node{}, false, removedClients, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-08-04 02:36:16 +08:00
|
|
|
logger.Log(3, "deleting ingress gateway")
|
2022-12-24 02:41:17 +08:00
|
|
|
wasFailover := node.Failover
|
2022-12-21 04:29:09 +08:00
|
|
|
node.LastModified = time.Now()
|
|
|
|
node.IsIngressGateway = false
|
2021-12-08 01:46:55 +08:00
|
|
|
node.IngressGatewayRange = ""
|
2022-12-21 04:29:09 +08:00
|
|
|
node.Failover = false
|
2021-12-08 01:46:55 +08:00
|
|
|
|
2022-12-21 04:29:09 +08:00
|
|
|
//logger.Log(3, "deleting ingress gateway firewall in use is '", host.FirewallInUse, "' and isEgressGateway is", node.IsEgressGateway)
|
2022-08-08 10:22:54 +08:00
|
|
|
if node.EgressGatewayRequest.NodeID != "" {
|
|
|
|
_, err := CreateEgressGateway(node.EgressGatewayRequest)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, fmt.Sprintf("failed to create egress gateway on node [%s] on network [%s]: %v",
|
|
|
|
node.EgressGatewayRequest.NodeID, node.EgressGatewayRequest.NetID, err))
|
2022-08-04 02:36:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:46:55 +08:00
|
|
|
data, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
2023-04-07 00:27:33 +08:00
|
|
|
return models.Node{}, false, removedClients, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
2022-12-21 04:29:09 +08:00
|
|
|
err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2023-04-07 00:27:33 +08:00
|
|
|
return models.Node{}, wasFailover, removedClients, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
|
|
|
err = SetNetworkNodesLastModified(networkName)
|
2023-04-07 00:27:33 +08:00
|
|
|
return node, wasFailover, removedClients, err
|
2021-12-08 01:46:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
|
|
|
|
func DeleteGatewayExtClients(gatewayID string, networkName string) error {
|
|
|
|
currentExtClients, err := GetNetworkExtClients(networkName)
|
2023-04-11 23:04:36 +08:00
|
|
|
if database.IsEmptyRecord(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-12-08 01:46:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, extClient := range currentExtClients {
|
|
|
|
if extClient.IngressGatewayID == gatewayID {
|
|
|
|
if err = DeleteExtClient(networkName, extClient.ClientID); err != nil {
|
|
|
|
logger.Log(1, "failed to remove ext client", extClient.ClientID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|