-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
906dbb2
commit 8321d01
Showing
5 changed files
with
157 additions
and
153 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
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 |
---|---|---|
@@ -1,58 +1,17 @@ | ||
package validation | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
authenticationv1 "k8s.io/api/authentication/v1" | ||
"k8s.io/klog/v2" | ||
"k8s.io/utils/strings/slices" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1" | ||
) | ||
|
||
const ( | ||
authenticatedGroup = "system:authenticated" | ||
mastersGroup = "system:masters" | ||
serviceAccountGroup = "system:serviceaccounts" | ||
bootstrapGroup = "system:bootstrappers" | ||
|
||
serviceAccountUser = "system:serviceaccount" | ||
mastersGroup = "system:masters" | ||
) | ||
|
||
// TODO: Get valid user names as flag and check to validate those user names. | ||
// TODO:(Arvindthiru) Get valid usernames as flag and allow those usernames. | ||
|
||
// ValidateUser checks to see if user is authenticated to make a request to the hub cluster's api-server. | ||
func ValidateUser(ctx context.Context, client client.Client, userInfo authenticationv1.UserInfo) error { | ||
// special case where users belong to the masters group. | ||
if slices.Contains(userInfo.Groups, mastersGroup) { | ||
return nil | ||
} | ||
if slices.Contains(userInfo.Groups, bootstrapGroup) && slices.Contains(userInfo.Groups, authenticatedGroup) { | ||
return nil | ||
} | ||
// this ensures all internal service accounts are validated. | ||
if slices.Contains(userInfo.Groups, serviceAccountGroup) && slices.Contains(userInfo.Groups, authenticatedGroup) { | ||
match := regexp.MustCompile(serviceAccountUser).FindStringSubmatch(userInfo.Username)[1] | ||
if match != "" { | ||
return nil | ||
} | ||
} | ||
// list all the member clusters | ||
var memberClusterList fleetv1alpha1.MemberClusterList | ||
if err := client.List(ctx, &memberClusterList); err != nil { | ||
klog.V(2).ErrorS(err, "failed to list member clusters") | ||
return err | ||
} | ||
identities := make([]string, len(memberClusterList.Items)) | ||
for i, memberCluster := range memberClusterList.Items { | ||
identities[i] = memberCluster.Spec.Identity.Name | ||
} | ||
// this ensures will allow all member agents are validated. | ||
if slices.Contains(identities, userInfo.Username) && slices.Contains(userInfo.Groups, authenticatedGroup) { | ||
return nil | ||
} | ||
return fmt.Errorf("failed to validate user %s in groups %v", userInfo.Username, userInfo.Groups) | ||
// ValidateUserForCRD checks to see if user is authenticated to make a request to modify fleet CRDs. | ||
func ValidateUserForCRD(userInfo authenticationv1.UserInfo) bool { | ||
return slices.Contains(userInfo.Groups, mastersGroup) | ||
} |
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
Oops, something went wrong.