config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. )
  20. // Config holds various options for establishing a transport.
  21. type Config struct {
  22. // UserAgent is an optional field that specifies the caller of this
  23. // request.
  24. UserAgent string
  25. // The base TLS configuration for this transport.
  26. TLS TLSConfig
  27. // Username and password for basic authentication
  28. Username string
  29. Password string
  30. // Bearer token for authentication
  31. BearerToken string
  32. // Impersonate is the config that this Config will impersonate using
  33. Impersonate ImpersonationConfig
  34. // Transport may be used for custom HTTP behavior. This attribute may
  35. // not be specified with the TLS client certificate options. Use
  36. // WrapTransport for most client level operations.
  37. Transport http.RoundTripper
  38. // WrapTransport will be invoked for custom HTTP behavior after the
  39. // underlying transport is initialized (either the transport created
  40. // from TLSClientConfig, Transport, or http.DefaultTransport). The
  41. // config may layer other RoundTrippers on top of the returned
  42. // RoundTripper.
  43. WrapTransport func(rt http.RoundTripper) http.RoundTripper
  44. // Dial specifies the dial function for creating unencrypted TCP connections.
  45. Dial func(ctx context.Context, network, address string) (net.Conn, error)
  46. }
  47. // ImpersonationConfig has all the available impersonation options
  48. type ImpersonationConfig struct {
  49. // UserName matches user.Info.GetName()
  50. UserName string
  51. // Groups matches user.Info.GetGroups()
  52. Groups []string
  53. // Extra matches user.Info.GetExtra()
  54. Extra map[string][]string
  55. }
  56. // HasCA returns whether the configuration has a certificate authority or not.
  57. func (c *Config) HasCA() bool {
  58. return len(c.TLS.CAData) > 0 || len(c.TLS.CAFile) > 0
  59. }
  60. // HasBasicAuth returns whether the configuration has basic authentication or not.
  61. func (c *Config) HasBasicAuth() bool {
  62. return len(c.Username) != 0
  63. }
  64. // HasTokenAuth returns whether the configuration has token authentication or not.
  65. func (c *Config) HasTokenAuth() bool {
  66. return len(c.BearerToken) != 0
  67. }
  68. // HasCertAuth returns whether the configuration has certificate authentication or not.
  69. func (c *Config) HasCertAuth() bool {
  70. return (len(c.TLS.CertData) != 0 || len(c.TLS.CertFile) != 0) && (len(c.TLS.KeyData) != 0 || len(c.TLS.KeyFile) != 0)
  71. }
  72. // HasCertCallbacks returns whether the configuration has certificate callback or not.
  73. func (c *Config) HasCertCallback() bool {
  74. return c.TLS.GetCert != nil
  75. }
  76. // TLSConfig holds the information needed to set up a TLS transport.
  77. type TLSConfig struct {
  78. CAFile string // Path of the PEM-encoded server trusted root certificates.
  79. CertFile string // Path of the PEM-encoded client certificate.
  80. KeyFile string // Path of the PEM-encoded client key.
  81. Insecure bool // Server should be accessed without verifying the certificate. For testing only.
  82. ServerName string // Override for the server name passed to the server for SNI and used to verify certificates.
  83. CAData []byte // Bytes of the PEM-encoded server trusted root certificates. Supercedes CAFile.
  84. CertData []byte // Bytes of the PEM-encoded client certificate. Supercedes CertFile.
  85. KeyData []byte // Bytes of the PEM-encoded client key. Supercedes KeyFile.
  86. GetCert func() (*tls.Certificate, error) // Callback that returns a TLS client certificate. CertData, CertFile, KeyData and KeyFile supercede this field.
  87. }