...

Source file src/go.etcd.io/etcd/client/pkg/v3/transport/tls.go

Documentation: go.etcd.io/etcd/client/pkg/v3/transport

     1  // Copyright 2016 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package transport
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  )
    23  
    24  // ValidateSecureEndpoints scans the given endpoints against tls info, returning only those
    25  // endpoints that could be validated as secure.
    26  func ValidateSecureEndpoints(tlsInfo TLSInfo, eps []string) ([]string, error) {
    27  	t, err := NewTransport(tlsInfo, 5*time.Second)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	defer t.CloseIdleConnections()
    32  
    33  	var errs []string
    34  	var endpoints []string
    35  	for _, ep := range eps {
    36  		if !strings.HasPrefix(ep, "https://") {
    37  			errs = append(errs, fmt.Sprintf("%q is insecure", ep))
    38  			continue
    39  		}
    40  		conn, cerr := t.DialContext(context.Background(), "tcp", ep[len("https://"):])
    41  		if cerr != nil {
    42  			errs = append(errs, fmt.Sprintf("%q failed to dial (%v)", ep, cerr))
    43  			continue
    44  		}
    45  		conn.Close()
    46  		endpoints = append(endpoints, ep)
    47  	}
    48  	if len(errs) != 0 {
    49  		err = fmt.Errorf("%s", strings.Join(errs, ","))
    50  	}
    51  	return endpoints, err
    52  }
    53  

View as plain text