|
| 1 | +// Copyright © by Jeff Foley 2017-2025. All rights reserved. |
| 2 | +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package aviato |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "log/slog" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/owasp-amass/amass/v4/engine/plugins/support" |
| 14 | + et "github.com/owasp-amass/amass/v4/engine/types" |
| 15 | + dbt "github.com/owasp-amass/asset-db/types" |
| 16 | + oam "github.com/owasp-amass/open-asset-model" |
| 17 | + "github.com/owasp-amass/open-asset-model/general" |
| 18 | + "github.com/owasp-amass/open-asset-model/org" |
| 19 | +) |
| 20 | + |
| 21 | +func (ce *companyEnrich) check(e *et.Event) error { |
| 22 | + _, ok := e.Entity.Asset.(*general.Identifier) |
| 23 | + if !ok { |
| 24 | + return errors.New("failed to extract the Identifier asset") |
| 25 | + } |
| 26 | + |
| 27 | + ds := e.Session.Config().GetDataSourceConfig(ce.plugin.name) |
| 28 | + if ds == nil || len(ds.Creds) == 0 { |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + var keys []string |
| 33 | + for _, cr := range ds.Creds { |
| 34 | + if cr != nil && cr.Apikey != "" { |
| 35 | + keys = append(keys, cr.Apikey) |
| 36 | + } |
| 37 | + } |
| 38 | + if len(keys) == 0 { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + since, err := support.TTLStartTime(e.Session.Config(), |
| 43 | + string(oam.Identifier), string(oam.Organization), ce.plugin.name) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + var o *dbt.Entity |
| 49 | + if support.AssetMonitoredWithinTTL(e.Session, e.Entity, ce.plugin.source, since) { |
| 50 | + o = ce.lookup(e, e.Entity, since) |
| 51 | + } else { |
| 52 | + o = ce.query(e, e.Entity, keys) |
| 53 | + support.MarkAssetMonitored(e.Session, e.Entity, ce.plugin.source) |
| 54 | + } |
| 55 | + |
| 56 | + if o != nil { |
| 57 | + ce.process(e, e.Entity, o) |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (ce *companyEnrich) lookup(e *et.Event, ident *dbt.Entity, since time.Time) *dbt.Entity { |
| 63 | + if edges, err := e.Session.Cache().IncomingEdges(ident, since, "id"); err == nil { |
| 64 | + for _, edge := range edges { |
| 65 | + if tags, err := e.Session.Cache().GetEdgeTags(edge, since, ce.plugin.source.Name); err != nil || len(tags) == 0 { |
| 66 | + continue |
| 67 | + } |
| 68 | + if a, err := e.Session.Cache().FindEntityById(edge.FromEntity.ID); err == nil && a != nil { |
| 69 | + if _, ok := a.Asset.(*org.Organization); ok { |
| 70 | + return a |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func (ce *companyEnrich) process(e *et.Event, ident, orgent *dbt.Entity) { |
| 79 | + o := orgent.Asset.(*org.Organization) |
| 80 | + |
| 81 | + _ = e.Dispatcher.DispatchEvent(&et.Event{ |
| 82 | + Name: fmt.Sprintf("%s:%s", o.Name, o.ID), |
| 83 | + Entity: orgent, |
| 84 | + Session: e.Session, |
| 85 | + }) |
| 86 | + |
| 87 | + id := ident.Asset.(*general.Identifier) |
| 88 | + e.Session.Log().Info("relationship discovered", "from", id.UniqueID, "relation", "id", |
| 89 | + "to", o.Name, slog.Group("plugin", "name", ce.plugin.name, "handler", ce.name)) |
| 90 | +} |
0 commit comments