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

Adding support for wildcard prefix in DNS internet intents (*.example.com) #256

Merged
merged 4 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions src/mapper/pkg/dnscache/dns_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net"
"strings"
"time"
)

Expand Down Expand Up @@ -38,6 +39,17 @@ func (d *DNSCache) GetResolvedIPs(dnsName string) []string {
return entry
}

func (d *DNSCache) GetMatchingEntriesForWildcard(dnsName string) []string {
evyatarmeged marked this conversation as resolved.
Show resolved Hide resolved
result := make([]string, 0)
dnsSuffix := strings.TrimPrefix(dnsName, "*") // Strip the wildcard, leave the '.example.com' suffix
for entry := range d.cache.items {
evyatarmeged marked this conversation as resolved.
Show resolved Hide resolved
if strings.HasSuffix(entry, dnsSuffix) {
result = append(result, entry)
}
}
return result
}

// CacheValue holds the value and its expiration time
type CacheValue[V any] struct {
Value V
Expand Down
27 changes: 27 additions & 0 deletions src/mapper/pkg/dnscache/dns_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ func (s *DNSCacheTestSuite) TestTTL() {

}

func (s *DNSCacheTestSuite) TestWildcardIP() {
cache := NewDNSCache()
cache.AddOrUpdateDNSData("www.surf-forecast.com", IP1, 60*time.Second)
dnsNames := cache.GetMatchingEntriesForWildcard("*.surf-forecast.com")
s.Require().Len(dnsNames, 1)
s.Require().Equal(dnsNames[0], "www.surf-forecast.com")

resolvedIPs := cache.GetResolvedIPs(dnsNames[0])
s.Require().Len(resolvedIPs, 1)
s.Require().Equal(resolvedIPs[0], IP1)
}

func (s *DNSCacheTestSuite) TestMultipleWildcardIPs() {
cache := NewDNSCache()
cache.AddOrUpdateDNSData("www.surf-forecast.com", IP1, 60*time.Second)
cache.AddOrUpdateDNSData("api.surf-forecast.com", IP2, 60*time.Second)
dnsNames := cache.GetMatchingEntriesForWildcard("*.surf-forecast.com")
s.Require().Len(dnsNames, 2)

resolvedIPs := make([]string, 0)
for _, dnsName := range dnsNames {
resolvedIPs = append(resolvedIPs, cache.GetResolvedIPs(dnsName)...)
}
s.Require().Len(resolvedIPs, 2)
s.Require().Equal(resolvedIPs, []string{IP1, IP2})
}

func TestDNSCacheTestSuite(t *testing.T) {
suite.Run(t, new(DNSCacheTestSuite))
}
22 changes: 17 additions & 5 deletions src/mapper/pkg/dnsintentspublisher/dns_intents_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"
)

Expand Down Expand Up @@ -163,19 +164,26 @@ func (p *Publisher) compareIntentsAndStatus(clientIntents otterizev2alpha1.Clien
}

func (p *Publisher) appendResolvedIps(dnsName string, resolvedIPsMap map[string]map[string]struct{}) bool {
resolvedIPs := p.dnsCache.GetResolvedIPs(dnsName)
resolvedIPs := make([]string, 0)
if p.isWildcardDNS(dnsName) {
matchingDNSNames := p.dnsCache.GetMatchingEntriesForWildcard(dnsName)
for _, name := range matchingDNSNames {
resolvedIPs = append(resolvedIPs, p.dnsCache.GetResolvedIPs(name)...)
}
} else {
resolvedIPs = p.dnsCache.GetResolvedIPs(dnsName)
}

ips, ok := resolvedIPsMap[dnsName]
if !ok {
ips = make(map[string]struct{})
}

if len(resolvedIPs) == 0 {
evyatarmeged marked this conversation as resolved.
Show resolved Hide resolved
// Try to resolve it ourselves
ctxTimeout, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
logrus.WithField("dnsName", dnsName).Warn("DNS cache miss, resolving it ourselves")

logrus.WithField("dnsName", dnsName).Debug("DNS cache miss, resolving it ourselves")
// Try to resolve it ourselves
ipaddrs, err := p.resolver.LookupIPAddr(ctxTimeout, dnsName)
if err != nil {
logrus.WithError(err).WithField("dnsName", dnsName).Error("Failed to resolve DNS")
Expand All @@ -184,7 +192,7 @@ func (p *Publisher) appendResolvedIps(dnsName string, resolvedIPsMap map[string]

for _, ip := range ipaddrs {
ips[ip.String()] = struct{}{}
p.dnsCache.AddOrUpdateDNSData(dnsName, ip.String(), 10*time.Second)
p.dnsCache.AddOrUpdateDNSData(dnsName, ip.String(), 120*time.Second)
}
} else {
logrus.WithField("dnsName", dnsName).Debug("DNS cache hit")
Expand All @@ -200,3 +208,7 @@ func (p *Publisher) appendResolvedIps(dnsName string, resolvedIPsMap map[string]
resolvedIPsMap[dnsName] = ips
return true
}

func (p *Publisher) isWildcardDNS(dnsName string) bool {
return strings.HasPrefix(dnsName, "*")
}
3 changes: 2 additions & 1 deletion src/mapper/pkg/resolvers/schema.helpers.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,12 @@ func (r *Resolver) handleDNSCaptureResultsAsExternalTraffic(_ context.Context, d
LastSeen: dest.LastSeen,
DNSName: dest.Destination,
}

ip := "(unknown)"
if dest.DestinationIP != nil {
ip = *dest.DestinationIP
intent.IPs = map[externaltrafficholder.IP]struct{}{externaltrafficholder.IP(*dest.DestinationIP): {}}
ttl := 60 * time.Second
ttl := 120 * time.Second
if dest.TTL != nil {
ttl = time.Duration(*dest.TTL) * time.Second
}
Expand Down
Loading