config.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transport
  14. import (
  15. "context"
  16. "crypto/tls"
  17. "net"
  18. "net/http"
  19. "net/url"
  20. )
  21. // Config holds various options for establishing a transport.
  22. type Config struct {
  23. // UserAgent is an optional field that specifies the caller of this
  24. // request.
  25. UserAgent string
  26. // The base TLS configuration for this transport.
  27. TLS TLSConfig
  28. // Username and password for basic authentication
  29. Username string
  30. Password string `datapolicy:"password"`
  31. // Bearer token for authentication
  32. BearerToken string `datapolicy:"token"`
  33. // Path to a file containing a BearerToken.
  34. // If set, the contents are periodically read.
  35. // The last successfully read value takes precedence over BearerToken.
  36. BearerTokenFile string
  37. // Impersonate is the config that this Config will impersonate using
  38. Impersonate ImpersonationConfig
  39. // DisableCompression bypasses automatic GZip compression requests to the
  40. // server.
  41. DisableCompression bool
  42. // Transport may be used for custom HTTP behavior. This attribute may
  43. // not be specified with the TLS client certificate options. Use
  44. // WrapTransport for most client level operations.
  45. Transport http.RoundTripper
  46. // WrapTransport will be invoked for custom HTTP behavior after the
  47. // underlying transport is initialized (either the transport created
  48. // from TLSClientConfig, Transport, or http.DefaultTransport). The
  49. // config may layer other RoundTrippers on top of the returned
  50. // RoundTripper.
  51. //
  52. // A future release will change this field to an array. Use config.Wrap()
  53. // instead of setting this value directly.
  54. WrapTransport WrapperFunc
  55. // DialHolder specifies the dial function for creating unencrypted TCP connections.
  56. // This struct indirection is used to make transport configs cacheable.
  57. DialHolder *DialHolder
  58. // Proxy is the proxy func to be used for all requests made by this
  59. // transport. If Proxy is nil, http.ProxyFromEnvironment is used. If Proxy
  60. // returns a nil *URL, no proxy is used.
  61. //
  62. // socks5 proxying does not currently support spdy streaming endpoints.
  63. Proxy func(*http.Request) (*url.URL, error)
  64. }
  65. // DialHolder is used to make the wrapped function comparable so that it can be used as a map key.
  66. type DialHolder struct {
  67. Dial func(ctx context.Context, network, address string) (net.Conn, error)
  68. }
  69. // ImpersonationConfig has all the available impersonation options
  70. type ImpersonationConfig struct {
  71. // UserName matches user.Info.GetName()
  72. UserName string
  73. // UID matches user.Info.GetUID()
  74. UID string
  75. // Groups matches user.Info.GetGroups()
  76. Groups []string
  77. // Extra matches user.Info.GetExtra()
  78. Extra map[string][]string
  79. }
  80. // HasCA returns whether the configuration has a certificate authority or not.
  81. func (c *Config) HasCA() bool {
  82. return len(c.TLS.CAData) > 0 || len(c.TLS.CAFile) > 0
  83. }
  84. // HasBasicAuth returns whether the configuration has basic authentication or not.
  85. func (c *Config) HasBasicAuth() bool {
  86. return len(c.Username) != 0
  87. }
  88. // HasTokenAuth returns whether the configuration has token authentication or not.
  89. func (c *Config) HasTokenAuth() bool {
  90. return len(c.BearerToken) != 0 || len(c.BearerTokenFile) != 0
  91. }
  92. // HasCertAuth returns whether the configuration has certificate authentication or not.
  93. func (c *Config) HasCertAuth() bool {
  94. return (len(c.TLS.CertData) != 0 || len(c.TLS.CertFile) != 0) && (len(c.TLS.KeyData) != 0 || len(c.TLS.KeyFile) != 0)
  95. }
  96. // HasCertCallback returns whether the configuration has certificate callback or not.
  97. func (c *Config) HasCertCallback() bool {
  98. return c.TLS.GetCertHolder != nil
  99. }
  100. // Wrap adds a transport middleware function that will give the caller
  101. // an opportunity to wrap the underlying http.RoundTripper prior to the
  102. // first API call being made. The provided function is invoked after any
  103. // existing transport wrappers are invoked.
  104. func (c *Config) Wrap(fn WrapperFunc) {
  105. c.WrapTransport = Wrappers(c.WrapTransport, fn)
  106. }
  107. // TLSConfig holds the information needed to set up a TLS transport.
  108. type TLSConfig struct {
  109. CAFile string // Path of the PEM-encoded server trusted root certificates.
  110. CertFile string // Path of the PEM-encoded client certificate.
  111. KeyFile string // Path of the PEM-encoded client key.
  112. ReloadTLSFiles bool // Set to indicate that the original config provided files, and that they should be reloaded
  113. Insecure bool // Server should be accessed without verifying the certificate. For testing only.
  114. ServerName string // Override for the server name passed to the server for SNI and used to verify certificates.
  115. CAData []byte // Bytes of the PEM-encoded server trusted root certificates. Supercedes CAFile.
  116. CertData []byte // Bytes of the PEM-encoded client certificate. Supercedes CertFile.
  117. KeyData []byte // Bytes of the PEM-encoded client key. Supercedes KeyFile.
  118. // NextProtos is a list of supported application level protocols, in order of preference.
  119. // Used to populate tls.Config.NextProtos.
  120. // To indicate to the server http/1.1 is preferred over http/2, set to ["http/1.1", "h2"] (though the server is free to ignore that preference).
  121. // To use only http/1.1, set to ["http/1.1"].
  122. NextProtos []string
  123. // Callback that returns a TLS client certificate. CertData, CertFile, KeyData and KeyFile supercede this field.
  124. // This struct indirection is used to make transport configs cacheable.
  125. GetCertHolder *GetCertHolder
  126. }
  127. // GetCertHolder is used to make the wrapped function comparable so that it can be used as a map key.
  128. type GetCertHolder struct {
  129. GetCert func() (*tls.Certificate, error)
  130. }