Skip to content

Commit

Permalink
continuing to improve associations
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Dec 8, 2024
1 parent f173be8 commit 9480aec
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 93 deletions.
8 changes: 4 additions & 4 deletions engine/plugins/expansion/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ func (cr *contactrec) check(e *et.Event) error {
return nil
}

func (cr *contactrec) lookup(e *et.Event, asset *dbt.Entity, m *config.Matches) []*support.Finding {
func (cr *contactrec) lookup(e *et.Event, entity *dbt.Entity, m *config.Matches) []*support.Finding {
var rtypes []string
confs := make(map[string]int)
sinces := make(map[string]time.Time)
conrec := entity.Asset.(*contact.ContactRecord)

for _, atype := range cr.transforms {
if !m.IsMatch(atype) {
Expand Down Expand Up @@ -124,7 +125,7 @@ func (cr *contactrec) lookup(e *et.Event, asset *dbt.Entity, m *config.Matches)
}

var findings []*support.Finding
if edges, err := e.Session.Cache().OutgoingEdges(asset, time.Time{}, rtypes...); err == nil && len(edges) > 0 {
if edges, err := e.Session.Cache().OutgoingEdges(entity, time.Time{}, rtypes...); err == nil && len(edges) > 0 {
for _, edge := range edges {
a, err := e.Session.Cache().FindEntityById(edge.ToEntity.ID)
if err != nil {
Expand All @@ -136,9 +137,8 @@ func (cr *contactrec) lookup(e *et.Event, asset *dbt.Entity, m *config.Matches)
continue
}

conrec := asset.Asset.(*contact.ContactRecord)
findings = append(findings, &support.Finding{
From: asset,
From: entity,
FromName: "ContactRecord: " + conrec.DiscoveredAt,
To: a,
ToName: a.Asset.Key(),
Expand Down
18 changes: 9 additions & 9 deletions engine/plugins/expansion/tls_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,6 @@ func (te *tlsexpand) storeContact(e *et.Event, c *tlsContact, asset *dbt.Entity,
return findings
}

t := asset.Asset.(*oamcert.TLSCertificate)
findings = append(findings, &support.Finding{
From: asset,
FromName: "TLSCertificate: " + t.SerialNumber,
To: cr,
ToName: "ContactRecord" + c.DiscoveredAt,
Rel: &relation.SimpleRelation{Name: c.RelationName},
})

if foundaddr && m.IsMatch(string(oam.Location)) {
var addr string
fields := [][]string{
Expand Down Expand Up @@ -403,6 +394,15 @@ func (te *tlsexpand) storeContact(e *et.Event, c *tlsContact, asset *dbt.Entity,
}
}

t := asset.Asset.(*oamcert.TLSCertificate)
findings = append(findings, &support.Finding{
From: asset,
FromName: "TLSCertificate: " + t.SerialNumber,
To: cr,
ToName: "ContactRecord" + c.DiscoveredAt,
Rel: &relation.SimpleRelation{Name: c.RelationName},
})

return findings
}

Expand Down
3 changes: 2 additions & 1 deletion engine/plugins/horizontals/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ func (h *horContact) check(e *et.Event) error {

if len(impacted) > 0 {
h.plugin.process(e, impacted)
h.plugin.addAssociatedRelationship(e, assocs)
}

h.plugin.addAssociatedRelationship(e, assocs)
}
return nil
}
Expand Down
9 changes: 0 additions & 9 deletions engine/plugins/horizontals/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ func (h *horizPlugin) makeAssocRelationshipEntries(e *et.Event, assoc, assoc2 *d
if assoc.ID == assoc2.ID {
return
}
// check that this relationship has not already been setup during this session
if edges, err := e.Session.Cache().OutgoingEdges(assoc,
e.Session.Cache().StartTime(), "associated_with"); err == nil && len(edges) > 0 {
for _, edge := range edges {
if edge.ToEntity.ID == assoc2.ID {
return
}
}
}

_, _ = e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "associated_with"},
Expand Down
96 changes: 26 additions & 70 deletions engine/plugins/rdap/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,7 @@ func (rd *rdapPlugin) storeEntity(e *et.Event, level int, entity *rdap.Entity, a
if err != nil {
return nil
}
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "url"},
FromEntity: cr,
ToEntity: a,
}); err == nil && edge != nil {
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
}
_ = rd.createContactEdge(e.Session, cr, a, &relation.SimpleRelation{Name: "url"}, src)
}

v := entity.VCard
Expand All @@ -248,30 +239,12 @@ func (rd *rdapPlugin) storeEntity(e *et.Event, level int, entity *rdap.Entity, a
if kind := strings.Join(prop.Values(), " "); name != "" && kind == "individual" {
if p := support.FullNameToPerson(name); p != nil && m.IsMatch(string(oam.Person)) {
if a, err := e.Session.Cache().CreateAsset(p); err == nil && a != nil {
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "person"},
FromEntity: cr,
ToEntity: a,
}); err == nil && edge != nil {
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
}
_ = rd.createContactEdge(e.Session, cr, a, &relation.SimpleRelation{Name: "person"}, src)
}
}
} else if name != "" && m.IsMatch(string(oam.Organization)) {
if a, err := e.Session.Cache().CreateAsset(&org.Organization{Name: name}); err == nil && a != nil {
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "organization"},
FromEntity: cr,
ToEntity: a,
}); err == nil && edge != nil {
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
}
_ = rd.createContactEdge(e.Session, cr, a, &relation.SimpleRelation{Name: "organization"}, src)
}
}
if adr := v.GetFirst("adr"); adr != nil && m.IsMatch(string(oam.Location)) {
Expand All @@ -281,63 +254,27 @@ func (rd *rdapPlugin) storeEntity(e *et.Event, level int, entity *rdap.Entity, a
addr := strings.Join(strings.Split(s, "\n"), " ")
if loc := support.StreetAddressToLocation(addr); loc != nil {
if a, err := e.Session.Cache().CreateAsset(loc); err == nil && a != nil {
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "location"},
FromEntity: cr,
ToEntity: a,
}); err == nil && edge != nil {
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
}
_ = rd.createContactEdge(e.Session, cr, a, &relation.SimpleRelation{Name: "location"}, src)
}
}
}
}
if email := support.EmailToOAMEmailAddress(v.Email()); email != nil && m.IsMatch(string(oam.EmailAddress)) {
if a, err := e.Session.Cache().CreateAsset(email); err == nil && a != nil {
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "email"},
FromEntity: cr,
ToEntity: a,
}); err == nil && edge != nil {
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
}
_ = rd.createContactEdge(e.Session, cr, a, &relation.SimpleRelation{Name: "email"}, src)
}
}
if m.IsMatch(string(oam.Phone)) {
if phone := support.PhoneToOAMPhone(v.Tel(), "", v.Country()); phone != nil {
phone.Type = contact.PhoneTypeRegular
if a, err := e.Session.Cache().CreateAsset(phone); err == nil && a != nil {
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "phone"},
FromEntity: cr,
ToEntity: a,
}); err == nil && edge != nil {
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
}
_ = rd.createContactEdge(e.Session, cr, a, &relation.SimpleRelation{Name: "phone"}, src)
}
}
if fax := support.PhoneToOAMPhone(v.Fax(), "", v.Country()); fax != nil {
fax.Type = contact.PhoneTypeFax
if a, err := e.Session.Cache().CreateAsset(fax); err == nil && a != nil {
if edge, err := e.Session.Cache().CreateEdge(&dbt.Edge{
Relation: &relation.SimpleRelation{Name: "phone"},
FromEntity: cr,
ToEntity: a,
}); err == nil && edge != nil {
_, _ = e.Session.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
}
_ = rd.createContactEdge(e.Session, cr, a, &relation.SimpleRelation{Name: "phone"}, src)
}
}
}
Expand All @@ -359,3 +296,22 @@ func (rd *rdapPlugin) getJSONLink(links []rdap.Link) *url.URL {
}
return url
}

func (rd *rdapPlugin) createContactEdge(sess et.Session, cr, a *dbt.Entity, rel oam.Relation, src *et.Source) error {
edge, err := sess.Cache().CreateEdge(&dbt.Edge{
Relation: rel,
FromEntity: cr,
ToEntity: a,
})
if err != nil {
return err
} else if edge == nil {
return errors.New("failed to create the edge")
}

_, err = sess.Cache().CreateEdgeProperty(edge, &property.SourceProperty{
Source: src.Name,
Confidence: src.Confidence,
})
return err
}

0 comments on commit 9480aec

Please sign in to comment.