Skip to content

Commit ccbfa36

Browse files
authored
update handler prioriy + remove the creation of new assets for txt plugin
1 parent a3c690b commit ccbfa36

File tree

2 files changed

+25
-61
lines changed

2 files changed

+25
-61
lines changed

engine/plugins/dns/plugin.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121

2222
type dnsPlugin struct {
2323
name string
24+
txt *dnsTXT
2425
log *slog.Logger
2526
apex *dnsApex
2627
cname *dnsCNAME
2728
ip *dnsIP
28-
txt *dnsTXT
2929
reverse *dnsReverse
3030
subs *dnsSubs
3131
firstSweepSize int
@@ -73,7 +73,7 @@ func (d *dnsPlugin) Start(r et.Registry) error {
7373
if err := r.RegisterHandler(&et.Handler{
7474
Plugin: d,
7575
Name: d.cname.name,
76-
Priority: 1,
76+
Priority: 2,
7777
MaxInstances: support.MaxHandlerInstances,
7878
Transforms: []string{string(oam.FQDN)},
7979
EventType: oam.FQDN,
@@ -90,7 +90,7 @@ func (d *dnsPlugin) Start(r et.Registry) error {
9090
if err := r.RegisterHandler(&et.Handler{
9191
Plugin: d,
9292
Name: d.ip.name,
93-
Priority: 2,
93+
Priority: 3,
9494
MaxInstances: support.MaxHandlerInstances,
9595
Transforms: []string{string(oam.IPAddress)},
9696
EventType: oam.FQDN,
@@ -130,7 +130,7 @@ func (d *dnsPlugin) Start(r et.Registry) error {
130130
if err := r.RegisterHandler(&et.Handler{
131131
Plugin: d,
132132
Name: d.txt.name,
133-
Priority: 3,
133+
Priority: 1,
134134
MaxInstances: support.MaxHandlerInstances,
135135
Transforms: []string{string(oam.FQDN)},
136136
EventType: oam.FQDN,

engine/plugins/dns/txt.go

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ type dnsTXT struct {
2020
plugin *dnsPlugin
2121
}
2222

23-
type relTXT struct {
24-
txt *dbt.Entity
25-
target *dbt.Entity
26-
}
27-
2823
func (d *dnsTXT) check(e *et.Event) error {
2924
_, ok := e.Entity.Asset.(*oamdns.FQDN)
3025
if !ok {
@@ -36,93 +31,62 @@ func (d *dnsTXT) check(e *et.Event) error {
3631
return err
3732
}
3833

39-
var txtRecords []*relTXT
34+
var txtRecords []*resolve.ExtractedAnswer
4035
src := d.plugin.source
4136
if support.AssetMonitoredWithinTTL(e.Session, e.Entity, src, since) {
42-
txtRecords = append(txtRecords, d.lookup(e, e.Entity, since)...)
37+
txtRecords = d.lookup(e, e.Entity, since)
4338
} else {
44-
txtRecords = append(txtRecords, d.query(e, e.Entity)...)
39+
txtRecords = d.query(e, e.Entity)
4540
}
4641

4742
if len(txtRecords) > 0 {
48-
d.process(e, txtRecords)
43+
d.process(e, e.Entity, txtRecords)
4944
}
5045
return nil
5146
}
5247

53-
func (d *dnsTXT) lookup(e *et.Event, fqdn *dbt.Entity, since time.Time) []*relTXT {
54-
var txtRecords []*relTXT
48+
func (d *dnsTXT) lookup(e *et.Event, fqdn *dbt.Entity, since time.Time) []*resolve.ExtractedAnswer {
49+
var txtRecords []*resolve.ExtractedAnswer
5550

5651
n, ok := fqdn.Asset.(*oamdns.FQDN)
5752
if !ok || n == nil {
5853
return txtRecords
5954
}
6055

61-
if assets := d.plugin.lookupWithinTTL(e.Session, n.Name, oam.FQDN, since, oam.BasicDNSRelation, 5); len(assets) > 0 {
56+
if assets := d.plugin.lookupWithinTTL(e.Session, n.Name, oam.FQDN, since, oam.BasicDNSRelation, dns.TypeTXT); len(assets) > 0 {
6257
for _, a := range assets {
63-
txtRecords = append(txtRecords, &relTXT{txt: fqdn, target: a})
58+
txtRecords = append(txtRecords, &resolve.ExtractedAnswer{
59+
Type: dns.TypeTXT,
60+
Data: a.Asset.(*oamdns.FQDN).Name,
61+
})
6462
}
6563
}
6664
return txtRecords
6765
}
6866

69-
func (d *dnsTXT) query(e *et.Event, name *dbt.Entity) []*relTXT {
70-
var txtRecords []*relTXT
67+
func (d *dnsTXT) query(e *et.Event, name *dbt.Entity) []*resolve.ExtractedAnswer {
68+
var txtRecords []*resolve.ExtractedAnswer
7169

7270
fqdn := name.Asset.(*oamdns.FQDN)
7371
if rr, err := support.PerformQuery(fqdn.Name, dns.TypeTXT); err == nil {
74-
if records := d.store(e, name, rr); len(records) > 0 {
75-
txtRecords = append(txtRecords, records...)
76-
support.MarkAssetMonitored(e.Session, name, d.plugin.source)
77-
}
72+
txtRecords = append(txtRecords, rr...)
73+
support.MarkAssetMonitored(e.Session, name, d.plugin.source)
7874
}
7975

8076
return txtRecords
8177
}
8278

83-
func (d *dnsTXT) store(e *et.Event, fqdn *dbt.Entity, rr []*resolve.ExtractedAnswer) []*relTXT {
84-
var txtRecords []*relTXT
85-
86-
for _, record := range rr {
79+
func (d *dnsTXT) process(e *et.Event, fqdn *dbt.Entity, txtRecords []*resolve.ExtractedAnswer) {
80+
for _, record := range txtRecords {
8781
if record.Type != dns.TypeTXT {
8882
continue
8983
}
9084

91-
if txt, err := e.Session.Cache().CreateAsset(&oamdns.FQDN{Name: record.Data}); err == nil && txt != nil {
92-
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
93-
Relation: &oamdns.BasicDNSRelation{
94-
Name: "dns_record",
95-
Header: oamdns.RRHeader{
96-
RRType: int(record.Type),
97-
Class: 1,
98-
},
99-
},
100-
FromEntity: fqdn,
101-
ToEntity: txt,
102-
}); err == nil && edge != nil {
103-
txtRecords = append(txtRecords, &relTXT{txt: fqdn, target: txt})
104-
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &general.SourceProperty{
105-
Source: d.plugin.source.Name,
106-
Confidence: d.plugin.source.Confidence,
107-
})
108-
}
109-
}
110-
}
111-
112-
return txtRecords
113-
}
114-
115-
func (d *dnsTXT) process(e *et.Event, txtRecords []*relTXT) {
116-
for _, a := range txtRecords {
117-
target := a.target.Asset.(*oamdns.FQDN)
118-
119-
_ = e.Dispatcher.DispatchEvent(&et.Event{
120-
Name: target.Name,
121-
Entity: a.target,
122-
Session: e.Session,
85+
_, _ = e.Session.Cache().CreateEntityProperty(fqdn, &oamdns.DNSProperty{
86+
Name: "TXT",
87+
Value: record.Data,
12388
})
12489

125-
e.Session.Log().Info("relationship discovered", "from", d.plugin.source.Name, "relation",
126-
"txt_record", "to", target.Name, slog.Group("plugin", "name", d.plugin.name, "handler", d.name))
90+
e.Session.Log().Info("TXT record discovered", "fqdn", fqdn.Asset.(*oamdns.FQDN).Name, "txt", record.Data, slog.Group("plugin", "name", d.plugin.name, "handler", d.name))
12791
}
12892
}

0 commit comments

Comments
 (0)