-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from kubescape/dns-tracer
enrich network neighbors with DNS data
- Loading branch information
Showing
12 changed files
with
355 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package dnsmanager | ||
|
||
import ( | ||
"context" | ||
"node-agent/pkg/config" | ||
"node-agent/pkg/k8sclient" | ||
"node-agent/pkg/storage" | ||
|
||
"github.com/goradd/maps" | ||
tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" | ||
) | ||
|
||
// DNSManager is used to manage DNS events and save IP resolutions. It exposes an API to resolve IP address to domain name. | ||
type DNSManager struct { | ||
addressToDomainMap maps.SafeMap[string, string] // this map is used to resolve IP address to domain name | ||
} | ||
|
||
var _ DNSManagerClient = (*DNSManager)(nil) | ||
var _ DNSResolver = (*DNSManager)(nil) | ||
|
||
func CreateDNSManager(ctx context.Context, cfg config.Config, k8sClient k8sclient.K8sClientInterface, storageClient storage.StorageClient, clusterName string) *DNSManager { | ||
return &DNSManager{} | ||
} | ||
|
||
func (dm *DNSManager) ProcessDNSEvent(dnsEvent tracerdnstype.Event) { | ||
for _, address := range dnsEvent.Addresses { | ||
dm.addressToDomainMap.Set(address, dnsEvent.DNSName) | ||
} | ||
} | ||
|
||
func (dm *DNSManager) ResolveIPAddress(ipAddr string) (string, bool) { | ||
domain := dm.addressToDomainMap.Get(ipAddr) | ||
return domain, domain != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dnsmanager | ||
|
||
import ( | ||
tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" | ||
) | ||
|
||
type DNSManagerClient interface { | ||
ProcessDNSEvent(networkEvent tracerdnstype.Event) | ||
} | ||
|
||
type DNSResolver interface { | ||
ResolveIPAddress(ipAddr string) (string, bool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dnsmanager | ||
|
||
import ( | ||
tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" | ||
) | ||
|
||
type DNSManagerMock struct { | ||
} | ||
|
||
var _ DNSManagerClient = (*DNSManagerMock)(nil) | ||
var _ DNSResolver = (*DNSManagerMock)(nil) | ||
|
||
func CreateDNSManagerMock() *DNSManagerMock { | ||
return &DNSManagerMock{} | ||
} | ||
|
||
func (n *DNSManagerMock) ProcessDNSEvent(event tracerdnstype.Event) { | ||
} | ||
|
||
func (n *DNSManagerMock) ResolveIPAddress(ipAddr string) (string, bool) { | ||
return "", false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dnsmanager | ||
|
||
import ( | ||
"testing" | ||
|
||
tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" | ||
) | ||
|
||
func TestResolveIPAddress(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
dnsEvent tracerdnstype.Event | ||
ipAddr string | ||
want string | ||
}{ | ||
{ | ||
name: "ip found", | ||
ipAddr: "14.23.332.4", | ||
dnsEvent: tracerdnstype.Event{ | ||
DNSName: "test.com", | ||
Addresses: []string{ | ||
"14.23.332.4", | ||
}, | ||
}, | ||
want: "test.com", | ||
}, | ||
{ | ||
name: "ip not found", | ||
ipAddr: "14.23.332.4", | ||
dnsEvent: tracerdnstype.Event{ | ||
DNSName: "test.com", | ||
Addresses: []string{ | ||
"54.23.332.4", | ||
}, | ||
}, | ||
want: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dm := &DNSManager{} | ||
dm.ProcessDNSEvent(tt.dnsEvent) | ||
got, _ := dm.ResolveIPAddress(tt.ipAddr) | ||
if got != tt.want { | ||
t.Errorf("ResolveIPAddress() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package containerwatcher | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection/networktracer" | ||
tracerdns "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/tracer" | ||
tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" | ||
"github.com/inspektor-gadget/inspektor-gadget/pkg/types" | ||
"github.com/kubescape/go-logger" | ||
"github.com/kubescape/go-logger/helpers" | ||
) | ||
|
||
func (ch *IGContainerWatcher) dnsEventCallback(event *tracerdnstype.Event) { | ||
if event.Type != types.NORMAL { | ||
logger.L().Ctx(ch.ctx).Warning("dns tracer got drop events - we may miss some realtime data", helpers.Interface("event", event), helpers.String("error", event.Message)) | ||
return | ||
} | ||
|
||
ch.containerCollection.EnrichByMntNs(&event.CommonData, event.MountNsID) | ||
|
||
_ = ch.dnsWorkerPool.Invoke(*event) | ||
} | ||
|
||
func (ch *IGContainerWatcher) startDNSTracing() error { | ||
if err := ch.tracerCollection.AddTracer(dnsTraceName, ch.containerSelector); err != nil { | ||
return fmt.Errorf("adding tracer: %w", err) | ||
} | ||
|
||
tracerDns, err := tracerdns.NewTracer() | ||
if err != nil { | ||
return fmt.Errorf("creating tracer: %w", err) | ||
} | ||
|
||
tracerDns.SetEventHandler(ch.dnsEventCallback) | ||
|
||
ch.dnsTracer = tracerDns | ||
|
||
config := &networktracer.ConnectToContainerCollectionConfig[tracerdnstype.Event]{ | ||
Tracer: ch.dnsTracer, | ||
Resolver: ch.containerCollection, | ||
Selector: ch.containerSelector, | ||
Base: tracerdnstype.Base, | ||
} | ||
|
||
_, err = networktracer.ConnectToContainerCollection(config) | ||
if err != nil { | ||
return fmt.Errorf("creating tracer: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ch *IGContainerWatcher) stopDNSTracing() error { | ||
// Stop dns tracer | ||
if err := ch.tracerCollection.RemoveTracer(dnsTraceName); err != nil { | ||
return fmt.Errorf("removing tracer: %w", err) | ||
} | ||
|
||
ch.dnsTracer.Close() | ||
|
||
return nil | ||
} |
Oops, something went wrong.