gateway.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "github.com/gravitl/netmaker/database"
  8. "github.com/gravitl/netmaker/logger"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/servercfg"
  11. )
  12. // CreateEgressGateway - creates an egress gateway
  13. func CreateEgressGateway(gateway models.EgressGatewayRequest) (models.Node, error) {
  14. node, err := GetNodeByID(gateway.NodeID)
  15. if err != nil {
  16. return models.Node{}, err
  17. }
  18. host, err := GetHost(node.HostID.String())
  19. if err != nil {
  20. return models.Node{}, err
  21. }
  22. for i, cidr := range gateway.Ranges {
  23. normalized, err := NormalizeCIDR(cidr)
  24. if err != nil {
  25. return models.Node{}, err
  26. }
  27. gateway.Ranges[i] = normalized
  28. }
  29. if host.OS != "linux" && host.OS != "freebsd" { // add in darwin later
  30. return models.Node{}, errors.New(host.OS + " is unsupported for egress gateways")
  31. }
  32. if host.OS == "linux" && host.FirewallInUse == models.FIREWALL_NONE {
  33. return models.Node{}, errors.New("firewall is not supported for egress gateways")
  34. }
  35. if gateway.NatEnabled == "" {
  36. gateway.NatEnabled = "yes"
  37. }
  38. err = ValidateEgressGateway(gateway)
  39. if err != nil {
  40. return models.Node{}, err
  41. }
  42. node.IsEgressGateway = true
  43. node.EgressGatewayRanges = gateway.Ranges
  44. node.EgressGatewayNatEnabled = models.ParseBool(gateway.NatEnabled)
  45. node.EgressGatewayRequest = gateway // store entire request for use when preserving the egress gateway
  46. node.SetLastModified()
  47. nodeData, err := json.Marshal(&node)
  48. if err != nil {
  49. return node, err
  50. }
  51. if err = database.Insert(node.ID.String(), string(nodeData), database.NODES_TABLE_NAME); err != nil {
  52. return models.Node{}, err
  53. }
  54. return node, nil
  55. }
  56. // ValidateEgressGateway - validates the egress gateway model
  57. func ValidateEgressGateway(gateway models.EgressGatewayRequest) error {
  58. var err error
  59. empty := len(gateway.Ranges) == 0
  60. if empty {
  61. err = errors.New("IP Ranges Cannot Be Empty")
  62. }
  63. empty = gateway.Interface == ""
  64. if empty {
  65. err = errors.New("interface cannot be empty")
  66. }
  67. return err
  68. }
  69. // DeleteEgressGateway - deletes egress from node
  70. func DeleteEgressGateway(network, nodeid string) (models.Node, error) {
  71. node, err := GetNodeByID(nodeid)
  72. if err != nil {
  73. return models.Node{}, err
  74. }
  75. node.IsEgressGateway = false
  76. node.EgressGatewayRanges = []string{}
  77. node.EgressGatewayRequest = models.EgressGatewayRequest{} // remove preserved request as the egress gateway is gone
  78. node.SetLastModified()
  79. data, err := json.Marshal(&node)
  80. if err != nil {
  81. return models.Node{}, err
  82. }
  83. if err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME); err != nil {
  84. return models.Node{}, err
  85. }
  86. return node, nil
  87. }
  88. // CreateIngressGateway - creates an ingress gateway
  89. func CreateIngressGateway(netid string, nodeid string, failover bool) (models.Node, error) {
  90. node, err := GetNodeByID(nodeid)
  91. if err != nil {
  92. return models.Node{}, err
  93. }
  94. host, err := GetHost(node.HostID.String())
  95. if err != nil {
  96. return models.Node{}, err
  97. }
  98. if host.OS != "linux" {
  99. return models.Node{}, errors.New("ingress can only be created on linux based node")
  100. }
  101. if host.FirewallInUse == models.FIREWALL_NONE {
  102. return models.Node{}, errors.New("firewall is not supported for ingress gateways")
  103. }
  104. network, err := GetParentNetwork(netid)
  105. if err != nil {
  106. return models.Node{}, err
  107. }
  108. node.IsIngressGateway = true
  109. node.IngressGatewayRange = network.AddressRange
  110. node.IngressGatewayRange6 = network.AddressRange6
  111. node.SetLastModified()
  112. if failover && servercfg.Is_EE {
  113. node.Failover = true
  114. }
  115. data, err := json.Marshal(&node)
  116. if err != nil {
  117. return models.Node{}, err
  118. }
  119. err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME)
  120. if err != nil {
  121. return models.Node{}, err
  122. }
  123. err = SetNetworkNodesLastModified(netid)
  124. return node, err
  125. }
  126. // DeleteIngressGateway - deletes an ingress gateway
  127. func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, error) {
  128. node, err := GetNodeByID(nodeid)
  129. if err != nil {
  130. return models.Node{}, false, err
  131. }
  132. //host, err := GetHost(node.ID.String())
  133. //if err != nil {
  134. //return models.Node{}, false, err
  135. //}
  136. //network, err := GetParentNetwork(networkName)
  137. if err != nil {
  138. return models.Node{}, false, err
  139. }
  140. // delete ext clients belonging to ingress gateway
  141. if err = DeleteGatewayExtClients(node.ID.String(), networkName); err != nil {
  142. return models.Node{}, false, err
  143. }
  144. logger.Log(3, "deleting ingress gateway")
  145. wasFailover := node.Failover
  146. node.LastModified = time.Now()
  147. node.IsIngressGateway = false
  148. node.IngressGatewayRange = ""
  149. node.Failover = false
  150. //logger.Log(3, "deleting ingress gateway firewall in use is '", host.FirewallInUse, "' and isEgressGateway is", node.IsEgressGateway)
  151. if node.EgressGatewayRequest.NodeID != "" {
  152. _, err := CreateEgressGateway(node.EgressGatewayRequest)
  153. if err != nil {
  154. logger.Log(0, fmt.Sprintf("failed to create egress gateway on node [%s] on network [%s]: %v",
  155. node.EgressGatewayRequest.NodeID, node.EgressGatewayRequest.NetID, err))
  156. }
  157. }
  158. data, err := json.Marshal(&node)
  159. if err != nil {
  160. return models.Node{}, false, err
  161. }
  162. err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME)
  163. if err != nil {
  164. return models.Node{}, wasFailover, err
  165. }
  166. err = SetNetworkNodesLastModified(networkName)
  167. return node, wasFailover, err
  168. }
  169. // DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
  170. func DeleteGatewayExtClients(gatewayID string, networkName string) error {
  171. currentExtClients, err := GetNetworkExtClients(networkName)
  172. if err != nil && !database.IsEmptyRecord(err) {
  173. return err
  174. }
  175. for _, extClient := range currentExtClients {
  176. if extClient.IngressGatewayID == gatewayID {
  177. if err = DeleteExtClient(networkName, extClient.ClientID); err != nil {
  178. logger.Log(1, "failed to remove ext client", extClient.ClientID)
  179. continue
  180. }
  181. }
  182. }
  183. return nil
  184. }