mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	plugin/template: Add client IP data (#4034)
Signed-off-by: Maxime Guyot <maxime@root314.com>
This commit is contained in:
		| @@ -48,6 +48,7 @@ Each resource record is a full-featured [Go template](https://golang.org/pkg/tex | ||||
| * `.Group` a map of the named capture groups. | ||||
| * `.Message` the complete incoming DNS message. | ||||
| * `.Question` the matched question section. | ||||
| * `.Remote` client’s IP address | ||||
| * `.Meta` a function that takes a metadata name and returns the value, if the | ||||
|   metadata plugin is enabled. For example, `.Meta "kubernetes/client-namespace"` | ||||
|  | ||||
|   | ||||
| @@ -48,6 +48,7 @@ type templateData struct { | ||||
| 	Type     string | ||||
| 	Message  *dns.Msg | ||||
| 	Question *dns.Question | ||||
| 	Remote   string | ||||
| 	md       map[string]metadata.Func | ||||
| } | ||||
|  | ||||
| @@ -145,7 +146,7 @@ func executeRRTemplate(server, section string, template *gotmpl.Template, data * | ||||
|  | ||||
| func (t template) match(ctx context.Context, state request.Request) (*templateData, bool, bool) { | ||||
| 	q := state.Req.Question[0] | ||||
| 	data := &templateData{md: metadata.ValueFuncs(ctx)} | ||||
| 	data := &templateData{md: metadata.ValueFuncs(ctx), Remote: state.IP()} | ||||
|  | ||||
| 	zone := plugin.Zones(t.zones).Matches(state.Name()) | ||||
| 	if zone == "" { | ||||
|   | ||||
| @@ -25,6 +25,14 @@ func TestHandler(t *testing.T) { | ||||
| 		fall:   fall.Root, | ||||
| 		zones:  []string{"."}, | ||||
| 	} | ||||
| 	exampleDomainIPATemplate := template{ | ||||
| 		regex:  []*regexp.Regexp{regexp.MustCompile(".*")}, | ||||
| 		answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }} 60 IN A {{ .Remote }}"))}, | ||||
| 		qclass: dns.ClassINET, | ||||
| 		qtype:  dns.TypeA, | ||||
| 		fall:   fall.Root, | ||||
| 		zones:  []string{"."}, | ||||
| 	} | ||||
| 	exampleDomainANSTemplate := template{ | ||||
| 		regex:      []*regexp.Regexp{regexp.MustCompile("(^|[.])ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$")}, | ||||
| 		answer:     []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"))}, | ||||
| @@ -196,6 +204,25 @@ func TestHandler(t *testing.T) { | ||||
| 				return nil | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:   "ExampleIPMatch", | ||||
| 			tmpl:   exampleDomainIPATemplate, | ||||
| 			qclass: dns.ClassINET, | ||||
| 			qtype:  dns.TypeA, | ||||
| 			qname:  "test.example.", | ||||
| 			verifyResponse: func(r *dns.Msg) error { | ||||
| 				if len(r.Answer) != 1 { | ||||
| 					return fmt.Errorf("expected 1 answer, got %v", len(r.Answer)) | ||||
| 				} | ||||
| 				if r.Answer[0].Header().Rrtype != dns.TypeA { | ||||
| 					return fmt.Errorf("expected an A record answer, got %v", dns.TypeToString[r.Answer[0].Header().Rrtype]) | ||||
| 				} | ||||
| 				if r.Answer[0].(*dns.A).A.String() != "10.240.0.1" { | ||||
| 					return fmt.Errorf("expected an A record for 10.95.12.8, got %v", r.Answer[0].String()) | ||||
| 				} | ||||
| 				return nil | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:   "ExampleDomainMatch", | ||||
| 			tmpl:   exampleDomainATemplate, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user