@@ -6,29 +6,21 @@ package gleif
6
6
7
7
import (
8
8
"context"
9
- "encoding/json"
10
9
"errors"
11
- "fmt"
12
10
"log/slog"
13
- "math"
14
- "net/url"
15
- "strings"
16
11
"time"
17
12
18
- "github.com/adrg/strutil"
19
- "github.com/adrg/strutil/metrics"
20
13
"github.com/owasp-amass/amass/v5/engine/plugins/support"
14
+ "github.com/owasp-amass/amass/v5/engine/plugins/support/org"
21
15
et "github.com/owasp-amass/amass/v5/engine/types"
22
- "github.com/owasp-amass/amass/v5/internal/net/http"
23
16
dbt "github.com/owasp-amass/asset-db/types"
24
17
oam "github.com/owasp-amass/open-asset-model"
25
- "github.com/owasp-amass/open-asset-model/contact"
26
18
"github.com/owasp-amass/open-asset-model/general"
27
- "github.com/owasp-amass/open-asset-model/org"
19
+ oamorg "github.com/owasp-amass/open-asset-model/org"
28
20
)
29
21
30
22
func (fc * fuzzyCompletions ) check (e * et.Event ) error {
31
- _ , ok := e .Entity .Asset .(* org .Organization )
23
+ _ , ok := e .Entity .Asset .(* oamorg .Organization )
32
24
if ! ok {
33
25
return errors .New ("failed to extract the Organization asset" )
34
26
}
@@ -72,190 +64,39 @@ func (fc *fuzzyCompletions) lookup(e *et.Event, orgent *dbt.Entity, since time.T
72
64
73
65
func (fc * fuzzyCompletions ) query (e * et.Event , orgent * dbt.Entity ) * dbt.Entity {
74
66
var conf int
75
- var rec * leiRecord
67
+ var rec * org. LEIRecord
76
68
77
69
if leient := fc .plugin .orgEntityToLEI (e , orgent ); leient != nil {
78
70
lei := leient .Asset .(* general.Identifier )
79
71
80
- if r , err := fc . plugin . getLEIRecord (lei ); err == nil {
72
+ if r , err := org . GLEIFGetLEIRecord (lei . ID ); err == nil {
81
73
rec = r
82
74
conf = 100
83
75
}
84
76
}
85
77
86
78
if rec == nil {
87
- o := orgent .Asset .(* org.Organization )
88
- brand := support .ExtractBrandName (o .Name )
89
- u := "https://api.gleif.org/api/v1/fuzzycompletions?field=entity.legalName&q=" + url .QueryEscape (brand )
90
-
79
+ o := orgent .Asset .(* oamorg.Organization )
80
+ brand := org .ExtractBrandName (o .Name )
91
81
_ = fc .plugin .rlimit .Wait (context .TODO ())
92
- resp , err := http .RequestWebPage (context .TODO (), & http.Request {URL : u })
93
- if err != nil || resp .Body == "" {
94
- msg := fmt .Sprintf ("Failed to obtain the LEI record for %s: %s" , o .Name , err )
95
- e .Session .Log ().Error (msg , slog .Group ("plugin" , "name" , fc .plugin .name , "handler" , fc .name ))
96
- return nil
97
- }
98
82
99
- var result struct {
100
- Data []struct {
101
- Type string `json:"type"`
102
- Attributes struct {
103
- Value string `json:"value"`
104
- } `json:"attributes"`
105
- Relationships struct {
106
- LEIRecords struct {
107
- Data struct {
108
- Type string `json:"type"`
109
- ID string `json:"id"`
110
- } `json:"data"`
111
- Links struct {
112
- Related string `json:"related"`
113
- } `json:"links"`
114
- } `json:"lei-records"`
115
- } `json:"relationships"`
116
- } `json:"data"`
117
- }
118
- if err := json .Unmarshal ([]byte (resp .Body ), & result ); err != nil {
119
- msg := fmt .Sprintf ("Failed to unmarshal the LEI record for %s: %s" , o .Name , err )
120
- e .Session .Log ().Error (msg , slog .Group ("plugin" , "name" , fc .plugin .name , "handler" , fc .name ))
121
- return nil
122
- } else if len (result .Data ) == 0 {
83
+ var err error
84
+ rec , err = org .GLEIFSearchFuzzyCompletions (e , orgent , brand )
85
+ if err != nil {
86
+ e .Session .Log ().Error (err .Error (), slog .Group ("plugin" , "name" , fc .plugin .name , "handler" , fc .name ))
123
87
return nil
124
88
}
125
-
126
- var names []string
127
- m := make (map [string ]string )
128
- for _ , d := range result .Data {
129
- if d .Type == "fuzzycompletions" && d .Relationships .LEIRecords .Data .Type == "lei-records" {
130
- names = append (names , d .Attributes .Value )
131
- m [d .Attributes .Value ] = d .Relationships .LEIRecords .Data .ID
132
- }
133
- }
134
-
135
- if exact , partial , found := support .OrganizationNameMatch (e .Session , orgent , names ); found {
136
- for _ , match := range exact {
137
- score := 30
138
-
139
- if len (exact ) == 1 {
140
- score += 30
141
- }
142
-
143
- lei := m [match ]
144
- id := & general.Identifier {
145
- UniqueID : fmt .Sprintf ("%s:%s" , general .LEICode , lei ),
146
- ID : lei ,
147
- Type : general .LEICode ,
148
- }
149
-
150
- if r , err := fc .plugin .getLEIRecord (id ); err == nil {
151
- if fc .locMatch (e , orgent , r ) {
152
- score += 40
153
- }
154
- if score > conf {
155
- rec = r
156
- conf = score
157
- }
158
- }
159
- }
160
-
161
- swg := metrics .NewSmithWatermanGotoh ()
162
- swg .CaseSensitive = false
163
- swg .GapPenalty = - 0.1
164
- swg .Substitution = metrics.MatchMismatch {
165
- Match : 1 ,
166
- Mismatch : - 0.5 ,
167
- }
168
-
169
- for _ , match := range partial {
170
- if ! strings .Contains (strings .ToLower (match ), strings .ToLower (brand )) {
171
- continue
172
- }
173
-
174
- sim := strutil .Similarity (o .Name , match , swg )
175
- score := int (math .Round (sim * 30 ))
176
-
177
- if len (partial ) == 1 {
178
- score += 30
179
- }
180
-
181
- lei := m [match ]
182
- id := & general.Identifier {
183
- UniqueID : fmt .Sprintf ("%s:%s" , general .LEICode , lei ),
184
- ID : lei ,
185
- Type : general .LEICode ,
186
- }
187
-
188
- if r , err := fc .plugin .getLEIRecord (id ); err == nil {
189
- if fc .locMatch (e , orgent , r ) {
190
- score += 40
191
- }
192
- if score > conf {
193
- rec = r
194
- conf = score
195
- }
196
- }
197
- }
198
- }
199
89
}
200
90
201
91
if rec == nil {
92
+ e .Session .Log ().Info ("no LEI record found for organization" ,
93
+ "org" , orgent .ID , slog .Group ("plugin" , "name" , fc .plugin .name , "handler" , fc .name ))
202
94
return nil
203
95
}
204
96
return fc .store (e , orgent , rec , conf )
205
97
}
206
98
207
- func (fc * fuzzyCompletions ) locMatch (e * et.Event , orgent * dbt.Entity , rec * leiRecord ) bool {
208
- if rec == nil {
209
- return false
210
- }
211
-
212
- legal_addr := rec .Attributes .Entity .LegalAddress
213
- hq_addr := rec .Attributes .Entity .HeadquartersAddress
214
- if edges , err := e .Session .Cache ().OutgoingEdges (orgent ,
215
- time.Time {}, "legal_address" , "hq_address" , "location" ); err == nil {
216
- for _ , edge := range edges {
217
- if a , err := e .Session .Cache ().FindEntityById (edge .ToEntity .ID ); err == nil && a != nil {
218
- if loc , ok := a .Asset .(* contact.Location ); ok {
219
- for _ , p := range append ([]leiAddress {legal_addr , hq_addr }, rec .Attributes .Entity .OtherAddresses ... ) {
220
- if loc .PostalCode == p .PostalCode {
221
- return true
222
- }
223
- }
224
- }
225
- }
226
- }
227
- }
228
-
229
- var crs []* dbt.Entity
230
- if edges , err := e .Session .Cache ().IncomingEdges (orgent , time.Time {}, "organization" ); err == nil {
231
- for _ , edge := range edges {
232
- if a , err := e .Session .Cache ().FindEntityById (edge .FromEntity .ID ); err == nil && a != nil {
233
- if _ , ok := a .Asset .(* contact.ContactRecord ); ok {
234
- crs = append (crs , a )
235
- }
236
- }
237
- }
238
- }
239
-
240
- for _ , cr := range crs {
241
- if edges , err := e .Session .Cache ().OutgoingEdges (cr , time.Time {}, "location" ); err == nil {
242
- for _ , edge := range edges {
243
- if a , err := e .Session .Cache ().FindEntityById (edge .ToEntity .ID ); err == nil && a != nil {
244
- if loc , ok := a .Asset .(* contact.Location ); ok {
245
- for _ , p := range append ([]leiAddress {legal_addr , hq_addr }, rec .Attributes .Entity .OtherAddresses ... ) {
246
- if loc .PostalCode == p .PostalCode {
247
- return true
248
- }
249
- }
250
- }
251
- }
252
- }
253
- }
254
- }
255
- return false
256
- }
257
-
258
- func (fc * fuzzyCompletions ) store (e * et.Event , orgent * dbt.Entity , rec * leiRecord , conf int ) * dbt.Entity {
99
+ func (fc * fuzzyCompletions ) store (e * et.Event , orgent * dbt.Entity , rec * org.LEIRecord , conf int ) * dbt.Entity {
259
100
fc .plugin .updateOrgFromLEIRecord (e , orgent , rec , conf )
260
101
261
102
ident , err := fc .plugin .createLEIFromRecord (e , orgent , rec , conf )
@@ -276,7 +117,7 @@ func (fc *fuzzyCompletions) process(e *et.Event, orgent, ident *dbt.Entity) {
276
117
Session : e .Session ,
277
118
})
278
119
279
- o := orgent .Asset .(* org .Organization )
120
+ o := orgent .Asset .(* oamorg .Organization )
280
121
e .Session .Log ().Info ("relationship discovered" , "from" , o .Name , "relation" , "id" ,
281
122
"to" , id .UniqueID , slog .Group ("plugin" , "name" , fc .plugin .name , "handler" , fc .name ))
282
123
}
0 commit comments