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-812: Deprecate Setting /etc/hosts entries #2733

Merged
merged 10 commits into from
Dec 20, 2023
1 change: 1 addition & 0 deletions compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ services:
- "443:443"

coredns:
#network_mode: host
container_name: coredns
image: coredns/coredns:1.10.1
command: -conf /root/dnsconfig/Corefile
Expand Down
56 changes: 23 additions & 33 deletions controllers/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

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

Expand All @@ -10,7 +11,6 @@ import (
"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"
)

Expand Down Expand Up @@ -170,24 +170,17 @@ func createDNS(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
if servercfg.IsDNSMode() {
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
}

logger.Log(1, "new DNS record added:", entry.Name)
if servercfg.IsMessageQueueBackend() {
go func() {
if err = mq.PublishPeerUpdate(); err != nil {
logger.Log(0, "failed to publish peer update after ACL update on", entry.Network)
}
if err := mq.PublishCustomDNS(&entry); err != nil {
logger.Log(0, "error publishing custom dns", err.Error())
}
}()
}
logger.Log(2, r.Header.Get("user"),
fmt.Sprintf("DNS entry is set: %+v", entry))
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -221,23 +214,17 @@ func deleteDNS(w http.ResponseWriter, r *http.Request) {
return
}
logger.Log(1, "deleted dns entry: ", entrytext)
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
if servercfg.IsDNSMode() {
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
}

json.NewEncoder(w).Encode(entrytext + " deleted.")
go func() {
dns := models.DNSUpdate{
Action: models.DNSDeleteByName,
Name: entrytext,
}
if err := mq.PublishDNSUpdate(params["network"], dns); err != nil {
logger.Log(0, "failed to publish dns update", err.Error())
}
}()

}

Expand Down Expand Up @@ -271,7 +258,10 @@ func GetDNSEntry(domain string, network string) (models.DNSEntry, error) {
func pushDNS(w http.ResponseWriter, r *http.Request) {
// Set header
w.Header().Set("Content-Type", "application/json")

if !servercfg.IsDNSMode() {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("DNS Mode is set to off"), "badrequest"))
return
}
err := logic.SetDNS()

if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions controllers/ext_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/servercfg"

"github.com/gravitl/netmaker/models"

Expand Down Expand Up @@ -426,8 +427,8 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
if err := mq.PublishPeerUpdate(); err != nil {
logger.Log(1, "error setting ext peers on "+nodeid+": "+err.Error())
}
if err := mq.PublishExtClientDNS(&extclient); err != nil {
logger.Log(1, "error publishing extclient dns", err.Error())
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}()
}
Expand Down Expand Up @@ -522,8 +523,8 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(newclient)
if changedID {
go func() {
if err := mq.PublishExtClientDNSUpdate(oldExtClient, newclient, oldExtClient.Network); err != nil {
logger.Log(1, "error pubishing dns update for extcient update", err.Error())
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}()
}
Expand Down Expand Up @@ -583,8 +584,8 @@ func deleteExtClient(w http.ResponseWriter, r *http.Request) {
if err := mq.PublishDeletedClientPeerUpdate(&extclient); err != nil {
logger.Log(1, "error setting ext peers on "+ingressnode.ID.String()+": "+err.Error())
}
if err = mq.PublishDeleteExtClientDNS(&extclient); err != nil {
logger.Log(1, "error publishing dns update for extclient deletion", err.Error())
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}()

Expand Down
29 changes: 17 additions & 12 deletions controllers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,8 @@ func updateHost(w http.ResponseWriter, r *http.Request) {
logger.Log(0, "fail to publish peer update: ", err.Error())
}
if newHost.Name != currHost.Name {
networks := logic.GetHostNetworks(currHost.ID.String())
if err := mq.PublishHostDNSUpdate(currHost, newHost, networks); err != nil {
var dnsError *models.DNSError
if errors.Is(err, dnsError) {
for _, message := range err.(models.DNSError).ErrorStrings {
logger.Log(0, message)
}
} else {
logger.Log(0, err.Error())
}
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}
}()
Expand Down Expand Up @@ -252,6 +244,12 @@ func deleteHost(w http.ResponseWriter, r *http.Request) {
go mq.PublishMqUpdatesForDeletedNode(node, false, gwClients)

}
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
// delete EMQX credentials for host
if err := mq.DeleteEmqxUser(currHost.ID.String()); err != nil {
slog.Error("failed to remove host credentials from EMQX", "id", currHost.ID, "error", err)
}
}
if err = logic.RemoveHost(currHost, forceDelete); err != nil {
logger.Log(0, r.Header.Get("user"), "failed to delete a host:", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
Expand Down Expand Up @@ -311,7 +309,9 @@ func addHostToNetwork(w http.ResponseWriter, r *http.Request) {
Node: *newNode,
})
mq.PublishPeerUpdate()
mq.HandleNewNodeDNS(currHost, newNode)
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}()
logger.Log(2, r.Header.Get("user"), fmt.Sprintf("added host %s to network %s", currHost.Name, network))
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -396,7 +396,12 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("failed to delete node"), "internal"))
return
}
go mq.PublishMqUpdatesForDeletedNode(*node, true, gwClients)
go func() {
mq.PublishMqUpdatesForDeletedNode(*node, true, gwClients)
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}()
logger.Log(2, r.Header.Get("user"), fmt.Sprintf("removed host %s from network %s", currHost.Name, network))
w.WriteHeader(http.StatusOK)
}
Expand Down
13 changes: 6 additions & 7 deletions controllers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
if err := mq.NodeUpdate(&node); err != nil {
slog.Error("error publishing node update to node", "node", node.ID, "error", err)
}
mq.PublishDeleteAllExtclientsDNS(node.Network, removedClients)
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}()
}
}
Expand Down Expand Up @@ -635,7 +637,7 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
}
newNode := newData.ConvertToServerNode(&currentNode)
relayUpdate := logic.RelayUpdates(&currentNode, newNode)
host, err := logic.GetHost(newNode.HostID.String())
_, err = logic.GetHost(newNode.HostID.String())
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("failed to get host for node [ %s ] info: %v", nodeid, err))
Expand All @@ -655,9 +657,6 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
if relayUpdate {
logic.UpdateRelayed(&currentNode, newNode)
}
if servercfg.IsDNSMode() {
logic.SetDNS()
}

