coredns-ldap/ldap.go

92 lines
2.5 KiB
Go
Raw Normal View History

2020-06-01 07:59:41 +00:00
// Package ldap is a CoreDNS plugin that resolves A, AAAA y PTR RR from a ldap backend.
//
2020-06-01 07:59:41 +00:00
// It serves as a backend connector for autoritative zone data.
// Ldap is often used for bare metal inventories. This use is the main use case
// for this plugin. Other use cases might eventually be supported.
// fqdn and ip4 / ip6 information is mapped from it's repsective ldap schema and
// served as DNS records over coredns. Mapping is configurable. To reduce load
// on the backend, a configurable cache is bundled.
2020-06-01 07:48:07 +00:00
package ldap
import (
"context"
"errors"
2020-06-04 17:41:04 +00:00
"strings"
2020-06-08 04:12:18 +00:00
"sync"
"time"
2020-06-09 03:01:53 +00:00
"net"
2017-09-15 20:29:47 +00:00
"github.com/coredns/coredns/plugin"
2020-06-04 17:41:04 +00:00
"github.com/coredns/coredns/plugin/etcd/msg"
2020-06-09 03:01:53 +00:00
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/fall"
2020-06-08 04:12:18 +00:00
"github.com/coredns/coredns/plugin/pkg/upstream"
2020-06-04 17:41:04 +00:00
"github.com/coredns/coredns/request"
2018-02-27 11:39:01 +00:00
"github.com/miekg/dns"
2020-06-03 17:47:21 +00:00
"gopkg.in/ldap.v3"
)
2020-06-09 03:01:53 +00:00
type ldapRecord struct {
fqdn string
ip net.IP
}
func (r *ldapRecord) A() (A *dns.A) {
return &dns.A{Hdr: dns.RR_Header{Name: r.fqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}, A: r.ip}
}
2020-06-01 07:59:41 +00:00
// Ldap is an ldap plugin to serve zone entries from a ldap backend.
2020-06-01 07:48:07 +00:00
type Ldap struct {
2020-06-09 03:01:53 +00:00
Next plugin.Handler
Fall fall.F
Upstream *upstream.Upstream
Client ldap.Client
Zones file.Zones
2020-06-08 04:12:18 +00:00
searchRequest *ldap.SearchRequest
ldapURL string
pagingLimit uint32
syncInterval time.Duration
username string
password string
sasl bool
2020-06-09 03:01:53 +00:00
fqdnAttr string
ip4Attr string
zMu sync.RWMutex
ttl time.Duration
}
2018-04-19 06:25:30 +00:00
2020-06-03 17:47:21 +00:00
// New returns an initialized Ldap with defaults.
2020-06-09 03:01:53 +00:00
func New(zoneNames []string) *Ldap {
l := new(Ldap)
l.Zones.Names = zoneNames
l.pagingLimit = 0
2020-06-08 04:12:18 +00:00
// SearchRequest defaults
2020-06-09 03:01:53 +00:00
l.searchRequest = new(ldap.SearchRequest)
l.searchRequest.DerefAliases = ldap.NeverDerefAliases // TODO: Reason
l.searchRequest.Scope = ldap.ScopeWholeSubtree // search whole subtree
l.searchRequest.SizeLimit = 500 // TODO: Reason
l.searchRequest.TimeLimit = 500 // TODO: Reason
l.searchRequest.TypesOnly = false // TODO: Reason
return l
2020-06-03 17:47:21 +00:00
}
var (
errNoItems = errors.New("no items found")
errNsNotExposed = errors.New("namespace is not exposed")
errInvalidRequest = errors.New("invalid query name")
)
2018-02-25 08:52:52 +00:00
2020-06-04 17:41:04 +00:00
// InitClient initializes a Ldap client.
2020-06-03 17:47:21 +00:00
func (l *Ldap) InitClient() (err error) {
2020-06-08 04:12:18 +00:00
l.Client, err = ldap.DialURL(l.ldapURL)
2020-06-03 17:47:21 +00:00
if err != nil {
log.Fatal(err)
2020-06-08 04:12:18 +00:00
return err
2020-06-03 17:47:21 +00:00
}
2020-06-04 17:41:04 +00:00
defer l.Client.Close()
2020-06-08 04:12:18 +00:00
return nil
2020-06-03 17:47:21 +00:00
}