-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dex): Allow multiple roles for one user (#1687)
- Currently we are treating the role of a user as one value but in the future user's might be a part of multiple roles. We need to add both of these roles to the context and then check the permissions for both of the roles
- Loading branch information
Showing
7 changed files
with
75 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/freiheit-com/kuberpult/pkg/grpc" | ||
"github.com/freiheit-com/kuberpult/pkg/logger" | ||
|
@@ -99,7 +100,7 @@ func (x *DummyGrpcContextReader) ReadUserFromGrpcContext(ctx context.Context) (* | |
Email: "[email protected]", | ||
Name: "userName", | ||
DexAuthContext: &DexAuthContext{ | ||
Role: x.Role, | ||
Role: []string{x.Role}, | ||
}, | ||
} | ||
return user, nil | ||
|
@@ -148,14 +149,16 @@ func (x *DexGrpcContextReader) ReadUserFromGrpcContext(ctx context.Context) (*Us | |
if len(rolesInHeader) == 0 { | ||
return useDexDefaultRole(ctx, x.DexDefaultRoleEnabled, u) | ||
} | ||
|
||
userRole, err := Decode64(rolesInHeader[0]) | ||
|
||
if err != nil { | ||
return nil, grpc.AuthError(ctx, fmt.Errorf("extract: non-base64 in author-role in grpc context %s", userRole)) | ||
var userRole []string | ||
for _, role := range rolesInHeader { | ||
newRole, err := Decode64(role) | ||
if err != nil { | ||
return nil, grpc.AuthError(ctx, fmt.Errorf("extract: non-base64 in author-role in grpc context %s", userRole)) | ||
} | ||
userRole = append(userRole, strings.Split(newRole, ",")...) | ||
} | ||
|
||
if userRole == "" { | ||
if len(userRole) == 0 { | ||
return useDexDefaultRole(ctx, x.DexDefaultRoleEnabled, u) | ||
} | ||
u.DexAuthContext = &DexAuthContext{ | ||
|
@@ -168,7 +171,7 @@ func (x *DexGrpcContextReader) ReadUserFromGrpcContext(ctx context.Context) (*Us | |
func useDexDefaultRole(ctx context.Context, dexDefaultRoleEnabled bool, u *User) (*User, error) { | ||
if dexDefaultRoleEnabled { | ||
u.DexAuthContext = &DexAuthContext{ | ||
Role: "default", | ||
Role: []string{"default"}, | ||
} | ||
logger.FromContext(ctx).Warn("role undefined but dex is enabled. Default user role enabled. Proceeding with default role.") | ||
return u, nil | ||
|
@@ -201,7 +204,7 @@ func ReadUserFromHttpHeader(ctx context.Context, r *http.Request) (*User, error) | |
Email: headerEmail, | ||
Name: headerName, | ||
DexAuthContext: &DexAuthContext{ | ||
Role: headerRole, | ||
Role: strings.Split(headerRole, ","), | ||
}, | ||
}, nil | ||
} | ||
|
@@ -220,7 +223,7 @@ func WriteUserToHttpHeader(r *http.Request, user User) { | |
// WriteUserRoleToHttpHeader writes the user role into http headers | ||
// it is used for requests like /release and managing locks which are delegated from frontend-service to cd-service | ||
func WriteUserRoleToHttpHeader(r *http.Request, role string) { | ||
r.Header.Set(HeaderUserRole, Encode64(role)) | ||
r.Header.Add(HeaderUserRole, Encode64(role)) | ||
} | ||
|
||
func GetUserOrDefault(u *User, defaultUser User) User { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ func MakeTestContextDexEnabledUser(role string) context.Context { | |
u := auth.User{ | ||
Email: "[email protected]", | ||
Name: "test tester", | ||
DexAuthContext: &auth.DexAuthContext{Role: role}, | ||
DexAuthContext: &auth.DexAuthContext{Role: []string{role}}, | ||
} | ||
ctx := auth.WriteUserToContext(context.Background(), u) | ||
ctx = metadata.NewIncomingContext(ctx, metadata.New(map[string]string{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters