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 7, 2024
1 parent 5ac0dbc commit 973c5e1
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 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 @@ -215,23 +216,9 @@ func getFnNamespace() (string, error) {

func isAuthorized(permissions []string, namespace, fn string) bool {
for _, permission := range permissions {
if permission == "*" {
if match := matchString(permission, fmt.Sprintf("%s:%s", namespace, fn)); match {
return true
}

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

if namespace != allowedNamespace {
continue
}

if allowedFunction != "*" && fn != allowedFunction {
continue
}

return true
}

return false
Expand All @@ -256,3 +243,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 973c5e1

Please sign in to comment.