1
0
mirror of https://github.com/containers/buildah.git synced 2026-02-05 09:45:38 +01:00
Commit Graph

531 Commits

Author SHA1 Message Date
Tom Sweeney
1d16a5d4c6 Merge pull request #6647 from tinovyatkin/feature/source-policy-file
Add --source-policy-file flag for BuildKit-compatible source policies
2026-01-29 18:38:26 -05:00
iTrooz
b1c9ff5f32 feat(build): print error on build flag --output=type=something
Signed-off-by: iTrooz <hey@itrooz.fr>
2026-01-27 15:48:41 -05:00
Konstantin Vyatkin
2c225f3ccd Add --source-policy-file flag for BuildKit-compatible source policies
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>
2026-01-21 14:07:07 +01:00
Lokesh Mandvekar
ceaff8fc22 build: add --iidfile-raw CLI option
Works much the same way as --iidfile but does not include algorithm
prefix.

Also aliased to --raw-iidfile.

Ref: https://github.com/containers/skopeo/issues/2750#issuecomment-3558195067

Signed-off-by: Lokesh Mandvekar <lsm5@redhat.com>
2025-12-03 09:49:14 -05:00
Nalin Dahyabhai
f57a5bcce4 imagebuildah.stageExecutor.runStageMountPoints(): correct an error
Add a missing "not" to an error message.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-11-04 10:10:30 -05:00
Nalin Dahyabhai
705ba3b9b6 imagebuildah: try to rein in use of transport names in image specs
Try to limit which image transports we accept in stages, and scope the
ones that use path names to the context directory.  At some point
anything that isn't an image ID or pullable spec should start being
rejected.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-11-04 10:10:29 -05:00
Nalin Dahyabhai
7c58fc17f7 imagebuildah: use a longer-lived overlay over the build context
Mount a read-write overlay directory over the build context directory to
restore the ability to use it as a covert cache of sorts during the
lifetime of each platform's build, but in a way that still ensures that
we don't modify the real build context directory.

N.B.: builds where FROM in one stage referenced a relative path which
had been written to a bind-mounted default build context directory by an
earlier stage broke when we started making those bind mounts into
overlays to prevent/discard modifications to that directory, and while
this extends the lifetime of that overlay so that it's consistent
throughout the build, those relative path names are still going to point
to the wrong location.

Since we need to determine SELinux labeling before mounting the overlay,
go ahead and calculate the labels to use before creating the first
builder, and remove the logic that had whichever stage thought it was
the first one set them in its parent object for use by other stages, in
what was probably a racey way.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-11-03 18:07:34 -05:00
Nalin Dahyabhai
1e6bb467fe Add --metadata-file
Add a MetadataFile field to BuildOptions, to which we write a dictionary
of information about a just-committed image.

Pay more attention to sourceDateEpoch than to timestamp when we're
tagging an existing image with the intended destination name.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-10-21 12:17:11 -04:00
Nalin Dahyabhai
91bc3aa43c imagebuildah: unexport the Executor and StageExecutor types
Rename the Executor and StageExecutor types to make them private to the
package.  Exporting them wasn't required for them to supply an interface
that they implemented for use by github.com/openshift/imagebuilder, and
we're about to change the signature for StageExecutor.Execute().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-10-21 12:15:41 -04:00
Adam Eijdenberg
8fa05dfd07 fix(build): make --tag oci-archive:xxx.tar work with simple images
Previously using outputing to a tar would fail with a simple `FROM xxx`
image.

This now will complete the copy (including passing through any specified
timestamp) and stop before trying to tag a non-registry stored result.

Signed-off-by: Adam Eijdenberg <adam@continusec.com>
2025-10-21 09:14:10 +11:00
Nalin Dahyabhai
5a849d1765 commit: always return the config digest as the image ID
When committing, if we didn't get an image ID off the bat because the
image wasn't being committed to local storage, try to return the image's
configuration blob digest, which is what is traditionally used as the
image's ID.

