@@ -2,6 +2,7 @@ package sourcepolicy
22
33import (
44 "context"
5+ "sync"
56
67 "github.com/moby/buildkit/solver/pb"
78 spb "github.com/moby/buildkit/sourcepolicy/pb"
@@ -25,31 +26,30 @@ var (
2526// Rule matching is delegated to the `Matcher` interface.
2627// Mutations are delegated to the `Mutater` interface.
2728type Engine struct {
28- pol []* spb.Policy
29- sources map [string ]* selectorCache
29+ pol []* spb.Policy
30+ sourcesMu sync.Mutex
31+ sources map [string ]* selectorCache
3032}
3133
3234// NewEngine creates a new source policy engine.
3335func NewEngine (pol []* spb.Policy ) * Engine {
3436 return & Engine {
35- pol : pol ,
37+ pol : pol ,
38+ sources : make (map [string ]* selectorCache ),
3639 }
3740}
3841
3942// TODO: The key here can't be used to cache attr constraint regexes.
4043func (e * Engine ) selectorCache (src * spb.Selector ) * selectorCache {
41- if e .sources == nil {
42- e .sources = map [string ]* selectorCache {}
43- }
44-
4544 key := src .MatchType .String () + " " + src .Identifier
4645
46+ e .sourcesMu .Lock ()
47+ defer e .sourcesMu .Unlock ()
4748 if s , ok := e .sources [key ]; ok {
4849 return s
4950 }
5051
5152 s := & selectorCache {Selector : src }
52-
5353 e .sources [key ] = s
5454 return s
5555}
@@ -130,7 +130,7 @@ func (e *Engine) evaluatePolicy(ctx context.Context, pol *spb.Policy, srcOp *pb.
130130 var deny bool
131131 for _ , rule := range pol .Rules {
132132 selector := e .selectorCache (rule .Selector )
133- matched , err := match (selector , ident , srcOp .Attrs )
133+ matched , err := match (selector , ident , rule . Selector . Constraints , srcOp .Attrs )
134134 if err != nil {
135135 return false , errors .Wrap (err , "error matching source policy" )
136136 }
0 commit comments