coredns-ldap/ldap.go

76 lines
2.6 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"
"fmt"
"io"
"os"
2017-09-15 20:29:47 +00:00
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
2018-04-25 18:32:42 +00:00
clog "github.com/coredns/coredns/plugin/pkg/log"
2018-02-27 11:39:01 +00:00
"github.com/miekg/dns"
2020-06-01 08:11:50 +00:00
"gopkg.in/ldap.v2"
)
2018-04-26 20:56:11 +00:00
// Define log to be a logger with the plugin name in it. This way we can just use log.Info and
// friends to log.
2020-06-01 07:48:07 +00:00
var log = clog.NewWithPlugin("ldap")
2018-04-25 18:32:42 +00:00
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 {
2017-09-15 20:29:47 +00:00
Next plugin.Handler
}
2020-06-01 07:48:07 +00:00
// ServeDNS implements the plugin.Handler interface. This method gets called when ldap is used
2018-02-25 08:52:52 +00:00
// in a Server.
2020-06-01 07:48:07 +00:00
func (e Ldap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
// This function could be simpler. I.e. just fmt.Println("ldap") here, but we want to show
// a slightly more complex ldap as to make this more interesting.
2018-02-25 08:52:52 +00:00
// Here we wrap the dns.ResponseWriter in a new ResponseWriter and call the next plugin, when the
2020-06-01 07:48:07 +00:00
// answer comes back, it will print "ldap".
2018-02-25 08:52:52 +00:00
2018-04-19 06:25:30 +00:00
// Debug log that we've have seen the query. This will only be shown when the debug plugin is loaded.
log.Debug("Received response")
2018-02-25 08:52:52 +00:00
// Wrap.
pw := NewResponsePrinter(w)
2018-02-25 08:52:52 +00:00
// Export metric with the server label set to the current server handling the request.
2018-04-26 20:56:11 +00:00
requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc()
2018-02-25 08:52:52 +00:00
// Call next plugin (if any).
2017-09-15 20:29:47 +00:00
return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r)
}
// Name implements the Handler interface.
2020-06-01 07:48:07 +00:00
func (e Ldap) Name() string { return "ldap" }
2020-06-01 07:48:07 +00:00
// ResponsePrinter wrap a dns.ResponseWriter and will write ldap to standard output when WriteMsg is called.
type ResponsePrinter struct {
dns.ResponseWriter
}
2018-02-25 08:52:52 +00:00
// NewResponsePrinter returns ResponseWriter.
func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter {
return &ResponsePrinter{ResponseWriter: w}
}
2020-06-01 07:48:07 +00:00
// WriteMsg calls the underlying ResponseWriter's WriteMsg method and prints "ldap" to standard output.
func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error {
2020-06-01 07:48:07 +00:00
fmt.Fprintln(out, "ldap")
return r.ResponseWriter.WriteMsg(res)
}
2018-02-25 08:52:52 +00:00
// Make out a reference to os.Stdout so we can easily overwrite it for testing.
var out io.Writer = os.Stdout