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
100111type 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
118208type 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 }
0 commit comments