Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NET-562] Persistent Keep Alive from node to host #2604

Merged
merged 25 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
eca1064
Move PKA field from models node to host level
gabrielseibel1 Sep 19, 2023
17d0ecf
Move PKA field from api models node to host level
gabrielseibel1 Sep 19, 2023
8a15144
Adapt logic package to node->host PKA
gabrielseibel1 Sep 19, 2023
10c3416
Adapt migration-related code to node->host PKA
gabrielseibel1 Sep 19, 2023
6d4c14c
Adapt cli code to node->host PKA
gabrielseibel1 Sep 19, 2023
81f5dd6
Change host PKA default to 20s
gabrielseibel1 Sep 19, 2023
59bf7c4
Merge branch 'develop' into net-562
gabrielseibel1 Sep 25, 2023
2c6b5ae
On IfaceDelta, check for PKA on host
gabrielseibel1 Sep 26, 2023
b0033e9
Merge branch 'net-562' of github.com:gravitl/netmaker into net-562
gabrielseibel1 Sep 26, 2023
f67bff1
On handleHostRegister, set default PKA
gabrielseibel1 Sep 27, 2023
c7d54c5
Use a default PKA
gabrielseibel1 Sep 27, 2023
459d9dc
Use int64 for api host pka
gabrielseibel1 Sep 27, 2023
febaa15
Reorder imports
gabrielseibel1 Sep 27, 2023
4ceda14
Don't use host pka in iface delta
gabrielseibel1 Sep 28, 2023
c02eb3a
Fix ConvertAPIHostToNMHost
gabrielseibel1 Sep 28, 2023
b54cbd3
Add swagger doc for host PKA field
gabrielseibel1 Sep 28, 2023
5ca35fe
Fix swagger.yml
gabrielseibel1 Oct 1, 2023
e9ee628
Merge branch 'develop' into net-562
gabrielseibel1 Oct 1, 2023
fcc150a
Set default PKA only for new hosts
gabrielseibel1 Oct 2, 2023
84ccc58
Remove TODO comment
gabrielseibel1 Oct 2, 2023
a51ddeb
Remove redundant check
gabrielseibel1 Oct 2, 2023
4950c03
Merge branch 'develop' into net-562
gabrielseibel1 Oct 2, 2023
016d0f3
Merge branch 'develop' into net-562
gabrielseibel1 Oct 4, 2023
4828648
Have api-host pka be specified in seconds
gabrielseibel1 Oct 5, 2023
33e13da
Merge branch 'develop' into net-562
gabrielseibel1 Oct 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/cmd/host/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
mtu int
isStatic bool
isDefault bool
keepAlive int
)

