coredns-ldap/sync.go

116 lines
2.5 KiB
Go
Raw Normal View History

2020-06-08 04:12:18 +00:00
package ldap
import (
"context"
"fmt"
"strings"
2020-06-09 03:01:53 +00:00
"net"
2020-06-08 04:12:18 +00:00
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/file"
)
// Run updates the zone from ldap.
func (l *Ldap) Run(ctx context.Context) error {
if err := l.UpdateZones(); err != nil {
2020-06-08 04:12:18 +00:00
return err
}
2020-06-10 07:41:44 +00:00
loop := func() {
2020-06-08 04:12:18 +00:00
for {
select {
case <-ctx.Done():
log.Infof("Breaking out of Ldap update loop: %v", ctx.Err())
return
2020-06-09 03:01:53 +00:00
case <-time.After(l.syncInterval):
if err := l.UpdateZones(); err != nil && ctx.Err() == nil {
2020-06-08 04:12:18 +00:00
log.Errorf("Failed to update zones: %v", err)
}
}
}
2020-06-10 07:41:44 +00:00
}
go loop()
2020-06-08 04:12:18 +00:00
return nil
}
func (l *Ldap) UpdateZones() error {
2020-06-09 03:01:53 +00:00
zoneFileMap := make(map[string]*file.Zone, len(l.Zones.Names))
for _, zn := range l.Zones.Names {
zoneFileMap[zn] = nil
zoneFileMap[zn] = file.NewZone(zn, "")
zoneFileMap[zn].Upstream = l.Upstream
err := zoneFileMap[zn].Insert(SOA(zn))
if err != nil {
return fmt.Errorf("updating zones: %w", err)
}
2020-06-08 04:12:18 +00:00
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
ldapRecords, err := l.fetchLdapRecords()
2020-06-08 04:12:18 +00:00
if err != nil {
2020-06-09 03:01:53 +00:00
return fmt.Errorf("updating zones: %w", err)
2020-06-08 04:12:18 +00:00
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
for zn, lrpz := range l.mapLdapRecordsToZone(ldapRecords) {
if len(lrpz) == 0 {
2020-06-09 03:01:53 +00:00
continue
2020-06-08 04:12:18 +00:00
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
for _, lr := range lrpz {
2021-05-28 23:47:39 +00:00
err = zoneFileMap[zn].Insert(lr.AAAA())
2020-06-10 08:03:59 +00:00
err = zoneFileMap[zn].Insert(lr.A())
if err != nil {
return fmt.Errorf("updating zones: %w", err)
}
2020-06-08 04:12:18 +00:00
}
2020-06-09 03:01:53 +00:00
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
l.zMu.Lock()
for zn, zf := range zoneFileMap {
l.Zones.Z[zn] = zf
2020-06-09 03:01:53 +00:00
}
l.zMu.Unlock()
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
return nil
}
2020-06-08 04:12:18 +00:00
2020-06-09 03:01:53 +00:00
func (l *Ldap) mapLdapRecordsToZone(ldapRecords []ldapRecord) (ldapRecordsPerZone map[string][]ldapRecord) {
lrpz := make(map[string][]ldapRecord, len(l.Zones.Names))
for _, zn := range l.Zones.Names {
lrpz[zn] = nil
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
for _, lr := range ldapRecords {
zone := plugin.Zones(l.Zones.Names).Matches(lr.fqdn)
if zone != "" {
lrpz[zone] = append(lrpz[zone], lr)
2020-06-08 04:12:18 +00:00
}
2020-06-09 03:01:53 +00:00
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
return lrpz
}
2020-06-08 04:12:18 +00:00
2020-06-09 03:01:53 +00:00
func (l *Ldap) fetchLdapRecords() (ldapRecords []ldapRecord, err error) {
searchResult, err := l.Client.SearchWithPaging(l.SearchRequest, l.pagingLimit)
2020-06-09 03:01:53 +00:00
if err != nil {
return nil, fmt.Errorf("fetching data from server: %w", err)
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
ldapRecords = make([]ldapRecord, len(searchResult.Entries))
for i := 0; i < len(ldapRecords); i++ {
fqdn := searchResult.Entries[i].GetAttributeValue(l.FqdnAttr)
if !strings.HasSuffix(fqdn, ".") {
fqdn = fqdn + "."
}
2020-06-09 03:01:53 +00:00
ldapRecords[i] = ldapRecord{
fqdn: fqdn,
2021-05-28 23:47:39 +00:00
ip4: net.ParseIP(searchResult.Entries[i].GetAttributeValue(l.Ip4Attr)),
ip6: net.ParseIP(searchResult.Entries[i].GetAttributeValue(l.Ip6Attr)),
2020-06-08 04:12:18 +00:00
}
}
2020-06-10 07:41:44 +00:00
2020-06-09 03:01:53 +00:00
return ldapRecords, nil
2020-06-08 04:12:18 +00:00
}