Skip to content

Commit 6eca3ad

Browse files
committed
xray result filtering based on the change-data-hash flag
1 parent ad2ee31 commit 6eca3ad

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

pkg/app/master/commands/xray/handler.go

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,18 @@ func OnCommand(
243243
}
244244

245245
if doSave {
246+
xc.Out.Info("image.data.inspection.save.image.start")
246247
err = dockerutil.SaveImage(client, imageID, iaPath, false, false)
247248
errutil.FailOn(err)
249+
xc.Out.Info("image.data.inspection.save.image.end")
248250
} else {
249251
logger.Debugf("exported image already exists - %s", iaPath)
250252
}
251253

254+
xc.Out.Info("image.data.inspection.process.image.start")
252255
imagePkg, err := dockerimage.LoadPackage(iaPath, imageID, false, topChangesMax, doHashData, changeDataHashMatchers, changePathMatchers, changeDataMatchers)
253256
errutil.FailOn(err)
257+
xc.Out.Info("image.data.inspection.process.image.end")
254258

255259
xc.Out.State("image.data.inspection.done")
256260

@@ -587,7 +591,7 @@ func printImagePackage(
587591

588592
if len(topList) > 0 {
589593
xc.Out.Info("layer.objects.top.start")
590-
for _, object := range topList {
594+
for _, topObject := range topList {
591595
var match bool
592596
for _, pm := range changePathMatchers {
593597
ptrn := strings.TrimSpace(pm.PathPattern)
@@ -596,13 +600,13 @@ func printImagePackage(
596600
}
597601

598602
var err error
599-
match, err = doublestar.Match(ptrn, object.Name)
603+
match, err = doublestar.Match(ptrn, topObject.Name)
600604
if err != nil {
601-
log.Errorf("doublestar.Match name='%s' error=%v", object.Name, err)
605+
log.Errorf("doublestar.Match name='%s' error=%v", topObject.Name, err)
602606
}
603607

604608
if match {
605-
log.Trace("Change path patterns match for 'top'. ptrn='%s' object.Name='%s'\n", ptrn, object.Name)
609+
log.Tracef("Change path patterns match for 'top'. ptrn='%s' object.Name='%s'\n", ptrn, topObject.Name)
606610
break
607611
//not collecting all file path matches here
608612
}
@@ -613,20 +617,30 @@ func printImagePackage(
613617
continue
614618
} else {
615619
if len(changeDataMatchers) > 0 {
616-
matchedPatterns, found := layer.DataMatches[object.Name]
620+
matchedPatterns, found := layer.DataMatches[topObject.Name]
617621
if !found {
618622
log.Trace("Change data patterns, no match. skipping 'top' change...")
619623
continue
620624
}
621625

622-
log.Trace("'%s' ('top' change) matched data patterns - %d", object.Name, len(matchedPatterns))
626+
log.Tracef("'%s' ('top' change) matched data patterns - %d", topObject.Name, len(matchedPatterns))
623627
for _, cdm := range matchedPatterns {
624-
log.Trace("matched => PP='%s' DP='%s'", cdm.PathPattern, cdm.DataPattern)
628+
log.Tracef("matched => PP='%s' DP='%s'", cdm.PathPattern, cdm.DataPattern)
629+
}
630+
} else {
631+
if len(changeDataHashMatchers) > 0 {
632+
matched, found := layer.DataHashMatches[topObject.Name]
633+
if !found {
634+
log.Trace("Change data hash patterns, no match. skipping 'top' change...")
635+
continue
636+
}
637+
638+
log.Tracef("'%s' ('top' change) matched data hash pattern - %s", topObject.Name, matched.Hash)
625639
}
626640
}
627641
}
628642

629-
printObject(xc, object)
643+
printObject(xc, topObject)
630644
}
631645
xc.Out.Info("layer.objects.top.end")
632646
}
@@ -744,9 +758,19 @@ func printImagePackage(
744758
continue
745759
}
746760

747-
log.Trace("'%s' ('modify' change) matched data patterns - %d", objectInfo.Name, len(matchedPatterns))
761+
log.Tracef("'%s' ('modify' change) matched data patterns - %d", objectInfo.Name, len(matchedPatterns))
748762
for _, cdm := range matchedPatterns {
749-
log.Trace("matched => PP='%s' DP='%s'", cdm.PathPattern, cdm.DataPattern)
763+
log.Tracef("matched => PP='%s' DP='%s'", cdm.PathPattern, cdm.DataPattern)
764+
}
765+
} else {
766+
if len(changeDataHashMatchers) > 0 {
767+
matched, found := layer.DataHashMatches[objectInfo.Name]
768+
if !found {
769+
log.Trace("Change data hash patterns, no match. skipping 'modify' change...")
770+
continue
771+
}
772+
773+
log.Tracef("'%s' ('modify' change) matched data hash pattern - %s", objectInfo.Name, matched.Hash)
750774
}
751775
}
752776
}
@@ -815,9 +839,19 @@ func printImagePackage(
815839
continue
816840
}
817841

818-
log.Trace("'%s' ('add' change) matched data patterns - %d", objectInfo.Name, len(matchedPatterns))
842+
log.Tracef("'%s' ('add' change) matched data patterns - %d", objectInfo.Name, len(matchedPatterns))
819843
for _, cdm := range matchedPatterns {
820-
log.Trace("matched => PP='%s' DP='%s'", cdm.PathPattern, cdm.DataPattern)
844+
log.Tracef("matched => PP='%s' DP='%s'", cdm.PathPattern, cdm.DataPattern)
845+
}
846+
} else {
847+
if len(changeDataHashMatchers) > 0 {
848+
matched, found := layer.DataHashMatches[objectInfo.Name]
849+
if !found {
850+
log.Trace("Change data hash patterns, no match. skipping 'add' change...")
851+
continue
852+
}
853+
854+
log.Tracef("'%s' ('add' change) matched data hash pattern - %s", objectInfo.Name, matched.Hash)
821855
}
822856
}
823857
}

pkg/docker/dockerimage/dockerimage.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ func (ct *ChangeType) UnmarshalJSON(b []byte) error {
189189
}
190190

191191
type ObjectMetadata struct {
192-
Change ChangeType `json:"change,omitempty"`
193-
Name string `json:"name,omitempty"`
192+
Change ChangeType `json:"change"`
193+
Name string `json:"name"`
194194
Size int64 `json:"size,omitempty"`
195195
SizeHuman string `json:"size_human,omitempty"`
196196
Mode os.FileMode `json:"mode,omitempty"`
197197
ModeHuman string `json:"mode_human,omitempty"`
198-
UID int `json:"uid,omitempty"`
199-
GID int `json:"gid,omitempty"`
198+
UID int `json:"uid"` //don't omit uid 0
199+
GID int `json:"gid"` //don't omit gid 0
200200
ModTime time.Time `json:"mod_time,omitempty"`
201201
ChangeTime time.Time `json:"change_time,omitempty"`
202202
LinkTarget string `json:"link_target,omitempty"`

0 commit comments

Comments
 (0)