Skip to content

Commit

Permalink
Merge branch 'develop' into NET-551
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek9686 authored Aug 24, 2023
2 parents ed5d022 + d2b8daa commit 93dfd80
Show file tree
Hide file tree
Showing 22 changed files with 806 additions and 87 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ body:
label: Version
description: What version are you running?
options:
- v0.21.0
- v0.20.6
- v0.20.5
- v0.20.4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<p align="center">
<a href="https://github.com/gravitl/netmaker/releases">
<img src="https://img.shields.io/badge/Version-0.20.6-informational?style=flat-square" />
<img src="https://img.shields.io/badge/Version-0.21.0-informational?style=flat-square" />
</a>
<a href="https://hub.docker.com/r/gravitl/netmaker/tags">
<img src="https://img.shields.io/docker/pulls/gravitl/netmaker?label=downloads" />
Expand Down
2 changes: 1 addition & 1 deletion compose/docker-compose.netclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.4"
services:
netclient:
container_name: netclient
image: 'gravitl/netclient:v0.20.6'
image: 'gravitl/netclient:v0.21.0'
hostname: netmaker-1
network_mode: host
restart: on-failure
Expand Down
2 changes: 1 addition & 1 deletion compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ services:

coredns:
container_name: coredns
image: coredns/coredns
image: coredns/coredns:1.10.1
command: -conf /root/dnsconfig/Corefile
env_file: ./netmaker.env
depends_on:
Expand Down
13 changes: 13 additions & 0 deletions controllers/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ func TestValidateDNSCreate(t *testing.T) {
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'name_unique' tag")
})
t.Run("WhiteSpace", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.10.10.5", Name: "white space", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'whitespace' tag")
})
t.Run("AllSpaces", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.10.10.5", Name: " ", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'whitespace' tag")
})

}

