util.go 743 B

1234567891011121314151617181920212223242526272829303132333435
  1. package flocker
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "io/ioutil"
  6. "net/http"
  7. )
  8. // newTLSClient returns a new TLS http client
  9. func newTLSClient(caCertPath, keyPath, certPath string) (*http.Client, error) {
  10. // Client certificate
  11. cert, err := tls.LoadX509KeyPair(certPath, keyPath)
  12. if err != nil {
  13. return nil, err
  14. }
  15. // CA certificate
  16. caCert, err := ioutil.ReadFile(caCertPath)
  17. if err != nil {
  18. return nil, err
  19. }
  20. caCertPool := x509.NewCertPool()
  21. caCertPool.AppendCertsFromPEM(caCert)
  22. tlsConfig := &tls.Config{
  23. Certificates: []tls.Certificate{cert},
  24. RootCAs: caCertPool,
  25. }
  26. tlsConfig.BuildNameToCertificate()
  27. transport := &http.Transport{TLSClientConfig: tlsConfig}
  28. return &http.Client{Transport: transport}, nil
  29. }