Skip to content

Commit 5ef9d59

Browse files
committed
add rego integration to source policies
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent f1a0fc5 commit 5ef9d59

File tree

1,161 files changed

+752641
-3192
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,161 files changed

+752641
-3192
lines changed

build/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/distribution/reference"
2121
"github.com/docker/buildx/builder"
2222
"github.com/docker/buildx/driver"
23+
"github.com/docker/buildx/policy"
2324
"github.com/docker/buildx/util/buildflags"
2425
"github.com/docker/buildx/util/confutil"
2526
"github.com/docker/buildx/util/desktop"
@@ -112,6 +113,8 @@ type Inputs struct {
112113
// DockerfileMappingSrc and DockerfileMappingDst are filled in by the builder.
113114
DockerfileMappingSrc string
114115
DockerfileMappingDst string
116+
117+
policy []policy.File
115118
}
116119

117120
type NamedContext struct {

build/opt.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"maps"
88
"os"
9+
"path"
910
"path/filepath"
1011
"slices"
1112
"strconv"
@@ -21,6 +22,7 @@ import (
2122
"github.com/distribution/reference"
2223
"github.com/docker/buildx/builder"
2324
"github.com/docker/buildx/driver"
25+
"github.com/docker/buildx/policy"
2426
"github.com/docker/buildx/util/buildflags"
2527
"github.com/docker/buildx/util/confutil"
2628
"github.com/docker/buildx/util/dockerutil"
@@ -38,6 +40,7 @@ import (
3840
"github.com/moby/buildkit/session/sshforward/sshprovider"
3941
"github.com/moby/buildkit/session/upload/uploadprovider"
4042
"github.com/moby/buildkit/solver/pb"
43+
"github.com/moby/buildkit/sourcepolicy/policysession"
4144
"github.com/moby/buildkit/util/apicaps"
4245
"github.com/moby/buildkit/util/entitlements"
4346
"github.com/moby/buildkit/util/gitutil"
@@ -322,6 +325,20 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt *O
322325
}
323326
defers = append(defers, releaseLoad)
324327

328+
if len(opt.Inputs.policy) > 0 {
329+
env := policy.Env{}
330+
for k, v := range opt.BuildArgs {
331+
if env.Args == nil {
332+
env.Args = map[string]*string{}
333+
}
334+
env.Args[k] = &v
335+
}
336+
env.Filename = path.Base(opt.Inputs.DockerfilePath)
337+
env.Target = opt.Target
338+
env.Labels = opt.Labels
339+
so.SourcePolicyProvider = policysession.NewPolicyProvider(policy.NewPolicy(opt.Inputs.policy, env).CheckPolicy)
340+
}
341+
325342
// add node identifier to shared key if one was specified
326343
if so.SharedKey != "" {
327344
so.SharedKey += ":" + cfg.TryNodeIdentifier()
@@ -535,6 +552,19 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw pro
535552
return nil, err
536553
}
537554
dockerfileName = handleLowercaseDockerfile(dockerfileDir, dockerfileName)
555+
556+
if fi, err := os.Lstat(filepath.Join(dockerfileDir, dockerfileName+".rego")); err == nil {
557+
if fi.Mode().IsRegular() {
558+
dt, err := os.ReadFile(filepath.Join(dockerfileDir, dockerfileName+".rego"))
559+
if err != nil {
560+
return nil, errors.Wrapf(err, "failed to read policy file %s.rego", dockerfileName)
561+
}
562+
inp.policy = append(inp.policy, policy.File{
563+
Filename: dockerfileName + ".rego",
564+
Data: dt,
565+
})
566+
}
567+
}
538568
}
539569

540570
target.FrontendAttrs["filename"] = dockerfileName

go.mod

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/docker/buildx
22

3-
go 1.24.3
3+
go 1.24.6
44

55
require (
66
github.com/Masterminds/semver/v3 v3.4.0
@@ -36,22 +36,23 @@ require (
3636
github.com/moby/sys/atomicwriter v0.1.0
3737
github.com/moby/sys/mountinfo v0.7.2
3838
github.com/morikuni/aec v1.0.0
39+
github.com/open-policy-agent/opa v1.10.1
3940
github.com/opencontainers/go-digest v1.0.0
4041
github.com/opencontainers/image-spec v1.1.1
4142
github.com/pelletier/go-toml v1.9.5
4243
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
4344
github.com/pkg/errors v0.9.1
4445
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10
4546
github.com/serialx/hashring v0.0.0-20200727003509-22c0c7ab6b1b
46-
github.com/sirupsen/logrus v1.9.3
47+
github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af
4748
github.com/spf13/cobra v1.10.1
4849
github.com/spf13/pflag v1.0.10
4950
github.com/stretchr/testify v1.11.1
5051
github.com/tonistiigi/fsutil v0.0.0-20250605211040-586307ad452f
5152
github.com/tonistiigi/go-csvvalue v0.0.0-20240814133006-030d3b2625d0
5253
github.com/tonistiigi/jaeger-ui-rest v0.0.0-20250408171107-3dd17559e117
5354
github.com/zclconf/go-cty v1.17.0
54-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0
55+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.63.0
5556
go.opentelemetry.io/otel v1.38.0
5657
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0
5758
go.opentelemetry.io/otel/metric v1.38.0
@@ -75,6 +76,7 @@ require (
7576
require (
7677
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
7778
github.com/agext/levenshtein v1.2.3 // indirect
79+
github.com/agnivade/levenshtein v1.2.1 // indirect
7880
github.com/apparentlymart/go-cidr v1.0.1 // indirect
7981
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
8082
github.com/aws/aws-sdk-go-v2 v1.38.1 // indirect
@@ -89,25 +91,31 @@ require (
8991
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.34.0 // indirect
9092
github.com/aws/aws-sdk-go-v2/service/sts v1.38.0 // indirect
9193
github.com/aws/smithy-go v1.22.5 // indirect
94+
github.com/beorn7/perks v1.0.1 // indirect
9295
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
96+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
9397
github.com/containerd/containerd/api v1.10.0 // indirect
9498
github.com/containerd/errdefs/pkg v0.3.0 // indirect
9599
github.com/containerd/ttrpc v1.2.7 // indirect
96100
github.com/containerd/typeurl/v2 v2.2.3 // indirect
97101
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
102+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
98103
github.com/docker/distribution v2.8.3+incompatible // indirect
99104
github.com/docker/docker-credential-helpers v0.9.4 // indirect
100105
github.com/docker/go-connections v0.6.0 // indirect
101106
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
102107
github.com/felixge/httpsnoop v1.0.4 // indirect
103108
github.com/fvbommel/sortorder v1.1.0 // indirect
104109
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
110+
github.com/go-ini/ini v1.67.0 // indirect
105111
github.com/go-logr/logr v1.4.3 // indirect
106112
github.com/go-logr/stdr v1.2.2 // indirect
107113
github.com/go-openapi/jsonpointer v0.21.0 // indirect
108114
github.com/go-openapi/jsonreference v0.20.2 // indirect
109115
github.com/go-openapi/swag v0.23.0 // indirect
110116
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
117+
github.com/gobwas/glob v0.2.3 // indirect
118+
github.com/goccy/go-json v0.10.5 // indirect
111119
github.com/gogo/protobuf v1.3.2 // indirect
112120
github.com/golang/protobuf v1.5.4 // indirect
113121
github.com/google/gnostic-models v0.7.0 // indirect
@@ -121,6 +129,14 @@ require (
121129
github.com/josharian/intern v1.0.0 // indirect
122130
github.com/json-iterator/go v1.1.12 // indirect
123131
github.com/klauspost/compress v1.18.1 // indirect
132+
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
133+
github.com/lestrrat-go/dsig v1.0.0 // indirect
134+
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 // indirect
135+
github.com/lestrrat-go/httpcc v1.0.1 // indirect
136+
github.com/lestrrat-go/httprc/v3 v3.0.1 // indirect
137+
github.com/lestrrat-go/jwx/v3 v3.0.11 // indirect
138+
github.com/lestrrat-go/option v1.0.1 // indirect
139+
github.com/lestrrat-go/option/v2 v2.0.0 // indirect
124140
github.com/mailru/easyjson v0.7.7 // indirect
125141
github.com/mattn/go-runewidth v0.0.16 // indirect
126142
github.com/mattn/go-shellwords v1.0.12 // indirect
@@ -139,16 +155,28 @@ require (
139155
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
140156
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
141157
github.com/pmezard/go-difflib v1.0.0 // indirect
142-
github.com/rivo/uniseg v0.2.0 // indirect
158+
github.com/prometheus/client_golang v1.23.2 // indirect
159+
github.com/prometheus/client_model v0.6.2 // indirect
160+
github.com/prometheus/common v0.66.1 // indirect
161+
github.com/prometheus/procfs v0.17.0 // indirect
162+
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect
163+
github.com/rivo/uniseg v0.4.7 // indirect
143164
github.com/russross/blackfriday/v2 v2.1.0 // indirect
144165
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 // indirect
145166
github.com/secure-systems-lab/go-securesystemslib v0.9.1 // indirect
167+
github.com/segmentio/asm v1.2.0 // indirect
146168
github.com/shibumi/go-pathspec v1.3.0 // indirect
169+
github.com/tchap/go-patricia/v2 v2.3.3 // indirect
147170
github.com/tonistiigi/dchapes-mode v0.0.0-20250318174251-73d941a28323 // indirect
148171
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirect
149172
github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab // indirect
173+
github.com/valyala/fastjson v1.6.4 // indirect
174+
github.com/vektah/gqlparser/v2 v2.5.30 // indirect
150175
github.com/x448/float16 v0.8.4 // indirect
176+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
177+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
151178
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
179+
github.com/yashtewari/glob-intersection v0.2.0 // indirect
152180
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
153181
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.61.0 // indirect
154182
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.61.0 // indirect

0 commit comments

Comments
 (0)