Skip to content

Commit

Permalink
Revert "Allow the network mapper to track previously resolved DNS add…
Browse files Browse the repository at this point in the history
…resses based on ongoing TCP traffic"

This reverts commit 6c1314d.
  • Loading branch information
orishoshan committed Sep 19, 2024
1 parent 7a539ca commit 6699366
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 44 deletions.
26 changes: 2 additions & 24 deletions src/mapper/pkg/dnscache/dns_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ import (
)

type DNSCache struct {
cache *ttlcache.Cache[string, string]
ipToNameCache *ttlcache.Cache[string, string]
cache *ttlcache.Cache[string, string]
}

func NewDNSCache() *DNSCache {
capacity := viper.GetInt(config.DNSCacheItemsMaxCapacityKey)
dnsRecordCache := ttlcache.New[string, string](ttlcache.WithCapacity[string, string](uint64(capacity)))
go dnsRecordCache.Start()
ipToNameCache := ttlcache.New[string, string](ttlcache.WithCapacity[string, string](uint64(capacity)))
go ipToNameCache.Start()

lastCapacityReachedErrorPrint := time.Time{}
ipToNameLastCapacityReachedErrorPrint := time.Time{}
dnsRecordCache.OnEviction(func(ctx context.Context, reason ttlcache.EvictionReason, item *ttlcache.Item[string, string]) {
if reason == ttlcache.EvictionReasonCapacityReached && time.Since(lastCapacityReachedErrorPrint) > time.Minute {
logrus.Warningf("DNS cache capacity reached entries are being dropped, consider increasing config '%s'",
Expand All @@ -31,23 +27,13 @@ func NewDNSCache() *DNSCache {
}
})

ipToNameCache.OnEviction(func(ctx context.Context, reason ttlcache.EvictionReason, item *ttlcache.Item[string, string]) {
if reason == ttlcache.EvictionReasonCapacityReached && time.Since(ipToNameLastCapacityReachedErrorPrint) > time.Minute {
logrus.Warningf("DNS cache capacity reached entries are being dropped, consider increasing config '%s'",
config.DNSCacheItemsMaxCapacityKey)
ipToNameLastCapacityReachedErrorPrint = time.Now()
}
})

return &DNSCache{
cache: dnsRecordCache,
ipToNameCache: ipToNameCache,
cache: dnsRecordCache,
}
}

func (d *DNSCache) AddOrUpdateDNSData(dnsName string, ip string, ttl time.Duration) {
d.cache.Set(dnsName, ip, ttl)
d.ipToNameCache.Set(ip, dnsName, ttl)
}

func (d *DNSCache) GetResolvedIP(dnsName string) (string, bool) {
Expand All @@ -57,11 +43,3 @@ func (d *DNSCache) GetResolvedIP(dnsName string) (string, bool) {
}
return entry.Value(), true
}

func (d *DNSCache) GetResolvedDNSName(ip string) (string, bool) {
entry := d.ipToNameCache.Get(ip)
if entry == nil {
return "", false
}
return entry.Value(), true
}
14 changes: 7 additions & 7 deletions src/mapper/pkg/resolvers/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package resolvers
import (
"context"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/bugsnag/bugsnag-go/v2"
"github.com/labstack/echo/v4"
"github.com/otterize/intents-operator/src/shared/errors"
"github.com/otterize/intents-operator/src/shared/serviceidresolver"
"github.com/otterize/intents-operator/src/shared/telemetries/errorreporter"
"github.com/otterize/network-mapper/src/mapper/pkg/awsintentsholder"
"github.com/otterize/network-mapper/src/mapper/pkg/dnscache"
"github.com/otterize/network-mapper/src/mapper/pkg/externaltrafficholder"
Expand Down Expand Up @@ -84,27 +84,27 @@ func (r *Resolver) Register(e *echo.Echo) {
func (r *Resolver) RunForever(ctx context.Context) error {
errgrp, errGrpCtx := errgroup.WithContext(ctx)
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
defer bugsnag.AutoNotify(errGrpCtx)
return runHandleLoop(errGrpCtx, r.dnsCaptureResults, r.handleReportCaptureResults)
})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
defer bugsnag.AutoNotify(errGrpCtx)
return runHandleLoop(errGrpCtx, r.tcpCaptureResults, r.handleReportTCPCaptureResults)
})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
defer bugsnag.AutoNotify(errGrpCtx)
return runHandleLoop(errGrpCtx, r.socketScanResults, r.handleReportSocketScanResults)
})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
defer bugsnag.AutoNotify(errGrpCtx)
return runHandleLoop(errGrpCtx, r.kafkaMapperResults, r.handleReportKafkaMapperResults)
})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
defer bugsnag.AutoNotify(errGrpCtx)
return runHandleLoop(errGrpCtx, r.istioConnectionResults, r.handleReportIstioConnectionResults)
})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
defer bugsnag.AutoNotify(errGrpCtx)
return runHandleLoop(errGrpCtx, r.awsOperations, r.handleAWSOperationReport)
})
err := errgrp.Wait()
Expand Down
15 changes: 2 additions & 13 deletions src/mapper/pkg/resolvers/schema.helpers.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (r *Resolver) handleReportTCPCaptureResults(ctx context.Context, results mo
}

for _, dest := range captureItem.Destinations {
r.handleIncomingTCPResult(ctx, srcSvcIdentity, dest)
r.handleExternalIncomingTrafficTCPResult(ctx, srcSvcIdentity, dest)
}
}
telemetrysender.SendNetworkMapper(telemetriesgql.EventTypeIntentsDiscoveredCapture, len(results.Results))
Expand Down Expand Up @@ -427,25 +427,14 @@ func (r *Resolver) reportIncomingInternetTraffic(ctx context.Context, srcIP stri
return nil
}

func (r *Resolver) handleIncomingTCPResult(ctx context.Context, srcIdentity model.OtterizeServiceIdentity, dest model.Destination) {
func (r *Resolver) handleExternalIncomingTrafficTCPResult(ctx context.Context, srcIdentity model.OtterizeServiceIdentity, dest model.Destination) {
lastSeen := dest.LastSeen
destIdentity, ok, err := r.resolveDestIdentity(ctx, dest, lastSeen)
if err != nil {
logrus.WithError(err).Error("could not resolve destination identity")
return
}
if !ok {
// If the destination is not in cluster, check if it's traffic that goes to an IP address that we previously resolved by DNS.
dnsName, found := r.dnsCache.GetResolvedDNSName(dest.Destination)
if found && dest.DestinationIP != nil {
intent := externaltrafficholder.ExternalTrafficIntent{
Client: srcIdentity,
LastSeen: dest.LastSeen,
DNSName: dnsName,
IPs: map[externaltrafficholder.IP]struct{}{externaltrafficholder.IP(*dest.DestinationIP): {}},
}
r.externalTrafficIntentsHolder.AddIntent(intent)
}
return
}

Expand Down

0 comments on commit 6699366

Please sign in to comment.