github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/client/alpn_test.go (about)

     1  /*
     2  Copyright 2023 Gravitational, Inc.
     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 client
    18  
    19  import (
    20  	"context"
    21  	"crypto/tls"
    22  	"crypto/x509"
    23  	"testing"
    24  
    25  	"github.com/gravitational/trace"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestALPNDialer_getTLSConfig(t *testing.T) {
    30  	t.Parallel()
    31  	cas := x509.NewCertPool()
    32  
    33  	tests := []struct {
    34  		name          string
    35  		input         ALPNDialerConfig
    36  		wantTLSConfig *tls.Config
    37  		wantError     bool
    38  	}{
    39  		{
    40  			name:      "missing tls config",
    41  			input:     ALPNDialerConfig{},
    42  			wantError: true,
    43  		},
    44  		{
    45  			name: "no update",
    46  			input: ALPNDialerConfig{
    47  				TLSConfig: &tls.Config{
    48  					ServerName: "example.com",
    49  				},
    50  			},
    51  			wantTLSConfig: &tls.Config{
    52  				ServerName: "example.com",
    53  			},
    54  		},
    55  		{
    56  			name: "no update when upgrade required",
    57  			input: ALPNDialerConfig{
    58  				TLSConfig: &tls.Config{
    59  					ServerName: "example.com",
    60  					RootCAs:    cas,
    61  				},
    62  				ALPNConnUpgradeRequired: true,
    63  			},
    64  			wantTLSConfig: &tls.Config{
    65  				ServerName: "example.com",
    66  				RootCAs:    cas,
    67  			},
    68  		},
    69  		{
    70  			name: "name updated",
    71  			input: ALPNDialerConfig{
    72  				TLSConfig: &tls.Config{},
    73  			},
    74  			wantTLSConfig: &tls.Config{
    75  				ServerName: "example.com",
    76  			},
    77  		},
    78  		{
    79  			name: "get cas failed",
    80  			input: ALPNDialerConfig{
    81  				TLSConfig:               &tls.Config{},
    82  				ALPNConnUpgradeRequired: true,
    83  				GetClusterCAs: func(_ context.Context) (*x509.CertPool, error) {
    84  					return nil, trace.AccessDenied("fail it")
    85  				},
    86  			},
    87  			wantError: true,
    88  		},
    89  		{
    90  			name: "cas updated",
    91  			input: ALPNDialerConfig{
    92  				TLSConfig:               &tls.Config{},
    93  				ALPNConnUpgradeRequired: true,
    94  				GetClusterCAs:           ClusterCAsFromCertPool(cas),
    95  			},
    96  			wantTLSConfig: &tls.Config{
    97  				ServerName: "example.com",
    98  				RootCAs:    cas,
    99  			},
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		t.Run(test.name, func(t *testing.T) {
   105  			dialer := NewALPNDialer(test.input).(*ALPNDialer)
   106  			tlsConfig, err := dialer.getTLSConfig(context.Background(), "example.com:443")
   107  			if test.wantError {
   108  				require.Error(t, err)
   109  			} else {
   110  				require.NoError(t, err)
   111  				require.Equal(t, test.wantTLSConfig, tlsConfig)
   112  			}
   113  		})
   114  	}
   115  }