mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-05 20:54:18 +08:00
add nameservers to peer update
This commit is contained in:
parent
dcd7fe72dd
commit
1b563b9483
9 changed files with 63 additions and 9 deletions
|
@ -43,7 +43,7 @@ func dnsHandlers(r *mux.Router) {
|
||||||
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(createNs))).Methods(http.MethodPost)
|
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(createNs))).Methods(http.MethodPost)
|
||||||
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(listNs))).Methods(http.MethodGet)
|
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(listNs))).Methods(http.MethodGet)
|
||||||
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(updateNs))).Methods(http.MethodPut)
|
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(updateNs))).Methods(http.MethodPut)
|
||||||
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(deleteEgress))).Methods(http.MethodDelete)
|
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(deleteNs))).Methods(http.MethodDelete)
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Create Nameserver
|
// @Summary Create Nameserver
|
||||||
|
@ -79,7 +79,7 @@ func createNs(w http.ResponseWriter, r *http.Request) {
|
||||||
ns := schema.Nameserver{
|
ns := schema.Nameserver{
|
||||||
ID: uuid.New().String(),
|
ID: uuid.New().String(),
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
Network: req.Network,
|
NetworkID: req.Network,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
MatchDomain: req.MatchDomain,
|
MatchDomain: req.MatchDomain,
|
||||||
Servers: req.Servers,
|
Servers: req.Servers,
|
||||||
|
@ -111,7 +111,7 @@ func createNs(w http.ResponseWriter, r *http.Request) {
|
||||||
Name: ns.Name,
|
Name: ns.Name,
|
||||||
Type: models.NameserverSub,
|
Type: models.NameserverSub,
|
||||||
},
|
},
|
||||||
NetworkID: models.NetworkID(ns.Network),
|
NetworkID: models.NetworkID(ns.NetworkID),
|
||||||
Origin: models.Dashboard,
|
Origin: models.Dashboard,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -135,13 +135,13 @@ func listNs(w http.ResponseWriter, r *http.Request) {
|
||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("network is required"), "badrequest"))
|
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("network is required"), "badrequest"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ns := schema.Nameserver{Network: network}
|
ns := schema.Nameserver{NetworkID: network}
|
||||||
list, err := ns.ListByNetwork(db.WithContext(r.Context()))
|
list, err := ns.ListByNetwork(db.WithContext(r.Context()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logic.ReturnErrorResponse(
|
logic.ReturnErrorResponse(
|
||||||
w,
|
w,
|
||||||
r,
|
r,
|
||||||
logic.FormatError(errors.New("error listing egress resource"+err.Error()), "internal"),
|
logic.FormatError(errors.New("error listing nameservers "+err.Error()), "internal"),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ func updateNs(w http.ResponseWriter, r *http.Request) {
|
||||||
Old: ns,
|
Old: ns,
|
||||||
New: updateNs,
|
New: updateNs,
|
||||||
},
|
},
|
||||||
NetworkID: models.NetworkID(ns.Network),
|
NetworkID: models.NetworkID(ns.NetworkID),
|
||||||
Origin: models.Dashboard,
|
Origin: models.Dashboard,
|
||||||
}
|
}
|
||||||
ns.Servers = updateNs.Servers
|
ns.Servers = updateNs.Servers
|
||||||
|
@ -270,7 +270,7 @@ func deleteNs(w http.ResponseWriter, r *http.Request) {
|
||||||
Name: ns.Name,
|
Name: ns.Name,
|
||||||
Type: models.NameserverSub,
|
Type: models.NameserverSub,
|
||||||
},
|
},
|
||||||
NetworkID: models.NetworkID(ns.Network),
|
NetworkID: models.NetworkID(ns.NetworkID),
|
||||||
Origin: models.Dashboard,
|
Origin: models.Dashboard,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -245,6 +245,7 @@ func pull(w http.ResponseWriter, r *http.Request) {
|
||||||
DefaultGwIp: hPU.DefaultGwIp,
|
DefaultGwIp: hPU.DefaultGwIp,
|
||||||
IsInternetGw: hPU.IsInternetGw,
|
IsInternetGw: hPU.IsInternetGw,
|
||||||
EndpointDetection: logic.IsEndpointDetectionEnabled(),
|
EndpointDetection: logic.IsEndpointDetectionEnabled(),
|
||||||
|
DnsNameservers: hPU.DnsNameservers,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log(1, hostID, "completed a pull")
|
logger.Log(1, hostID, "completed a pull")
|
||||||
|
|
43
logic/dns.go
43
logic/dns.go
|
@ -1,6 +1,7 @@
|
||||||
package logic
|
package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -11,6 +12,7 @@ import (
|
||||||
|
|
||||||
validator "github.com/go-playground/validator/v10"
|
validator "github.com/go-playground/validator/v10"
|
||||||
"github.com/gravitl/netmaker/database"
|
"github.com/gravitl/netmaker/database"
|
||||||
|
"github.com/gravitl/netmaker/db"
|
||||||
"github.com/gravitl/netmaker/logger"
|
"github.com/gravitl/netmaker/logger"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
"github.com/gravitl/netmaker/schema"
|
"github.com/gravitl/netmaker/schema"
|
||||||
|
@ -332,6 +334,9 @@ func ValidateNameserverReq(ns models.NameserverReq) error {
|
||||||
if ns.Name == "" {
|
if ns.Name == "" {
|
||||||
return errors.New("name is required")
|
return errors.New("name is required")
|
||||||
}
|
}
|
||||||
|
if ns.Network == "" {
|
||||||
|
return errors.New("network is required")
|
||||||
|
}
|
||||||
if len(ns.Servers) == 0 {
|
if len(ns.Servers) == 0 {
|
||||||
return errors.New("atleast one nameserver should be specified")
|
return errors.New("atleast one nameserver should be specified")
|
||||||
}
|
}
|
||||||
|
@ -354,6 +359,44 @@ func ValidateUpdateNameserverReq(updateNs schema.Nameserver) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetNameserversForHost(h *models.Host) (returnNsLi []models.Nameserver) {
|
||||||
|
if h.DNS != "yes" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, nodeID := range h.Nodes {
|
||||||
|
node, err := GetNodeByID(nodeID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ns := &schema.Nameserver{
|
||||||
|
NetworkID: node.Network,
|
||||||
|
}
|
||||||
|
nsLi, _ := ns.ListByNetwork(db.WithContext(context.TODO()))
|
||||||
|
for _, nsI := range nsLi {
|
||||||
|
if !nsI.Status {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, all := nsI.Tags["*"]
|
||||||
|
if all {
|
||||||
|
returnNsLi = append(returnNsLi, models.Nameserver{
|
||||||
|
IPs: ns.Servers,
|
||||||
|
MatchDomain: ns.MatchDomain,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for tagI := range node.Tags {
|
||||||
|
if _, ok := nsI.Tags[tagI.String()]; ok {
|
||||||
|
returnNsLi = append(returnNsLi, models.Nameserver{
|
||||||
|
IPs: ns.Servers,
|
||||||
|
MatchDomain: ns.MatchDomain,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// IsValidMatchDomain reports whether s is a valid "match domain".
|
// IsValidMatchDomain reports whether s is a valid "match domain".
|
||||||
// Rules (simple/ASCII):
|
// Rules (simple/ASCII):
|
||||||
// - "~." is allowed (match all).
|
// - "~." is allowed (match all).
|
||||||
|
|
|
@ -142,6 +142,7 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N
|
||||||
NodePeers: []wgtypes.PeerConfig{},
|
NodePeers: []wgtypes.PeerConfig{},
|
||||||
HostNetworkInfo: models.HostInfoMap{},
|
HostNetworkInfo: models.HostInfoMap{},
|
||||||
ServerConfig: GetServerInfo(),
|
ServerConfig: GetServerInfo(),
|
||||||
|
DnsNameservers: GetNameserversForHost(host),
|
||||||
}
|
}
|
||||||
if host.DNS == "no" {
|
if host.DNS == "no" {
|
||||||
hostPeerUpdate.ManageDNS = false
|
hostPeerUpdate.ManageDNS = false
|
||||||
|
|
|
@ -28,10 +28,16 @@ type HostPeerUpdate struct {
|
||||||
FwUpdate FwUpdate `json:"fw_update"`
|
FwUpdate FwUpdate `json:"fw_update"`
|
||||||
ReplacePeers bool `json:"replace_peers"`
|
ReplacePeers bool `json:"replace_peers"`
|
||||||
NameServers []string `json:"name_servers"`
|
NameServers []string `json:"name_servers"`
|
||||||
|
DnsNameservers []Nameserver `json:"dns_nameservers"`
|
||||||
ServerConfig
|
ServerConfig
|
||||||
OldPeerUpdateFields
|
OldPeerUpdateFields
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Nameserver struct {
|
||||||
|
IPs []string `json:"ips"`
|
||||||
|
MatchDomain string `json:"match_domain"`
|
||||||
|
}
|
||||||
|
|
||||||
type OldPeerUpdateFields struct {
|
type OldPeerUpdateFields struct {
|
||||||
NodePeers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
|
NodePeers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
|
||||||
OldPeers []wgtypes.PeerConfig `json:"Peers"`
|
OldPeers []wgtypes.PeerConfig `json:"Peers"`
|
||||||
|
|
|
@ -254,6 +254,7 @@ type HostPull struct {
|
||||||
DefaultGwIp net.IP `json:"default_gw_ip"`
|
DefaultGwIp net.IP `json:"default_gw_ip"`
|
||||||
IsInternetGw bool `json:"is_inet_gw"`
|
IsInternetGw bool `json:"is_inet_gw"`
|
||||||
EndpointDetection bool `json:"endpoint_detection"`
|
EndpointDetection bool `json:"endpoint_detection"`
|
||||||
|
DnsNameservers []Nameserver `json:"dns_nameservers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultGwInfo struct {
|
type DefaultGwInfo struct {
|
||||||
|
|
|
@ -113,6 +113,7 @@ func PublishSingleHostPeerUpdate(host *models.Host, allNodes []models.Node, dele
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, nodeID := range host.Nodes {
|
for _, nodeID := range host.Nodes {
|
||||||
|
|
||||||
node, err := logic.GetNodeByID(nodeID)
|
node, err := logic.GetNodeByID(nodeID)
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
type Nameserver struct {
|
type Nameserver struct {
|
||||||
ID string `gorm:"primaryKey" json:"id"`
|
ID string `gorm:"primaryKey" json:"id"`
|
||||||
Name string `gorm:"name" json:"name"`
|
Name string `gorm:"name" json:"name"`
|
||||||
Network string `gorm:"network" json:"network"`
|
NetworkID string `gorm:"network_id" json:"network_id"`
|
||||||
Description string `gorm:"description" json:"description"`
|
Description string `gorm:"description" json:"description"`
|
||||||
Servers datatypes.JSONSlice[string] `gorm:"servers" json:"servers"`
|
Servers datatypes.JSONSlice[string] `gorm:"servers" json:"servers"`
|
||||||
MatchDomain string `gorm:"match_domain" json:"match_domain"`
|
MatchDomain string `gorm:"match_domain" json:"match_domain"`
|
||||||
|
@ -35,7 +35,7 @@ func (ns *Nameserver) Create(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ns *Nameserver) ListByNetwork(ctx context.Context) (dnsli []Nameserver, err error) {
|
func (ns *Nameserver) ListByNetwork(ctx context.Context) (dnsli []Nameserver, err error) {
|
||||||
err = db.FromContext(ctx).Model(&Nameserver{}).Where("network_id = ?", ns.Network).Find(&dnsli).Error
|
err = db.FromContext(ctx).Model(&Nameserver{}).Where("network_id = ?", ns.NetworkID).Find(&dnsli).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,5 +7,6 @@ func ListModels() []interface{} {
|
||||||
&Egress{},
|
&Egress{},
|
||||||
&UserAccessToken{},
|
&UserAccessToken{},
|
||||||
&Event{},
|
&Event{},
|
||||||
|
&Nameserver{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue