Skip to content

Commit 5a5d59f

Browse files
authored
Merge pull request #3539 from tonistiigi/opa-source-policy
add rego integration to source policies
2 parents ee2ecd0 + e5116c1 commit 5a5d59f

2,249 files changed

Lines changed: 958311 additions & 8786 deletions

File tree

Some content is hidden

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

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ARG ALPINE_VERSION=3.23
55
ARG XX_VERSION=1.7.0
66

77
# for testing
8-
ARG DOCKER_VERSION=29.0.0
8+
ARG DOCKER_VERSION=29.1
99
ARG DOCKER_VERSION_ALT_28=28.5
1010
ARG DOCKER_VERSION_ALT_27=27.5.1
1111
ARG DOCKER_CLI_VERSION=${DOCKER_VERSION}

build/build.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"io"
11+
"io/fs"
1112
"maps"
1213
"os"
1314
"slices"
@@ -21,6 +22,7 @@ import (
2122
noderesolver "github.com/docker/buildx/build/resolver"
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/desktop"
@@ -95,6 +97,15 @@ type Options struct {
9597
SourcePolicy *spb.Policy
9698
GroupRef string
9799
Annotations map[exptypes.AnnotationKey]string // Not used during build, annotations are already set in Exports. Just used to check for support with drivers.
100+
Policy []PolicyConfig
101+
}
102+
103+
type PolicyConfig struct {
104+
Files []policy.File
105+
Reset bool
106+
Disabled bool
107+
Strict *bool
108+
LogLevel *logrus.Level
98109
}
99110

100111
type CallFunc struct {
@@ -113,6 +124,85 @@ type Inputs struct {
113124
// DockerfileMappingSrc and DockerfileMappingDst are filled in by the builder.
114125
DockerfileMappingSrc string
115126
DockerfileMappingDst string
127+
128+
policy *policyOpt
129+
}
130+
131+
type policyOpt struct {
132+
Files []policy.File
133+
FS func() (fs.StatFS, func() error, error)
134+
Strict bool
135+
LogLevel *logrus.Level
136+
}
137+
138+
func withPolicyConfig(defaultPolicy policyOpt, configs []PolicyConfig) ([]policyOpt, error) {
139+
if len(configs) == 0 {
140+
if len(defaultPolicy.Files) == 0 {
141+
return nil, nil
142+
}
143+
return []policyOpt{defaultPolicy}, nil
144+
}
145+
146+
for _, cfg := range configs {
147+
if !cfg.Disabled {
148+
continue
149+
}
150+
if cfg.Reset || cfg.Strict != nil || cfg.LogLevel != nil || len(cfg.Files) > 0 {
151+
return nil, errors.New("disabled policy cannot be combined with other policy flags")
152+
}
153+
if len(configs) > 1 {
154+
return nil, errors.New("disabled policy cannot be combined with other policy flags")
155+
}
156+
return nil, nil
157+
}
158+
159+
out := make([]policyOpt, 0, len(configs)+1)
160+
if len(defaultPolicy.Files) != 0 {
161+
out = append(out, defaultPolicy)
162+
}
163+
164+
var last PolicyConfig
165+
166+
for _, cfg := range configs {
167+
if cfg.Reset {
168+
out = nil
169+
}
170+
171+
if len(cfg.Files) == 0 {
172+
if len(out) == 0 {
173+
last = cfg
174+
} else {
175+
last := &out[len(out)-1]
176+
if cfg.Strict != nil {
177+
last.Strict = *cfg.Strict
178+
}
179+
if cfg.LogLevel != nil {
180+
last.LogLevel = cfg.LogLevel
181+
}
182+
}
183+
continue
184+
}
185+
186+
opt := policyOpt{
187+
Files: cfg.Files,
188+
}
189+
if last.Strict != nil {
190+
opt.Strict = *last.Strict
191+
}
192+
if last.LogLevel != nil {
193+
opt.LogLevel = last.LogLevel
194+
}
195+
if cfg.Strict != nil {
196+
opt.Strict = *cfg.Strict
197+
}
198+
if cfg.LogLevel != nil {
199+
opt.LogLevel = cfg.LogLevel
200+
}
201+
opt.FS = defaultPolicy.FS
202+
out = append(out, opt)
203+
}
204+
205+
return out, nil
116206
}
117207

118208
type NamedContext struct {
@@ -926,7 +1016,7 @@ func detectSharedMounts(ctx context.Context, reqs map[string][]*reqForNode) (_ m
9261016
}
9271017
fsMap := m[nodeName]
9281018
for name, m := range req.so.LocalMounts {
929-
fs, ok := m.(*fs)
1019+
fs, ok := m.(*fsMount)
9301020
if !ok {
9311021
continue
9321022
}

build/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func getGitAttributes(ctx context.Context, contextPath, dockerfilePath string) (
135135
}
136136

137137
for key, mount := range so.LocalMounts {
138-
fs, ok := mount.(*fs)
138+
fs, ok := mount.(*fsMount)
139139
if !ok {
140140
continue
141141
}

0 commit comments

Comments
 (0)