@@ -44,6 +44,7 @@ import (
44
44
"k8s.io/apimachinery/pkg/types"
45
45
"k8s.io/apimachinery/pkg/util/httpstream"
46
46
"k8s.io/apimachinery/pkg/util/httpstream/spdy"
47
+ "k8s.io/apimachinery/pkg/util/sets"
47
48
"k8s.io/apiserver/pkg/authentication/authenticator"
48
49
"k8s.io/apiserver/pkg/authentication/user"
49
50
"k8s.io/apiserver/pkg/authorization/authorizer"
@@ -575,6 +576,72 @@ func TestAuthzCoverage(t *testing.T) {
575
576
}
576
577
}
577
578
579
+ func TestInstallAuthNotRequiredHandlers (t * testing.T ) {
580
+ fw := newServerTestWithDebug (false , nil )
581
+ defer fw .testHTTPServer .Close ()
582
+
583
+ // No new handlers should be added to this list.
584
+ allowedAuthNotRequiredHandlers := sets .NewString (
585
+ "/healthz" ,
586
+ "/healthz/log" ,
587
+ "/healthz/ping" ,
588
+ "/healthz/syncloop" ,
589
+ "/metrics" ,
590
+ "/metrics/slis" ,
591
+ "/metrics/cadvisor" ,
592
+ "/metrics/probes" ,
593
+ "/metrics/resource" ,
594
+ "/pods/" ,
595
+ "/stats/" ,
596
+ "/stats/summary" ,
597
+ )
598
+
599
+ // These handlers are explicitly disabled.
600
+ debuggingDisabledHandlers := sets .NewString (
601
+ "/run/" ,
602
+ "/exec/" ,
603
+ "/attach/" ,
604
+ "/portForward/" ,
605
+ "/containerLogs/" ,
606
+ "/runningpods/" ,
607
+ "/debug/pprof/" ,
608
+ "/logs/" ,
609
+ )
610
+ allowedAuthNotRequiredHandlers .Insert (debuggingDisabledHandlers .UnsortedList ()... )
611
+
612
+ // Test all the non-web-service handlers
613
+ for _ , path := range fw .serverUnderTest .restfulCont .RegisteredHandlePaths () {
614
+ if ! allowedAuthNotRequiredHandlers .Has (path ) {
615
+ t .Errorf ("New handler %q must require auth" , path )
616
+ }
617
+ }
618
+
619
+ // Test all the generated web-service paths
620
+ for _ , ws := range fw .serverUnderTest .restfulCont .RegisteredWebServices () {
621
+ for _ , r := range ws .Routes () {
622
+ if ! allowedAuthNotRequiredHandlers .Has (r .Path ) {
623
+ t .Errorf ("New handler %q must require auth" , r .Path )
624
+ }
625
+ }
626
+ }
627
+
628
+ // Ensure the disabled handlers are in fact disabled.
629
+ for path := range debuggingDisabledHandlers {
630
+ for _ , method := range []string {"GET" , "POST" , "PUT" , "PATCH" , "DELETE" } {
631
+ t .Run (method + ":" + path , func (t * testing.T ) {
632
+ req , err := http .NewRequest (method , fw .testHTTPServer .URL + path , nil )
633
+ require .NoError (t , err )
634
+
635
+ resp , err := http .DefaultClient .Do (req )
636
+ require .NoError (t , err )
637
+ defer resp .Body .Close () //nolint:errcheck
638
+
639
+ assert .Equal (t , http .StatusMethodNotAllowed , resp .StatusCode )
640
+ })
641
+ }
642
+ }
643
+ }
644
+
578
645
func TestAuthFilters (t * testing.T ) {
579
646
// Enable features.ContainerCheckpoint during test
580
647
featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ContainerCheckpoint , true )
0 commit comments