2017-10-13 21:37:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package apimachinery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2018-12-10 00:24:38 +00:00
|
|
|
"k8s.io/client-go/util/cert"
|
|
|
|
"k8s.io/client-go/util/keyutil"
|
2019-01-23 06:31:32 +00:00
|
|
|
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
|
2017-10-13 21:37:37 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certContext struct {
|
|
|
|
cert []byte
|
|
|
|
key []byte
|
|
|
|
signingCert []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the server cert. For example, user apiservers and admission webhooks
|
|
|
|
// can use the cert to prove their identify to the kube-apiserver
|
|
|
|
func setupServerCert(namespaceName, serviceName string) *certContext {
|
|
|
|
certDir, err := ioutil.TempDir("", "test-e2e-server-cert")
|
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create a temp dir for cert generation %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(certDir)
|
2019-01-23 06:31:32 +00:00
|
|
|
signingKey, err := pkiutil.NewPrivateKey()
|
2017-10-13 21:37:37 +00:00
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create CA private key %v", err)
|
|
|
|
}
|
2018-12-10 00:24:38 +00:00
|
|
|
signingCert, err := cert.NewSelfSignedCACert(cert.Config{CommonName: "e2e-server-cert-ca"}, signingKey)
|
2017-10-13 21:37:37 +00:00
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create CA cert for apiserver %v", err)
|
|
|
|
}
|
|
|
|
caCertFile, err := ioutil.TempFile(certDir, "ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create a temp file for ca cert generation %v", err)
|
|
|
|
}
|
2019-01-23 06:31:32 +00:00
|
|
|
if err := ioutil.WriteFile(caCertFile.Name(), pkiutil.EncodeCertPEM(signingCert), 0644); err != nil {
|
2017-10-13 21:37:37 +00:00
|
|
|
framework.Failf("Failed to write CA cert %v", err)
|
|
|
|
}
|
2019-01-23 06:31:32 +00:00
|
|
|
key, err := pkiutil.NewPrivateKey()
|
2017-10-13 21:37:37 +00:00
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create private key for %v", err)
|
|
|
|
}
|
2019-01-23 06:31:32 +00:00
|
|
|
signedCert, err := pkiutil.NewSignedCert(
|
2018-12-10 00:24:38 +00:00
|
|
|
&cert.Config{
|
2017-10-13 21:37:37 +00:00
|
|
|
CommonName: serviceName + "." + namespaceName + ".svc",
|
|
|
|
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
},
|
|
|
|
key, signingCert, signingKey,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create cert%v", err)
|
|
|
|
}
|
|
|
|
certFile, err := ioutil.TempFile(certDir, "server.crt")
|
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create a temp file for cert generation %v", err)
|
|
|
|
}
|
|
|
|
keyFile, err := ioutil.TempFile(certDir, "server.key")
|
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to create a temp file for key generation %v", err)
|
|
|
|
}
|
2019-01-23 06:31:32 +00:00
|
|
|
if err = ioutil.WriteFile(certFile.Name(), pkiutil.EncodeCertPEM(signedCert), 0600); err != nil {
|
2017-10-13 21:37:37 +00:00
|
|
|
framework.Failf("Failed to write cert file %v", err)
|
|
|
|
}
|
2018-12-10 00:24:38 +00:00
|
|
|
privateKeyPEM, err := keyutil.MarshalPrivateKeyToPEM(key)
|
|
|
|
if err != nil {
|
|
|
|
framework.Failf("Failed to marshal key %v", err)
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(keyFile.Name(), privateKeyPEM, 0644); err != nil {
|
2017-10-13 21:37:37 +00:00
|
|
|
framework.Failf("Failed to write key file %v", err)
|
|
|
|
}
|
|
|
|
return &certContext{
|
2019-01-23 06:31:32 +00:00
|
|
|
cert: pkiutil.EncodeCertPEM(signedCert),
|
2018-12-10 00:24:38 +00:00
|
|
|
key: privateKeyPEM,
|
2019-01-23 06:31:32 +00:00
|
|
|
signingCert: pkiutil.EncodeCertPEM(signingCert),
|
2017-10-13 21:37:37 +00:00
|
|
|
}
|
|
|
|
}
|