-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add entitlements assignment at the time of project creation (#4963)
* add: automatic-entitlements-assignment * fix: lint order imports * update: refactoring + updated query * update: revert and update user_test * add: test; refactoring * update: refactoring * update: var name * update: slight refactoring
- Loading branch information
1 parent
2f26b0e
commit 6bd6f55
Showing
11 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -83,6 +83,8 @@ func TestCreateUser_gRPC(t *testing.T) { | |
store.EXPECT(). | ||
CreateUser(gomock.Any(), gomock.Any()). | ||
Return(returnedUser, nil) | ||
store.EXPECT().CreateEntitlements(gomock.Any(), gomock.Any()). | ||
Return(nil) | ||
store.EXPECT().Commit(gomock.Any()) | ||
store.EXPECT().Rollback(gomock.Any()) | ||
tokenResult, _ := openid.NewBuilder().GivenName("Foo").FamilyName("Bar").Email("[email protected]").Subject("subject1").Build() | ||
|
@@ -262,6 +264,7 @@ func TestCreateUser_gRPC(t *testing.T) { | |
authz, | ||
marketplaces.NewNoopMarketplace(), | ||
&serverconfig.DefaultProfilesConfig{}, | ||
&serverconfig.FeaturesConfig{}, | ||
), | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/mindersec/minder/internal/auth/jwt" | ||
) | ||
|
||
// FeaturesConfig is the configuration for the features | ||
type FeaturesConfig struct { | ||
// MembershipFeatureMapping maps a membership to a feature | ||
MembershipFeatureMapping map[string]string `mapstructure:"membership_feature_mapping"` | ||
} | ||
|
||
// GetFeaturesForMemberships returns the features associated with the memberships in the context | ||
func (fc *FeaturesConfig) GetFeaturesForMemberships(ctx context.Context) []string { | ||
memberships := extractMembershipsFromContext(ctx) | ||
|
||
features := make([]string, 0, len(memberships)) | ||
for _, m := range memberships { | ||
if feature := fc.MembershipFeatureMapping[m]; feature != "" { | ||
features = append(features, feature) | ||
} | ||
} | ||
|
||
return features | ||
} | ||
|
||
// extractMembershipsFromContext extracts memberships from the JWT in the context. | ||
// Returns empty slice if no memberships are found. | ||
func extractMembershipsFromContext(ctx context.Context) []string { | ||
realmAccess, ok := jwt.GetUserClaimFromContext[map[string]any](ctx, "realm_access") | ||
if !ok { | ||
return nil | ||
} | ||
|
||
rawMemberships, ok := realmAccess["roles"].([]any) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
memberships := make([]string, 0, len(rawMemberships)) | ||
for _, membership := range rawMemberships { | ||
if membershipStr, ok := membership.(string); ok { | ||
memberships = append(memberships, membershipStr) | ||
} | ||
} | ||
|
||
return memberships | ||
} |