Skip to content

Commit 5e9a744

Browse files
committed
refactor: SCA-83 unify dependency relation tracking and replace IsDirectDependency field
1 parent db73ebf commit 5e9a744

File tree

21 files changed

+148
-67
lines changed

21 files changed

+148
-67
lines changed

envinspection/inspection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func inspectInstalledSoftware(ctx context.Context, module *model.Module) {
104104
}
105105
for i := range module.Dependencies {
106106
module.Dependencies[i].IsOnline.SetOnline(false)
107-
module.Dependencies[i].IsDirectDependency = true
107+
module.Dependencies[i].DependencyRelation = model.DependencyRelationDirect
108108
module.Dependencies[i].EcoRepo.Repository = module.PackageManager
109109
}
110110
task.Modules = append(task.Modules, *module)

model/dependency.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package model
22

33
type DependencyItem struct {
44
Component
5-
Dependencies []DependencyItem `json:"dependencies,omitempty"`
6-
IsDirectDependency bool `json:"is_direct_dependency,omitempty"`
7-
MavenScope string `json:"maven_scope,omitempty"`
8-
IsOnline IsOnline `json:"is_online"`
5+
Dependencies []DependencyItem `json:"dependencies,omitempty"`
6+
DependencyRelation DependencyRelation `json:"dependency_relation"`
7+
MavenScope string `json:"maven_scope,omitempty"`
8+
IsOnline IsOnline `json:"is_online"`
99
}

model/dependency_relation.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package model
2+
3+
import (
4+
"encoding"
5+
"errors"
6+
)
7+
8+
//go:generate stringer -linecomment -type DependencyRelation -output dependency_relation_string.go
9+
type DependencyRelation int
10+
11+
func (i DependencyRelation) MarshalText() (text []byte, err error) {
12+
return []byte(i.String()), nil
13+
}
14+
15+
func (i *DependencyRelation) UnmarshalText(text []byte) error {
16+
switch string(text) {
17+
case DependencyRelationUnknown.String():
18+
*i = DependencyRelationUnknown
19+
return nil
20+
case DependencyRelationDirect.String():
21+
*i = DependencyRelationDirect
22+
return nil
23+
case DependencyRelationTransitive.String():
24+
*i = DependencyRelationTransitive
25+
return nil
26+
default:
27+
return errors.New("bad dependency relation")
28+
}
29+
}
30+
31+
const (
32+
DependencyRelationUnknown DependencyRelation = 1 // Unknown
33+
DependencyRelationDirect DependencyRelation = 2 // Direct
34+
DependencyRelationTransitive DependencyRelation = 3 // Transitive
35+
)
36+
37+
var _ encoding.TextUnmarshaler = (*DependencyRelation)(nil)
38+
var _ encoding.TextMarshaler = DependencyRelation(0)

model/dependency_relation_string.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module/arkts/arkts.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package arkts
22

33
import (
44
"context"
5-
"github.com/murphysecurity/murphysec/infra/logctx"
6-
"github.com/murphysecurity/murphysec/model"
7-
"github.com/murphysecurity/murphysec/utils"
8-
"github.com/samber/lo"
9-
"github.com/titanous/json5"
105
"io"
116
"os"
127
"path/filepath"
138
"sort"
149
"strings"
10+
11+
"github.com/murphysecurity/murphysec/infra/logctx"
12+
"github.com/murphysecurity/murphysec/model"
13+
"github.com/murphysecurity/murphysec/utils"
14+
"github.com/samber/lo"
15+
"github.com/titanous/json5"
1516
)
1617

1718
const (
@@ -77,7 +78,7 @@ func _buildDepTreeVisit(visited map[[2]string]struct{}, next [2]string, root *lo
7778
EcoRepo: ecoRepo,
7879
},
7980
Dependencies: nil,
80-
IsDirectDependency: false,
81+
DependencyRelation: model.DependencyRelationTransitive,
8182
}
8283

8384
if _, ok := visited[next]; ok {
@@ -181,7 +182,7 @@ func analyze(ctx context.Context) (e error) {
181182
m.Dependencies = append(m.Dependencies, _buildDepTreeVisit(map[[2]string]struct{}{}, it, &lock))
182183
}
183184
for i := range m.Dependencies {
184-
m.Dependencies[i].IsDirectDependency = true
185+
m.Dependencies[i].DependencyRelation = model.DependencyRelationDirect
185186
}
186187
task.AddModule(m)
187188
return

module/bundler/gem.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package bundler
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
7+
58
"github.com/murphysecurity/murphysec/infra/logctx"
69
"github.com/murphysecurity/murphysec/model"
710
"github.com/murphysecurity/murphysec/utils"
811
"github.com/pkg/errors"
912
"go.uber.org/zap"
10-
"os"
11-
"path/filepath"
1213
)
1314