var hostUpdateCmd = &cobra.Command{
Expand All @@ -43,6 +44,7 @@ var hostUpdateCmd = &cobra.Command{
apiHost.MTU = mtu
apiHost.IsStatic = isStatic
apiHost.IsDefault = isDefault
apiHost.PersistentKeepalive = int32(keepAlive)
}
functions.PrettyPrint(functions.UpdateHost(args[0], apiHost))
},
Expand All @@ -54,6 +56,7 @@ func init() {
hostUpdateCmd.Flags().StringVar(&name, "name", "", "Host name")
hostUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Listen port of the host")
hostUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "Host MTU size")
hostUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval in which packets are sent to keep connections open with peers")
hostUpdateCmd.Flags().BoolVar(&isStatic, "static", false, "Make Host Static ?")
hostUpdateCmd.Flags().BoolVar(&isDefault, "default", false, "Make Host Default ?")
rootCmd.AddCommand(hostUpdateCmd)
Expand Down
1 change: 0 additions & 1 deletion cli/cmd/node/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var (
name string
postUp string
postDown string
keepAlive int
relayedNodes string
egressGatewayRanges string
expirationDateTime int
Expand Down
2 changes: 0 additions & 2 deletions cli/cmd/node/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var nodeUpdateCmd = &cobra.Command{
node.Address = address
node.Address6 = address6
node.LocalAddress = localAddress
node.PersistentKeepalive = int32(keepAlive)
if relayedNodes != "" {
node.RelayedNodes = strings.Split(relayedNodes, ",")
}
Expand All @@ -61,7 +60,6 @@ func init() {
nodeUpdateCmd.Flags().StringVar(&name, "name", "", "Node name")
nodeUpdateCmd.Flags().StringVar(&postUp, "post_up", "", "Commands to run after node is up `;` separated")
nodeUpdateCmd.Flags().StringVar(&postDown, "post_down", "", "Commands to run after node is down `;` separated")
nodeUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval in which packets are sent to keep connections open with peers")
nodeUpdateCmd.Flags().StringVar(&relayedNodes, "relayed_nodes", "", "relayed nodes if node acts as a relay")
nodeUpdateCmd.Flags().StringVar(&egressGatewayRanges, "egress_addrs", "", "Addresses for egressing traffic if node acts as an egress")
nodeUpdateCmd.Flags().IntVar(&expirationDateTime, "expiry", 0, "UNIX timestamp after which node will lose access to the network")
Expand Down
2 changes: 1 addition & 1 deletion controllers/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func migrate(w http.ResponseWriter, r *http.Request) {
host.Name = data.HostName
host.HostPass = data.Password
host.OS = data.OS
host.PersistentKeepalive = time.Duration(legacy.PersistentKeepalive)
if err := logic.CreateHost(&host); err != nil {
slog.Error("create host", "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
Expand Down Expand Up @@ -198,7 +199,6 @@ func convertLegacyNode(legacy models.LegacyNode, hostID uuid.UUID) models.Node {
node.IsRelay = false
node.RelayedNodes = []string{}
node.DNSOn = models.ParseBool(legacy.DNSOn)
node.PersistentKeepalive = time.Duration(int64(time.Second) * int64(legacy.PersistentKeepalive))
node.LastModified = time.Now()
node.ExpirationDateTime = time.Unix(legacy.ExpirationDateTime, 0)
node.EgressGatewayNatEnabled = models.ParseBool(legacy.EgressGatewayNatEnabled)
Expand Down
12 changes: 7 additions & 5 deletions logic/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sort"
"strconv"
"sync"

"github.com/devilcove/httpclient"
"github.com/google/uuid"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
"golang.org/x/crypto/bcrypt"
"net/http"
gabrielseibel1 marked this conversation as resolved.
Show resolved Hide resolved
"sort"
"strconv"
"sync"
)

var (
Expand Down Expand Up @@ -217,6 +216,9 @@ func UpdateHost(newHost, currentHost *models.Host) {
newHost.ListenPort = currentHost.ListenPort
}

if newHost.PersistentKeepalive == 0 {
newHost.PersistentKeepalive = currentHost.PersistentKeepalive
}
}

// UpdateHostFromClient - used for updating host on server with update recieved from client
Expand Down
3 changes: 0 additions & 3 deletions logic/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,6 @@ func SetNodeDefaults(node *models.Node) {
node.DefaultACL = parentNetwork.DefaultACL
}

if node.PersistentKeepalive == 0 {
node.PersistentKeepalive = time.Second * time.Duration(parentNetwork.DefaultKeepalive)
}
node.SetLastModified()
node.SetLastCheckIn()
node.SetDefaultConnected()
Expand Down
4 changes: 2 additions & 2 deletions logic/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N
}
relayPeer := wgtypes.PeerConfig{
PublicKey: relayHost.PublicKey,
PersistentKeepaliveInterval: &relayNode.PersistentKeepalive,
PersistentKeepaliveInterval: &relayHost.PersistentKeepalive,
ReplaceAllowedIPs: true,
AllowedIPs: GetAllowedIPs(&node, &relayNode, nil),
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N
}
peerConfig := wgtypes.PeerConfig{
PublicKey: peerHost.PublicKey,
PersistentKeepaliveInterval: &peer.PersistentKeepalive,
PersistentKeepaliveInterval: &peerHost.PersistentKeepalive, // TODO remove?
ReplaceAllowedIPs: true,
gabrielseibel1 marked this conversation as resolved.
Show resolved Hide resolved
}
if peer.IsEgressGateway {
Expand Down
1 change: 0 additions & 1 deletion logic/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func IfaceDelta(currentNode *models.Node, newNode *models.Node) bool {
newNode.IsEgressGateway != currentNode.IsEgressGateway ||
newNode.IsIngressGateway != currentNode.IsIngressGateway ||
newNode.IsRelay != currentNode.IsRelay ||
newNode.PersistentKeepalive != currentNode.PersistentKeepalive ||
gabrielseibel1 marked this conversation as resolved.
Show resolved Hide resolved
newNode.DNSOn != currentNode.DNSOn ||
newNode.Connected != currentNode.Connected {
return true
Expand Down
23 changes: 23 additions & 0 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrate

import (
"encoding/json"
"time"

"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
Expand All @@ -14,6 +15,7 @@ import (
func Run() {
updateEnrollmentKeys()
assignSuperAdmin()
updateHosts()
}

func assignSuperAdmin() {
Expand Down Expand Up @@ -87,3 +89,24 @@ func updateEnrollmentKeys() {

}
}

func updateHosts() {
rows, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
if err != nil {
logger.Log(0, "failed to fetch database records for hosts")
}
for _, row := range rows {
var host models.Host
if err := json.Unmarshal([]byte(row), &host); err != nil {
logger.Log(0, "failed to unmarshal database row to host", "row", row)
continue
}
if host.PersistentKeepalive == 0 {
host.PersistentKeepalive = 20 * time.Second
if err := logic.UpsertHost(&host); err != nil {
logger.Log(0, "failed to upsert host", host.ID.String())
continue
}
}
}
}
55 changes: 31 additions & 24 deletions models/api_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ package models
import (
"net"
"strings"
"time"
)

// ApiHost - the host struct for API usage
type ApiHost struct {
ID string `json:"id"`
Verbosity int `json:"verbosity"`
FirewallInUse string `json:"firewallinuse"`
Version string `json:"version"`
Name string `json:"name"`
OS string `json:"os"`
Debug bool `json:"debug"`
IsStatic bool `json:"isstatic"`
ListenPort int `json:"listenport"`
WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
MTU int `json:"mtu" yaml:"mtu"`
Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
EndpointIP string `json:"endpointip" yaml:"endpointip"`
PublicKey string `json:"publickey"`
MacAddress string `json:"macaddress"`
Nodes []string `json:"nodes"`
IsDefault bool `json:"isdefault" yaml:"isdefault"`
IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
NatType string `json:"nat_type" yaml:"nat_type"`
ID string `json:"id"`
Verbosity int `json:"verbosity"`
FirewallInUse string `json:"firewallinuse"`
Version string `json:"version"`
Name string `json:"name"`
OS string `json:"os"`
Debug bool `json:"debug"`
IsStatic bool `json:"isstatic"`
ListenPort int `json:"listenport"`
WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
MTU int `json:"mtu" yaml:"mtu"`
Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
EndpointIP string `json:"endpointip" yaml:"endpointip"`
PublicKey string `json:"publickey"`
MacAddress string `json:"macaddress"`
Nodes []string `json:"nodes"`
IsDefault bool `json:"isdefault" yaml:"isdefault"`
IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
NatType string `json:"nat_type" yaml:"nat_type"`
PersistentKeepalive int32 `json:"persistentkeepalive" yaml:"persistentkeepalive"`
}

// Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
Expand Down Expand Up @@ -57,6 +59,7 @@ func (h *Host) ConvertNMHostToAPI() *ApiHost {
a.Version = h.Version
a.IsDefault = h.IsDefault
a.NatType = h.NatType
a.PersistentKeepalive = int32(h.PersistentKeepalive)
return &a
}

Expand Down Expand Up @@ -94,6 +97,10 @@ func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
h.IsDefault = a.IsDefault
h.NatType = currentHost.NatType
h.TurnEndpoint = currentHost.TurnEndpoint

if h.PersistentKeepalive == 0 {
h.PersistentKeepalive = time.Duration(a.PersistentKeepalive)
} else {
h.PersistentKeepalive = currentHost.PersistentKeepalive
}
gabrielseibel1 marked this conversation as resolved.
Show resolved Hide resolved
return &h
}
3 changes: 0 additions & 3 deletions models/api_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type ApiNode struct {
Address6 string `json:"address6" validate:"omitempty,ipv6"`
LocalAddress string `json:"localaddress" validate:"omitempty,ipv4"`
AllowedIPs []string `json:"allowedips"`
PersistentKeepalive int32 `json:"persistentkeepalive"`
LastModified int64 `json:"lastmodified"`
ExpirationDateTime int64 `json:"expdatetime"`
LastCheckIn int64 `json:"lastcheckin"`
Expand Down Expand Up @@ -68,7 +67,6 @@ func (a *ApiNode) ConvertToServerNode(currentNode *Node) *Node {
convertedNode.IngressDNS = a.IngressDns
convertedNode.EgressGatewayRequest = currentNode.EgressGatewayRequest
convertedNode.EgressGatewayNatEnabled = currentNode.EgressGatewayNatEnabled
convertedNode.PersistentKeepalive = time.Second * time.Duration(a.PersistentKeepalive)
convertedNode.RelayedNodes = a.RelayedNodes
convertedNode.DefaultACL = a.DefaultACL
convertedNode.OwnerID = currentNode.OwnerID
Expand Down Expand Up @@ -127,7 +125,6 @@ func (nm *Node) ConvertToAPINode() *ApiNode {
if isEmptyAddr(apiNode.LocalAddress) {
apiNode.LocalAddress = ""
}
apiNode.PersistentKeepalive = int32(nm.PersistentKeepalive.Seconds())
apiNode.LastModified = nm.LastModified.Unix()
apiNode.LastCheckIn = nm.LastCheckIn.Unix()
apiNode.LastPeerUpdate = nm.LastPeerUpdate.Unix()
Expand Down
63 changes: 32 additions & 31 deletions models/host.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package models

import (
"net"
"net/netip"

"github.com/google/uuid"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net"
"net/netip"
"time"
)

// OS_Types - list of OS types Netmaker cares about
Expand Down Expand Up @@ -37,34 +37,35 @@ const WIREGUARD_INTERFACE = "netmaker"

// Host - represents a host on the network
type Host struct {
ID uuid.UUID `json:"id" yaml:"id"`
Verbosity int `json:"verbosity" yaml:"verbosity"`
FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"`
Version string `json:"version" yaml:"version"`
IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"`
DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"`
AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
HostPass string `json:"hostpass" yaml:"hostpass"`
Name string `json:"name" yaml:"name"`
OS string `json:"os" yaml:"os"`
Interface string `json:"interface" yaml:"interface"`
Debug bool `json:"debug" yaml:"debug"`
ListenPort int `json:"listenport" yaml:"listenport"`
WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
MTU int `json:"mtu" yaml:"mtu"`
PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
Nodes []string `json:"nodes" yaml:"nodes"`
Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
IsDocker bool `json:"isdocker" yaml:"isdocker"`
IsK8S bool `json:"isk8s" yaml:"isk8s"`
IsStatic bool `json:"isstatic" yaml:"isstatic"`
IsDefault bool `json:"isdefault" yaml:"isdefault"`
NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
ID uuid.UUID `json:"id" yaml:"id"`
Verbosity int `json:"verbosity" yaml:"verbosity"`
FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"`
Version string `json:"version" yaml:"version"`
IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"`
DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"`
AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
HostPass string `json:"hostpass" yaml:"hostpass"`
Name string `json:"name" yaml:"name"`
OS string `json:"os" yaml:"os"`
Interface string `json:"interface" yaml:"interface"`
Debug bool `json:"debug" yaml:"debug"`
ListenPort int `json:"listenport" yaml:"listenport"`
WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
MTU int `json:"mtu" yaml:"mtu"`
PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
Nodes []string `json:"nodes" yaml:"nodes"`
Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
IsDocker bool `json:"isdocker" yaml:"isdocker"`
IsK8S bool `json:"isk8s" yaml:"isk8s"`
IsStatic bool `json:"isstatic" yaml:"isstatic"`
IsDefault bool `json:"isdefault" yaml:"isdefault"`
NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
PersistentKeepalive time.Duration `json:"persistentkeepalive" yaml:"persistentkeepalive"`
}

// FormatBool converts a boolean to a [yes|no] string
Expand Down
Loading