Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS: fix SOA #1384

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions pkg/roles/dhcp/dhcp_handler4_discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ func TestDHCPDiscover(t *testing.T) {
},
}),
))
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPDiscoverPayload)
Expand Down Expand Up @@ -122,9 +120,7 @@ func TestDHCPDiscover_Gateway(t *testing.T) {
},
}),
))
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPDiscoverGatewayPayload)
Expand Down Expand Up @@ -200,9 +196,7 @@ func TestDHCPDiscover_RequestedIP_WrongSubnet(t *testing.T) {
},
}),
))
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPDiscoverGatewayPayload)
Expand Down Expand Up @@ -249,9 +243,7 @@ func TestDHCPDiscoverDNS(t *testing.T) {
}),
))

tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPDiscoverPayload)
Expand Down
4 changes: 1 addition & 3 deletions pkg/roles/dhcp/dhcp_handler4_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ func TestDHCPRelease(t *testing.T) {
Hostname: "linux-test.home.arpa",
}),
))
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPReleasePayload)
Expand Down
4 changes: 1 addition & 3 deletions pkg/roles/dhcp/dhcp_handler4_request_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ func BenchmarkRoleDHCP_Request(b *testing.B) {
}),
))

tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()
c, err := netip.ParsePrefix("10.100.0.0/24")
if err != nil {
Expand Down
24 changes: 6 additions & 18 deletions pkg/roles/dhcp/dhcp_handler4_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func TestDHCPRequest(t *testing.T) {
},
}),
))
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPRequestPayload)
Expand Down Expand Up @@ -89,9 +87,7 @@ func TestDHCPRequest_Hook(t *testing.T) {
}`,
}),
))
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPRequestPayload)
Expand Down Expand Up @@ -138,9 +134,7 @@ func TestDHCPRequest_Hook_UniFi(t *testing.T) {
}`,
}),
))
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPRequestPayload)
Expand Down Expand Up @@ -188,9 +182,7 @@ func TestDHCPRequestDNS(t *testing.T) {
}),
))

tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

req, err := dhcpv4.FromBytes(DHCPRequestPayload)
Expand Down Expand Up @@ -258,9 +250,7 @@ func TestDHCPRequestDNS_ChangedScope(t *testing.T) {
}),
))

tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

// First ensure the lease is created as we expect
Expand Down Expand Up @@ -353,9 +343,7 @@ func TestDHCP_Parallel(t *testing.T) {
}),
))

tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

wg := sync.WaitGroup{}
Expand Down
4 changes: 1 addition & 3 deletions pkg/roles/dhcp/ipam_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ func TestIPAMInternal_NextFreeAddress_UniqueParallel(t *testing.T) {
}

