netmaker/logic/gateway.go

201 lines
5.9 KiB
Go
Raw Normal View History

2021-12-08 01:46:55 +08:00
package logic
import (
"encoding/json"
"errors"
"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
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
return models.Node{}, errors.New(host.OS + " is unsupported for egress gateways")
}
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
}
if gateway.NatEnabled == "" {
gateway.NatEnabled = "yes"
}
2021-12-08 01:46:55 +08:00
err = ValidateEgressGateway(gateway)
if err != nil {
return models.Node{}, err
2021-12-08 01:46:55 +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)
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
}
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
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 {
return models.Node{}, err
2021-12-08 01:46:55 +08:00
}
node.IsEgressGateway = false
2021-12-08 01:46:55 +08:00
node.EgressGatewayRanges = []string{}
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 {
return models.Node{}, err
2021-12-08 01:46:55 +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
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)
if err != nil {
return models.Node{}, err
2022-12-13 20:06:36 +08:00
}
host, err := GetHost(node.HostID.String())
2021-12-08 01:46:55 +08:00
if err != nil {
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")
}
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 {
return models.Node{}, err
2021-12-08 01:46:55 +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 {
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 {
return models.Node{}, err
2021-12-08 01:46:55 +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 {
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)
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
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
}
logger.Log(3, "deleting ingress gateway")
wasFailover := node.Failover
node.LastModified = time.Now()
node.IsIngressGateway = false
2021-12-08 01:46:55 +08:00
node.IngressGatewayRange = ""
node.Failover = false
2021-12-08 01:46:55 +08:00
//logger.Log(3, "deleting ingress gateway firewall in use is '", host.FirewallInUse, "' and isEgressGateway is", node.IsEgressGateway)
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))
}
}
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
}
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)
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
}