@@ -16,6 +16,7 @@ import (
1616 dbt "github.com/owasp-amass/asset-db/types"
1717 oam "github.com/owasp-amass/open-asset-model"
1818 "github.com/owasp-amass/open-asset-model/general"
19+ "github.com/owasp-amass/open-asset-model/org"
1920)
2021
2122type relatedOrgs struct {
@@ -27,29 +28,76 @@ func (ro *relatedOrgs) check(e *et.Event) error {
2728 ident , ok := e .Entity .Asset .(* general.Identifier )
2829 if ! ok {
2930 return errors .New ("failed to extract the Identifier asset" )
31+ } else if ident .Type != general .LEICode {
32+ return nil
3033 }
3134
32- since , err := support .TTLStartTime (e .Session .Config (), string (oam .Identifier ), string (oam .Identifier ), lr .name )
35+ since , err := support .TTLStartTime (e .Session .Config (), string (oam .Identifier ), string (oam .Identifier ), ro .name )
3336 if err != nil {
3437 return err
3538 }
3639
37- var names []* dbt.Entity
40+ var orgs []* dbt.Entity
3841 if support .AssetMonitoredWithinTTL (e .Session , e .Entity , ro .plugin .source , since ) {
39- names = append (names , ro .lookup (e , ident , ro .plugin .source , since )... )
42+ orgs = append (orgs , ro .lookup (e , e . Entity , ro .plugin .source , since )... )
4043 } else {
41- names = append (names , ro .query (e , ident , ro .plugin .source )... )
44+ orgs = append (orgs , ro .query (e , ident , ro .plugin .source )... )
4245 support .MarkAssetMonitored (e .Session , e .Entity , ro .plugin .source )
4346 }
4447
45- if len (names ) > 0 {
46- ro .process (e , names , ro .plugin .source )
48+ if len (orgs ) > 0 {
49+ ro .process (e , orgs , ro .plugin .source )
4750 }
4851 return nil
4952}
5053
51- func (ro * relatedOrgs ) lookup (e * et.Event , ident * general.Identifier , src * et.Source , since time.Time ) []* dbt.Entity {
52- return support .SourceToAssetsWithinTTL (e .Session , ident .Key (), string (oam .Identifier ), ro .plugin .source , since )
54+ func (ro * relatedOrgs ) lookup (e * et.Event , ident * dbt.Entity , src * et.Source , since time.Time ) []* dbt.Entity {
55+ var o * dbt.Entity
56+
57+ if edges , err := e .Session .Cache ().IncomingEdges (ident , since , "id" ); err == nil {
58+ for _ , edge := range edges {
59+ if tags , err := e .Session .Cache ().GetEdgeTags (edge , since , src .Name ); err != nil || len (tags ) == 0 {
60+ continue
61+ }
62+ if a , err := e .Session .Cache ().FindEntityById (edge .FromEntity .ID ); err == nil && a != nil {
63+ if _ , ok := a .Asset .(* org.Organization ); ok {
64+ o = a
65+ break
66+ }
67+ }
68+ }
69+ }
70+
71+ var p * dbt.Entity
72+ if edges , err := e .Session .Cache ().OutgoingEdges (o , since , "parent" ); err == nil {
73+ for _ , edge := range edges {
74+ if tags , err := e .Session .Cache ().GetEdgeTags (edge , since , src .Name ); err != nil || len (tags ) == 0 {
75+ continue
76+ }
77+ if a , err := e .Session .Cache ().FindEntityById (edge .ToEntity .ID ); err == nil && a != nil {
78+ if _ , ok := a .Asset .(* org.Organization ); ok {
79+ p = a
80+ break
81+ }
82+ }
83+ }
84+ }
85+
86+ var children []* dbt.Entity
87+ if edges , err := e .Session .Cache ().OutgoingEdges (o , since , "subsidiary" ); err == nil {
88+ for _ , edge := range edges {
89+ if tags , err := e .Session .Cache ().GetEdgeTags (edge , since , src .Name ); err != nil || len (tags ) == 0 {
90+ continue
91+ }
92+ if a , err := e .Session .Cache ().FindEntityById (edge .ToEntity .ID ); err == nil && a != nil {
93+ if _ , ok := a .Asset .(* org.Organization ); ok {
94+ children = append (children , a )
95+ }
96+ }
97+ }
98+ }
99+
100+ return append ([]* dbt.Entity {o , p }, children ... )
53101}
54102
55103func (ro * relatedOrgs ) query (e * et.Event , ident * general.Identifier , src * et.Source ) []* dbt.Entity {
@@ -62,11 +110,11 @@ func (ro *relatedOrgs) query(e *et.Event, ident *general.Identifier, src *et.Sou
62110 }
63111
64112 var result singleResponse
65- if err := json .Unmarshal ([]byte (resp .Body ), & result ); err != nil || len ( result . Data ) == 0 {
113+ if err := json .Unmarshal ([]byte (resp .Body ), & result ); err != nil {
66114 return nil
67115 }
68116
69- return ro .store (e , & result , ro . plugin . source )
117+ return ro .store (e , & result , src )
70118}
71119
72120func (ro * relatedOrgs ) store (e * et.Event , lei * singleResponse , src * et.Source ) []* dbt.Entity {
0 commit comments