This allows the --iidfile flag to write a value to a file in situations
where the image isn't being written to local storage.  The image ID is
of limited value in these cases, since we can't use it to look up the
image anywhere else, but at least we don't write a file that just has
the digest name prefix or log an empty string.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-10-14 11:17:41 -04:00
Nalin Dahyabhai
16680a4dfd modernize: use strings.CutPrefix/SplitSeq/FieldsSeq
Use the CutPrefix(), SplitSeq(), and FieldsSeq() functions from the
strings package when chopping up or iterating over parts of strings, per
golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-09-10 13:11:00 -04:00
Jan Kaluza
a0a9ac6380 Switch common, storage and image to monorepo.
Signed-off-by: Jan Kaluza <jkaluza@redhat.com>
2025-08-29 15:04:28 +02:00
Nalin Dahyabhai
c4cfbcda8d Handle tagged+digested references when processing --all-platforms
When checking the platforms of the base images, if we encounter a
reference which includes both a tag and a digest, work around it like
the pull logic does: try it without the tag.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-25 15:18:53 -04:00
Nalin Dahyabhai
fd498cbf5d imagebuildah.Executor/StageExecutor: check numeric --from= values
When we look up a stage that's referred to in a COPY --from argument,
treat the string as a stage number not only if it parses as one, as we
checked before, but now also require that the number correspond to one
of the stages that would be completed before the one into which the
content will be copied.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-11 14:10:16 -04:00
openshift-merge-bot[bot]
3ed8ff91a7 Merge pull request #6314 from nalind/no-more-instructions
imagebuildah.StageExecutor.Execute: commit more "no instructions" cases
2025-08-07 15:11:29 +00:00
flouthoc
e31b047293 generatePathChecksum: ignore ModTime, AccessTime and ChangeTime
When generating a checksum for files mounted into container via
`--mount=type=bind` ignore their `ModTime`, `AccessTime` and
`ChangeTime` so we can maintain cache burst consistency with `COPY`
command.

Closes: https://github.com/containers/buildah/issues/6291

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-08-06 08:28:42 -07:00
Nalin Dahyabhai
70d0451b86 History should note unset-label, timestamp, and rewrite-timestamp
Make the history created-by field note when a label was unset, or if the
one of the CLI flags that modifies the timestamps in the layer were
used, so that the cache evaluation logic can factor that into account.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-04 18:49:45 -04:00
Nalin Dahyabhai
fb6ce9d07b imagebuildah.StageExecutor.Execute: commit more "no instructions" cases
When there are no instructions to process, we try to reuse the base
image.  When we've been told, out of band, to remove labels or
environment variables, or affect annotations, we still need to, though,
so check for values of more of those flags.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-04 14:30:25 -04:00
flouthoc
3502889676 stage_executor: check platform of cache candidates
When building images for `manifest` list using `--platform` same image
is used for multiple platform if base is `scratch` , following PR adds a
check to always verify `platform` of `cache` with `target`.

Closes: https://github.com/containers/podman/issues/18723

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-07-11 09:44:07 -07:00
openshift-merge-bot[bot]
252cc24fd5 Merge pull request #6240 from 2004joshua/link
feat: ADD/COPY --link support for buildah
2025-07-11 15:27:00 +00:00
Joshua Arrevillaga
eea4838d88 ADD/COPY --link support added
What type of PR is this?
/kind feature

What this PR does / why we need it:
It implements --link for COPY and ADD instructions and enables the creation of
cachable layers that can be reused independently across builds.

Follows buildkit `--link` specifications

How to verify it
bats tests/bud.bats

Which issue(s) this PR fixes:
Fixes #4325

Does this PR introduce a user-facing change?
Yes, gives extra functionality to Containerfiles

Signed-off-by: Joshua Arrevillaga <2004jarrevillaga@gmail.com>
2025-07-09 17:14:28 -04:00
Patrick Stoeckle
9f809a88ff chore(typos): fix typos
Signed-off-by: Patrick Stoeckle <patrick.stoeckle@siemens.com>
2025-06-30 15:45:47 +02:00
Nalin Dahyabhai
149bf968f5 Accept SOURCE_DATE_EPOCH as a build-arg
When SOURCE_DATE_EPOCH is passed in as a build-arg, treat it as we would
if it was passed in via the environment or its own CLI flag.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-25 15:19:32 -04:00
Nalin Dahyabhai
5968d82047 build, commit: set the OCI ...created annotation on OCI images
When building or committing an image in OCI format, default to setting
the org.opencontainers.image.created annotation to the value used in the
image's config blob for the image's creation date. The behavior can be
controlled using the new --created-annotation flag.

