Skip to content

Commit fdaf71a

Browse files
committed
feat(scan): add build warnings for conan/nuget dynamic build failures
1 parent f15b9bf commit fdaf71a

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

module/conan/conan.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/murphysecurity/murphysec/errors"
77
"github.com/murphysecurity/murphysec/infra/logctx"
88
"github.com/murphysecurity/murphysec/model"
9+
"github.com/murphysecurity/murphysec/scanerr"
910
"github.com/murphysecurity/murphysec/utils"
1011
"go.uber.org/zap"
1112
"os"
@@ -29,26 +30,48 @@ func (Inspector) CheckDir(ctx context.Context, dir string) bool {
2930
utils.IsFile(filepath.Join(dir, "conan.py"))
3031
}
3132
func (Inspector) InspectProject(ctx context.Context) error {
33+
task := model.UseInspectionTask(ctx)
34+
registeredAutoBuild := task.RegisterAutoBuild()
3235
if env.DoNotBuild {
36+
scanerr.Add(ctx, scanerr.Param{Kind: scanerr.KindBuildDisabled})
37+
registeredAutoBuild.MarkDisabled()
3338
return nil
3439
}
35-
task := model.UseInspectionTask(ctx)
3640
logger := logctx.Use(ctx)
3741
cmdInfo, e := getConanInfo(ctx)
3842
if e != nil {
43+
registeredAutoBuild.MarkFailed()
44+
kind := scanerr.KindConanFailed
45+
if errors.Is(e, ErrConanNotFound) {
46+
kind = scanerr.KindConanNotFound
47+
}
48+
scanerr.Add(ctx, scanerr.Param{
49+
Kind: kind,
50+
Content: e.Error(),
51+
})
3952
return e
4053
}
4154
jsonFilePath, e := ExecuteConanInfoCmd(ctx, cmdInfo.Path, task.Dir())
4255

4356
var conanErr conanError
4457
if errors.As(e, &conanErr) {
58+
registeredAutoBuild.MarkFailed()
59+
scanerr.Add(ctx, scanerr.Param{
60+
Kind: scanerr.KindConanFailed,
61+
Content: conanErr.Error(),
62+
})
4563
if !env.ScannerScan {
4664
badConanView(ctx)
4765
printConanError(ctx, &conanErr)
4866
}
4967
return e
5068
}
5169
if e != nil {
70+
registeredAutoBuild.MarkFailed()
71+
scanerr.Add(ctx, scanerr.Param{
72+
Kind: scanerr.KindConanFailed,
73+
Content: e.Error(),
74+
})
5275
return e
5376
}
5477
defer func() {
@@ -58,10 +81,20 @@ func (Inspector) InspectProject(ctx context.Context) error {
5881
}()
5982
var conanJson _ConanInfoJsonFile
6083
if e := conanJson.ReadFromFile(jsonFilePath); e != nil {
84+
registeredAutoBuild.MarkFailed()
85+
scanerr.Add(ctx, scanerr.Param{
86+
Kind: scanerr.KindConanFailed,
87+
Content: e.Error(),
88+
})
6189
return e
6290
}
6391
t, e := conanJson.Tree()
6492
if e != nil {
93+
registeredAutoBuild.MarkFailed()
94+
scanerr.Add(ctx, scanerr.Param{
95+
Kind: scanerr.KindConanFailed,
96+
Content: e.Error(),
97+
})
6598
return e
6699
}
67100
task.AddModule(model.Module{

module/nuget/nuget.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package nuget
22

33
import (
44
"context"
5+
"errors"
56
"path/filepath"
67

78
"github.com/murphysecurity/murphysec/env"
89
"github.com/murphysecurity/murphysec/infra/logctx"
910
"github.com/murphysecurity/murphysec/infra/ui"
1011
"github.com/murphysecurity/murphysec/model"
12+
"github.com/murphysecurity/murphysec/scanerr"
1113
"github.com/murphysecurity/murphysec/utils"
1214
)
1315

@@ -31,10 +33,20 @@ func (Inspector) InspectProject(ctx context.Context) error {
3133
task := model.UseInspectionTask(ctx)
3234
allowFallback := !env.ScannerScan
3335
doOld := false
36+
registeredAutoBuild := task.RegisterAutoBuild()
3437

3538
var e error
3639
if !task.IsNoBuild() {
37-
if multipleBuilds(ctx, task) != nil {
40+
if buildErr := multipleBuilds(ctx, task); buildErr != nil {
41+
registeredAutoBuild.MarkFailed()
42+
kind := scanerr.KindNugetFailed
43+
if errors.Is(buildErr, _ErrDotnetNotFound) {
44+
kind = scanerr.KindDotnetNotFound
45+
}
46+
scanerr.Add(ctx, scanerr.Param{
47+
Kind: kind,
48+
Content: buildErr.Error(),
49+
})
3850
logger.Warn("multipleBuilds no build")
3951
ui.Use(ctx).Display(ui.MsgWarn, "通过 Nuget获取依赖信息失败,可能会导致检测结果不完整或失败,访问 https://murphysec.com/docs/faqs/quick-start-for-beginners/programming-language-supported.html 了解详情")
4052
if allowFallback {
@@ -44,6 +56,8 @@ func (Inspector) InspectProject(ctx context.Context) error {
4456
e = noBuildEntrance(ctx, task, &doOld)
4557
}
4658
} else {
59+
registeredAutoBuild.MarkDisabled()
60+
scanerr.Add(ctx, scanerr.Param{Kind: scanerr.KindBuildDisabled})
4761
logger.Warn("multipleBuilds no build")
4862
if allowFallback {
4963
e = noBuildEntrance(ctx, task, &doOld)

module/nuget/nuget_cmd_build.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func multipleBuilds(ctx context.Context, task *model.InspectionTask) error {
3636
logger.Sugar().Debugf("findCLNList: %v", filePath)
3737
numCPU := utils.Coerce(runtime.NumCPU(), 1, 4)
3838
var wg sync.WaitGroup
39+
var mu sync.Mutex
40+
var errs []error
3941
ch := make(chan string, len(filePath))
4042
for _, j := range filePath {
4143
ch <- j
@@ -48,11 +50,17 @@ func multipleBuilds(ctx context.Context, task *model.InspectionTask) error {
4850
for j := range ch {
4951
if err := buildEntrance(ctx, task, j); err != nil {
5052
logger.Warn(j + "buildEntrance faild:" + err.Error())
53+
mu.Lock()
54+
errs = append(errs, fmt.Errorf("%s: %w", j, err))
55+
mu.Unlock()
5156
}
5257
}
5358
}()
5459
}
5560
wg.Wait()
61+
if len(errs) > 0 {
62+
return errors.Join(errs...)
63+
}
5664
return nil
5765

5866
}

scanerr/scan_err.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ const KindMavenNotFound = "mvn_not_found"
4242
const KindMavenFailed = "mvn_failed"
4343
const KindBuildDisabled = "build_disabled"
4444
const KindMavenTimeout = "mvn_timeout_killed"
45+
const KindConanNotFound = "conan_not_found"
46+
const KindConanFailed = "conan_failed"
47+
const KindDotnetNotFound = "dotnet_not_found"
48+
const KindNugetFailed = "nuget_failed"

0 commit comments

Comments
 (0)