@@ -97,6 +97,7 @@ type policyEntryFilter struct {
9797 name string
9898 elementsEntries []policyEntryFilter
9999 memberReplace * policyEntryReplace
100+ onlyIfEmpty bool
100101}
101102
102103type policyEntryReplace struct {
@@ -148,6 +149,9 @@ var policyEntryFilters = []policyEntryFilter{
148149 regexp : regexp .MustCompile (`^[a-z0-9]{4,}(-[a-z0-9]{4,})+$` ),
149150 replace : "uuid-for-permissions-on-related-indices" ,
150151 }},
152+
153+ // Namespaces may not be present in older versions of the stack.
154+ {name : "namespaces" , onlyIfEmpty : true },
151155}
152156
153157// cleanPolicy prepares a policy YAML as returned by the download API to be compared with other
@@ -170,15 +174,16 @@ func cleanPolicy(policy []byte, entriesToClean []policyEntryFilter) ([]byte, err
170174
171175func cleanPolicyMap (policyMap common.MapStr , entries []policyEntryFilter ) (common.MapStr , error ) {
172176 for _ , entry := range entries {
177+ v , err := policyMap .GetValue (entry .name )
178+ if errors .Is (err , common .ErrKeyNotFound ) {
179+ continue
180+ }
181+ if err != nil {
182+ return nil , err
183+ }
184+
173185 switch {
174186 case len (entry .elementsEntries ) > 0 :
175- v , err := policyMap .GetValue (entry .name )
176- if errors .Is (err , common .ErrKeyNotFound ) {
177- continue
178- }
179- if err != nil {
180- return nil , err
181- }
182187 list , err := common .ToMapStrSlice (v )
183188 if err != nil {
184189 return nil , err
@@ -197,10 +202,6 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
197202 return nil , err
198203 }
199204 case entry .memberReplace != nil :
200- v , err := policyMap .GetValue (entry .name )
201- if errors .Is (err , common .ErrKeyNotFound ) {
202- continue
203- }
204205 m , ok := v .(common.MapStr )
205206 if ! ok {
206207 return nil , fmt .Errorf ("expected map, found %T" , v )
@@ -212,6 +213,9 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
212213 }
213214 }
214215 default :
216+ if entry .onlyIfEmpty && ! isEmpty (v ) {
217+ continue
218+ }
215219 err := policyMap .Delete (entry .name )
216220 if errors .Is (err , common .ErrKeyNotFound ) {
217221 continue
@@ -224,3 +228,16 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
224228
225229 return policyMap , nil
226230}
231+
232+ func isEmpty (v any ) bool {
233+ switch v := v .(type ) {
234+ case nil :
235+ return true
236+ case []any :
237+ return len (v ) == 0
238+ case map [string ]any :
239+ return len (v ) == 0
240+ }
241+
242+ return false
243+ }
0 commit comments