Skip to content

Commit

Permalink
added webhook check for invalid DiscoveredCluster displayName (#292)
Browse files Browse the repository at this point in the history
Signed-off-by: Disaiah Bennett <[email protected]>
  • Loading branch information
dislbenn authored Jul 15, 2024
1 parent c13a3f0 commit 36ff291
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions api/v1/discovery_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1

import (
"fmt"
"regexp"

admissionregistration "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -116,6 +117,15 @@ func (r *DiscoveredCluster) ValidateCreate() (admission.Warnings, error) {
return nil, err
}

if !IsStringValid(r.Spec.DisplayName) && r.Spec.ImportAsManagedCluster {
err := fmt.Errorf(
"cannot update DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters with an invalid display name '%s'. "+
"Display name must consist of lowercase alphanumeric characters, '-' or '.'", r.Name, r.Spec.DisplayName)

discoveredclusterLog.Error(err, "validation failed")
return nil, err
}

return nil, nil
}

Expand All @@ -134,6 +144,15 @@ func (r *DiscoveredCluster) ValidateUpdate(old runtime.Object) (admission.Warnin
return nil, err
}

if !IsStringValid(r.Spec.DisplayName) && r.Spec.ImportAsManagedCluster {
err := fmt.Errorf(
"cannot update DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters with an invalid display name '%s'. "+
"Display name must consist of lowercase alphanumeric characters, '-' or '.'", r.Name, r.Spec.DisplayName)

discoveredclusterLog.Error(err, "validation failed")
return nil, err
}

return nil, nil
}

Expand All @@ -152,3 +171,9 @@ func IsSupportedClusterType(clusterType string) bool {

return supportedTypes[clusterType]
}

// IsStringValid returns true if the string conforms to the RFC 1123 standards.
func IsStringValid(s string) bool {
re := regexp.MustCompile(`^[a-zA-Z0-9.-]+$`)
return re.MatchString(s)
}

0 comments on commit 36ff291

Please sign in to comment.