coredns-ldap/ldap.go

103 lines
3.0 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"
2020-06-04 17:41:04 +00:00
"crypto/tls"
"errors"
"fmt"
2020-06-04 17:41:04 +00:00
"strings"
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"
"github.com/coredns/coredns/plugin/pkg/fall"
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-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-04 17:41:04 +00:00
Next plugin.Handler
Fall fall.F
Zones []string
Client *ldap.Client
2020-06-03 17:47:21 +00:00
clientConfig
}
2018-04-19 06:25:30 +00:00
2020-06-03 17:47:21 +00:00
// New returns an initialized Ldap with defaults.
func New(zones []string) *Ldap {
k := new(Ldap)
k.Zones = zones
return k
}
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-04 17:41:04 +00:00
l.Client, err = Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
2020-06-03 17:47:21 +00:00
if err != nil {
log.Fatal(err)
}
2020-06-04 17:41:04 +00:00
defer l.Client.Close()
2020-06-03 17:47:21 +00:00
// Reconnect with TLS
2020-06-04 17:41:04 +00:00
err = l.Client.StartTLS(&tls.Config{InsecureSkipVerify: true})
2020-06-03 17:47:21 +00:00
if err != nil {
log.Fatal(err)
}
}
// Services implements the ServiceBackend interface.
func (l *Ldap) Services(ctx context.Context, state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
services, err = l.Records(ctx, state, exact)
if err != nil {
return
}
services = msg.Group(services)
return
}
// Reverse implements the ServiceBackend interface.
func (l *Ldap) Reverse(ctx context.Context, state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
return l.Services(ctx, state, exact, opt)
}
// Lookup implements the ServiceBackend interface.
func (l *Ldap) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
return l.Upstream.Lookup(ctx, state, name, typ)
}
// IsNameError implements the ServiceBackend interface.
func (l *Ldap) IsNameError(err error) bool {
return err == errNoItems || err == errNsNotExposed || err == errInvalidRequest
}
// Records looks up records in ldap. If exact is true, it will lookup just this
// name. This is used when find matches when completing SRV lookups for instance.
func (l *Ldap) Records(ctx context.Context, state request.Request, exact bool) ([]msg.Service, error) {
name := state.Name()
path, star := msg.PathWithWildcard(name, l.PathPrefix)
r, err := l.get(ctx, path, !exact)
if err != nil {
return nil, err
}
segments := strings.Split(msg.Path(name, l.PathPrefix), "/")
return l.loopNodes(r.Kvs, segments, star, state.QType())
2020-06-03 17:47:21 +00:00
}