1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Update bind docs and Getting Started (#53)

* intitial work for bind refactor

* relax requirement that creds.json exists.

* Updating bind docs.
Fixes #48

* typo

* can exclude provider from default set in creds.json

* Add bind to integration tests. Fix for IDNs.
This commit is contained in:
Craig Peterson
2017-03-22 10:38:08 -06:00
committed by GitHub
parent 33b4d2b748
commit 854c84e652
8 changed files with 98 additions and 84 deletions

View File

@ -11,10 +11,6 @@ bind -
if an update is actually needed. The old zonefile is also used
as the basis for generating the new SOA serial number.
If -bind_skeletin_src and -bind_skeletin_dst is defined, a
recursive file copy is performed from src to dst. This is
useful for copying named.ca and other static files.
*/
import (
@ -50,20 +46,20 @@ func (s SoaInfo) String() string {
}
type Bind struct {
Default_NS []string `json:"default_ns"`
Default_Soa SoaInfo `json:"default_soa"`
nameservers []*models.Nameserver `json:"-"`
DefaultNS []string `json:"default_ns"`
DefaultSoa SoaInfo `json:"default_soa"`
nameservers []*models.Nameserver
}
var bindBaseDir = flag.String("bindtree", "zones", "BIND: Directory that stores BIND zonefiles.")
//var bindSkeletin = flag.String("bind_skeletin", "skeletin/master/var/named/chroot/var/named/master", "")
func rrToRecord(rr dns.RR, origin string, replace_serial uint32) (models.RecordConfig, uint32) {
func rrToRecord(rr dns.RR, origin string, replaceSerial uint32) (models.RecordConfig, uint32) {
// Convert's dns.RR into our native data type (models.RecordConfig).
// Records are translated directly with no changes.
// If it is an SOA for the apex domain and
// replace_serial != 0, change the serial to replace_serial.
// replaceSerial != 0, change the serial to replaceSerial.
// WARNING(tlim): This assumes SOAs do not have serial=0.
// If one is found, we replace it with serial=1.
var old_serial, new_serial uint32
@ -92,8 +88,8 @@ func rrToRecord(rr dns.RR, origin string, replace_serial uint32) (models.RecordC
old_serial = 1
}
new_serial = v.Serial
if rc.Name == "@" && replace_serial != 0 {
new_serial = replace_serial
if rc.Name == "@" && replaceSerial != 0 {
new_serial = replaceSerial
}
rc.Target = fmt.Sprintf("%v %v %v %v %v %v %v",
v.Ns, v.Mbox, new_serial, v.Refresh, v.Retry, v.Expire, v.Minttl)
@ -107,12 +103,12 @@ func rrToRecord(rr dns.RR, origin string, replace_serial uint32) (models.RecordC
func makeDefaultSOA(info SoaInfo, origin string) *models.RecordConfig {
// Make a default SOA record in case one isn't found:
soa_rec := models.RecordConfig{
soaRec := models.RecordConfig{
Type: "SOA",
Name: "@",
}
soa_rec.NameFQDN = dnsutil.AddOrigin(soa_rec.Name, origin)
soaRec.NameFQDN = dnsutil.AddOrigin(soaRec.Name, origin)
//TODO(cpeterson): are these sane defaults?
if len(info.Ns) == 0 {
info.Ns = "DEFAULT_NOT_SET"
}
@ -134,9 +130,9 @@ func makeDefaultSOA(info SoaInfo, origin string) *models.RecordConfig {
if info.Minttl == 0 {
info.Minttl = 1440
}
soa_rec.Target = info.String()
soaRec.Target = info.String()
return &soa_rec
return &soaRec
}
func (c *Bind) GetNameservers(string) ([]*models.Nameserver, error) {
@ -144,7 +140,7 @@ func (c *Bind) GetNameservers(string) ([]*models.Nameserver, error) {
}
func (c *Bind) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
dc.Punycode()
// Phase 1: Copy everything to []*models.RecordConfig:
// expectedRecords < dc.Records[i]
// foundRecords < zonefile
@ -159,34 +155,34 @@ func (c *Bind) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correcti
// diff.Inc...(foundDiffRecords, expectedDiffRecords )
// Default SOA record. If we see one in the zone, this will be replaced.
soa_rec := makeDefaultSOA(c.Default_Soa, dc.Name)
soaRec := makeDefaultSOA(c.DefaultSoa, dc.Name)
// Read foundRecords:
foundRecords := make([]*models.RecordConfig, 0)
var old_serial, new_serial uint32
var oldSerial, newSerial uint32
zonefile := filepath.Join(*bindBaseDir, strings.ToLower(dc.Name)+".zone")
found_fh, err := os.Open(zonefile)
zone_file_found := err == nil
foundFH, err := os.Open(zonefile)
zoneFileFound := err == nil
if err != nil && !os.IsNotExist(os.ErrNotExist) {
// Don't whine if the file doesn't exist. However all other
// errors will be reported.
fmt.Printf("Could not read zonefile: %v\n", err)
} else {
for x := range dns.ParseZone(found_fh, dc.Name, zonefile) {
for x := range dns.ParseZone(foundFH, dc.Name, zonefile) {
if x.Error != nil {
log.Println("Error in zonefile:", x.Error)
} else {
rec, serial := rrToRecord(x.RR, dc.Name, old_serial)
if serial != 0 && old_serial != 0 {
rec, serial := rrToRecord(x.RR, dc.Name, oldSerial)
if serial != 0 && oldSerial != 0 {
log.Fatalf("Multiple SOA records in zonefile: %v\n", zonefile)
}
if serial != 0 {
// This was an SOA record. Update the serial.
old_serial = serial
new_serial = generate_serial(old_serial)
oldSerial = serial
newSerial = generate_serial(oldSerial)
// Regenerate with new serial:
*soa_rec, _ = rrToRecord(x.RR, dc.Name, new_serial)
rec = *soa_rec
*soaRec, _ = rrToRecord(x.RR, dc.Name, newSerial)
rec = *soaRec
}
foundRecords = append(foundRecords, &rec)
}
@ -195,7 +191,7 @@ func (c *Bind) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correcti
// Add SOA record to expected set:
if !dc.HasRecordTypeName("SOA", "@") {
dc.Records = append(dc.Records, soa_rec)
dc.Records = append(dc.Records, soaRec)
}
differ := diff.New(dc)
@ -206,24 +202,24 @@ func (c *Bind) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correcti
changes := false
for _, i := range create {
changes = true
if zone_file_found {
if zoneFileFound {
fmt.Fprintln(buf, i)
}
}
for _, i := range del {
changes = true
if zone_file_found {
if zoneFileFound {
fmt.Fprintln(buf, i)
}
}
for _, i := range mod {
changes = true
if zone_file_found {
if zoneFileFound {
fmt.Fprintln(buf, i)
}
}
msg := fmt.Sprintf("GENERATE_ZONEFILE: %s\n", dc.Name)
if !zone_file_found {
if !zoneFileFound {
msg = msg + fmt.Sprintf(" (%d records)\n", len(create))
}
msg += buf.String()
@ -270,7 +266,7 @@ func initBind(config map[string]string, providermeta json.RawMessage) (providers
return nil, err
}
}
api.nameservers = models.StringsToNameservers(api.Default_NS)
api.nameservers = models.StringsToNameservers(api.DefaultNS)
return api, nil
}