// Package ldap is a CoreDNS plugin that resolves A, AAAA y PTR RR from a ldap backend. // // 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. package ldap import ( "context" "fmt" "io" "os" "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/metrics" clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/miekg/dns" "gopkg.in/ldap.v2" ) // Define log to be a logger with the plugin name in it. This way we can just use log.Info and // friends to log. var log = clog.NewWithPlugin("ldap") // Ldap is an ldap plugin to serve zone entries from a ldap backend. type Ldap struct { Next plugin.Handler } // ServeDNS implements the plugin.Handler interface. This method gets called when ldap is used // in a Server. func (l 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. // Here we wrap the dns.ResponseWriter in a new ResponseWriter and call the next plugin, when the // answer comes back, it will print "ldap". // Debug log that we've have seen the query. This will only be shown when the debug plugin is loaded. log.Debug("Received response") // Wrap. pw := NewResponsePrinter(w) // Export metric with the server label set to the current server handling the request. requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc() // Call next plugin (if any). return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r) } // Name implements the Handler interface. func (l Ldap) Name() string { return "ldap" } // ResponsePrinter wrap a dns.ResponseWriter and will write ldap to standard output when WriteMsg is called. type ResponsePrinter struct { dns.ResponseWriter } // NewResponsePrinter returns ResponseWriter. func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter { return &ResponsePrinter{ResponseWriter: w} } // WriteMsg calls the underlying ResponseWriter's WriteMsg method and prints "ldap" to standard output. func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error { fmt.Fprintln(out, "ldap") return r.ResponseWriter.WriteMsg(res) } // Make out a reference to os.Stdout so we can easily overwrite it for testing. var out io.Writer = os.Stdout