1415
type Inspector struct{}
@@ -43,7 +44,7 @@ func (Inspector) InspectProject(ctx context.Context) error {
4344
CompVersion: j.Version,
4445
EcoRepo: EcoRepo,
4546
},
46-
IsDirectDependency: true,
47+
DependencyRelation: model.DependencyRelationDirect,
4748
})
4849
}
4950
task.AddModule(model.Module{

module/cargo/cargo_lock.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package cargo
22

33
import (
44
"fmt"
5+
"strings"
6+
57
"github.com/murphysecurity/murphysec/model"
68
"github.com/murphysecurity/murphysec/utils/simpletoml"
79
"github.com/repeale/fp-go"
810
"github.com/samber/lo"
911
"golang.org/x/exp/maps"
10-
"strings"
1112
)
1213

1314
func splitNameVersionFromDepLine(line string) (name, version string) {
@@ -98,7 +99,7 @@ func analyzeCargoLock(input []byte) (rs []*model.DependencyItem, err error) {
9899
if r == nil {
99100
continue
100101
}
101-
r.IsDirectDependency = true
102+
r.DependencyRelation = model.DependencyRelationDirect
102103
rs = append(rs, r)
103104
}
104105
return
@@ -116,6 +117,7 @@ func _buildTree(lock map[[2]string][][2]string, key [2]string, visited map[[2]st
116117
CompVersion: key[1],
117118
EcoRepo: EcoRepo,
118119
},
120+
DependencyRelation: model.DependencyRelationTransitive,
119121
}
120122
if _, ok := visited[key]; ok {
121123
return r

module/go_mod/gopkg.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package go_mod
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
7+
58
"github.com/BurntSushi/toml"
69
"github.com/murphysecurity/murphysec/infra/logctx"
710
"github.com/murphysecurity/murphysec/model"
811
"go.uber.org/zap"
9-
"os"
10-
"path/filepath"
1112
)
1213

1314
type goPkgLock struct {
@@ -47,7 +48,7 @@ func parserGoPkgLock(ctx context.Context) error {
4748
CompVersion: j.Version,
4849
EcoRepo: EcoRepo,
4950
},
50-
IsDirectDependency: true,
51+
DependencyRelation: model.DependencyRelationDirect,
5152
})
5253
}
5354

module/go_mod/gotree.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func buildScan(ctx context.Context) error {
9191
CompVersion: nameVersionMp[j],
9292
EcoRepo: EcoRepo,
9393
},
94-
IsDirectDependency: true,
94+
DependencyRelation: model.DependencyRelationDirect,
9595
}
9696
logger.Debug("buildTree start : " + j)
9797
dependencies = append(dependencies, buildingDependencyTree(nameVersionMp, &dependencie, sonTree, &packageToPackageUsed, logger))
@@ -124,11 +124,11 @@ func buildingDependencyTree(dInfo map[string]string, d *model.DependencyItem, so
124124
CompVersion: dInfo[j],
125125
EcoRepo: EcoRepo,
126126
},
127-
IsDirectDependency: false,
127+
DependencyRelation: model.DependencyRelationTransitive,
128128
}
129129
(*packageToPackageUsed)[d.CompName] = append((*packageToPackageUsed)[d.CompName], j)
130130
t := buildingDependencyTree(dInfo, &mod, sonTree, packageToPackageUsed, logger)
131-
t.IsDirectDependency = false
131+
t.DependencyRelation = model.DependencyRelationTransitive
132132
d.Dependencies = append(d.Dependencies, t)
133133
}
134134
}
@@ -365,17 +365,17 @@ func baseScan(ctx context.Context) error {
365365
}
366366
modName := file.Module.Mod.Path
367367
for _, req := range file.Require {
368-
isDirectDependency := true
368+
isDirectDependency := model.DependencyRelationDirect
369369
if _, ok := indirectMp[req.Mod.Path]; ok {
370-
isDirectDependency = false
370+
isDirectDependency = model.DependencyRelationTransitive
371371
}
372372
dependencies = append(dependencies, model.DependencyItem{
373373
Component: model.Component{
374374
CompName: req.Mod.Path,
375375
CompVersion: req.Mod.Version,
376376
EcoRepo: EcoRepo,
377377
},
378-
IsDirectDependency: isDirectDependency,
378+
DependencyRelation: isDirectDependency,
379379
IsOnline: model.IsOnline{Value: true, Valid: true},
380380
})
381381
}

module/gradle/gradle.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (g *GradleDependencyInfo) BaseModule(basePath string) model.Module {
154154
func convDep(input []DepElement) []model.DependencyItem {
155155
var r = _convDep(input)
156156
for i := range r {
157-
r[i].IsDirectDependency = true
157+
r[i].DependencyRelation = model.DependencyRelationDirect
158158
}
159159
return r
160160
}
@@ -168,7 +168,8 @@ func _convDep(input []DepElement) []model.DependencyItem {
168168
CompVersion: it.Version,
169169
EcoRepo: EcoRepo,
170170
},
171-
Dependencies: _convDep(it.Children),
171+
Dependencies: _convDep(it.Children),
172+
DependencyRelation: model.DependencyRelationTransitive,
172173
})
173174
}
174175
return rs

0 commit comments

Comments
 (0)