@@ -113,9 +113,13 @@ func (c *RegistryRuleSetCache) GetOrBuild(specRules []rules.OCIRegistry) (rs *Ru
113113 return built , false , nil
114114}
115115
116+ // Match matches a reference against target, regex and pullPolicy.
117+ // Admission deny/allow/audit evaluation should usually use MatchReference instead,
118+ // because it needs to distinguish "regex matched but pullPolicy is forbidden" from
119+ // "regex did not match".
116120func (c * RegistryRuleSetCache ) Match (
117121 specRules []rules.OCIRegistry ,
118- image string ,
122+ reference string ,
119123 pullPolicy corev1.PullPolicy ,
120124 target rules.RegistryValidationTarget ,
121125) (* CompiledRule , error ) {
@@ -128,12 +132,13 @@ func (c *RegistryRuleSetCache) Match(
128132 return nil , nil
129133 }
130134
131- return c .MatchRuleSet (rs , image , pullPolicy , target )
135+ return c .MatchRuleSet (rs , reference , pullPolicy , target )
132136}
133137
138+ // MatchRuleSet matches a reference against target, regex and pullPolicy.
134139func (c * RegistryRuleSetCache ) MatchRuleSet (
135140 rs * RuleSet ,
136- image string ,
141+ reference string ,
137142 pullPolicy corev1.PullPolicy ,
138143 target rules.RegistryValidationTarget ,
139144) (* CompiledRule , error ) {
@@ -165,7 +170,46 @@ func (c *RegistryRuleSetCache) MatchRuleSet(
165170 return nil , err
166171 }
167172
168- if compiled .MatchString (image ) {
173+ if compiled .MatchString (reference ) {
174+ return rule , nil
175+ }
176+ }
177+
178+ return nil , nil
179+ }
180+
181+ // MatchReference matches a reference against target and regex only.
182+ // It intentionally does not check pullPolicy.
183+ func (c * RegistryRuleSetCache ) MatchReference (
184+ rs * RuleSet ,
185+ reference string ,
186+ target rules.RegistryValidationTarget ,
187+ ) (* CompiledRule , error ) {
188+ if c == nil {
189+ return nil , fmt .Errorf ("registry rule set cache is nil" )
190+ }
191+
192+ if c .regexCache == nil {
193+ return nil , fmt .Errorf ("regex cache is nil" )
194+ }
195+
196+ if rs == nil {
197+ return nil , nil
198+ }
199+
200+ for i := range rs .Compiled {
201+ rule := & rs .Compiled [i ]
202+
203+ if ! rule .MatchesTarget (target ) {
204+ continue
205+ }
206+
207+ compiled , _ , err := c .regexCache .GetOrCompile (rule .Expression )
208+ if err != nil {
209+ return nil , err
210+ }
211+
212+ if compiled .MatchString (reference ) {
169213 return rule , nil
170214 }
171215 }
@@ -174,6 +218,10 @@ func (c *RegistryRuleSetCache) MatchRuleSet(
174218}
175219
176220func (c * RegistryRuleSetCache ) Stats () int {
221+ if c == nil {
222+ return 0
223+ }
224+
177225 c .mu .RLock ()
178226 defer c .mu .RUnlock ()
179227
@@ -182,6 +230,10 @@ func (c *RegistryRuleSetCache) Stats() int {
182230
183231// activeIDs: set of ids currently referenced by RuleStatus in cluster.
184232func (c * RegistryRuleSetCache ) PruneActive (activeIDs map [string ]struct {}) int {
233+ if c == nil {
234+ return 0
235+ }
236+
185237 c .mu .Lock ()
186238 defer c .mu .Unlock ()
187239
@@ -267,6 +319,10 @@ func (c *RegistryRuleSetCache) HashRules(specRules []rules.OCIRegistry) string {
267319
268320// Has is useful in tests and debugging.
269321func (c * RegistryRuleSetCache ) Has (id string ) bool {
322+ if c == nil {
323+ return false
324+ }
325+
270326 c .mu .RLock ()
271327 defer c .mu .RUnlock ()
272328
@@ -276,47 +332,14 @@ func (c *RegistryRuleSetCache) Has(id string) bool {
276332}
277333
278334func (c * RegistryRuleSetCache ) Reset () {
279- c .mu .Lock ()
280- defer c .mu .Unlock ()
281-
282- c .rs = make (map [string ]* RuleSet )
283- }
284-
285- func (c * RegistryRuleSetCache ) MatchReference (
286- rs * RuleSet ,
287- reference string ,
288- target rules.RegistryValidationTarget ,
289- ) (* CompiledRule , error ) {
290335 if c == nil {
291- return nil , fmt . Errorf ( "registry rule set cache is nil" )
336+ return
292337 }
293338
294- if c .regexCache == nil {
295- return nil , fmt .Errorf ("regex cache is nil" )
296- }
297-
298- if rs == nil {
299- return nil , nil
300- }
301-
302- for i := range rs .Compiled {
303- rule := & rs .Compiled [i ]
304-
305- if ! rule .MatchesTarget (target ) {
306- continue
307- }
308-
309- compiled , _ , err := c .regexCache .GetOrCompile (rule .Expression )
310- if err != nil {
311- return nil , err
312- }
313-
314- if compiled .MatchString (reference ) {
315- return rule , nil
316- }
317- }
339+ c .mu .Lock ()
340+ defer c .mu .Unlock ()
318341
319- return nil , nil
342+ c . rs = make ( map [ string ] * RuleSet )
320343}
321344
322345// InsertForTest can be behind a build tag if you prefer, but it is fine to keep simple.
0 commit comments