Skip to content

Commit

Permalink
feat: add the since parameter to public methods
Browse files Browse the repository at this point in the history
* added the since parameter to public asset find methods
* added the since parameter to public Relations methods
  • Loading branch information
caffix authored Jul 8, 2023
1 parent decaba9 commit f9aed06
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 98 deletions.
41 changes: 25 additions & 16 deletions assetdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package assetdb

import (
"time"

"github.com/owasp-amass/asset-db/repository"
"github.com/owasp-amass/asset-db/types"
oam "github.com/owasp-amass/open-asset-model"
Expand Down Expand Up @@ -53,38 +55,45 @@ func (as *AssetDB) DeleteRelation(id string) error {
return as.repository.DeleteRelation(id)
}

// FindByContent finds assets in the database based on their content.
// FindByContent finds assets in the database based on their content and last seen after the since parameter.
// If since.IsZero(), the parameter will be ignored.
// It returns a list of matching assets and an error, if any.
func (as *AssetDB) FindByContent(asset oam.Asset) ([]*types.Asset, error) {
return as.repository.FindAssetByContent(asset)
func (as *AssetDB) FindByContent(asset oam.Asset, since time.Time) ([]*types.Asset, error) {
return as.repository.FindAssetByContent(asset, since)
}

// FindById finds an asset in the database by its ID.
// FindById finds an asset in the database by its ID and last seen after the since parameter.
// If since.IsZero(), the parameter will be ignored.
// It returns the matching asset and an error, if any.
func (as *AssetDB) FindById(id string) (*types.Asset, error) {
return as.repository.FindAssetById(id)
func (as *AssetDB) FindById(id string, since time.Time) (*types.Asset, error) {
return as.repository.FindAssetById(id, since)
}

// FindByScope finds assets in the database by applying all the scope constraints provided.
// FindByScope finds assets in the database by applying all the scope constraints provided
// and last seen after the since parameter.
// If since.IsZero(), the parameter will be ignored.
// It returns the matching assets and an error, if any.
func (as *AssetDB) FindByScope(constraints ...oam.Asset) ([]*types.Asset, error) {
return as.repository.FindAssetByScope(constraints...)
func (as *AssetDB) FindByScope(constraints []oam.Asset, since time.Time) ([]*types.Asset, error) {
return as.repository.FindAssetByScope(constraints, since)
}

// FindByType finds all assets in the database of the provided asset type.
// FindByType finds all assets in the database of the provided asset type and last seen after the since parameter.
// If since.IsZero(), the parameter will be ignored.
// It returns the matching assets and an error, if any.
func (as *AssetDB) FindByType(atype oam.AssetType) ([]*types.Asset, error) {
return as.repository.FindAssetByType(atype)
func (as *AssetDB) FindByType(atype oam.AssetType, since time.Time) ([]*types.Asset, error) {
return as.repository.FindAssetByType(atype, since)
}

// IncomingRelations finds all relations pointing to `asset“ for the specified `relationTypes`, if any.
// If since.IsZero(), the parameter will be ignored.
// If no `relationTypes` are specified, all incoming relations are returned.
func (as *AssetDB) IncomingRelations(asset *types.Asset, relationTypes ...string) ([]*types.Relation, error) {
return as.repository.IncomingRelations(asset, relationTypes...)
func (as *AssetDB) IncomingRelations(asset *types.Asset, since time.Time, relationTypes ...string) ([]*types.Relation, error) {
return as.repository.IncomingRelations(asset, since, relationTypes...)
}

// OutgoingRelations finds all relations from `asset“ to another asset for the specified `relationTypes`, if any.
// If since.IsZero(), the parameter will be ignored.
// If no `relationTypes` are specified, all outgoing relations are returned.
func (as *AssetDB) OutgoingRelations(asset *types.Asset, relationTypes ...string) ([]*types.Relation, error) {
return as.repository.OutgoingRelations(asset, relationTypes...)
func (as *AssetDB) OutgoingRelations(asset *types.Asset, since time.Time, relationTypes ...string) ([]*types.Relation, error) {
return as.repository.OutgoingRelations(asset, since, relationTypes...)
}
82 changes: 55 additions & 27 deletions assetdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/owasp-amass/asset-db/types"
oam "github.com/owasp-amass/open-asset-model"
Expand Down Expand Up @@ -33,23 +34,23 @@ func (m *mockAssetDB) DeleteRelation(id string) error {
return args.Error(0)
}

func (m *mockAssetDB) FindAssetById(id string) (*types.Asset, error) {
args := m.Called(id)
func (m *mockAssetDB) FindAssetById(id string, since time.Time) (*types.Asset, error) {
args := m.Called(id, since)
return args.Get(0).(*types.Asset), args.Error(1)
}

func (m *mockAssetDB) FindAssetByContent(asset oam.Asset) ([]*types.Asset, error) {
args := m.Called(asset)
func (m *mockAssetDB) FindAssetByContent(asset oam.Asset, since time.Time) ([]*types.Asset, error) {
args := m.Called(asset, since)
return args.Get(0).([]*types.Asset), args.Error(1)
}

func (m *mockAssetDB) FindAssetByScope(constraints ...oam.Asset) ([]*types.Asset, error) {
args := m.Called(constraints)
func (m *mockAssetDB) FindAssetByScope(constraints []oam.Asset, since time.Time) ([]*types.Asset, error) {
args := m.Called(constraints, since)
return args.Get(0).([]*types.Asset), args.Error(1)
}

func (m *mockAssetDB) FindAssetByType(atype oam.AssetType) ([]*types.Asset, error) {
args := m.Called(atype)
func (m *mockAssetDB) FindAssetByType(atype oam.AssetType, since time.Time) ([]*types.Asset, error) {
args := m.Called(atype, since)
return args.Get(0).([]*types.Asset), args.Error(1)
}

Expand All @@ -58,13 +59,13 @@ func (m *mockAssetDB) Link(source *types.Asset, relation string, destination *ty
return args.Get(0).(*types.Relation), args.Error(1)
}

func (m *mockAssetDB) IncomingRelations(asset *types.Asset, relationTypes ...string) ([]*types.Relation, error) {
args := m.Called(asset, relationTypes)
func (m *mockAssetDB) IncomingRelations(asset *types.Asset, since time.Time, relationTypes ...string) ([]*types.Relation, error) {
args := m.Called(asset, since, relationTypes)
return args.Get(0).([]*types.Relation), args.Error(1)
}

func (m *mockAssetDB) OutgoingRelations(asset *types.Asset, relationTypes ...string) ([]*types.Relation, error) {
args := m.Called(asset, relationTypes)
func (m *mockAssetDB) OutgoingRelations(asset *types.Asset, since time.Time, relationTypes ...string) ([]*types.Relation, error) {
args := m.Called(asset, since, relationTypes)
return args.Get(0).([]*types.Relation), args.Error(1)
}

Expand All @@ -75,6 +76,8 @@ func TestMain(m *testing.M) {
}

func TestAssetDB(t *testing.T) {
start := time.Now()

t.Run("Create", func(t *testing.T) {
relationType := "foo_relation"

Expand Down Expand Up @@ -147,9 +150,9 @@ func TestAssetDB(t *testing.T) {
repository: mockAssetDB,
}

mockAssetDB.On("FindAssetById", tc.id).Return(tc.expected, tc.expectedError)
mockAssetDB.On("FindAssetById", tc.id, start).Return(tc.expected, tc.expectedError)

result, err := adb.FindById(tc.id)
result, err := adb.FindById(tc.id, start)

assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
Expand All @@ -163,11 +166,14 @@ func TestAssetDB(t *testing.T) {
testCases := []struct {
description string
asset oam.Asset
since time.Time
expected []*types.Asset
expectedError error
}{
{"an asset is found", domain.FQDN{Name: "www.domain.com"}, []*types.Asset{{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is not found", domain.FQDN{Name: "www.domain.com"}, []*types.Asset{}, fmt.Errorf("asset not found")},
{"an asset is found", domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}}, nil},
{"an asset is not found", domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{}, fmt.Errorf("asset not found")},
{"asset last seen after since", domain.FQDN{Name: "www.domain.com"}, start, []*types.Asset{{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}}}, nil},
{"asset last seen before since", domain.FQDN{Name: "www.domain.com"}, time.Now(), []*types.Asset{}, fmt.Errorf("asset last seen before since")},
}

for _, tc := range testCases {
Expand All @@ -177,9 +183,9 @@ func TestAssetDB(t *testing.T) {
repository: mockAssetDB,
}

mockAssetDB.On("FindAssetByContent", tc.asset).Return(tc.expected, tc.expectedError)
mockAssetDB.On("FindAssetByContent", tc.asset, tc.since).Return(tc.expected, tc.expectedError)

result, err := adb.FindByContent(tc.asset)
result, err := adb.FindByContent(tc.asset, tc.since)

assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
Expand Down Expand Up @@ -207,9 +213,9 @@ func TestAssetDB(t *testing.T) {
repository: mockAssetDB,
}

mockAssetDB.On("FindAssetByScope", tc.assets).Return(tc.expected, tc.expectedError)
mockAssetDB.On("FindAssetByScope", tc.assets, start).Return(tc.expected, tc.expectedError)

result, err := adb.FindByScope(tc.assets...)
result, err := adb.FindByScope(tc.assets, start)

assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
Expand Down Expand Up @@ -237,9 +243,9 @@ func TestAssetDB(t *testing.T) {
repository: mockAssetDB,
}

mockAssetDB.On("FindAssetByType", tc.atype).Return(tc.expected, tc.expectedError)
mockAssetDB.On("FindAssetByType", tc.atype, start).Return(tc.expected, tc.expectedError)

result, err := adb.FindByType(tc.atype)
result, err := adb.FindByType(tc.atype, start)

assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
Expand All @@ -253,13 +259,15 @@ func TestAssetDB(t *testing.T) {
testCases := []struct {
description string
asset *types.Asset
since time.Time
relationTypes []string
expected []*types.Relation
expectedError error
}{
{
description: "successfully find incoming relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{
{
Expand All @@ -280,10 +288,19 @@ func TestAssetDB(t *testing.T) {
{
description: "error finding incoming relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
expectedError: fmt.Errorf("error finding incoming relations"),
},
{
description: "incoming relations before since parameter",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
since: time.Now().Add(time.Minute),
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
expectedError: nil,
},
}

for _, tc := range testCases {
Expand All @@ -293,9 +310,9 @@ func TestAssetDB(t *testing.T) {
repository: mockAssetDB,
}

mockAssetDB.On("IncomingRelations", tc.asset, tc.relationTypes).Return(tc.expected, tc.expectedError)
mockAssetDB.On("IncomingRelations", tc.asset, tc.since, tc.relationTypes).Return(tc.expected, tc.expectedError)

result, err := adb.IncomingRelations(tc.asset, tc.relationTypes...)
result, err := adb.IncomingRelations(tc.asset, tc.since, tc.relationTypes...)

assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
Expand All @@ -309,13 +326,15 @@ func TestAssetDB(t *testing.T) {
testCases := []struct {
description string
asset *types.Asset
since time.Time
relationTypes []string
expected []*types.Relation
expectedError error
}{
{
description: "successfully find incoming relations",
description: "successfully find outgoing relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{
{
Expand All @@ -336,10 +355,19 @@ func TestAssetDB(t *testing.T) {
{
description: "error finding outgoing relations",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
since: start,
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
expectedError: fmt.Errorf("error finding outgoing relations"),
},
{
description: "outgoing relations before since parameter",
asset: &types.Asset{ID: "1", Asset: domain.FQDN{Name: "www.domain.com"}},
since: time.Now().Add(time.Minute),
relationTypes: []string{"ns_record", "cname_record"},
expected: []*types.Relation{},
expectedError: nil,
},
}

for _, tc := range testCases {
Expand All @@ -349,9 +377,9 @@ func TestAssetDB(t *testing.T) {
repository: mockAssetDB,
}

mockAssetDB.On("OutgoingRelations", tc.asset, tc.relationTypes).Return(tc.expected, tc.expectedError)
mockAssetDB.On("OutgoingRelations", tc.asset, tc.since, tc.relationTypes).Return(tc.expected, tc.expectedError)

result, err := adb.OutgoingRelations(tc.asset, tc.relationTypes...)
result, err := adb.OutgoingRelations(tc.asset, tc.since, tc.relationTypes...)

assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
Expand Down
14 changes: 8 additions & 6 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package repository

import (
"time"

"github.com/owasp-amass/asset-db/types"
oam "github.com/owasp-amass/open-asset-model"
)
Expand All @@ -13,11 +15,11 @@ type Repository interface {
CreateAsset(asset oam.Asset) (*types.Asset, error)
DeleteAsset(id string) error
DeleteRelation(id string) error
FindAssetById(id string) (*types.Asset, error)
FindAssetByContent(asset oam.Asset) ([]*types.Asset, error)
FindAssetByType(atype oam.AssetType) ([]*types.Asset, error)
FindAssetByScope(constraints ...oam.Asset) ([]*types.Asset, error)
FindAssetById(id string, since time.Time) (*types.Asset, error)
FindAssetByContent(asset oam.Asset, since time.Time) ([]*types.Asset, error)
FindAssetByType(atype oam.AssetType, since time.Time) ([]*types.Asset, error)
FindAssetByScope(constraints []oam.Asset, since time.Time) ([]*types.Asset, error)
Link(source *types.Asset, relation string, destination *types.Asset) (*types.Relation, error)
IncomingRelations(asset *types.Asset, relationTypes ...string) ([]*types.Relation, error)
OutgoingRelations(asset *types.Asset, relationTypes ...string) ([]*types.Relation, error)
IncomingRelations(asset *types.Asset, since time.Time, relationTypes ...string) ([]*types.Relation, error)
OutgoingRelations(asset *types.Asset, since time.Time, relationTypes ...string) ([]*types.Relation, error)
}
Loading

0 comments on commit f9aed06

Please sign in to comment.