func createHost() {
Expand Down
2 changes: 1 addition & 1 deletion controllers/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// Schemes: https
// BasePath: /
// Version: 0.20.6
// Version: 0.21.0
// Host: netmaker.io
//
// Consumes:
Expand Down
211 changes: 168 additions & 43 deletions controllers/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package controller

import (
"encoding/json"
"fmt"
"net"
"net/http"
"time"

"github.com/gravitl/netmaker/auth"
"github.com/google/uuid"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mq"
"github.com/gravitl/netmaker/servercfg"
"golang.org/x/crypto/bcrypt"
"golang.org/x/exp/slog"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

// swagger:route PUT /api/v1/nodes/migrate nodes migrateNode
Expand All @@ -26,63 +32,182 @@ import (
// 200: nodeJoinResponse
func migrate(w http.ResponseWriter, r *http.Request) {
data := models.MigrationData{}
host := models.Host{}
node := models.Node{}
nodes := []models.Node{}
server := models.ServerConfig{}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}

var networksToAdd = []string{}
for i := range data.LegacyNodes {
legacyNode := data.LegacyNodes[i]
record, err := database.FetchRecord(database.NODES_TABLE_NAME, legacyNode.ID)
for i, legacy := range data.LegacyNodes {
record, err := database.FetchRecord(database.NODES_TABLE_NAME, legacy.ID)
if err != nil {
logger.Log(0, "no record for legacy node", legacyNode.ID, err.Error())
continue
} else {
var oldLegacyNode models.LegacyNode
if err = json.Unmarshal([]byte(record), &oldLegacyNode); err != nil {
logger.Log(0, "error decoding legacy node", err.Error())
slog.Error("legacy node not found", "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("legacy node not found %w", err), "badrequest"))
return
}
var legacyNode models.LegacyNode
if err = json.Unmarshal([]byte(record), &legacyNode); err != nil {
slog.Error("decoding legacy node", "errror", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("decode legacy node %w", err), "badrequest"))
return
}
if err := bcrypt.CompareHashAndPassword([]byte(legacyNode.Password), []byte(legacy.Password)); err != nil {
slog.Error("legacy node invalid password", "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("invalid password %w", err), "unauthorized"))
return
}
if i == 0 {
host, node = convertLegacyHostNode(legacy)
host.Name = data.HostName
host.HostPass = data.Password
host.OS = data.OS
if err := logic.CreateHost(&host); err != nil {
slog.Error("create host", "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
continue
return
}
server = servercfg.GetServerInfo()
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
server.MQUserName = host.ID.String()
}
if err := bcrypt.CompareHashAndPassword([]byte(oldLegacyNode.Password), []byte(legacyNode.Password)); err != nil {
logger.Log(0, "error decoding legacy password", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "unauthorized"))
continue
key, keyErr := logic.RetrievePublicTrafficKey()
if keyErr != nil {
slog.Error("retrieving traffickey", "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
networksToAdd = append(networksToAdd, oldLegacyNode.Network)
_ = database.DeleteRecord(database.NODES_TABLE_NAME, oldLegacyNode.ID)
server.TrafficKey = key
} else {
node = convertLegacyNode(legacyNode, host.ID)
}
}
if len(networksToAdd) == 0 {
logger.Log(0, "no valid networks to migrate for host", data.NewHost.Name)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "unauthorized"))
return
}
if !logic.HostExists(&data.NewHost) {
logic.CheckHostPorts(&data.NewHost)
if err = logic.CreateHost(&data.NewHost); err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
if err := logic.UpsertNode(&node); err != nil {
slog.Error("update node", "error", err)
continue
}
host.Nodes = append(host.Nodes, node.ID.String())

nodes = append(nodes, node)
}
key, keyErr := logic.RetrievePublicTrafficKey()
if keyErr != nil {
logger.Log(0, "error retrieving key:", keyErr.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
if err := logic.UpsertHost(&host); err != nil {
slog.Error("save host", "error", err)
}
server := servercfg.GetServerInfo()
server.TrafficKey = key
response := models.RegisterResponse{
ServerConf: server,
RequestedHost: data.NewHost,
go mq.PublishPeerUpdate()
response := models.HostPull{
Host: host,
Nodes: nodes,
ServerConfig: server,
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(&response)
logger.Log(0, "successfully migrated host", data.NewHost.Name, data.NewHost.ID.String())
// notify host of changes, peer and node updates
go auth.CheckNetRegAndHostUpdate(networksToAdd, &data.NewHost)

slog.Info("migrated nodes")
// check for gateways
for _, node := range data.LegacyNodes {
if node.IsEgressGateway == "yes" {
egressGateway := models.EgressGatewayRequest{
NodeID: node.ID,
Ranges: node.EgressGatewayRanges,
NatEnabled: node.EgressGatewayNatEnabled,
}
if _, err := logic.CreateEgressGateway(egressGateway); err != nil {
logger.Log(0, "error creating egress gateway for node", node.ID, err.Error())
}
}
if node.IsIngressGateway == "yes" {
ingressGateway := models.IngressRequest{}
ingressNode, err := logic.CreateIngressGateway(node.Network, node.ID, ingressGateway)
if err != nil {
logger.Log(0, "error creating ingress gateway for node", node.ID, err.Error())
}
runUpdates(&ingressNode, true)
}
}
}

func convertLegacyHostNode(legacy models.LegacyNode) (models.Host, models.Node) {
//convert host
host := models.Host{}
host.ID = uuid.New()
host.IPForwarding = models.ParseBool(legacy.IPForwarding)
host.AutoUpdate = servercfg.AutoUpdateEnabled()
host.Interface = "netmaker"
host.ListenPort = int(legacy.ListenPort)
host.MTU = int(legacy.MTU)
host.PublicKey, _ = wgtypes.ParseKey(legacy.PublicKey)
host.MacAddress = net.HardwareAddr(legacy.MacAddress)
host.TrafficKeyPublic = legacy.TrafficKeys.Mine
host.Nodes = append([]string{}, legacy.ID)
host.Interfaces = legacy.Interfaces
//host.DefaultInterface = legacy.Defaul
host.EndpointIP = net.ParseIP(legacy.Endpoint)
host.IsDocker = models.ParseBool(legacy.IsDocker)
host.IsK8S = models.ParseBool(legacy.IsK8S)
host.IsStatic = models.ParseBool(legacy.IsStatic)
node := convertLegacyNode(legacy, host.ID)
return host, node
}

func convertLegacyNode(legacy models.LegacyNode, hostID uuid.UUID) models.Node {
//convert node
node := models.Node{}
node.ID, _ = uuid.Parse(legacy.ID)
node.HostID = hostID
node.Network = legacy.Network
valid4 := true
valid6 := true
_, cidr4, err := net.ParseCIDR(legacy.NetworkSettings.AddressRange)
if err != nil {
valid4 = false
slog.Warn("parsing address range", "error", err)
} else {
node.NetworkRange = *cidr4
}
_, cidr6, err := net.ParseCIDR(legacy.NetworkSettings.AddressRange6)
if err != nil {
valid6 = false
slog.Warn("parsing address range6", "error", err)
} else {
node.NetworkRange6 = *cidr6
}
node.Server = servercfg.GetServer()
node.Connected = models.ParseBool(legacy.Connected)
if valid4 {
node.Address = net.IPNet{
IP: net.ParseIP(legacy.Address),
Mask: cidr4.Mask,
}
}
if valid6 {
node.Address6 = net.IPNet{
IP: net.ParseIP(legacy.Address6),
Mask: cidr6.Mask,
}
}
node.Action = models.NODE_NOOP
node.LocalAddress = net.IPNet{
IP: net.ParseIP(legacy.LocalAddress),
}
node.IsEgressGateway = models.ParseBool(legacy.IsEgressGateway)
node.EgressGatewayRanges = legacy.EgressGatewayRanges
node.IsIngressGateway = models.ParseBool(legacy.IsIngressGateway)
node.IsRelayed = false
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)
node.EgressGatewayRequest = legacy.EgressGatewayRequest
node.IngressGatewayRange = legacy.IngressGatewayRange
node.IngressGatewayRange6 = legacy.IngressGatewayRange6
node.DefaultACL = legacy.DefaultACL
node.OwnerID = legacy.OwnerID
node.FailoverNode, _ = uuid.Parse(legacy.FailoverNode)
node.Failover = models.ParseBool(legacy.Failover)
return node
}
16 changes: 8 additions & 8 deletions ee/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ var errValidation = fmt.Errorf(license_validation_err_msg)
type LicenseKey struct {
LicenseValue string `json:"license_value"` // actual (public) key and the unique value for the key
Expiration int64 `json:"expiration"`
UsageServers int `json:"usage_servers"`
UsageUsers int `json:"usage_users"`
UsageClients int `json:"usage_clients"`
UsageHosts int `json:"usage_hosts"`
UsageNetworks int `json:"usage_networks"`
UsageIngresses int `json:"usage_ingresses"`
UsageEgresses int `json:"usage_egresses"`
UsageServers int `json:"limit_servers"`
UsageUsers int `json:"limit_users"`
UsageClients int `json:"limit_clients"`
UsageHosts int `json:"limit_hosts"`
UsageNetworks int `json:"limit_networks"`
UsageIngresses int `json:"limit_ingresses"`
UsageEgresses int `json:"limit_egresses"`
Metadata string `json:"metadata"`
IsActive bool `json:"is_active"` // yes if active
}
Expand All @@ -46,7 +46,7 @@ type ValidatedLicense struct {
// LicenseSecret - the encrypted struct for sending user-id
type LicenseSecret struct {
AssociatedID string `json:"associated_id" binding:"required"` // UUID for user foreign key to User table
Usage Usage `json:"usage" binding:"required"`
Usage Usage `json:"limits" binding:"required"`
}

// Usage - struct for license usage
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.19

require (
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/go-playground/validator/v10 v10.15.0
github.com/go-playground/validator/v10 v10.15.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/lib/pq v1.10.9
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.15.0 h1:nDU5XeOKtB3GEa+uB7GNYwhVKsgjAR7VgKoNB6ryXfw=
github.com/go-playground/validator/v10 v10.15.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-playground/validator/v10 v10.15.1 h1:BSe8uhN+xQ4r5guV/ywQI4gO59C2raYcGffYWZEjZzM=
github.com/go-playground/validator/v10 v10.15.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand All @@ -43,8 +43,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
Expand Down
2 changes: 1 addition & 1 deletion k8s/client/netclient-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
hostNetwork: true
containers:
- name: netclient
image: gravitl/netclient:v0.20.6
image: gravitl/netclient:v0.21.0
env:
- name: TOKEN
value: "TOKEN_VALUE"
Expand Down
2 changes: 1 addition & 1 deletion k8s/client/netclient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
# - "<node label value>"
containers:
- name: netclient
image: gravitl/netclient:v0.20.6
image: gravitl/netclient:v0.21.0
env:
- name: TOKEN
value: "TOKEN_VALUE"
Expand Down
Loading

0 comments on commit 93dfd80

Please sign in to comment.