Skip to content

Commit e12cbba

Browse files
committed
Fix error handling in admission controller validation.
Remove inappropriate fallback logic that silently downgraded to weaker validation when Kubernetes API calls failed. Now properly returns errors when client creation or cluster listing fails, ensuring fail-secure behavior. Also removed unused validateBasicProviderServiceAccountPattern function to keep code clean.
1 parent 427897e commit e12cbba

1 file changed

Lines changed: 2 additions & 16 deletions

File tree

pkg/syncer/admissionhandler/validate_cnsfileaccessconfig.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,13 @@ func getExpectedProviderServiceAccountName(ctx context.Context) (string, error)
360360
func validateAgainstAllClusters(ctx context.Context, serviceAccountName string) (bool, error) {
361361
k8sClient, err := getClusterAPIClient(ctx)
362362
if err != nil {
363-
// Fall back to basic validation if we can't get client
364-
return validateBasicProviderServiceAccountPattern(serviceAccountName), nil
363+
return false, fmt.Errorf("failed to create cluster API client: %w", err)
365364
}
366365

367366
// Get all CAPI clusters
368367
clusterList := &ccV1beta2.ClusterList{}
369368
if err := k8sClient.List(ctx, clusterList); err != nil {
370-
// Fall back to basic validation if we can't list clusters
371-
return validateBasicProviderServiceAccountPattern(serviceAccountName), nil
369+
return false, fmt.Errorf("failed to list clusters: %w", err)
372370
}
373371

374372
// Check if the service account name matches any cluster's expected ProviderServiceAccount name
@@ -383,15 +381,3 @@ func validateAgainstAllClusters(ctx context.Context, serviceAccountName string)
383381
// Service account name doesn't match any existing cluster
384382
return false, nil
385383
}
386-
387-
// validateBasicProviderServiceAccountPattern provides fallback validation when cluster API is not available
388-
func validateBasicProviderServiceAccountPattern(serviceAccountName string) bool {
389-
// Basic validation: must end with -pvcsi, not be empty, and not contain colons (format confusion attack prevention)
390-
if !strings.HasSuffix(serviceAccountName, "-pvcsi") {
391-
return false
392-
}
393-
394-
clusterName := strings.TrimSuffix(serviceAccountName, "-pvcsi")
395-
// Cluster name should not be empty and not contain colons
396-
return len(clusterName) > 0 && !strings.Contains(clusterName, ":")
397-
}

0 commit comments

Comments
 (0)