http_config.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2016 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package config
  14. import (
  15. "crypto/tls"
  16. "crypto/x509"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "net/url"
  21. "strings"
  22. yaml "gopkg.in/yaml.v2"
  23. )
  24. // BasicAuth contains basic HTTP authentication credentials.
  25. type BasicAuth struct {
  26. Username string `yaml:"username"`
  27. Password Secret `yaml:"password"`
  28. // Catches all undefined fields and must be empty after parsing.
  29. XXX map[string]interface{} `yaml:",inline"`
  30. }
  31. // URL is a custom URL type that allows validation at configuration load time.
  32. type URL struct {
  33. *url.URL
  34. }
  35. // UnmarshalYAML implements the yaml.Unmarshaler interface for URLs.
  36. func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
  37. var s string
  38. if err := unmarshal(&s); err != nil {
  39. return err
  40. }
  41. urlp, err := url.Parse(s)
  42. if err != nil {
  43. return err
  44. }
  45. u.URL = urlp
  46. return nil
  47. }
  48. // MarshalYAML implements the yaml.Marshaler interface for URLs.
  49. func (u URL) MarshalYAML() (interface{}, error) {
  50. if u.URL != nil {
  51. return u.String(), nil
  52. }
  53. return nil, nil
  54. }
  55. // HTTPClientConfig configures an HTTP client.
  56. type HTTPClientConfig struct {
  57. // The HTTP basic authentication credentials for the targets.
  58. BasicAuth *BasicAuth `yaml:"basic_auth,omitempty"`
  59. // The bearer token for the targets.
  60. BearerToken Secret `yaml:"bearer_token,omitempty"`
  61. // The bearer token file for the targets.
  62. BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
  63. // HTTP proxy server to use to connect to the targets.
  64. ProxyURL URL `yaml:"proxy_url,omitempty"`
  65. // TLSConfig to use to connect to the targets.
  66. TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
  67. // Catches all undefined fields and must be empty after parsing.
  68. XXX map[string]interface{} `yaml:",inline"`
  69. }
  70. func (c *HTTPClientConfig) validate() error {
  71. if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
  72. return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured")
  73. }
  74. if c.BasicAuth != nil && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) {
  75. return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured")
  76. }
  77. return nil
  78. }
  79. // UnmarshalYAML implements the yaml.Unmarshaler interface
  80. func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  81. type plain HTTPClientConfig
  82. err := unmarshal((*plain)(c))
  83. if err != nil {
  84. return err
  85. }
  86. err = c.validate()
  87. if err != nil {
  88. return c.validate()
  89. }
  90. return checkOverflow(c.XXX, "http_client_config")
  91. }
  92. // UnmarshalYAML implements the yaml.Unmarshaler interface.
  93. func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error {
  94. type plain BasicAuth
  95. err := unmarshal((*plain)(a))
  96. if err != nil {
  97. return err
  98. }
  99. return checkOverflow(a.XXX, "basic_auth")
  100. }
  101. // NewHTTPClientFromConfig returns a new HTTP client configured for the
  102. // given config.HTTPClientConfig.
  103. func NewHTTPClientFromConfig(cfg *HTTPClientConfig) (*http.Client, error) {
  104. tlsConfig, err := NewTLSConfig(&cfg.TLSConfig)
  105. if err != nil {
  106. return nil, err
  107. }
  108. // It's the caller's job to handle timeouts
  109. var rt http.RoundTripper = &http.Transport{
  110. Proxy: http.ProxyURL(cfg.ProxyURL.URL),
  111. DisableKeepAlives: true,
  112. TLSClientConfig: tlsConfig,
  113. }
  114. // If a bearer token is provided, create a round tripper that will set the
  115. // Authorization header correctly on each request.
  116. bearerToken := cfg.BearerToken
  117. if len(bearerToken) == 0 && len(cfg.BearerTokenFile) > 0 {
  118. b, err := ioutil.ReadFile(cfg.BearerTokenFile)
  119. if err != nil {
  120. return nil, fmt.Errorf("unable to read bearer token file %s: %s", cfg.BearerTokenFile, err)
  121. }
  122. bearerToken = Secret(strings.TrimSpace(string(b)))
  123. }
  124. if len(bearerToken) > 0 {
  125. rt = NewBearerAuthRoundTripper(bearerToken, rt)
  126. }
  127. if cfg.BasicAuth != nil {
  128. rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, Secret(cfg.BasicAuth.Password), rt)
  129. }
  130. // Return a new client with the configured round tripper.
  131. return &http.Client{Transport: rt}, nil
  132. }
  133. type bearerAuthRoundTripper struct {
  134. bearerToken Secret
  135. rt http.RoundTripper
  136. }
  137. type basicAuthRoundTripper struct {
  138. username string
  139. password Secret
  140. rt http.RoundTripper
  141. }
  142. // NewBasicAuthRoundTripper will apply a BASIC auth authorization header to a request unless it has
  143. // already been set.
  144. func NewBasicAuthRoundTripper(username string, password Secret, rt http.RoundTripper) http.RoundTripper {
  145. return &basicAuthRoundTripper{username, password, rt}
  146. }
  147. func (rt *bearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  148. if len(req.Header.Get("Authorization")) == 0 {
  149. req = cloneRequest(req)
  150. req.Header.Set("Authorization", "Bearer "+string(rt.bearerToken))
  151. }
  152. return rt.rt.RoundTrip(req)
  153. }
  154. // NewBearerAuthRoundTripper adds the provided bearer token to a request unless the authorization
  155. // header has already been set.
  156. func NewBearerAuthRoundTripper(bearer Secret, rt http.RoundTripper) http.RoundTripper {
  157. return &bearerAuthRoundTripper{bearer, rt}
  158. }
  159. func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  160. if len(req.Header.Get("Authorization")) != 0 {
  161. return rt.RoundTrip(req)
  162. }
  163. req = cloneRequest(req)
  164. req.SetBasicAuth(rt.username, string(rt.password))
  165. return rt.rt.RoundTrip(req)
  166. }
  167. // cloneRequest returns a clone of the provided *http.Request.
  168. // The clone is a shallow copy of the struct and its Header map.
  169. func cloneRequest(r *http.Request) *http.Request {
  170. // Shallow copy of the struct.
  171. r2 := new(http.Request)
  172. *r2 = *r
  173. // Deep copy of the Header.
  174. r2.Header = make(http.Header)
  175. for k, s := range r.Header {
  176. r2.Header[k] = s
  177. }
  178. return r2
  179. }
  180. // NewTLSConfig creates a new tls.Config from the given config.TLSConfig.
  181. func NewTLSConfig(cfg *TLSConfig) (*tls.Config, error) {
  182. tlsConfig := &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify}
  183. // If a CA cert is provided then let's read it in so we can validate the
  184. // scrape target's certificate properly.
  185. if len(cfg.CAFile) > 0 {
  186. caCertPool := x509.NewCertPool()
  187. // Load CA cert.
  188. caCert, err := ioutil.ReadFile(cfg.CAFile)
  189. if err != nil {
  190. return nil, fmt.Errorf("unable to use specified CA cert %s: %s", cfg.CAFile, err)
  191. }
  192. caCertPool.AppendCertsFromPEM(caCert)
  193. tlsConfig.RootCAs = caCertPool
  194. }
  195. if len(cfg.ServerName) > 0 {
  196. tlsConfig.ServerName = cfg.ServerName
  197. }
  198. // If a client cert & key is provided then configure TLS config accordingly.
  199. if len(cfg.CertFile) > 0 && len(cfg.KeyFile) == 0 {
  200. return nil, fmt.Errorf("client cert file %q specified without client key file", cfg.CertFile)
  201. } else if len(cfg.KeyFile) > 0 && len(cfg.CertFile) == 0 {
  202. return nil, fmt.Errorf("client key file %q specified without client cert file", cfg.KeyFile)
  203. } else if len(cfg.CertFile) > 0 && len(cfg.KeyFile) > 0 {
  204. cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
  205. if err != nil {
  206. return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %s", cfg.CertFile, cfg.KeyFile, err)
  207. }
  208. tlsConfig.Certificates = []tls.Certificate{cert}
  209. }
  210. tlsConfig.BuildNameToCertificate()
  211. return tlsConfig, nil
  212. }
  213. // TLSConfig configures the options for TLS connections.
  214. type TLSConfig struct {
  215. // The CA cert to use for the targets.
  216. CAFile string `yaml:"ca_file,omitempty"`
  217. // The client cert file for the targets.
  218. CertFile string `yaml:"cert_file,omitempty"`
  219. // The client key file for the targets.
  220. KeyFile string `yaml:"key_file,omitempty"`
  221. // Used to verify the hostname for the targets.
  222. ServerName string `yaml:"server_name,omitempty"`
  223. // Disable target certificate validation.
  224. InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
  225. // Catches all undefined fields and must be empty after parsing.
  226. XXX map[string]interface{} `yaml:",inline"`
  227. }
  228. // UnmarshalYAML implements the yaml.Unmarshaler interface.
  229. func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  230. type plain TLSConfig
  231. if err := unmarshal((*plain)(c)); err != nil {
  232. return err
  233. }
  234. return checkOverflow(c.XXX, "TLS config")
  235. }
  236. func (c HTTPClientConfig) String() string {
  237. b, err := yaml.Marshal(c)
  238. if err != nil {
  239. return fmt.Sprintf("<error creating http client config string: %s>", err)
  240. }
  241. return string(b)
  242. }