apiNode := newNode.ConvertToAPINode()
logger.Log(1, r.Header.Get("user"), "updated node", currentNode.ID.String(), "on network", currentNode.Network)
Expand All @@ -672,8 +671,8 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
logger.Log(0, "error during node ACL update for node", newNode.ID.String())
}
}
if err := mq.PublishReplaceDNS(&currentNode, newNode, host); err != nil {
logger.Log(1, "failed to publish dns update", err.Error())
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}(aclUpdate, relayUpdate, newNode)
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ func deleteUser(w http.ResponseWriter, r *http.Request) {
}
}
}
if servercfg.IsDNSMode() {
logic.SetDNS()
}
}()
logger.Log(1, username, "was deleted")
json.NewEncoder(w).Encode(params["username"] + " deleted.")
Expand Down
31 changes: 29 additions & 2 deletions logic/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
"github.com/txn2/txeh"
)

Expand All @@ -36,6 +37,10 @@ func SetDNS() error {
hostfile.AddHost(entry.Address, entry.Name)
}
}
dns := GetExtclientDNS()
for _, entry := range dns {
hostfile.AddHost(entry.Address, entry.Name)
}
if corefilestring == "" {
corefilestring = "example.com"
}
Expand Down Expand Up @@ -69,6 +74,28 @@ func GetDNS(network string) ([]models.DNSEntry, error) {
return dns, nil
}

// GetExtclientDNS - gets all extclients dns entries
func GetExtclientDNS() []models.DNSEntry {
extclients, err := GetAllExtClients()
if err != nil {
return []models.DNSEntry{}
}
var dns []models.DNSEntry
for _, extclient := range extclients {
var entry = models.DNSEntry{}
entry.Name = fmt.Sprintf("%s.%s", extclient.ClientID, extclient.Network)
entry.Network = extclient.Network
if extclient.Address != "" {
entry.Address = extclient.Address
}
if extclient.Address6 != "" {
entry.Address6 = extclient.Address6
}
dns = append(dns, entry)
}
return dns
}

// GetNodeDNS - gets the DNS of a network node
func GetNodeDNS(network string) ([]models.DNSEntry, error) {

Expand Down Expand Up @@ -142,6 +169,7 @@ func SetCorefile(domains string) error {
}

corefile := domains + ` {
bind %s
reload 15s
hosts /root/dnsconfig/netmaker.hosts {
fallthrough
Expand All @@ -150,8 +178,7 @@ func SetCorefile(domains string) error {
log
}
`
corebytes := []byte(corefile)

corebytes := []byte(fmt.Sprintf(corefile, servercfg.GetCoreDNSAddr()))
err = os.WriteFile(dir+"/config/dnsconfig/Corefile", corebytes, 0644)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions logic/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ func RemoveHost(h *models.Host, forceDelete bool) error {
if servercfg.CacheEnabled() {
deleteHostFromCache(h.ID.String())
}
go func() {
if servercfg.IsDNSMode() {
SetDNS()
}
}()

return nil
}
Expand Down
10 changes: 0 additions & 10 deletions mq/emqx.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,6 @@ func CreateHostACL(hostID, serverName string) error {
Permission: "allow",
Action: "all",
},
{
Topic: fmt.Sprintf("dns/all/%s/%s", hostID, serverName),
Permission: "allow",
Action: "all",
},
{
Topic: fmt.Sprintf("dns/update/%s/%s", hostID, serverName),
Permission: "allow",
Action: "all",
},
{
Topic: fmt.Sprintf("host/serverupdate/%s/%s", serverName, hostID),
Permission: "allow",
Expand Down
Loading
Loading