netmaker/logic/dns.go
Matthew R Kasun 74778b6757
Net 503 - use NewHosts to init hostfile (#2473)
* use NewHosts to init hostfile

* Revert "Bump github.com/txn2/txeh from 1.4.0 to 1.5.3 (#2464)" (#2475)

This reverts commit aafaa91af0.

* NET-433: Only Set Routes for NAT enabled egress GW (#2469)

* remove related fields and code

* remover metrics collection from server code

* fw update struct

* add ext client flag to metrics data

* simply nat types

* rm proxy update from cli

* remove ingress routes from firewall update

* check if egress ranges are present

* rm unused func

* ignore network parameter passed to delete ingress endpoint (#2471)

* fix(NET-388): fix to update node dns toggle (#2472)

* use local port for metrics pinger (#2479)

* v0.20.5 release notes (#2482)

* v0.20.5 release notes

* add a known issue

* update develop to v0.20.6 (#2485)

* enforce unique names for ext client names (#2476)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* prune(NET-483): remove defunct host.internetgateway field (#2487)

* don't reference host on err (#2493)

* deprecrate netclient install scripts (#2490)

* Net 500: validate network parameter passed to node endpoints (#2480)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* validate network parameter passed to node endpoints

---------

Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* NET-513 (#2492)

nm-certs.sh now requests certificate for EE and CE edition domains accordingly.

* [NET-404] Run in limited mode when ee checks fail (#2474)

* Add limited http handlers functionality to rest handler

* Export ee.errValidation (ee.ErrValidation)

* Export a fatal error handled by the hook manager

* Export a new status variable for unlicensed server

* Mark server as unlicensed when ee checks fail

* Handle license validation failures with a (re)boot in a limited state

* Revert "Export a fatal error handled by the hook manager"

This reverts commit 069c21974a8d36e889c73ad78023448d787d62a5.

* Revert "Export ee.errValidation (ee.ErrValidation)"

This reverts commit 59dbab8c79773ca5d879f28cbaf53f3dd4297b9b.

* Revert "Add limited http handlers functionality to rest handler"

This reverts commit e2f1f28facaca54713db76a588839cd2733cf673.

* Revert "Handle license validation failures with a (re)boot in a limited state"

This reverts commit 58cfbbaf522a1345aac1fa67964ebff0a6d60cd8.

* Revert "Mark server as unlicensed when ee checks fail"

This reverts commit 77c6dbdd3c9cfa6e7d6becedef6251e8617ae367.

* Handle license validation failures with a middleware

* Forbid responses if unlicensed ee and not in status api

* Remove unused func

* feat(NET-449): add sync feature to request a host pull from server (#2491)

* fix(NET-486): change client name length validation (#2498)

set limit to 5<=x<=32

* [NET-477] Pick AMB URL dynamically (#2489)

* Introduce config for environment

* Introduce func to get environment

* Choose accounts api host from environment

* Test the ee package on workflows

* Use build tag ee for license_test.go

* [Feature]: nm-quick script tackling arm TODO support (#2488)

* domain flag for auto installs

* use static servers with custom domain (#2421)

* send delete peer update always

* fix add/remove host api calls

* keep mq updates in a single go func

* move branch test logic to devops (#2443)

* handle IOT OS

* save server name to env (#2460)

* ensure branch test servers available after test runs (#2467)

* save server name to env

* free server always; add PR to discord messages

* use correct method to delete droplets (#2468)

* quick fix for the launcher

* removed exit when triggering not supported exit and removed the TODO comments related to this issue

---------

Co-authored-by: Matthew R Kasun <mkasun@nusak.ca>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* Bump github.com/go-playground/validator/v10 from 10.14.1 to 10.15.0 (#2503)

Bumps [github.com/go-playground/validator/v10](https://github.com/go-playground/validator) from 10.14.1 to 10.15.0.
- [Release notes](https://github.com/go-playground/validator/releases)
- [Commits](https://github.com/go-playground/validator/compare/v10.14.1...v10.15.0)

---
updated-dependencies:
- dependency-name: github.com/go-playground/validator/v10
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump golang.org/x/oauth2 from 0.10.0 to 0.11.0 (#2502)

Bumps [golang.org/x/oauth2](https://github.com/golang/oauth2) from 0.10.0 to 0.11.0.
- [Commits](https://github.com/golang/oauth2/compare/v0.10.0...v0.11.0)

---
updated-dependencies:
- dependency-name: golang.org/x/oauth2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump alpine from 3.18.2 to 3.18.3 (#2500)

Bumps alpine from 3.18.2 to 3.18.3.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Net 509 515 (#2496)

* NET-509

* External client config files with IPv6 endpoints now have the [] separating the address and port.

* NET-515

* Increased network name max length to 32

* NET-509-515

* Updated unit test for network name max length check.

* Updated extclient endpoint ip string manipulation to use sprintf

* Added proper error message for network name length more than max allowed.

* NET-515 small typo fix for error strings should not be capitalized

* [NET-494 / ACC-322] New free tier limits (#2495)

* Rename var

* Rename consts and use iota

* Use switch instead of repeated else if

* Rename limits related vars

* Introduce new free tier limits

* Measure new limits and report on license validation

* Separate usage and limits, have new ones

* Don't check for hosts and clients limits, but for machines instead

* Error on egress creation @ free tier w/ internet gateways

* Remove clients and hosts limit from code

* Rename var

* Rename consts and use iota

* Use switch instead of repeated else if

* Rename limits related vars

* Introduce new free tier limits

* Measure new limits and report on license validation

* Separate usage and limits, have new ones

* Don't check for hosts and clients limits, but for machines instead

* Error on egress creation @ free tier w/ internet gateways

* Remove clients and hosts limit from code

* NET-507 (#2506)

* NET-507

* Fixed server restart changing the node expiration date to the defaults.

* Removed expired nodes removal from zombie cleanup routine.

* Added a new expired nodes deletion routine which removes expired nodes every hour.

* NET-507 suggested changes

* Possible fix for zombie nodes upon node deletion from the UI.

* Suggested changes implemented for expired nodes deletion go routine.

* NET-507 typo fix

* typo fix for DeleteNode purge parameter

* NET-517 (#2510)

* Fixed emqx docker compose file syntax and added volumes for persistence.

* Added helper comments for docker-compose.yml and netmaker.env files to enable emqx broker support.

* migration (#2509)

* create gateways during migration

* set version for testing

* restruct migration

* debug logging

* enforce unique names for ext client names (#2476)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* prune(NET-483): remove defunct host.internetgateway field (#2487)

* don't reference host on err (#2493)

* deprecrate netclient install scripts (#2490)

* Net 500: validate network parameter passed to node endpoints (#2480)

* enforce unique names for ext client names

* only check for unique id on creation

* check for unique id if changed

* validate network parameter passed to node endpoints

---------

Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* NET-513 (#2492)

nm-certs.sh now requests certificate for EE and CE edition domains accordingly.

* [NET-404] Run in limited mode when ee checks fail (#2474)

* Add limited http handlers functionality to rest handler

* Export ee.errValidation (ee.ErrValidation)

* Export a fatal error handled by the hook manager

* Export a new status variable for unlicensed server

* Mark server as unlicensed when ee checks fail

* Handle license validation failures with a (re)boot in a limited state

* Revert "Export a fatal error handled by the hook manager"

This reverts commit 069c21974a8d36e889c73ad78023448d787d62a5.

* Revert "Export ee.errValidation (ee.ErrValidation)"

This reverts commit 59dbab8c79773ca5d879f28cbaf53f3dd4297b9b.

* Revert "Add limited http handlers functionality to rest handler"

This reverts commit e2f1f28facaca54713db76a588839cd2733cf673.

* Revert "Handle license validation failures with a (re)boot in a limited state"

This reverts commit 58cfbbaf522a1345aac1fa67964ebff0a6d60cd8.

* Revert "Mark server as unlicensed when ee checks fail"

This reverts commit 77c6dbdd3c9cfa6e7d6becedef6251e8617ae367.

* Handle license validation failures with a middleware

* Forbid responses if unlicensed ee and not in status api

* Remove unused func

* feat(NET-449): add sync feature to request a host pull from server (#2491)

* fix(NET-486): change client name length validation (#2498)

set limit to 5<=x<=32

* [NET-477] Pick AMB URL dynamically (#2489)

* Introduce config for environment

* Introduce func to get environment

* Choose accounts api host from environment

* Test the ee package on workflows

* Use build tag ee for license_test.go

* [Feature]: nm-quick script tackling arm TODO support (#2488)

* domain flag for auto installs

* use static servers with custom domain (#2421)

* send delete peer update always

* fix add/remove host api calls

* keep mq updates in a single go func

* move branch test logic to devops (#2443)

* handle IOT OS

* save server name to env (#2460)

* ensure branch test servers available after test runs (#2467)

* save server name to env

* free server always; add PR to discord messages

* use correct method to delete droplets (#2468)

* quick fix for the launcher

* removed exit when triggering not supported exit and removed the TODO comments related to this issue

---------

Co-authored-by: Matthew R Kasun <mkasun@nusak.ca>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>

* rebase conflict

* include pass and os in mirgration data

* node network ranges

* remove debugging logs

* add gateways

* use sent node

* upgrade shell script

* associate node to host during migration

* add node to host.Nodes and publish peer update

* save host outside loop

* fix script name

* simplify upgrade script

* don't migrate relays

* simplify upgrade script even more

* guard against blank address or address6

* typos

---------

Co-authored-by: Aceix <aceixsmartX@gmail.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>
Co-authored-by: Farukh Khan <farukhkhan21@gmail.com>
Co-authored-by: Gabriel de Souza Seibel <gabrielseibel1@gmail.com>
Co-authored-by: bornav <51048565+bornav@users.noreply.github.com>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>

* dns tests

* go mod tidy

* set dns dir when testing

* troubleshoot test failure

* test fail troubleshooting

* use txeh.RenderHostFile to compare

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>
Co-authored-by: Aceix <aceixsmartX@gmail.com>
Co-authored-by: Christopher Blaha <crispspiceguitar@gmail.com>
Co-authored-by: Farukh Khan <farukhkhan21@gmail.com>
Co-authored-by: Gabriel de Souza Seibel <gabrielseibel1@gmail.com>
Co-authored-by: bornav <51048565+bornav@users.noreply.github.com>
Co-authored-by: Alex Feiszli <31018251+afeiszli@users.noreply.github.com>
Co-authored-by: Abhishek Kondur <abhi281342@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-25 10:55:47 +05:30

292 lines
6.5 KiB
Go

package logic
import (
"encoding/json"
"os"
"regexp"
"sort"
validator "github.com/go-playground/validator/v10"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
"github.com/txn2/txeh"
)
// SetDNS - sets the dns on file
func SetDNS() error {
hostfile, err := txeh.NewHosts(&txeh.HostsConfig{})
if err != nil {
return err
}
var corefilestring string
networks, err := GetNetworks()
if err != nil && !database.IsEmptyRecord(err) {
return err
}
for _, net := range networks {
corefilestring = corefilestring + net.NetID + " "
dns, err := GetDNS(net.NetID)
if err != nil && !database.IsEmptyRecord(err) {
return err
}
for _, entry := range dns {
hostfile.AddHost(entry.Address, entry.Name+"."+entry.Network)
}
}
if corefilestring == "" {
corefilestring = "example.com"
}
err = hostfile.SaveAs("./config/dnsconfig/netmaker.hosts")
if err != nil {
return err
}
/* if something goes wrong with server DNS, check here
// commented out bc we were not using IsSplitDNS
if servercfg.IsSplitDNS() {
err = SetCorefile(corefilestring)
}
*/
return err
}
// GetDNS - gets the DNS of a current network
func GetDNS(network string) ([]models.DNSEntry, error) {
dns, err := GetNodeDNS(network)
if err != nil && !database.IsEmptyRecord(err) {
return dns, err
}
customdns, err := GetCustomDNS(network)
if err != nil && !database.IsEmptyRecord(err) {
return dns, err
}
dns = append(dns, customdns...)
return dns, nil
}
// GetNodeDNS - gets the DNS of a network node
func GetNodeDNS(network string) ([]models.DNSEntry, error) {
var dns []models.DNSEntry
nodes, err := GetNetworkNodes(network)
if err != nil {
return dns, err
}
for _, node := range nodes {
if node.Network != network {
continue
}
host, err := GetHost(node.HostID.String())
if err != nil {
continue
}
var entry = models.DNSEntry{}
entry.Name = host.Name
entry.Network = network
if node.Address.IP != nil {
entry.Address = node.Address.IP.String()
}
if node.Address6.IP != nil {
entry.Address6 = node.Address6.IP.String()
}
dns = append(dns, entry)
}
return dns, nil
}
// GetCustomDNS - gets the custom DNS of a network
func GetCustomDNS(network string) ([]models.DNSEntry, error) {
var dns []models.DNSEntry
collection, err := database.FetchRecords(database.DNS_TABLE_NAME)
if err != nil {
return dns, err
}
for _, value := range collection { // filter for entries based on network
var entry models.DNSEntry
if err := json.Unmarshal([]byte(value), &entry); err != nil {
continue
}
if entry.Network == network {
dns = append(dns, entry)
}
}
return dns, err
}
// SetCorefile - sets the core file of the system
func SetCorefile(domains string) error {
dir, err := os.Getwd()
if err != nil {
return err
}
_, err = os.Stat(dir + "/config/dnsconfig")
if os.IsNotExist(err) {
err = os.MkdirAll(dir+"/config/dnsconfig", 0744)
}
if err != nil {
logger.Log(0, "couldnt find or create /config/dnsconfig")
return err
}
corefile := domains + ` {
reload 15s
hosts /root/dnsconfig/netmaker.hosts {
fallthrough
}
forward . 8.8.8.8 8.8.4.4
log
}
`
corebytes := []byte(corefile)
err = os.WriteFile(dir+"/config/dnsconfig/Corefile", corebytes, 0644)
if err != nil {
return err
}
return err
}
// GetAllDNS - gets all dns entries
func GetAllDNS() ([]models.DNSEntry, error) {
var dns []models.DNSEntry
networks, err := GetNetworks()
if err != nil && !database.IsEmptyRecord(err) {
return []models.DNSEntry{}, err
}
for _, net := range networks {
netdns, err := GetDNS(net.NetID)
if err != nil {
return []models.DNSEntry{}, nil
}
dns = append(dns, netdns...)
}
return dns, nil
}
// GetDNSEntryNum - gets which entry the dns was
func GetDNSEntryNum(domain string, network string) (int, error) {
num := 0
entries, err := GetDNS(network)
if err != nil {
return 0, err
}
for i := 0; i < len(entries); i++ {
if domain == entries[i].Name {
num++
}
}
return num, nil
}
// SortDNSEntrys - Sorts slice of DNSEnteys by their Address alphabetically with numbers first
func SortDNSEntrys(unsortedDNSEntrys []models.DNSEntry) {
sort.Slice(unsortedDNSEntrys, func(i, j int) bool {
return unsortedDNSEntrys[i].Address < unsortedDNSEntrys[j].Address
})
}
// ValidateDNSCreate - checks if an entry is valid
func ValidateDNSCreate(entry models.DNSEntry) error {
v := validator.New()
_ = v.RegisterValidation("whitespace", func(f1 validator.FieldLevel) bool {
match, _ := regexp.MatchString(`\s`, entry.Name)
return !match
})
_ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
num, err := GetDNSEntryNum(entry.Name, entry.Network)
return err == nil && num == 0
})
_ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
_, err := GetParentNetwork(entry.Network)
return err == nil
})
err := v.Struct(entry)
if err != nil {
for _, e := range err.(validator.ValidationErrors) {
logger.Log(1, e.Error())
}
}
return err
}
// ValidateDNSUpdate - validates a DNS update
func ValidateDNSUpdate(change models.DNSEntry, entry models.DNSEntry) error {
v := validator.New()
_ = v.RegisterValidation("whitespace", func(f1 validator.FieldLevel) bool {
match, _ := regexp.MatchString(`\s`, entry.Name)
return !match
})
_ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
//if name & net not changing name we are good
if change.Name == entry.Name && change.Network == entry.Network {
return true
}
num, err := GetDNSEntryNum(change.Name, change.Network)
return err == nil && num == 0
})
_ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
_, err := GetParentNetwork(change.Network)
return err == nil
})
err := v.Struct(change)
if err != nil {
for _, e := range err.(validator.ValidationErrors) {
logger.Log(1, e.Error())
}
}
return err
}
// DeleteDNS - deletes a DNS entry
func DeleteDNS(domain string, network string) error {
key, err := GetRecordKey(domain, network)
if err != nil {
return err
}
err = database.DeleteRecord(database.DNS_TABLE_NAME, key)
return err
}
// CreateDNS - creates a DNS entry
func CreateDNS(entry models.DNSEntry) (models.DNSEntry, error) {
k, err := GetRecordKey(entry.Name, entry.Network)
if err != nil {
return models.DNSEntry{}, err
}
data, err := json.Marshal(&entry)
if err != nil {
return models.DNSEntry{}, err
}
err = database.Insert(k, string(data), database.DNS_TABLE_NAME)
return entry, err
}