Skip to content

Commit

Permalink
api: refactor anyNamespaceAllows method
Browse files Browse the repository at this point in the history
Don't rely on magic input (`""`) change the behaviour of
anyNamespaceAllows. Use two different methods instead with the core
logic implemented in a shared method.
  • Loading branch information
lgfa29 committed Jul 6, 2022
1 parent 0b9ca17 commit a7a06b8
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions acl/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (a *ACL) AllowNamespaceOperation(ns string, op string) bool {

// If using the all namespaces wildcard, allow if any namespace allows the
// operation.
if ns == AllNamespacesSentinel && a.anyNamespaceAllows(op) {
if ns == AllNamespacesSentinel && a.anyNamespaceAllowsOp(op) {
return true
}

Expand Down Expand Up @@ -268,7 +268,7 @@ func (a *ACL) AllowNamespace(ns string) bool {

// If using the all namespaces wildcard, allow if any namespace allows any
// operation.
if ns == AllNamespacesSentinel && a.anyNamespaceAllows("") {
if ns == AllNamespacesSentinel && a.anyNamespaceAllowsAnyOp() {
return true
}

Expand Down Expand Up @@ -340,22 +340,33 @@ func (a *ACL) matchingNamespaceCapabilitySet(ns string) (capabilitySet, bool) {
return a.findClosestMatchingGlob(a.wildcardNamespaces, ns)
}

// anyNamespaceAllows returns true if any namespace in the ACL object allows
// the given operation.
// If op is an empty string it returns true if any namespace allows any
// operation.
func (a *ACL) anyNamespaceAllows(op string) bool {
// anyNamespaceAllowsOp returns true if any namespace in ACL object allows the
// given operation.
func (a *ACL) anyNamespaceAllowsOp(op string) bool {
return a.anyNamespaceAllows(func(c capabilitySet) bool {
return c.Check(op)
})
}

// anyNamespaceAllowsAnyOp returns true if any namespace in ACL object allows
// at least one operation.
func (a *ACL) anyNamespaceAllowsAnyOp() bool {
return a.anyNamespaceAllows(func(c capabilitySet) bool {
return len(c) > 0 && !c.Check(PolicyDeny)
})
}

// anyNamespaceAllows returns true if the callback cb returns true for any
// namespace operation of the ACL object.
func (a *ACL) anyNamespaceAllows(cb func(capabilitySet) bool) bool {
allow := false

checkFn := func(_ []byte, iv interface{}) bool {
v := iv.(capabilitySet)

allowAnyOp := op == "" && len(v) > 0 && !v.Check(PolicyDeny)
if allowAnyOp || v.Check(op) {
allow = true
allow = cb(v)
if allow {
return true
}

return false
}

Expand Down

0 comments on commit a7a06b8

Please sign in to comment.