Skip to content

Commit

Permalink
initial implementation of the new cache
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Nov 16, 2024
1 parent f8b8361 commit a6eb30c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
45 changes: 42 additions & 3 deletions engine/cache/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cache

import (
"reflect"
"time"

"github.com/owasp-amass/asset-db/types"

Check failure on line 11 in engine/cache/edge.go

View workflow job for this annotation

GitHub Actions / Coverage

missing go.sum entry for module providing package github.com/owasp-amass/asset-db/types (imported by github.com/owasp-amass/amass/v4/cmd/oam_assoc); to add:

Check failure on line 11 in engine/cache/edge.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.23.1)

missing go.sum entry for module providing package github.com/owasp-amass/asset-db/types (imported by github.com/owasp-amass/amass/v4/cmd/oam_assoc); to add:
Expand All @@ -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.
Expand All @@ -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)
}
})

Expand Down
6 changes: 6 additions & 0 deletions engine/cache/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions engine/cache/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a6eb30c

Please sign in to comment.