@@ -142,39 +142,76 @@ func marshalDockerfile(ctx context.Context, dt []byte, opts ...llb.ConstraintsOp
142142}
143143
144144func getSigningConfigFromContext (ctx context.Context , client gwclient.Client , cfgPath string , configCtxName string , sOpt dalec.SourceOpts , opts ... llb.ConstraintsOpt ) (* dalec.PackageSigner , error ) {
145- src := dalec.Source {Path : cfgPath , Context : & dalec.SourceContext {Name : configCtxName }}
146- signConfigState := src .ToState ("" , sOpt , opts ... )
147-
148- scDef , err := signConfigState .Marshal (ctx )
145+ dt , err := readConfigFromContext (ctx , client , cfgPath , configCtxName , sOpt , opts ... )
149146 if err != nil {
150147 return nil , err
151148 }
152149
150+ var pc dalec.PackageConfig
151+ if err := yaml .Unmarshal (dt , & pc ); err != nil {
152+ return nil , err
153+ }
154+
155+ return pc .Signer , nil
156+ }
157+
158+ func getSourceFilterConfigFromContext (ctx context.Context , client gwclient.Client , cfgPath string , configState llb.State ) (dalec.SourceFilterConfig , error ) {
159+ scDef , err := configState .Marshal (ctx )
160+ if err != nil {
161+ return dalec.SourceFilterConfig {}, err
162+ }
163+
153164 res , err := client .Solve (ctx , gwclient.SolveRequest {
154165 Definition : scDef .ToPB (),
155166 })
156167 if err != nil {
157- return nil , err
168+ return dalec. SourceFilterConfig {} , err
158169 }
159170
160171 ref , err := res .SingleRef ()
161172 if err != nil {
162- return nil , err
173+ return dalec. SourceFilterConfig {} , err
163174 }
164175
165176 dt , err := ref .ReadFile (ctx , gwclient.ReadRequest {
166177 Filename : cfgPath ,
167178 })
179+ if err != nil {
180+ return dalec.SourceFilterConfig {}, err
181+ }
182+
183+ var cfg dalec.SourceFilterConfig
184+ if err := yaml .Unmarshal (dt , & cfg ); err != nil {
185+ return dalec.SourceFilterConfig {}, err
186+ }
187+
188+ return cfg , nil
189+ }
190+
191+ func readConfigFromContext (ctx context.Context , client gwclient.Client , cfgPath string , configCtxName string , sOpt dalec.SourceOpts , opts ... llb.ConstraintsOpt ) ([]byte , error ) {
192+ src := dalec.Source {Path : cfgPath , Context : & dalec.SourceContext {Name : configCtxName }}
193+ configState := src .ToState ("" , sOpt .WithoutSourceFilter (), opts ... )
194+
195+ scDef , err := configState .Marshal (ctx )
168196 if err != nil {
169197 return nil , err
170198 }
171199
172- var pc dalec.PackageConfig
173- if err := yaml .Unmarshal (dt , & pc ); err != nil {
200+ res , err := client .Solve (ctx , gwclient.SolveRequest {
201+ Definition : scDef .ToPB (),
202+ })
203+ if err != nil {
174204 return nil , err
175205 }
176206
177- return pc .Signer , nil
207+ ref , err := res .SingleRef ()
208+ if err != nil {
209+ return nil , err
210+ }
211+
212+ return ref .ReadFile (ctx , gwclient.ReadRequest {
213+ Filename : cfgPath ,
214+ })
178215}
179216
180217func MaybeSign (ctx context.Context , client gwclient.Client , st llb.State , spec * dalec.Spec , targetKey string , sOpt dalec.SourceOpts , opts ... llb.ConstraintsOpt ) llb.State {
@@ -242,6 +279,36 @@ func getSignConfigCtxName(client gwclient.Client) string {
242279 return client .BuildOpts ().Opts ["build-arg:" + buildArgDalecSigningConfigContextName ]
243280}
244281
282+ func getSourceFilterConfigPath (client gwclient.Client ) string {
283+ return client .BuildOpts ().Opts ["build-arg:" + dalec .BuildArgDalecSourceFilterConfigPath ]
284+ }
285+
286+ func getSourceFilterContextNameWithDefault (client gwclient.Client ) string {
287+ configCtxName := dalec .DefaultSourceOptionsContextName
288+ if cn := client .BuildOpts ().Opts ["build-arg:" + dalec .BuildArgDalecSourceFilterContextName ]; cn != "" {
289+ configCtxName = cn
290+ }
291+ return configCtxName
292+ }
293+
294+ func loadSourceFilterConfig (ctx context.Context , client gwclient.Client , getContext func (string , ... llb.LocalOption ) (* llb.State , error ), opts ... llb.LocalOption ) (dalec.SourceFilterConfig , error ) {
295+ cfgPath := getSourceFilterConfigPath (client )
296+ if cfgPath == "" {
297+ return dalec.SourceFilterConfig {}, nil
298+ }
299+
300+ configCtxName := getSourceFilterContextNameWithDefault (client )
301+ configState , err := getContext (configCtxName , opts ... )
302+ if err != nil {
303+ return dalec.SourceFilterConfig {}, err
304+ }
305+ if configState == nil {
306+ return dalec.SourceFilterConfig {}, errors .Errorf ("context %q not found" , configCtxName )
307+ }
308+
309+ return getSourceFilterConfigFromContext (ctx , client , cfgPath , * configState )
310+ }
311+
245312func forwardToSigner (ctx context.Context , client gwclient.Client , cfg * dalec.PackageSigner , s llb.State , opts ... llb.ConstraintsOpt ) (llb.State , error ) {
246313 const (
247314 // See https://github.com/moby/buildkit/blob/d8d946b85c52095d34a52ce210960832f4e06775/frontend/dockerui/context.go#L29
0 commit comments