-
Notifications
You must be signed in to change notification settings - Fork 8.1k
build: rego source policies #23782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dvdksn
wants to merge
2
commits into
docker:main
Choose a base branch
from
dvdksn:build-input-policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,718
−11
Open
build: rego source policies #23782
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| --- | ||
| title: Validating build inputs with policies | ||
| linkTitle: Validating builds | ||
| description: Secure your Docker builds by validating images, Git repositories, and dependencies with build policies | ||
| keywords: build policies, opa, rego, docker security, supply chain, attestations | ||
| weight: 70 | ||
| params: | ||
| sidebar: | ||
| badge: | ||
| color: blue | ||
| text: Experimental | ||
| --- | ||
|
|
||
| Building with Docker often involves downloading remote resources of some kind. | ||
| These external dependencies are called **build inputs**: Docker images, Git | ||
| repositories, remote files, and other artifacts your build pulls in. | ||
|
|
||
| For example: | ||
|
|
||
| - Pulling images from a registry | ||
| - Cloning a source code repository | ||
| - Fetching files from a server over HTTPS | ||
|
|
||
| When consuming build inputs, it's a good idea to verify the contents are what | ||
| you expect them to be. One way to do this is to use the `--checksum` option for | ||
| the `ADD` Dockerfile instruction. This lets you verify the SHA256 checksum of a | ||
| remote resource when pulling it into a build: | ||
|
|
||
| ```dockerfile | ||
| ADD --checksum=sha256:c0ff3312345… https://example.com/archive.tar.gz / | ||
| ``` | ||
|
|
||
| If the remote `archive.tar.gz` file does not match the checksum that the | ||
| Dockerfile expects, the build fails. | ||
|
|
||
| Checksums verify that content matches what you expect, but only for the `ADD` | ||
| instruction. They don't tell you anything about where the content came from or | ||
| how it was produced. You can't use checksums to enforce constraints like | ||
| "images must be signed" or "dependencies must come from approved sources." | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Build policies is currently an experimental features. To try it out, you'll | ||
| need: | ||
|
|
||
| - Buildx 0.31.0 or later - Check your version: `docker buildx version` | ||
| - BuildKit 0.26.0 or later - Verify with: `docker buildx inspect --bootstrap` | ||
|
|
||
| If you're using Docker Desktop, ensure you're on a version that includes these | ||
| updates. | ||
|
|
||
| ## Build policies | ||
|
|
||
| Buildx version 0.31.0 added support for **build policies**. Build policies are | ||
| rules for securing your Docker build supply chain, and help protect against | ||
| upstream compromises, malicious dependencies, and unauthorized modifications to | ||
| your build inputs. | ||
|
|
||
| Build policies let you enforce extended verifications on inputs used to build | ||
| your projects, such as: | ||
|
|
||
| - Docker images must use digest references (not tags alone) | ||
| - Images must have provenance attestations and cosign signatures | ||
| - Git tags are signed by maintainers with a PGP public key | ||
| - All remote artifacts must use HTTPS and include a checksum for verification | ||
|
|
||
| Build policies are defined in a declarative policy language, called Rego, | ||
| created for the [Open Policy Agent (OPA)](https://www.openpolicyagent.org/). | ||
| The following example shows a minimal build policy in Rego. | ||
|
|
||
| ```rego {title="Dockerfile.rego"} | ||
| package docker | ||
|
|
||
| default allow := false | ||
|
|
||
| # Allow any local inputs for this build | ||
| # For example: a local build context, or a local Dockerfile | ||
| allow if input.local | ||
|
|
||
| # Allow images, but only if they have provenance attestations | ||
| allow if { | ||
| input.image.hasProvenance | ||
| } | ||
|
|
||
| decision := {"allow": allow} | ||
| ``` | ||
|
|
||
| If the Dockerfile associated with this policy references an image with no | ||
| provenance attestation in a `FROM` instruction, the policy would be violated | ||
| and the build would fail. | ||
|
|
||
| ## How policies work | ||
|
|
||
| When you run `docker buildx build`, buildx: | ||
|
|
||
| 1. Resolves all build inputs (images, Git repos, HTTP downloads) | ||
| 2. Looks for a policy file matching your Dockerfile name (e.g., | ||
| `Dockerfile.rego`) | ||
| 3. Evaluates each input against the policy before the build starts | ||
| 4. Allows the build to proceed only if all inputs pass the policy | ||
|
|
||
| Policies are written in Rego (Open Policy Agent's policy language). You don't | ||
| need to be a Rego expert - the [Introduction](./intro.md) tutorial teaches you | ||
| everything needed. | ||
|
|
||
| Policy files live alongside your Dockerfile: | ||
|
|
||
| ```text | ||
| project/ | ||
| ├── Dockerfile | ||
| ├── Dockerfile.rego | ||
| └── src/ | ||
| ``` | ||
|
|
||
| No additional configuration is needed - buildx automatically finds and loads | ||
| the policy when you build. | ||
|
|
||
| ## Use cases | ||
|
|
||
| Build policies help you enforce security and compliance requirements on your | ||
| Docker builds. Common scenarios where policies provide value: | ||
|
|
||
| ### Enforce base image standards | ||
|
|
||
| Require all production Dockerfiles to use specific, approved base images with | ||
| digest references. Prevent developers from using arbitrary images that haven't | ||
| been vetted by your security team. | ||
|
|
||
| ### Validate third-party dependencies | ||
|
|
||
| When your build downloads files, libraries, or tools from the internet, verify | ||
| they come from trusted sources and match expected checksums or signatures. This | ||
| protects against supply chain attacks where an upstream dependency is | ||
| compromised. | ||
|
|
||
| ### Ensure signed releases | ||
|
|
||
| Require that all dependencies have valid signatures from trusted parties. | ||
|
|
||
| - Check GPG signatures for Git repositories you clone in your builds | ||
| - Verify provenance attestation signatures with Sigstore | ||
|
|
||
| ### Meet compliance requirements | ||
|
|
||
| Some regulatory frameworks require evidence that you validate your build | ||
| inputs. Build policies give you an auditable, declarative way to demonstrate | ||
| you're checking dependencies against security standards. | ||
|
|
||
| ### Separate development and production rules | ||
|
|
||
| Apply stricter validation for production builds while allowing more flexibility | ||
| during development. The same policy file can contain conditional rules based on | ||
| build context or target. | ||
|
|
||
| ## Get started | ||
|
|
||
| Ready to start writing policies? The [Introduction](./intro.md) tutorial walks | ||
| you through creating your first policy and teaches the Rego basics you need. | ||
|
|
||
| For practical usage guidance, see [Using build policies](./usage.md). | ||
|
|
||
| For practical examples you can copy and adapt, see the [Example | ||
| policies](./examples.md) library. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.