k8s.io/apiserver@v0.31.1/pkg/util/webhook/client_test.go (about)

     1  /*
     2  Copyright 2024 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package webhook
    18  
    19  import (
    20  	"testing"
    21  
    22  	"golang.org/x/net/http2"
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  )
    25  
    26  func TestWebhookClientConfig(t *testing.T) {
    27  	cm, _ := NewClientManager([]schema.GroupVersion{})
    28  	authInfoResolver, err := NewDefaultAuthenticationInfoResolver("")
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	cm.SetAuthenticationInfoResolver(authInfoResolver)
    33  	cm.SetServiceResolver(NewDefaultServiceResolver())
    34  
    35  	tests := []struct {
    36  		name             string
    37  		url              string
    38  		expectAllowHTTP2 bool
    39  	}{
    40  		{
    41  			name:             "force http1",
    42  			url:              "https://webhook.example.com",
    43  			expectAllowHTTP2: false,
    44  		},
    45  		{
    46  			name:             "allow http2 for localhost",
    47  			url:              "https://localhost",
    48  			expectAllowHTTP2: true,
    49  		},
    50  		{
    51  			name:             "allow http2 for 127.0.0.1",
    52  			url:              "https://127.0.0.1",
    53  			expectAllowHTTP2: true,
    54  		},
    55  		{
    56  			name:             "allow http2 for [::1]:0",
    57  			url:              "https://[::1]",
    58  			expectAllowHTTP2: true,
    59  		},
    60  	}
    61  	for _, tc := range tests {
    62  		t.Run(tc.name, func(t *testing.T) {
    63  
    64  			cc := ClientConfig{
    65  				URL: tc.url,
    66  			}
    67  			cfg, err := cm.hookClientConfig(cc)
    68  			if err != nil {
    69  				t.Fatal(err)
    70  			}
    71  			if tc.expectAllowHTTP2 && !allowHTTP2(cfg.NextProtos) {
    72  				t.Errorf("expected allow http/2, got: %v", cfg.NextProtos)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func allowHTTP2(nextProtos []string) bool {
    79  	if len(nextProtos) == 0 {
    80  		// the transport expressed no NextProto preference, allow
    81  		return true
    82  	}
    83  	for _, p := range nextProtos {
    84  		if p == http2.NextProtoTLS {
    85  			// the transport explicitly allowed http/2
    86  			return true
    87  		}
    88  	}
    89  	// the transport explicitly set NextProtos and excluded http/2
    90  	return false
    91  }