-
Notifications
You must be signed in to change notification settings - Fork 12
/
dns.go
75 lines (62 loc) · 1.83 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package dns
import (
"fmt"
"strings"
"github.com/kubefirst/runtime/pkg"
"github.com/lixiangzhong/dnsutil"
"github.com/rs/zerolog/log"
)
const (
// Google
dnsLookupHost string = "8.8.8.8"
)
var (
CivoNameServers []string = []string{"ns0.civo.com", "ns1.civo.com"}
DigitalOceanNameServers []string = []string{"ns1.digitalocean.com", "ns2.digitalocean.com", "ns3.digitalocean.com"}
VultrNameservers []string = []string{"ns1.vultr.com", "ns2.vultr.com"}
)
// VerifyProviderDNS
func VerifyProviderDNS(cloudProvider string, cloudRegion string, domainName string, nameServers []string) error {
switch cloudProvider {
case "aws":
case "civo":
nameServers = CivoNameServers
case "digitalocean":
nameServers = DigitalOceanNameServers
case "vultr":
nameServers = VultrNameservers
default:
return fmt.Errorf("unsupported cloud provider for dns verification: %s", cloudProvider)
}
foundNSRecords, err := GetDomainNSRecords(domainName)
if err != nil {
return err
}
for _, reqrec := range nameServers {
if pkg.FindStringInSlice(foundNSRecords, reqrec) {
log.Info().Msgf("found NS record %s for domain %s", reqrec, domainName)
} else {
return fmt.Errorf("missing record %s for domain %s - please add the NS record", reqrec, domainName)
}
}
return nil
}
// GetDomainNSRecords
func GetDomainNSRecords(domainName string) ([]string, error) {
var dig dnsutil.Dig
dig.SetDNS(dnsLookupHost)
records, err := dig.NS(domainName)
if err != nil {
return []string{}, fmt.Errorf("error checking NS record for domain %s: %s", domainName, err)
}
var foundNSRecords []string
for _, rec := range records {
foundNSRecords = append(foundNSRecords, strings.TrimSuffix(rec.Ns, "."))
}
return foundNSRecords, nil
}