Add --annotation and --unsetannotation flags to `buildah commit` which
mimic the same flags for `buildah build`.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-23 15:05:02 -04:00
Nalin Dahyabhai
df36bb835a commit: exclude parents of mount targets, too
When RUN requires us to create the target for a mountpoint, make note of
it and any parent directories that needed to be created, and filter them
out when generating a layer diff or --output data.

The exceptions will be directories that the conformance tests confirm
that BuildKit also leaves behind, though for compatibility with the
classic builder, we have to make that conditional.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-20 11:19:27 -04:00
flouthoc
47da2f29b2 build: add support for --inherit-annotations
Allows users to specify if they want to inherit annotations from base image
or not.

Closes: https://github.com/containers/buildah/issues/6153

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-06-13 12:28:12 -07:00
Nalin Dahyabhai
962ab4bb1e commit,build: --source-date-epoch/--timestamp omit identity label
When using either --source-date-epoch or --timestamp, default to not
adding a label with our version number in it, since it can change
between builds.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-10 16:18:50 -04:00
openshift-merge-bot[bot]
d14b4f8dc7 Merge pull request #6211 from nalind/source-date-epoch-static-hostname
build: --source-date-epoch/--timestamp use static hostname/cid
2025-06-10 13:55:54 +00:00
openshift-merge-bot[bot]
714f5ba2e5 Merge pull request #6195 from flouthoc/unsetanno
build,config: add support for `--unsetannotation`
2025-06-09 14:19:35 +00:00
Nalin Dahyabhai
2d0152e99d build: --source-date-epoch/--timestamp use static hostname/cid
When using either --source-date-epoch or --timestamp, make sure that
handling of RUN instructions uses a defined hostname if possible, and
commits using a reference to a static container name.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-06 15:33:01 -04:00
Nalin Dahyabhai
865daceebb build: add --source-date-epoch and --rewrite-timestamp flags
Use $SOURCE_DATE_EPOCH as the default for the --source-date-epoch flag
to the "build" CLI.

When a source-date-epoch is set, we'll use it when writing new history
entries, force timestamps in data written for --output to the specified
timestamp, and populate a "SOURCE_DATE_EPOCH" ARG that we treat as
always being set, and which we don't complain about being left unused.
By default, this will not affect timestamps in newly-added layers.

Add a --rewrite-timestamp flag, which "clamps" timestamps in newly-added
layers to not be later than the --source-date-epoch value if the
--source-date-epoch flag is set, but has no effect otherwise.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-05 10:22:56 -04:00
flouthoc
83acf720d9 build,config: add support for --unsetannotation
Just like `--unsetlabel` add support for `--unsetannotation`.

Closes: https://github.com/containers/buildah/issues/6183

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-06-04 12:58:43 -07:00
Nalin Dahyabhai
6c82e7eac0 commit: add --source-date-epoch and --rewrite-timestamp flags
Add a --source-date-epoch flag, defaulting to $SOURCE_DATE_EPOCH if set,
which sets the created-on date and the timestamp for the new history
entries, but does not default to modifying the timestamps on contents in
new layers.

Add a --rewrite-timestamp flag, which "clamps" timestamps in the new
layers to not be later than the --source-date-epoch value if both
the --rewrite-timestamp and --source-date-epoch flags were set.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-04 13:02:26 -04:00
openshift-merge-bot[bot]
b178f3b7ef Merge pull request #6177 from nalind/build-outputs
build: allow --output to be specified multiple times
2025-05-28 19:02:01 +00:00
Nalin Dahyabhai
3a85df003e build: allow --output to be specified multiple times
Allow --output to be specified multiple times for `buildah build`.
That's of limited usefulness right now, but as exporters get added, it
won't be, and it's better to provide the new multiple-values API field
sooner rather than later.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-05-21 09:53:04 -04:00
flouthoc
8f885843db imagebuildah: select most recent layer for cache
If multiple layers are found as cache candidates then select the layer
which was created recently.

