go.etcd.io/etcd@v3.3.27+incompatible/pkg/transport/tls.go (about)

     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  	"fmt"
    19  	"strings"
    20  	"time"
    21  )
    22  
    23  // ValidateSecureEndpoints scans the given endpoints against tls info, returning only those
    24  // endpoints that could be validated as secure.
    25  func ValidateSecureEndpoints(tlsInfo TLSInfo, eps []string) ([]string, error) {
    26  	t, err := NewTransport(tlsInfo, 5*time.Second)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	var errs []string
    31  	var endpoints []string
    32  	for _, ep := range eps {
    33  		if !strings.HasPrefix(ep, "https://") {
    34  			errs = append(errs, fmt.Sprintf("%q is insecure", ep))
    35  			continue
    36  		}
    37  		conn, cerr := t.Dial("tcp", ep[len("https://"):])
    38  		if cerr != nil {
    39  			errs = append(errs, fmt.Sprintf("%q failed to dial (%v)", ep, cerr))
    40  			continue
    41  		}
    42  		conn.Close()
    43  		endpoints = append(endpoints, ep)
    44  	}
    45  	if len(errs) != 0 {
    46  		err = fmt.Errorf("%s", strings.Join(errs, ","))
    47  	}
    48  	return endpoints, err
    49  }