Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Coredns
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
Coredns
Commits
b3d08f9b
Commit
b3d08f9b
authored
Aug 10, 2020
by
Maxime Guyot
Committed by
GitHub
Aug 10, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
plugin/template: Add client IP data (#4034)
Signed-off-by:
Maxime Guyot
<
maxime@root314.com
>
parent
7d5f5b87
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
1 deletion
+30
-1
plugin/template/README.md
plugin/template/README.md
+1
-0
plugin/template/template.go
plugin/template/template.go
+2
-1
plugin/template/template_test.go
plugin/template/template_test.go
+27
-0
No files found.
plugin/template/README.md
View file @
b3d08f9b
...
@@ -48,6 +48,7 @@ Each resource record is a full-featured [Go template](https://golang.org/pkg/tex
...
@@ -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.
*
`.Group`
a map of the named capture groups.
*
`.Message`
the complete incoming DNS message.
*
`.Message`
the complete incoming DNS message.
*
`.Question`
the matched question section.
*
`.Question`
the matched question section.
*
`.Remote`
client’s IP address
*
`.Meta`
a function that takes a metadata name and returns the value, if the
*
`.Meta`
a function that takes a metadata name and returns the value, if the
metadata plugin is enabled. For example,
`.Meta "kubernetes/client-namespace"`
metadata plugin is enabled. For example,
`.Meta "kubernetes/client-namespace"`
...
...
plugin/template/template.go
View file @
b3d08f9b
...
@@ -48,6 +48,7 @@ type templateData struct {
...
@@ -48,6 +48,7 @@ type templateData struct {
Type
string
Type
string
Message
*
dns
.
Msg
Message
*
dns
.
Msg
Question
*
dns
.
Question
Question
*
dns
.
Question
Remote
string
md
map
[
string
]
metadata
.
Func
md
map
[
string
]
metadata
.
Func
}
}
...
@@ -145,7 +146,7 @@ func executeRRTemplate(server, section string, template *gotmpl.Template, data *
...
@@ -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
)
{
func
(
t
template
)
match
(
ctx
context
.
Context
,
state
request
.
Request
)
(
*
templateData
,
bool
,
bool
)
{
q
:=
state
.
Req
.
Question
[
0
]
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
())
zone
:=
plugin
.
Zones
(
t
.
zones
)
.
Matches
(
state
.
Name
())
if
zone
==
""
{
if
zone
==
""
{
...
...
plugin/template/template_test.go
View file @
b3d08f9b
...
@@ -25,6 +25,14 @@ func TestHandler(t *testing.T) {
...
@@ -25,6 +25,14 @@ func TestHandler(t *testing.T) {
fall
:
fall
.
Root
,
fall
:
fall
.
Root
,
zones
:
[]
string
{
"."
},
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
{
exampleDomainANSTemplate
:=
template
{
regex
:
[]
*
regexp
.
Regexp
{
regexp
.
MustCompile
(
"(^|[.])ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$"
)},
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 }}"
))},
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) {
...
@@ -196,6 +204,25 @@ func TestHandler(t *testing.T) {
return
nil
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"
,
name
:
"ExampleDomainMatch"
,
tmpl
:
exampleDomainATemplate
,
tmpl
:
exampleDomainATemplate
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment