mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
plugin/metadata: metadata is just label=value (#1914)
This revert 17d807f0 and re-adds the metadata plugin as a plugin that
just sets a label to a value function.
Add package documentation on how to use the metadata package. Make it
clear that any caching is up to the Func implemented.
There are now - no in tree users. We could add the request metadata by
default under names that copy request.Request, i.e
request/ip - remote IP
request/port - remote port
Variables.go has been deleted.
Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -1,104 +0,0 @@
|
||||
package variables
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/coredns/coredns/request"
|
||||
)
|
||||
|
||||
const (
|
||||
queryName = "qname"
|
||||
queryType = "qtype"
|
||||
clientIP = "client_ip"
|
||||
clientPort = "client_port"
|
||||
protocol = "protocol"
|
||||
serverIP = "server_ip"
|
||||
serverPort = "server_port"
|
||||
)
|
||||
|
||||
// All is a list of available variables provided by GetMetadataValue
|
||||
var All = []string{queryName, queryType, clientIP, clientPort, protocol, serverIP, serverPort}
|
||||
|
||||
// GetValue calculates and returns the data specified by the variable name.
|
||||
// Supported varNames are listed in allProvidedVars.
|
||||
func GetValue(state request.Request, varName string) ([]byte, error) {
|
||||
switch varName {
|
||||
case queryName:
|
||||
return []byte(state.QName()), nil
|
||||
|
||||
case queryType:
|
||||
return uint16ToWire(state.QType()), nil
|
||||
|
||||
case clientIP:
|
||||
return ipToWire(state.Family(), state.IP())
|
||||
|
||||
case clientPort:
|
||||
return portToWire(state.Port())
|
||||
|
||||
case protocol:
|
||||
return []byte(state.Proto()), nil
|
||||
|
||||
case serverIP:
|
||||
ip, _, err := net.SplitHostPort(state.W.LocalAddr().String())
|
||||
if err != nil {
|
||||
ip = state.W.RemoteAddr().String()
|
||||
}
|
||||
return ipToWire(state.Family(), ip)
|
||||
|
||||
case serverPort:
|
||||
_, port, err := net.SplitHostPort(state.W.LocalAddr().String())
|
||||
if err != nil {
|
||||
port = "0"
|
||||
}
|
||||
return portToWire(port)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unable to extract data for variable %s", varName)
|
||||
}
|
||||
|
||||
// uint16ToWire writes unit16 to wire/binary format
|
||||
func uint16ToWire(data uint16) []byte {
|
||||
buf := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(buf, uint16(data))
|
||||
return buf
|
||||
}
|
||||
|
||||
// ipToWire writes IP address to wire/binary format, 4 or 16 bytes depends on IPV4 or IPV6.
|
||||
func ipToWire(family int, ipAddr string) ([]byte, error) {
|
||||
|
||||
switch family {
|
||||
case 1:
|
||||
return net.ParseIP(ipAddr).To4(), nil
|
||||
case 2:
|
||||
return net.ParseIP(ipAddr).To16(), nil
|
||||
}
|
||||
return nil, fmt.Errorf("invalid IP address family (i.e. version) %d", family)
|
||||
}
|
||||
|
||||
// portToWire writes port to wire/binary format, 2 bytes
|
||||
func portToWire(portStr string) ([]byte, error) {
|
||||
|
||||
port, err := strconv.ParseUint(portStr, 10, 16)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return uint16ToWire(uint16(port)), nil
|
||||
}
|
||||
|
||||
// Family returns the family of the transport, 1 for IPv4 and 2 for IPv6.
|
||||
func family(ip net.Addr) int {
|
||||
var a net.IP
|
||||
if i, ok := ip.(*net.UDPAddr); ok {
|
||||
a = i.IP
|
||||
}
|
||||
if i, ok := ip.(*net.TCPAddr); ok {
|
||||
a = i.IP
|
||||
}
|
||||
if a.To4() != nil {
|
||||
return 1
|
||||
}
|
||||
return 2
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package variables
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/coredns/plugin/test"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestGetValue(t *testing.T) {
|
||||
// test.ResponseWriter has the following values:
|
||||
// The remote will always be 10.240.0.1 and port 40212.
|
||||
// The local address is always 127.0.0.1 and port 53.
|
||||
tests := []struct {
|
||||
varName string
|
||||
expectedValue []byte
|
||||
shouldErr bool
|
||||
}{
|
||||
{
|
||||
queryName,
|
||||
[]byte("example.com."),
|
||||
false,
|
||||
},
|
||||
{
|
||||
queryType,
|
||||
[]byte{0x00, 0x01},
|
||||
false,
|
||||
},
|
||||
{
|
||||
clientIP,
|
||||
[]byte{10, 240, 0, 1},
|
||||
false,
|
||||
},
|
||||
{
|
||||
clientPort,
|
||||
[]byte{0x9D, 0x14},
|
||||
false,
|
||||
},
|
||||
{
|
||||
protocol,
|
||||
[]byte("udp"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
serverIP,
|
||||
[]byte{127, 0, 0, 1},
|
||||
false,
|
||||
},
|
||||
{
|
||||
serverPort,
|
||||
[]byte{0, 53},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"wrong_var",
|
||||
[]byte{},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion("example.com.", dns.TypeA)
|
||||
m.Question[0].Qclass = dns.ClassINET
|
||||
state := request.Request{W: &test.ResponseWriter{}, Req: m}
|
||||
|
||||
value, err := GetValue(state, tc.varName)
|
||||
|
||||
if tc.shouldErr && err == nil {
|
||||
t.Errorf("Test %d: Expected error, but didn't recieve", i)
|
||||
}
|
||||
if !tc.shouldErr && err != nil {
|
||||
t.Errorf("Test %d: Expected no error, but got error: %v", i, err.Error())
|
||||
}
|
||||
|
||||
if !bytes.Equal(tc.expectedValue, value) {
|
||||
t.Errorf("Test %d: Expected %v but got %v", i, tc.expectedValue, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user