Skip to content

Commit

Permalink
Validators logic fix
Browse files Browse the repository at this point in the history
This PR fixes the logic in the permitted group Validators. If a permitted groups is not found, the webhook will log it but will not raise and error and continue running on the rest of the groups
  • Loading branch information
dvirgilad committed Apr 4, 2024
1 parent 62875ae commit 6eb23fc
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/common/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"

Check failure on line 6 in internal/common/validators.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"net/http"
"os"
"slices"
Expand All @@ -24,7 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

const PERMITTEDGROUPLABEL = "PERMITTED_GROUPS"
const PermittedGroups = "PERMITTED_GROUPS"

// ValidateNamespaceExist validates that a namespace exists.
func ValidateNamespaceExist(ns *objectcontext.ObjectContext) admission.Response {
Expand Down Expand Up @@ -209,16 +210,20 @@ func CheckGroup(ctx context.Context, user, groupName string, k8sClient client.Cl
func ValidatePermittedGroups(ctx context.Context, user string, k8sClient client.Client) (bool, error) {
logger := log.FromContext(ctx)

permittedGroups, found := os.LookupEnv(PERMITTEDGROUPLABEL)
permittedGroups, found := os.LookupEnv(PermittedGroups)
if !found {
logger.Info("no permitted groups found")
} else {
permittedGroupsSlice := strings.Split(permittedGroups, ",")
for _, groupName := range permittedGroupsSlice {
inGroup, err := CheckGroup(ctx, user, groupName, k8sClient)
if err != nil {
logger.Info(fmt.Sprintf("group %s not found", groupName))
return false, nil
if errors.IsNotFound(err) {
logger.Info(fmt.Sprintf("group %s not found", groupName))
} else {
logger.Error(err, "failed checking if user in group")
return false, nil
}
}
if inGroup {
logger.Info(fmt.Sprintf("user %s found in group %s", user, groupName))
Expand Down

0 comments on commit 6eb23fc

Please sign in to comment.