Skip to content

Commit 37cd179

Browse files
committed
present request header cert CA
1 parent 7c187ca commit 37cd179

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎pkg/genericapiserver/config.go‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package genericapiserver
1818

1919
import (
20+
"crypto/x509"
2021
"fmt"
2122
"io"
2223
"net"
@@ -165,7 +166,7 @@ type SecureServingInfo struct {
165166
// SNICerts are named CertKeys for serving secure traffic with SNI support.
166167
SNICerts []NamedCertKey
167168
// ClientCA is the certificate bundle for all the signers that you'll recognize for incoming client certificates
168-
ClientCA string
169+
ClientCA *x509.CertPool
169170
}
170171

171172
type CertKey struct {
@@ -254,14 +255,42 @@ func (c *Config) ApplyOptions(options *options.ServerRunOptions) *Config {
254255
},
255256
},
256257
SNICerts: []NamedCertKey{},
257-
ClientCA: options.ClientCAFile,
258258
}
259259
if options.TLSCertFile == "" && options.TLSPrivateKeyFile == "" {
260260
secureServingInfo.ServerCert.Generate = true
261261
secureServingInfo.ServerCert.CertFile = path.Join(options.CertDirectory, "apiserver.crt")
262262
secureServingInfo.ServerCert.KeyFile = path.Join(options.CertDirectory, "apiserver.key")
263263
}
264264

265+
if len(options.ClientCAFile) > 0 {
266+
clientCAs, err := certutil.CertsFromFile(options.ClientCAFile)
267+
if err != nil {
268+
// normally this would be no-no, but its the minimal change to backport to 1.5 and
269+
// every caller is going to do this.
270+
panic(fmt.Errorf("unable to load client CA file: %v", err))
271+
}
272+
if secureServingInfo.ClientCA == nil {
273+
secureServingInfo.ClientCA = x509.NewCertPool()
274+
}
275+
for _, cert := range clientCAs {
276+
secureServingInfo.ClientCA.AddCert(cert)
277+
}
278+
}
279+
if len(options.RequestHeaderClientCAFile) > 0 {
280+
clientCAs, err := certutil.CertsFromFile(options.RequestHeaderClientCAFile)
281+
if err != nil {
282+
// normally this would be no-no, but its the minimal change to backport to 1.5 and
283+
// every caller is going to do this.
284+
panic(fmt.Errorf("unable to load requestheader client CA file: %v", err))
285+
}
286+
if secureServingInfo.ClientCA == nil {
287+
secureServingInfo.ClientCA = x509.NewCertPool()
288+
}
289+
for _, cert := range clientCAs {
290+
secureServingInfo.ClientCA.AddCert(cert)
291+
}
292+
}
293+
265294
secureServingInfo.SNICerts = nil
266295
for _, nkc := range options.SNICertKeys {
267296
secureServingInfo.SNICerts = append(secureServingInfo.SNICerts, NamedCertKey{

‎pkg/genericapiserver/serve.go‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"sync"
2727
"time"
2828

29-
certutil "k8s.io/kubernetes/pkg/util/cert"
3029
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
3130
"k8s.io/kubernetes/pkg/util/validation"
3231

@@ -78,16 +77,12 @@ func (s *GenericAPIServer) serveSecurely(stopCh <-chan struct{}) error {
7877
secureServer.TLSConfig.Certificates = append(secureServer.TLSConfig.Certificates, *c)
7978
}
8079

81-
if len(s.SecureServingInfo.ClientCA) > 0 {
82-
clientCAs, err := certutil.NewPool(s.SecureServingInfo.ClientCA)
83-
if err != nil {
84-
return fmt.Errorf("unable to load client CA file: %v", err)
85-
}
80+
if s.SecureServingInfo.ClientCA != nil {
8681
// Populate PeerCertificates in requests, but don't reject connections without certificates
8782
// This allows certificates to be validated by authenticators, while still allowing other auth types
8883
secureServer.TLSConfig.ClientAuth = tls.RequestClientCert
8984
// Specify allowed CAs for client certificates
90-
secureServer.TLSConfig.ClientCAs = clientCAs
85+
secureServer.TLSConfig.ClientCAs = s.SecureServingInfo.ClientCA
9186
}
9287

9388
glog.Infof("Serving securely on %s", s.SecureServingInfo.BindAddress)

‎pkg/genericapiserver/validation/universal_validation.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/golang/glog"
2323
"k8s.io/kubernetes/pkg/genericapiserver/options"
24+
certutil "k8s.io/kubernetes/pkg/util/cert"
2425
utilerrors "k8s.io/kubernetes/pkg/util/errors"
2526
)
2627

@@ -69,6 +70,22 @@ func verifySecureAndInsecurePort(options *options.ServerRunOptions) []error {
6970
return errors
7071
}
7172

73+
func verifyClientCAs(options *options.ServerRunOptions) []error {
74+
errors := []error{}
75+
if len(options.ClientCAFile) > 0 {
76+
if _, err := certutil.CertsFromFile(options.ClientCAFile); err != nil {
77+
errors = append(errors, fmt.Errorf("unable to load client CA file: %v", err))
78+
}
79+
}
80+
if len(options.RequestHeaderClientCAFile) > 0 {
81+
if _, err := certutil.CertsFromFile(options.RequestHeaderClientCAFile); err != nil {
82+
errors = append(errors, fmt.Errorf("unable to load requestheader client CA file: %v", err))
83+
}
84+
}
85+
86+
return errors
87+
}
88+
7289
func ValidateRunOptions(options *options.ServerRunOptions) {
7390
errors := []error{}
7491
if errs := verifyClusterIPFlags(options); len(errs) > 0 {
@@ -80,6 +97,9 @@ func ValidateRunOptions(options *options.ServerRunOptions) {
8097
if errs := verifySecureAndInsecurePort(options); len(errs) > 0 {
8198
errors = append(errors, errs...)
8299
}
100+
if errs := verifyClientCAs(options); len(errs) > 0 {
101+
errors = append(errors, errs...)
102+
}
83103
if err := utilerrors.NewAggregate(errors); err != nil {
84104
glog.Fatalf("Validate server run options failed: %v", err)
85105
}

‎pkg/util/cert/io.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func WriteKey(keyPath string, data []byte) error {
7979
// NewPool returns an x509.CertPool containing the certificates in the given PEM-encoded file.
8080
// Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates
8181
func NewPool(filename string) (*x509.CertPool, error) {
82-
certs, err := certsFromFile(filename)
82+
certs, err := CertsFromFile(filename)
8383
if err != nil {
8484
return nil, err
8585
}
@@ -90,9 +90,9 @@ func NewPool(filename string) (*x509.CertPool, error) {
9090
return pool, nil
9191
}
9292

93-
// certsFromFile returns the x509.Certificates contained in the given PEM-encoded file.
93+
// CertsFromFile returns the x509.Certificates contained in the given PEM-encoded file.
9494
// Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates
95-
func certsFromFile(file string) ([]*x509.Certificate, error) {
95+
func CertsFromFile(file string) ([]*x509.Certificate, error) {
9696
if len(file) == 0 {
9797
return nil, errors.New("error reading certificates from an empty filename")
9898
}

0 commit comments

Comments
 (0)