role := dhcp.New(inst)
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()
ipam, err := dhcp.NewInternalIPAM(role, &scope)
assert.NoError(t, err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/roles/dhcp/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (r *Role) Start(ctx context.Context, config []byte) error {
go r.startWatchScopes()
go r.startWatchLeases()

if r.cfg.Port < 1 {
return nil
}

err := r.initServer4()
if err != nil {
r.log.Warn("failed to setup server", zap.Error(err))
Expand Down
8 changes: 2 additions & 6 deletions pkg/roles/dhcp/role_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ func TestAPIRoleConfigGet(t *testing.T) {
ctx := tests.Context()
inst := rootInst.ForRole("dhcp", ctx)
role := dhcp.New(inst)
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

var output dhcp.APIRoleConfigOutput
Expand All @@ -31,9 +29,7 @@ func TestAPIRoleConfigPut(t *testing.T) {
ctx := tests.Context()
inst := rootInst.ForRole("dhcp", ctx)
role := dhcp.New(inst)
tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
tests.PanicIfError(role.Start(ctx, RoleConfig()))
defer role.Stop()

assert.NoError(t, role.APIRoleConfigPut().Interact(ctx, dhcp.APIRoleConfigInput{
Expand Down
6 changes: 6 additions & 0 deletions pkg/roles/dhcp/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/stretchr/testify/assert"
)

func RoleConfig() []byte {
return []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 0,
}))
}

func TestRoleStartNoConfig(t *testing.T) {
defer tests.Setup(t)()
rootInst := instance.New()
Expand Down
6 changes: 3 additions & 3 deletions pkg/roles/discovery/api_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestDeviceApplyDHCP(t *testing.T) {
// Create DHCP role to register events
dhcpRole := dhcp.New(rootInst.ForRole("dhcp", ctx))
tests.PanicIfError(dhcpRole.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
Port: 0,
}))))
defer dhcpRole.Stop()

Expand Down Expand Up @@ -156,7 +156,7 @@ func TestDeviceApplyDHCPWithDNS(t *testing.T) {
// Create DHCP role to register events
dhcpRole := dhcp.New(rootInst.ForRole("dhcp", ctx))
tests.PanicIfError(dhcpRole.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
Port: 0,
}))))
defer dhcpRole.Stop()
// Create DNS role to register events
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestDeviceApplyDNSWithReverse(t *testing.T) {
// Create DNS role to register events
dnsRole := dns.New(rootInst.ForRole("dns", ctx))
tests.PanicIfError(dnsRole.Start(ctx, []byte(tests.MustJSON(dns.RoleConfig{
Port: 1054,
Port: 0,
}))))
defer dnsRole.Stop()

Expand Down
5 changes: 4 additions & 1 deletion pkg/roles/dns/api_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
recordCount := slices.Collect(func(yield func(int) bool) {
for _, v := range rawRecords.Kvs {
if strings.HasPrefix(string(v.Key), _zone.etcdKey+"/") {
yield(1)
ok := yield(1)
if !ok {
return
}

Check warning on line 75 in pkg/roles/dns/api_zones.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/dns/api_zones.go#L72-L75

Added lines #L72 - L75 were not covered by tests
}
}
})
Expand Down
47 changes: 47 additions & 0 deletions pkg/roles/dns/handler_int_soa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dns

import (
"fmt"

"beryju.io/gravity/pkg/roles/dns/utils"
"github.com/miekg/dns"
)

type SOAIntHandler struct {
zone *Zone
}

func (sih *SOAIntHandler) Identifier() string {
return "int_soa"
}

func (sih *SOAIntHandler) Handle(w *utils.FakeDNSWriter, r *utils.DNSRequest) *dns.Msg {
if !sih.zone.Authoritative {
return nil
}
m := new(dns.Msg)
m.Authoritative = true
m.Answer = []dns.RR{
&dns.SOA{
Hdr: dns.RR_Header{
Name: sih.zone.Name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: sih.zone.DefaultTTL,
},
Ns: sih.zone.Name,
Mbox: fmt.Sprintf("root.%s", sih.zone.Name),
Serial: 1337,
Refresh: 600,
Retry: 15,
Expire: 5,
Minttl: sih.zone.DefaultTTL,
},
}
for _, qs := range r.Question {
if qs.Qtype == dns.TypeSOA {
return m
}
}
return nil

Check warning on line 46 in pkg/roles/dns/handler_int_soa.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/dns/handler_int_soa.go#L46

Added line #L46 was not covered by tests
}
53 changes: 53 additions & 0 deletions pkg/roles/dns/handler_int_soa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dns_test

import (
"testing"

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dns"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
d "github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)

func TestRoleDNS_SOA(t *testing.T) {
defer tests.Setup(t)()
rootInst := instance.New()
ctx := tests.Context()
inst := rootInst.ForRole("dns", ctx)
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyZones,
".",
).String(),
tests.MustJSON(dns.Zone{
Authoritative: true,
HandlerConfigs: []map[string]interface{}{
{
"type": "forward_ip",
"to": "127.0.0.1:1053",
},
},
}),
))
role := dns.New(inst)
assert.NotNil(t, role)
assert.Nil(t, role.Start(ctx, RoleConfig()))
defer role.Stop()

fw := NewNullDNSWriter()
role.Handler(fw, &d.Msg{
Question: []d.Question{
{
Name: ".",
Qtype: d.TypeSOA,
Qclass: d.ClassINET,
},
},
})
ans := fw.Msg().Answer[0]
assert.Equal(t, ".", ans.(*d.SOA).Ns)
}
3 changes: 3 additions & 0 deletions pkg/roles/dns/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (r *Role) Start(ctx context.Context, config []byte) error {
),
),
)
if r.cfg.Port < 1 {
return nil
}
listen := extconfig.Get().Listen(r.cfg.Port)
if runtime.GOOS == "darwin" {
listen = fmt.Sprintf(":%d", r.cfg.Port)
Expand Down
2 changes: 1 addition & 1 deletion pkg/roles/dns/role_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAPIRoleConfigPut(t *testing.T) {

assert.NoError(t, role.APIRoleConfigPut().Interact(ctx, dns.APIRoleConfigInput{
Config: dns.RoleConfig{
Port: 1054,
Port: 0,
},
}, &struct{}{}))
}
2 changes: 1 addition & 1 deletion pkg/roles/dns/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func RoleConfig() []byte {
return []byte(tests.MustJSON(dns.RoleConfig{
Port: 1054,
Port: 0,
}))
}

Expand Down
Loading
Loading