Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACM-12690] Added webhook check for invalid DiscoveredCluster displayName #292

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading