Skip to content

Conversation

@tinovyatkin
Copy link
Contributor

@tinovyatkin tinovyatkin commented Jan 21, 2026

Summary

  • Adds a new --source-policy-file flag to buildah build that enables users to control and transform source references during builds without modifying Dockerfiles
  • Implements BuildKit-compatible source policy JSON schema with ALLOW, DENY, and CONVERT actions
  • Supports EXACT and WILDCARD match types for flexible policy rules

Background

This implements support for the BuildKit source policy feature as documented in BuildKit's build reproducibility guide. Docker Buildx supports this via the EXPERIMENTAL_BUILDKIT_SOURCE_POLICY environment variable. The JSON schema is compatible with BuildKit's source policy format.

Motivation

This feature allows organizations to:

  • Pin base image tags to specific digests at build time for reproducibility
  • Deny specific sources from being used to enforce security policies
  • Transform source references transparently without modifying Containerfiles

Changes

  • pkg/sourcepolicy/: New package implementing policy parsing, validation, and matching
  • define/build.go: Added SourcePolicyFile field to BuildOptions
  • pkg/cli/common.go: Added --source-policy-file flag definition
  • imagebuildah/executor.go: Policy loading in newExecutor()
  • imagebuildah/stage_executor.go: Policy evaluation in prepare() function
  • docs/buildah-build.1.md: Man page documentation with examples
  • tests/source-policy.bats: Integration tests
  • pkg/sourcepolicy/policy_test.go: Unit tests

Example Usage

Pin alpine:latest to a specific digest:

{
  "rules": [
    {
      "action": "CONVERT",
      "selector": {
        "identifier": "docker-image://docker.io/library/alpine:latest"
      },
      "updates": {
        "identifier": "docker-image://docker.io/library/alpine@sha256:abc123"
      }
    }
  ]
}
buildah build --source-policy-file policy.json -t myimage .

Test plan

  • Unit tests for policy parsing, validation, and matching (pkg/sourcepolicy/policy_test.go)
  • Integration tests covering DENY, ALLOW, CONVERT actions and WILDCARD matching (tests/source-policy.bats)
  • Manual testing with real images

🤖 Generated with Claude Code

@tinovyatkin tinovyatkin force-pushed the feature/source-policy-file branch from 4dcc534 to 445591e Compare January 21, 2026 02:24
@tinovyatkin

This comment was marked as resolved.

@tinovyatkin tinovyatkin force-pushed the feature/source-policy-file branch from d0d291b to 65256a1 Compare January 21, 2026 09:05
@tinovyatkin

This comment was marked as resolved.

@tinovyatkin tinovyatkin force-pushed the feature/source-policy-file branch from 65256a1 to 15f208c Compare January 21, 2026 10:38
Implements support for the BuildKit source policy feature, bringing
feature parity with `buildctl build --source-policy-file`. The JSON schema
is compatible with BuildKit's source policy format.

Features:
- New `--source-policy-file` flag for `buildah build`
- ALLOW, DENY, and CONVERT actions for controlling source references
- EXACT and WILDCARD match types for flexible policy rules
- Automatic image reference normalization to docker-image:// format

This allows organizations to:
- Pin base image tags to specific digests at build time
- Deny specific sources from being used to enforce security policies
- Transform source references without modifying Containerfiles

Changes:
- pkg/sourcepolicy/: New package for policy parsing, validation, matching
- define/build.go: Added SourcePolicyFile field to BuildOptions
- pkg/cli/common.go: Added --source-policy-file flag definition
- imagebuildah/executor.go: Policy loading in newExecutor()
- imagebuildah/stage_executor.go: Policy evaluation in prepare()
- docs/buildah-build.1.md: Man page documentation with examples
- tests/source-policy.bats: Integration tests
- pkg/sourcepolicy/policy_test.go: Unit tests

Signed-off-by: Konstantin Vyatkin <tino@vtkn.io>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@tinovyatkin tinovyatkin force-pushed the feature/source-policy-file branch from 15f208c to 2c225f3 Compare January 21, 2026 13:07
@tinovyatkin tinovyatkin marked this pull request as ready for review January 21, 2026 13:49
@dosubot dosubot bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jan 21, 2026
@TomSweeneyRedHat
Copy link
Member

Couple small nits, otherwise LGTM. @nalind?

Copy link
Member

@nalind nalind left a comment

Choose a reason for hiding this comment

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

Overall, I rather like this. I'd be tempted to handle wildcard matching using filepath.Match(), but it looks like the implementation we're trying to be compatible with isn't as flexible as that. A couple of notes on the normalization of docker image names and the default matching method, but otherwise it looks pretty good to me.

tinovyatkin and others added 2 commits January 27, 2026 01:35
Change the default match type from EXACT to WILDCARD based on
BuildKit's protobuf/json tagging which indicates wildcard should
be the default behavior.

Addresses: containers#6647 (comment)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Konstantin Vyatkin <tino@vtkn.io>
Add note explaining that CONVERT rules are processed after --build-context
substitutions but before containers-registries.conf substitutions. Also
update matchType default documentation to reflect WILDCARD being the default.

Addresses: containers#6647 (comment)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Konstantin Vyatkin <tino@vtkn.io>
@packit-as-a-service
Copy link

Ephemeral COPR build failed. @containers/packit-build please check.

tinovyatkin and others added 2 commits January 27, 2026 01:38
Replace custom image reference normalization with the proper
go.podman.io/image/v5/docker/reference.ParseNormalizedNamed()
function. Updated test cases to use valid SHA256 digests.

Addresses: containers#6647 (comment)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Konstantin Vyatkin <tino@vtkn.io>
Co-authored-by: Tom Sweeney <tsweeney@redhat.com>
Signed-off-by: Konstantin Vyatkin <tino@vtkn.io>
Copy link
Member

@nalind nalind left a comment

Choose a reason for hiding this comment

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

LGTM, restarted a test run that failed due to a known flake.

@TomSweeneyRedHat
Copy link
Member

LGTM

@TomSweeneyRedHat TomSweeneyRedHat merged commit 1d16a5d into containers:main Jan 29, 2026
37 checks passed
@tinovyatkin tinovyatkin deleted the feature/source-policy-file branch January 30, 2026 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants