This commit is contained in:
Donavan Fritz 2024-02-21 15:47:00 -08:00
parent b7eab281fb
commit eabac20beb
3 changed files with 36 additions and 14 deletions

View File

@ -0,0 +1,14 @@
name: Synthetic CoreDNS Plugin CI/CD Build
on:
push:
branches:
- main
jobs:
test:
runs-on: main
steps:
- name: run tests
run: |
set -x
docker build \
--progress plain .

8
Dockerfile Normal file
View File

@ -0,0 +1,8 @@
# run go test inside a docker container for consistency as acceptance testing
FROM golang:1.21
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go test -v

View File

@ -26,7 +26,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
// Create a new state for this request. This is used to store state and allows us to pass this // Create a new state for this request. This is used to store state and allows us to pass this
state := request.Request{W: w, Req: r} state := request.Request{W: w, Req: r}
log.Info("Received request for ", state.QName(), " of type ", state.QType()) log.Debug("Received request for ", state.QName(), " of type ", state.QType())
// //
// FOR FORWARD LOOKUPS // FOR FORWARD LOOKUPS
@ -35,7 +35,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
// //
if state.QType() == dns.TypeA || state.QType() == dns.TypeAAAA { if state.QType() == dns.TypeA || state.QType() == dns.TypeAAAA {
if strings.HasPrefix(state.Name(), s.Config.prefix) { if strings.HasPrefix(state.Name(), s.Config.prefix) {
log.Info("Possible synthetic response for:", state.QName()) log.Debug("Possible synthetic response for:", state.QName())
// pull out the ip address // pull out the ip address
ipStr := strings.TrimPrefix(strings.Split(state.Name(), ".")[0], s.Config.prefix) ipStr := strings.TrimPrefix(strings.Split(state.Name(), ".")[0], s.Config.prefix)
@ -46,7 +46,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
// respond according to the IP type and the request type // respond according to the IP type and the request type
if ip != nil { if ip != nil {
log.Info("Valid IP from hostname:", ip) log.Debug("Valid IP from hostname:", ip)
// check if ip is within the synthetic network // check if ip is within the synthetic network
var found bool var found bool
@ -58,10 +58,10 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
} }
// don't continue if the IP is not in the synthetic network (fall back to recorded response) // don't continue if the IP is not in the synthetic network (fall back to recorded response)
if found { if found {
log.Info("IP ", ip, " is in synthetic network") log.Debug("IP ", ip, " is in synthetic network")
if ip.To4() == nil && state.QType() == dns.TypeAAAA { if ip.To4() == nil && state.QType() == dns.TypeAAAA {
log.Info("Responding to AAAA request for ", state.QName()) log.Debug("Responding to AAAA request for ", state.QName())
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl} hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
@ -70,7 +70,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
return dns.RcodeSuccess, nil return dns.RcodeSuccess, nil
} }
if ip.To4() != nil && state.QType() == dns.TypeA { if ip.To4() != nil && state.QType() == dns.TypeA {
log.Info("Responding to A request for ", state.QName()) log.Debug("Responding to A request for ", state.QName())
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl} hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
@ -79,24 +79,24 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
return dns.RcodeSuccess, nil return dns.RcodeSuccess, nil
} }
if ip.To4() == nil && state.QType() == dns.TypeA { if ip.To4() == nil && state.QType() == dns.TypeA {
log.Info("Responding to A request for ", state.QName(), " with empty answer") log.Debug("Responding to A request for ", state.QName(), " with empty answer")
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
w.WriteMsg(m) w.WriteMsg(m)
return dns.RcodeSuccess, nil return dns.RcodeSuccess, nil
} }
if ip.To4() != nil && state.QType() == dns.TypeAAAA { if ip.To4() != nil && state.QType() == dns.TypeAAAA {
log.Info("Responding to AAAA request for ", state.QName(), " with empty answer") log.Debug("Responding to AAAA request for ", state.QName(), " with empty answer")
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
w.WriteMsg(m) w.WriteMsg(m)
return dns.RcodeSuccess, nil return dns.RcodeSuccess, nil
} }
log.Info("Unexpected code path for: ", state.QName()) log.Debug("Unexpected code path for: ", state.QName())
} }
log.Info("IP not in a valid network: ", ip) log.Debug("IP not in a valid network: ", ip)
} }
log.Info("Invalid IP from hostname: ", state.QName()) log.Debug("Invalid IP from hostname: ", state.QName())
} }
} }
@ -112,16 +112,16 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
// If the next plugin in the chain's recorded response is success, we go with that. // If the next plugin in the chain's recorded response is success, we go with that.
if rc == dns.RcodeSuccess && len(rec.Msg.Answer) > 0 { if rc == dns.RcodeSuccess && len(rec.Msg.Answer) > 0 {
log.Info("Next Plugin's answers are acceptable. no synthetic response") log.Debug("Next Plugin's answers are acceptable. no synthetic response")
w.WriteMsg(rec.Msg) w.WriteMsg(rec.Msg)
return rc, err return rc, err
} }
if state.QType() == dns.TypePTR { if state.QType() == dns.TypePTR {
log.Info("Attempting to inject synthetic response for reverse lookup: ", state.QName()) log.Debug("Attempting to inject synthetic response for reverse lookup: ", state.QName())
ip := inArpaToIp(state.QName()) ip := inArpaToIp(state.QName())
log.Info("Parsed IP: ", ip) log.Debug("Parsed IP: ", ip)
if ip != nil { if ip != nil {
forward := ipToDomainName(s.Config.prefix, ip, s.Config.forward) forward := ipToDomainName(s.Config.prefix, ip, s.Config.forward)