Skip to content

Commit

Permalink
Use regex for permission pattern matching
Browse files Browse the repository at this point in the history
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
welteki committed May 14, 2024
1 parent 8805db3 commit 19d7e56
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions executor/jwt_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -219,19 +220,9 @@ func isAuthorized(permissions []string, namespace, fn string) bool {
return true
}

parts := strings.Split(permission, ":")
allowedNamespace := parts[0]
allowedFunction := parts[1]

if namespace != allowedNamespace {
continue
}

if allowedFunction != "*" && fn != allowedFunction {
continue
if matchString(permission, fmt.Sprintf("%s:%s", namespace, fn)) {
return true
}

return true
}

return false
Expand All @@ -256,3 +247,29 @@ func getPermissions(mapClaims jwt.MapClaims) []string {
}
return values
}

func matchString(pattern string, value string) bool {
if len(pattern) > 0 {
result, _ := regexp.MatchString(wildCardToRegexp(pattern), value)
return result
}

return pattern == value
}

// wildCardToRegexp converts a wildcard pattern to a regular expression pattern.
func wildCardToRegexp(pattern string) string {
var result strings.Builder
for i, literal := range strings.Split(pattern, "*") {

// Replace * with .*
if i > 0 {
result.WriteString(".*")
}

// Quote any regular expression meta characters in the
// literal text.
result.WriteString(regexp.QuoteMeta(literal))
}
return result.String()
}

0 comments on commit 19d7e56

Please sign in to comment.