Skip to content

Support/use recursive matching required file patterns as needed#1394

Merged
bcrochet merged 3 commits intoredhat-openshift-ecosystem:mainfrom
caxu-rh:required-file-patterns-globstar
Mar 31, 2026
Merged

Support/use recursive matching required file patterns as needed#1394
bcrochet merged 3 commits intoredhat-openshift-ecosystem:mainfrom
caxu-rh:required-file-patterns-globstar

Conversation

@caxu-rh
Copy link
Copy Markdown
Contributor

@caxu-rh caxu-rh commented Mar 26, 2026

Alternative fix for the problem/context mentioned in #1393.

  • Introduce https://github.com/bmatcuk/doublestar library to support Bash-style globstar patterns
  • Only use the nested/recursive matching (ending in **) in the situations where it's needed (so far, only HasLicenseCheck)
  • Add a test case
  • Catch a corner case that was previously passing (which should not): /licenses directory containing only directories at the top level (regardless of whether these child directories contained licenses or were empty) would always be considered passing HasLicense check

caxu-rh added 2 commits March 26, 2026 11:03
It looks like there are some cases where checks need to extract
recursive subdirectories/files. Use the doublestar library to
support Bash-style globstar patterns, which can support this.

Signed-off-by: Caleb Xu <caxu@redhat.com>
Some container images may place licenses in a nested subdirectory
structure in /licenses (while no license files reside in /licenses
itself). This was previously supported before f7103d0.

Signed-off-by: Caleb Xu <caxu@redhat.com>
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 📋 Review Summary

This PR introduces the doublestar library to support recursive matching (globstar) in the engine's untar function and uses it for HasLicenseCheck. While the changes to the engine are solid and include a good test case, the HasLicenseCheck update is currently incomplete as its validation logic is not yet recursive.

🔍 General Feedback

  • Recursion Depth: The engine now correctly extracts files recursively when using **.
  • Validation Consistency: Ensure that checks using recursive patterns also update their internal validation logic to traverse the extracted directories.
  • Dependency: doublestar is a well-established library for this purpose in the Go ecosystem.

Comment thread internal/policy/container/has_license.go
Comment thread internal/engine/untar.go

matches := slices.ContainsFunc(filterPatterns, func(p string) bool {
result, _ := filepath.Match(p, header.Name)
result, _ := doublestar.Match(p, header.Name)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 The error returned from doublestar.Match is ignored here. While it was also ignored for filepath.Match, it might be worth logging or handling it if a pattern is malformed (e.g., if an invalid globstar pattern is used). Since the patterns are defined within the codebase, it's low risk, but good for future-proofing.

@coveralls
Copy link
Copy Markdown

coveralls commented Mar 26, 2026

Coverage Status

coverage: 84.442% (-0.03%) from 84.47%
when pulling 0b859f7 on caxu-rh:required-file-patterns-globstar
into 10e94ec on redhat-openshift-ecosystem:main.

@dcibot
Copy link
Copy Markdown

dcibot commented Mar 26, 2026

@dcibot
Copy link
Copy Markdown

dcibot commented Mar 26, 2026

@caxu-rh caxu-rh force-pushed the required-file-patterns-globstar branch from 3dccf7b to 4a98bc6 Compare March 26, 2026 19:21
@openshift-ci openshift-ci Bot added lgtm Indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Mar 26, 2026
@dcibot
Copy link
Copy Markdown

dcibot commented Mar 26, 2026

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 📋 Review Summary

This PR introduces recursive file pattern matching using the doublestar library, primarily to support deeper license file checks in the HasLicenseCheck. The overall approach is solid and the test coverage for nested subdirectories is a good addition.

🔍 General Feedback

  • Recursion behavior: The use of doublestar.PathMatch is recommended over doublestar.Match to ensure that existing glob patterns (using *) remain single-level unless explicitly switched to **.
  • Validation errors: In Preflight, validation failures (like a missing directory) should generally return false, nil rather than an error to distinguish them from engine-level execution failures.
  • Refactoring: The code cleanup in has_license.go to use slices.ContainsFunc is a nice improvement in readability and follows modern Go idioms.

Comment thread internal/policy/container/has_license_test.go
Comment thread internal/policy/container/has_license.go Outdated
Comment thread internal/engine/untar.go

matches := slices.ContainsFunc(filterPatterns, func(p string) bool {
result, _ := filepath.Match(p, header.Name)
result, _ := doublestar.Match(p, header.Name)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Use doublestar.PathMatch instead of doublestar.Match.

doublestar.Match allows * and ? to match across directory separators (e.g., licenses/* would match licenses/nested/LICENSE). This change affects all existing checks that use * but do not expect recursive matching, potentially leading to more files being extracted than necessary.

doublestar.PathMatch is a closer replacement for filepath.Match as it restricts * to a single directory level while still supporting ** for explicit recursion, which aligns with the goal of only using nested matching where explicitly requested (via **).

Suggested change
result, _ := doublestar.Match(p, header.Name)
matches := slices.ContainsFunc(filterPatterns, func(p string) bool {
result, _ := doublestar.PathMatch(p, header.Name)
return result
})

@caxu-rh caxu-rh force-pushed the required-file-patterns-globstar branch from 4a98bc6 to 3770fe0 Compare March 26, 2026 21:43
@openshift-ci openshift-ci Bot removed the lgtm Indicates that a PR is ready to be merged. label Mar 26, 2026
Only consider files, not directories, as valid license files, and
search in subdirs within the license directory, as valid license
files may not be at the top level.

Signed-off-by: Caleb Xu <caxu@redhat.com>
@caxu-rh caxu-rh force-pushed the required-file-patterns-globstar branch from 3770fe0 to 0b859f7 Compare March 26, 2026 21:53
Comment thread internal/policy/container/has_license.go
@dcibot
Copy link
Copy Markdown

dcibot commented Mar 26, 2026

@acornett21
Copy link
Copy Markdown
Contributor

/lgtm

@openshift-ci openshift-ci Bot added the lgtm Indicates that a PR is ready to be merged. label Mar 30, 2026
Copy link
Copy Markdown
Contributor

@bcrochet bcrochet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@openshift-ci
Copy link
Copy Markdown

openshift-ci Bot commented Mar 31, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: acornett21, bcrochet, caxu-rh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [acornett21,bcrochet]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@bcrochet bcrochet merged commit 8381364 into redhat-openshift-ecosystem:main Mar 31, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants