Skip to content

Commit

Permalink
defaultJWTClaimMapper treat namespace as case sensitive (#4244)
Browse files Browse the repository at this point in the history
Change defaultJWTClaimMapper treat namespace as case sensitive.
Change system namespace from system to temporal-system and make it case sensitive.
  • Loading branch information
yiminc authored Apr 27, 2023
1 parent 62c35aa commit 7a24d8e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 4 additions & 3 deletions common/authorization/default_jwt_claim_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/golang-jwt/jwt/v4"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/common/primitives"

"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
Expand All @@ -40,7 +41,7 @@ const (
defaultPermissionsClaimName = "permissions"
authorizationBearer = "bearer"
headerSubject = "sub"
permissionScopeSystem = "system"
permissionScopeSystem = primitives.SystemLocalNamespace
permissionRead = "read"
permissionWrite = "write"
permissionWorker = "worker"
Expand Down Expand Up @@ -110,8 +111,8 @@ func (a *defaultJWTClaimMapper) extractPermissions(permissions []interface{}, cl
a.logger.Warn(fmt.Sprintf("ignoring permission in unexpected format: %v", permission))
continue
}
namespace := strings.ToLower(parts[0])
if strings.EqualFold(namespace, permissionScopeSystem) {
namespace := parts[0]
if namespace == permissionScopeSystem {
claims.System |= permissionToRole(parts[1])
} else {
if claims.Namespaces == nil {
Expand Down
24 changes: 23 additions & 1 deletion common/authorization/default_jwt_claim_mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.temporal.io/server/common/primitives"

"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
Expand Down Expand Up @@ -65,7 +66,7 @@ const (
)

var (
permissionsAdmin = []string{"system:admin", "default:read"}
permissionsAdmin = []string{primitives.SystemLocalNamespace + ":admin", "default:read"}
permissionsReaderWriterWorker = []string{"default:read", "default:write", "default:worker"}
)

Expand Down Expand Up @@ -163,6 +164,27 @@ func (s *defaultClaimMapperSuite) testTokenWithAdminPermissions(alg keyAlgorithm
s.Equal(RoleReader, defaultRole)
}

func (s *defaultClaimMapperSuite) TestNamespacePermissionCaseSensitive() {
tokenString, err := s.tokenGenerator.generateToken(RSA,
testSubject, []string{"Temporal-system:admin", "Foo:read"}, errorTestOptionNoError)
s.NoError(err)
authInfo := &AuthInfo{
AddBearer(tokenString),
nil,
nil,
"",
"",
}
claims, err := s.claimMapper.GetClaims(authInfo)
s.NoError(err)
s.Equal(testSubject, claims.Subject)
s.Equal(RoleUndefined, claims.System) // no system role
s.Equal(2, len(claims.Namespaces))
// claims contain namespace role for 'Foo', not for 'foo'.
s.Equal(RoleReader, claims.Namespaces["Foo"])
s.Equal(RoleAdmin, claims.Namespaces["Temporal-system"])
}

func (s *defaultClaimMapperSuite) TestTokenWithReaderWriterWorkerPermissionsRSA() {
s.testTokenWithReaderWriterWorkerPermissions(RSA)
}
Expand Down

0 comments on commit 7a24d8e

Please sign in to comment.