From a6eb30c58849f77bdfb4c30076e573d1d02c3395 Mon Sep 17 00:00:00 2001 From: caffix Date: Sat, 16 Nov 2024 01:24:29 -0500 Subject: [PATCH] initial implementation of the new cache --- engine/cache/edge.go | 45 +++++++++++++++++++++++++++++++++++++++--- engine/cache/entity.go | 6 ++++++ engine/cache/tag.go | 6 ++++++ go.mod | 2 +- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/engine/cache/edge.go b/engine/cache/edge.go index 7fd9ba9c..62d1c55d 100644 --- a/engine/cache/edge.go +++ b/engine/cache/edge.go @@ -5,6 +5,7 @@ package cache import ( + "reflect" "time" "github.com/owasp-amass/asset-db/types" @@ -29,12 +30,18 @@ func (c *Cache) Link(edge *types.Edge) (*types.Edge, error) { // IncomingEdges implements the Repository interface. func (c *Cache) IncomingEdges(entity *types.Entity, since time.Time, labels ...string) ([]*types.Edge, error) { + c.Lock() + defer c.Unlock() + return c.cache.IncomingEdges(entity, since, labels...) } // OutgoingEdges implements the Repository interface. func (c *Cache) OutgoingEdges(entity *types.Entity, since time.Time, labels ...string) ([]*types.Edge, error) { + c.Lock() + defer c.Unlock() + return c.cache.OutgoingEdges(entity, since, labels...) } // DeleteEdge implements the Repository interface. @@ -47,14 +54,46 @@ func (c *Cache) DeleteEdge(id string) error { return err } - entity, err := c.cache.FindEntityById(id) + edge, err := c.cache.FindEdgeById(id) + if err != nil { + return nil + } + + sub, err := c.cache.FindEntityById(edge.FromEntity.ID) + if err != nil { + return nil + } + + obj, err := c.cache.FindEntityById(edge.ToEntity.ID) if err != nil { return nil } c.appendToDBQueue(func() { - if e, err := c.db.FindEntityByContent(entity.Asset, time.Time{}); err == nil && len(e) == 1 { - _ = c.db.UpdateEntityLastSeen(e[0].ID) + s, err := c.db.FindEntityByContent(sub.Asset, time.Time{}) + if err != nil || len(s) != 1 { + return + } + + o, err := c.db.FindEntityByContent(obj.Asset, time.Time{}) + if err != nil || len(o) != 1 { + return + } + + edges, err := c.db.OutgoingEdges(s, time.Time{}, edge.Relation.Label()) + if err != nil || len(edges) == 0 { + return + } + + var target *types.Edge + for _, e := range edges { + if e.ID == o.ID && reflect.DeepEqual(e.Relation, o.Relation) { + target = e + break + } + } + if target != nil { + _ = c.db.DeleteEdge(target.ID) } }) diff --git a/engine/cache/entity.go b/engine/cache/entity.go index 830ccfdb..6379931e 100644 --- a/engine/cache/entity.go +++ b/engine/cache/entity.go @@ -62,12 +62,18 @@ func (c *Cache) FindEntityById(id string) (*types.Entity, error) { // FindEntityByContent implements the Repository interface. func (c *Cache) FindEntityByContent(asset oam.Asset, since time.Time) ([]*types.Entity, error) { + c.Lock() + defer c.Unlock() + return c.cache.FindEntityByContent(asset, since) } // FindEntitiesByType implements the Repository interface. func (c *Cache) FindEntitiesByType(atype oam.AssetType, since time.Time) ([]*types.Entity, error) { + c.Lock() + defer c.Unlock() + return c.cache.FindEntitiesByType(atype, since) } // DeleteEntity implements the Repository interface. diff --git a/engine/cache/tag.go b/engine/cache/tag.go index f6702f13..52df93e3 100644 --- a/engine/cache/tag.go +++ b/engine/cache/tag.go @@ -39,7 +39,10 @@ func (c *Cache) FindEntityTagById(id string) (*types.EntityTag, error) { // GetEntityTags implements the Repository interface. func (c *Cache) GetEntityTags(entity *types.Entity, since time.Time, names ...string) ([]*types.EntityTag, error) { + c.Lock() + defer c.Unlock() + return c.cache.GetEntityTags(entity, since, names...) } // DeleteEntityTag implements the Repository interface. @@ -105,7 +108,10 @@ func (c *Cache) FindEdgeTagById(id string) (*types.EdgeTag, error) { // GetEdgeTags implements the Repository interface. func (c *Cache) GetEdgeTags(edge *types.Edge, since time.Time, names ...string) ([]*types.EdgeTag, error) { + c.Lock() + defer c.Unlock() + return c.cache.GetEdgeTags(edge, since, names...) } // DeleteEdgeTag implements the Repository interface. diff --git a/go.mod b/go.mod index fefa922e..3d74cf0c 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/miekg/dns v1.1.62 github.com/nyaruka/phonenumbers v1.4.1 github.com/openrdap/rdap v0.9.1 - github.com/owasp-amass/asset-db v0.12.0 + github.com/owasp-amass/asset-db v0.13.0 github.com/owasp-amass/open-asset-model v0.12.0 github.com/owasp-amass/resolve v0.8.1 github.com/rubenv/sql-migrate v1.7.0