Skip to content

Commit 575cd8d

Browse files
committed
fix(gradle): add a new flag --gradle-project-name
1 parent 0cd23b1 commit 575cd8d

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

cmd/murphy/internal/scan/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/murphysecurity/murphysec/infra/ui"
1515
"github.com/murphysecurity/murphysec/inspector"
1616
"github.com/murphysecurity/murphysec/model"
17+
"github.com/murphysecurity/murphysec/module/gradle"
1718
"github.com/murphysecurity/murphysec/module/maven"
1819
"github.com/murphysecurity/murphysec/scanerr"
1920
"github.com/murphysecurity/murphysec/utils"
@@ -40,6 +41,7 @@ var webhookAddr string
4041
var webhookMode common.WebhookModeFlag
4142
var extraData string
4243
var scanCodeHash bool
44+
var gradleProjectFilter gradle.ProjectFilter
4345

4446
func Cmd() *cobra.Command {
4547
var c cobra.Command
@@ -82,6 +84,7 @@ func DfCmd() *cobra.Command {
8284
c.Flags().Var(&webhookMode, "webhook-mode", "specify the webhook mode, currently supports: simple, full(default)")
8385
c.Flags().StringVar(&extraData, "extra-data", "", "specify the extra data")
8486
c.Flags().BoolVar(&scanCodeHash, "scan-snippets", false, "Enable scanning of code snippets to detect SBOM and vulnerabilities. Disabled by default")
87+
c.Flags().StringArrayVar(&gradleProjectFilter.ProjectNames, "gradle-project-name", make([]string, 0), "specify the name of the Gradle project")
8588
return &c
8689
}
8790

@@ -270,6 +273,7 @@ func dfScanRun(cmd *cobra.Command, args []string) {
270273
return
271274
}
272275
logger := logctx.Use(ctx).Sugar()
276+
ctx = context.WithValue(ctx, gradle.ProjectFilterCtxKey, gradleProjectFilter)
273277
r, e := scan(ctx, scanDir, model.AccessTypeCli, model.ScanModeSource)
274278
if errors.Is(e, inspector.ErrNoWait) {
275279
return

module/gradle/gradle.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/murphysecurity/murphysec/model"
1212
"github.com/murphysecurity/murphysec/utils"
1313
"github.com/repeale/fp-go"
14+
"golang.org/x/exp/slices"
1415
"golang.org/x/sync/errgroup"
1516
"gopkg.in/yaml.v3"
1617
"io"
@@ -304,7 +305,7 @@ func parseGradleScriptOutputAsyncBuilder(ctx context.Context) (handler func(path
304305
}
305306
defer func() { _ = f.Close() }()
306307
var _modules []model.Module
307-
_modules, e = decodeGradleScriptOutput(f, path)
308+
_modules, e = decodeGradleScriptOutput(ctx, f, path)
308309
if e != nil {
309310
return
310311
}
@@ -320,13 +321,19 @@ func parseGradleScriptOutputAsyncBuilder(ctx context.Context) (handler func(path
320321
}
321322
}
322323

323-
func decodeGradleScriptOutput(reader io.Reader, dir string) (modules []model.Module, e error) {
324+
func decodeGradleScriptOutput(ctx context.Context, reader io.Reader, dir string) (modules []model.Module, e error) {
325+
var logger = logctx.Use(ctx).Sugar()
324326
var decoder = yaml.NewDecoder(reader)
325327
var data dtoProjectData
326328
e = decoder.Decode(&data)
327329
if e != nil {
328330
return
329331
}
332+
pf, ok := ctx.Value(ProjectFilterCtxKey).(ProjectFilter)
333+
if ok && len(pf.ProjectNames) > 0 && !slices.Contains(pf.ProjectNames, data.Project) {
334+
logger.Infof("project %s not in filter list, skip", data.Project)
335+
return
336+
}
330337
for _, configuration := range data.Configurations {
331338
var online = model.IsOnlineFalse()
332339
if !strings.Contains(strings.ToLower(configuration.Configuration), "test") {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package gradle
2+
3+
type ProjectFilter struct {
4+
ProjectNames []string
5+
}
6+
7+
type _ProjectFilterKey struct{}
8+
9+
func (_ProjectFilterKey) String() string {
10+
return "GradleProjectFilter"
11+
}
12+
13+
var ProjectFilterCtxKey = &_ProjectFilterKey{}

0 commit comments

Comments
 (0)