http_client.go 733 B

12345678910111213141516171819202122232425262728293031323334
  1. package authority
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "net/http"
  7. )
  8. // newHTTPClient returns an HTTP client that trusts the system cert pool and the
  9. // given roots.
  10. func newHTTPClient(roots ...*x509.Certificate) (*http.Client, error) {
  11. pool, err := x509.SystemCertPool()
  12. if err != nil {
  13. return nil, fmt.Errorf("error initializing http client: %w", err)
  14. }
  15. for _, crt := range roots {
  16. pool.AddCert(crt)
  17. }
  18. tr, ok := http.DefaultTransport.(*http.Transport)
  19. if !ok {
  20. return nil, fmt.Errorf("error initializing http client: type is not *http.Transport")
  21. }
  22. tr = tr.Clone()
  23. tr.TLSClientConfig = &tls.Config{
  24. MinVersion: tls.VersionTLS12,
  25. RootCAs: pool,
  26. }
  27. return &http.Client{
  28. Transport: tr,
  29. }, nil
  30. }