| 1 | // Copyright 2018 Google Inc. All Rights Reserved. |
| 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 provides a mechanism to send requests with https cert, |
| 16 | // key, and CA. |
| 17 | package transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/ |
| 18 | |
| 19 | import ( |
| 20 | "crypto/tlsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/" |
| 21 | "crypto/x509scip-go gomod github.com/golang/go/src go1.24.0 `crypto/x509`/" |
| 22 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 23 | "net/httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/" |
| 24 | "osscip-go gomod github.com/golang/go/src go1.24.0 os/" |
| 25 | "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/" |
| 26 | |
| 27 | "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/" |
| 28 | ) |
| 29 | |
| 30 | type transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport# struct { |
| 31 | certscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#cert. *string |
| 32 | keyscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#key. *string |
| 33 | cascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#ca. *string |
| 34 | caCertPoolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#caCertPool. *x509scip-go gomod github.com/golang/go/src go1.24.0 `crypto/x509`/.CertPoolscip-go gomod github.com/golang/go/src go1.24.0 `crypto/x509`/CertPool# |
| 35 | certsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#certs. []tlsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/.Certificatescip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/Certificate# |
| 36 | initOncescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initOnce. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Oncescip-go gomod github.com/golang/go/src go1.24.0 sync/Once# |
| 37 | initErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initErr. error |
| 38 | } |
| 39 | |
| 40 | const extraUsagescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/extraUsage. = ` -tls_cert TLS client certificate file for fetching profile and symbols |
| 41 | -tls_key TLS private key file for fetching profile and symbols |
| 42 | -tls_ca TLS CA certs file for fetching profile and symbols` |
| 43 | |
| 44 | // New returns a round tripper for making requests with the |
| 45 | // specified cert, key, and ca. The flags tls_cert, tls_key, and tls_ca are |
| 46 | // added to the flagset to allow a user to specify the cert, key, and ca. If |
| 47 | // the flagset is nil, no flags will be added, and users will not be able to |
| 48 | // use these flags. |
| 49 | func Newscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/New().(flagset pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.FlagSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/FlagSet#) httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.RoundTripperscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/RoundTripper# { |
| 50 | if flagset == nil { |
| 51 | return &transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#{} |
| 52 | } |
| 53 | flagset.AddExtraUsagescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/FlagSet#AddExtraUsage.(extraUsagescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/extraUsage.) |
| 54 | return &transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#{ |
| 55 | certscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#cert.: flagset.Stringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/FlagSet#String.("tls_cert", "", "TLS client certificate file for fetching profile and symbols"), |
| 56 | keyscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#key.: flagset.Stringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/FlagSet#String.("tls_key", "", "TLS private key file for fetching profile and symbols"), |
| 57 | cascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#ca.: flagset.Stringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/FlagSet#String.("tls_ca", "", "TLS CA certs file for fetching profile and symbols"), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // initialize uses the cert, key, and ca to initialize the certs |
| 62 | // to use these when making requests. |
| 63 | func (tr *transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#) initializescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initialize().() error { |
| 64 | var cert, key, ca string |
| 65 | if tr.certscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#cert. != nil { |
| 66 | cert = *tr.certscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#cert. |
| 67 | } |
| 68 | if tr.keyscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#key. != nil { |
| 69 | key = *tr.keyscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#key. |
| 70 | } |
| 71 | if tr.cascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#ca. != nil { |
| 72 | ca = *tr.cascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#ca. |
| 73 | } |
| 74 | |
| 75 | if cert != "" && key != "" { |
| 76 | tlsCert, err := tlsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/.LoadX509KeyPairscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/LoadX509KeyPair().(cert, key) |
| 77 | if err != nil { |
| 78 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("could not load certificate/key pair specified by -tls_cert and -tls_key: %v", err) |
| 79 | } |
| 80 | tr.certsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#certs. = []tlsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/.Certificatescip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/Certificate#{tlsCert} |
| 81 | } else if cert == "" && key != "" { |
| 82 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("-tls_key is specified, so -tls_cert must also be specified") |
| 83 | } else if cert != "" && key == "" { |
| 84 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("-tls_cert is specified, so -tls_key must also be specified") |
| 85 | } |
| 86 | |
| 87 | if ca != "" { |
| 88 | caCertPool := x509scip-go gomod github.com/golang/go/src go1.24.0 `crypto/x509`/.NewCertPoolscip-go gomod github.com/golang/go/src go1.24.0 `crypto/x509`/NewCertPool().() |
| 89 | caCert, err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.24.0 os/ReadFile().(ca) |
| 90 | if err != nil { |
| 91 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("could not load CA specified by -tls_ca: %v", err) |
| 92 | } |
| 93 | caCertPool.AppendCertsFromPEMscip-go gomod github.com/golang/go/src go1.24.0 `crypto/x509`/CertPool#AppendCertsFromPEM().(caCert) |
| 94 | tr.caCertPoolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#caCertPool. = caCertPool |
| 95 | } |
| 96 | |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | // RoundTrip executes a single HTTP transaction, returning |
| 101 | // a Response for the provided Request. |
| 102 | func (tr *transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#) RoundTripscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#RoundTrip().(req *httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Request#) (*httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#, error) { |
| 103 | tr.initOncescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initOnce..Doscip-go gomod github.com/golang/go/src go1.24.0 sync/Once#Do().(func() { |
| 104 | tr.initErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initErr. = tr.initializescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initialize().() |
| 105 | }) |
| 106 | if tr.initErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initErr. != nil { |
| 107 | return nil, tr.initErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#initErr. |
| 108 | } |
| 109 | |
| 110 | tlsConfig := &tlsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/.Configscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/Config#{ |
| 111 | RootCAsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/Config#RootCAs.: tr.caCertPoolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#caCertPool., |
| 112 | Certificatesscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/Config#Certificates.: tr.certsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/transport#certs., |
| 113 | } |
| 114 | |
| 115 | if req.URLscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Request#URL..Schemescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Scheme. == "https+insecure" { |
| 116 | // Make shallow copy of request, and req.URL, so the request's URL can be |
| 117 | // modified. |
| 118 | r := *req |
| 119 | *r.URLscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Request#URL. = *req.URLscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Request#URL. |
| 120 | req = &r |
| 121 | tlsConfig.InsecureSkipVerifyscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/Config#InsecureSkipVerify. = true |
| 122 | req.URLscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Request#URL..Schemescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Scheme. = "https" |
| 123 | } |
| 124 | |
| 125 | transport := httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.Transportscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Transport#{ |
| 126 | Proxyscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Transport#Proxy.: httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.ProxyFromEnvironmentscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/ProxyFromEnvironment()., |
| 127 | TLSClientConfigscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Transport#TLSClientConfig.: tlsConfig, |
| 128 | } |
| 129 | |
| 130 | return transport.RoundTripscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Transport#RoundTrip().(req) |
| 131 | } |