Skip to content

Commit

Permalink
Use CAFile even if client certificate is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Jan 12, 2015
1 parent 6f43074 commit 1f8a746
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
11 changes: 10 additions & 1 deletion pkg/client/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ func RESTClientFor(config *Config) (*RESTClient, error) {
// default http.DefaultTransport if no special case behavior is needed.
func TransportFor(config *Config) (http.RoundTripper, error) {
// Set transport level security
if config.Transport != nil && (config.CertFile != "" || config.Insecure) {
if config.Transport != nil && (config.CAFile != "" || config.CertFile != "" || config.Insecure) {
return nil, fmt.Errorf("using a custom transport with TLS certificate options or the insecure flag is not allowed")
}
if config.CAFile != "" && config.Insecure {
return nil, fmt.Errorf("specifying a root certificates file with the insecure flag is not allowed")
}
var transport http.RoundTripper
switch {
case config.Transport != nil:
Expand All @@ -199,6 +202,12 @@ func TransportFor(config *Config) (http.RoundTripper, error) {
return nil, err
}
transport = t
case config.CAFile != "":
t, err := NewTLSTransport(config.CAFile)
if err != nil {
return nil, err
}
transport = t
case config.Insecure:
transport = NewUnsafeTLSTransport()
default:
Expand Down
8 changes: 7 additions & 1 deletion pkg/client/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ type HTTPKubeletClient struct {

func NewKubeletClient(config *KubeletConfig) (KubeletClient, error) {
transport := http.DefaultTransport
if config.CAFile != "" {
if config.CertFile != "" {
t, err := NewClientCertTLSTransport(config.CertFile, config.KeyFile, config.CAFile)
if err != nil {
return nil, err
}
transport = t
} else if config.CAFile != "" {
t, err := NewTLSTransport(config.CAFile)
if err != nil {
return nil, err
}
transport = t
}

c := &http.Client{Transport: transport}
Expand Down
16 changes: 16 additions & 0 deletions pkg/client/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func NewClientCertTLSTransport(certFile, keyFile, caFile string) (*http.Transpor
}, nil
}

func NewTLSTransport(caFile string) (*http.Transport, error) {
data, err := ioutil.ReadFile(caFile)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(data)
return &http.Transport{
TLSClientConfig: &tls.Config{
// Change default from SSLv3 to TLSv1.0 (because of POODLE vulnerability)
MinVersion: tls.VersionTLS10,
RootCAs: certPool,
},
}, nil
}

func NewUnsafeTLSTransport() *http.Transport {
return &http.Transport{
TLSClientConfig: &tls.Config{
Expand Down

0 comments on commit 1f8a746

Please sign in to comment.