forked from owasp-amass/asset-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetdb.go
116 lines (97 loc) · 4.68 KB
/
assetdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package assetdb provides a service to interacting with an asset database.
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"
)
// AssetDB represents the asset database service.
type AssetDB struct {
repository repository.Repository
}
// New creates a new assetDB instance.
// It initializes the asset database with the specified database type and DSN.
func New(dbType repository.DBType, dsn string) *AssetDB {
database := repository.New(dbType, dsn)
return &AssetDB{
repository: database,
}
}
// Create creates a new asset in the database.
// If source is nil, the discovered asset will be created and relation will be ignored
// If source and relation are provided, the asset is created and linked to the source asset using the specified relation.
// It returns the newly created asset and an error, if any.
func (as *AssetDB) Create(source *types.Asset, relation string, discovered oam.Asset) (*types.Asset, error) {
if source == nil || relation == "" {
return as.repository.CreateAsset(discovered)
}
newAsset, err := as.repository.CreateAsset(discovered)
if err != nil {
return nil, err
}
_, err = as.repository.Link(source, relation, newAsset)
if err != nil {
return nil, err
}
return newAsset, nil
}
// DeleteAsset removes an asset in the database by its ID.
func (as *AssetDB) DeleteAsset(id string) error {
return as.repository.DeleteAsset(id)
}
// DeleteRelation removes a relation in the database by its ID.
func (as *AssetDB) DeleteRelation(id string) error {
return as.repository.DeleteRelation(id)
}
// 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, since time.Time) ([]*types.Asset, error) {
return as.repository.FindAssetByContent(asset, since)
}
// 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, 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
// 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, since time.Time) ([]*types.Asset, error) {
return as.repository.FindAssetByScope(constraints, since)
}
// 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, 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, 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, since time.Time, relationTypes ...string) ([]*types.Relation, error) {
return as.repository.OutgoingRelations(asset, since, relationTypes...)
}
// GetDBType returns the type of the underlying database.
func (as *AssetDB) GetDBType() string {
return as.repository.GetDBType()
}
// AssetQuery executes a query against the asset table of the db.
// For SQL databases, the query will start with "SELECT * FROM assets " and then add the provided constraints.
func (as *AssetDB) AssetQuery(constraints string) ([]*types.Asset, error) {
return as.repository.AssetQuery(constraints)
}
// RelationQuery executes a query against the relation table of the db.
// For SQL databases, the query will start with "SELECT * FROM relations " and then add the provided constraints.
func (as *AssetDB) RelationQuery(constraints string) ([]*types.Relation, error) {
return as.repository.RelationQuery(constraints)
}