@@ -526,6 +526,166 @@ func TestWebhookCreateIPv6Details(t *testing.T) {
526526 }
527527}
528528
529+ func TestWebhookValidateAccessEntries (t * testing.T ) {
530+ tests := []struct {
531+ name string
532+ accessConfig * AccessConfig
533+ expectError bool
534+ errorSubstr string
535+ }{
536+ {
537+ name : "valid access entries with API auth mode" ,
538+ accessConfig : & AccessConfig {
539+ AuthenticationMode : EKSAuthenticationModeAPI ,
540+ AccessEntries : []AccessEntry {
541+ {
542+ PrincipalARN : "arn:aws:iam::123456789012:role/EKSAdmin" ,
543+ Type : "STANDARD" ,
544+ KubernetesGroups : []string {"system:masters" },
545+ },
546+ },
547+ },
548+ expectError : false ,
549+ },
550+ {
551+ name : "valid access entries with API_AND_CONFIG_MAP auth mode" ,
552+ accessConfig : & AccessConfig {
553+ AuthenticationMode : EKSAuthenticationModeAPIAndConfigMap ,
554+ AccessEntries : []AccessEntry {
555+ {
556+ PrincipalARN : "arn:aws:iam::123456789012:role/EKSAdmin" ,
557+ Type : "STANDARD" ,
558+ KubernetesGroups : []string {"system:masters" },
559+ },
560+ },
561+ },
562+ expectError : false ,
563+ },
564+ {
565+ name : "invalid access entries with CONFIG_MAP auth mode" ,
566+ accessConfig : & AccessConfig {
567+ AuthenticationMode : EKSAuthenticationModeConfigMap ,
568+ AccessEntries : []AccessEntry {
569+ {
570+ PrincipalARN : "arn:aws:iam::123456789012:role/EKSAdmin" ,
571+ Type : "STANDARD" ,
572+ KubernetesGroups : []string {"system:masters" },
573+ },
574+ },
575+ },
576+ expectError : true ,
577+ errorSubstr : "accessEntries can only be used when authenticationMode is set to API or API_AND_CONFIG_MAP" ,
578+ },
579+ {
580+ name : "invalid EC2_LINUX access entry with kubernetes groups" ,
581+ accessConfig : & AccessConfig {
582+ AuthenticationMode : EKSAuthenticationModeAPI ,
583+ AccessEntries : []AccessEntry {
584+ {
585+ PrincipalARN : "arn:aws:iam::123456789012:role/EKSAdmin" ,
586+ Type : "EC2_LINUX" ,
587+ KubernetesGroups : []string {"system:masters" },
588+ },
589+ },
590+ },
591+ expectError : true ,
592+ errorSubstr : "kubernetesGroups cannot be specified when type is EC2_LINUX or EC2_WINDOWS" ,
593+ },
594+ {
595+ name : "invalid EC2_WINDOWS access entry with access policies" ,
596+ accessConfig : & AccessConfig {
597+ AuthenticationMode : EKSAuthenticationModeAPI ,
598+ AccessEntries : []AccessEntry {
599+ {
600+ PrincipalARN : "arn:aws:iam::123456789012:role/EKSAdmin" ,
601+ Type : "EC2_WINDOWS" ,
602+ AccessPolicies : []AccessPolicyReference {
603+ {
604+ PolicyARN : "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy" ,
605+ AccessScope : AccessScope {
606+ Type : "cluster" ,
607+ },
608+ },
609+ },
610+ },
611+ },
612+ },
613+ expectError : true ,
614+ errorSubstr : "accessPolicies cannot be specified when type is EC2_LINUX or EC2_WINDOWS" ,
615+ },
616+ {
617+ name : "invalid access policy with namespace type and no namespaces" ,
618+ accessConfig : & AccessConfig {
619+ AuthenticationMode : EKSAuthenticationModeAPI ,
620+ AccessEntries : []AccessEntry {
621+ {
622+ PrincipalARN : "arn:aws:iam::123456789012:role/EKSAdmin" ,
623+ Type : "STANDARD" ,
624+ AccessPolicies : []AccessPolicyReference {
625+ {
626+ PolicyARN : "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy" ,
627+ AccessScope : AccessScope {
628+ Type : "namespace" ,
629+ },
630+ },
631+ },
632+ },
633+ },
634+ },
635+ expectError : true ,
636+ errorSubstr : "at least one value must be provided when accessScope type is namespace" ,
637+ },
638+ {
639+ name : "valid access policy with namespace type and namespaces" ,
640+ accessConfig : & AccessConfig {
641+ AuthenticationMode : EKSAuthenticationModeAPI ,
642+ AccessEntries : []AccessEntry {
643+ {
644+ PrincipalARN : "arn:aws:iam::123456789012:role/EKSAdmin" ,
645+ Type : "STANDARD" ,
646+ AccessPolicies : []AccessPolicyReference {
647+ {
648+ PolicyARN : "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy" ,
649+ AccessScope : AccessScope {
650+ Type : "namespace" ,
651+ Namespaces : []string {"default" , "kube-system" },
652+ },
653+ },
654+ },
655+ },
656+ },
657+ },
658+ expectError : false ,
659+ },
660+ }
661+
662+ for _ , tc := range tests {
663+ t .Run (tc .name , func (t * testing.T ) {
664+ g := NewWithT (t )
665+
666+ mcp := & AWSManagedControlPlane {
667+ Spec : AWSManagedControlPlaneSpec {
668+ EKSClusterName : "default_cluster1" ,
669+ AccessConfig : tc .accessConfig ,
670+ },
671+ }
672+
673+ warn , err := (& awsManagedControlPlaneWebhook {}).ValidateCreate (context .Background (), mcp )
674+
675+ if tc .expectError {
676+ g .Expect (err ).ToNot (BeNil ())
677+ if tc .errorSubstr != "" {
678+ g .Expect (err .Error ()).To (ContainSubstring (tc .errorSubstr ))
679+ }
680+ } else {
681+ g .Expect (err ).To (BeNil ())
682+ }
683+ // Nothing emits warnings yet
684+ g .Expect (warn ).To (BeEmpty ())
685+ })
686+ }
687+ }
688+
529689func TestWebhookUpdate (t * testing.T ) {
530690 tests := []struct {
531691 name string
0 commit comments