-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
dns_node.go
72 lines (62 loc) · 1.79 KB
/
dns_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
// Package activitytree holds activitytree related files
package activitytree
import (
"strings"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/utils"
)
// DNSNode is used to store a DNS node
type DNSNode struct {
MatchedRules []*model.MatchedRule
ImageTags []string
GenerationType NodeGenerationType
Requests []model.DNSEvent
}
// NewDNSNode returns a new DNSNode instance
func NewDNSNode(event *model.DNSEvent, rules []*model.MatchedRule, generationType NodeGenerationType, imageTag string) *DNSNode {
node := &DNSNode{
MatchedRules: rules,
GenerationType: generationType,
Requests: []model.DNSEvent{*event},
}
if imageTag != "" {
node.ImageTags = []string{imageTag}
}
return node
}
func dnsFilterSubdomains(name string, maxDepth int) string {
tab := strings.Split(name, ".")
if len(tab) < maxDepth {
return name
}
result := ""
for i := 0; i < maxDepth; i++ {
if result != "" {
result = "." + result
}
result = tab[len(tab)-i-1] + result
}
return result
}
func (dn *DNSNode) appendImageTag(imageTag string) {
dn.ImageTags, _ = AppendIfNotPresent(dn.ImageTags, imageTag)
}
func (dn *DNSNode) evictImageTag(imageTag string, DNSNames *utils.StringKeys) bool {
imageTags, removed := removeImageTagFromList(dn.ImageTags, imageTag)
if removed {
if len(imageTags) == 0 {
return true
}
dn.ImageTags = imageTags
}
// reconstruct the list of all DNS requests
if len(dn.Requests) > 0 {
DNSNames.Insert(dn.Requests[0].Name)
}
return false
}