Closes: https://github.com/containers/buildah/issues/6152

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-05-20 10:34:21 -07:00
Aditya R
a235033662 build: add support for inherit-labels
Allows users to specify if they want to inherit labels from base image
or not.

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-04-14 14:01:00 -07:00
flouthoc
bc05858e50 stage_executor,getCreatedBy: expand buildArgs for sources correctly
Patch in PR https://github.com/containers/buildah/pull/5691 added a
function to calculate and write checksum to history of `source` in
`--mount` instructions but it did not add part to expand build args if
they are present in `source` path.

Following PR Just corrects the above issue and also adds a new test to
make sure we don't break this in future again.

Closes: https://github.com/containers/podman/issues/25425

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-04-10 10:02:56 -07:00
Kir Kolyshkin
f261d6641c Use maps.Copy
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 15:26:09 -07:00
Kir Kolyshkin
0835cb4760 Use slices.Clone
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 15:26:09 -07:00
Kir Kolyshkin
84a3905f61 Use slices.Contains
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 15:26:01 -07:00
Kir Kolyshkin
17777cf8ac Use for range over integers
Available since Go 1.22 (see https://tip.golang.org/ref/spec#For_range).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 10:19:51 -07:00
Kir Kolyshkin
1ede7ddce7 Use any instead of interface{}
Brought to you by

	gofmt -r 'interface{} -> any' -w .

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 10:19:51 -07:00
Kir Kolyshkin
b343757b11 Do not capitalize error strings
This fixes "ST1005: error strings should not be capitalized
(staticcheck)" warnings.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
Kir Kolyshkin
7b15646547 Apply De Morgan's law to fix QF1001 warnings
This fixes the following warnings:

> chroot/run_test.go:319:39: QF1001: could apply De Morgan's law (staticcheck)
> 				if limit == unix.RLIM_INFINITY && !(rlim == nil || (rlim.Soft == unix.RLIM_INFINITY && rlim.Hard == unix.RLIM_INFINITY)) {
> 				                                  ^
> copier/copier.go:1012:92: QF1001: could apply De Morgan's law (staticcheck)
> 			if target, err := os.Readlink(filepath.Join(workingPath, components[0])); err == nil && !(len(components) == 1 && !evaluateFinalComponent) {
> 			                                                                                        ^
> run_linux.go:459:118: QF1001: could apply De Morgan's law (staticcheck)
> 	if !slices.Contains(volumes, resolvconf.DefaultResolvConf) && options.ConfigureNetwork != define.NetworkDisabled && !(len(b.CommonBuildOpts.DNSServers) == 1 && strings.ToLower(b.CommonBuildOpts.DNSServers[0]) == "none") {
> 	                                                                                                                    ^

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
Kir Kolyshkin
cc2c6d361c imagebuildah: fix revive warning
> imagebuildah/stage_executor.go:714:13: superfluous-else: if block ends with a break statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
> 					} else {
> 						// Treat the source's name as the name of an image.
> 						mountPoint, err := s.getImageRootfs(s.ctx, from)
> 						if err != nil {
> 							return nil, fmt.Errorf("%s from=%s: no stage or image found with that name", flag, from)
> 						}
> 						stageMountPoints[from] = internal.StageMountDetails{
> 							IsImage:    true,
> 							DidExecute: true,
> 							MountPoint: mountPoint,
> 						}
> 						break
> 					}
>

(The alternative is to keep "else" and remove "break", but there are
other break statements above it, so for style consistency it's better to
keep using break.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
flouthoc
022ff233fc stage_executor: reset platform in systemcontext for stages
Every stage now has its own copy of systemcontext.

On processing of every stage platform spec in systemcontext must be
correctly reset.

Closes: https://github.com/containers/buildah/issues/5968

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-04-03 09:19:05 -07:00
Jan Rodák
38df169fcb Use builtin arg defaults from imagebuilder
Signed-off-by: Jan Rodák <hony.com@seznam.cz>
2025-03-21 15:06:10 +01:00
Jan Rodák
7ca9f3464b Add --parents option for COPY in Dockerfiles
It also includes an implementation of the --parents flag for the buildah copy command.

Fixes: https://issues.redhat.com/browse/RUN-2193
Fixes: https://github.com/containers/buildah/issues/5557

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
2025-03-18 21:58:27 +01:00