2016-04-01 00:29:36 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-04-01 00:29:36 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
package ssl
|
2016-04-01 00:29:36 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-05 21:59:10 +00:00
|
|
|
"crypto/x509"
|
2016-04-01 00:29:36 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-06-05 13:36:00 +00:00
|
|
|
|
2017-08-05 21:59:10 +00:00
|
|
|
certutil "k8s.io/client-go/util/cert"
|
|
|
|
"k8s.io/client-go/util/cert/triple"
|
2017-09-17 18:42:31 +00:00
|
|
|
|
2018-01-18 19:14:42 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/file"
|
2016-04-01 00:29:36 +00:00
|
|
|
)
|
|
|
|
|
2017-08-05 21:59:10 +00:00
|
|
|
// generateRSACerts generates a self signed certificate using a self generated ca
|
|
|
|
func generateRSACerts(host string) (*triple.KeyPair, *triple.KeyPair, error) {
|
|
|
|
ca, err := triple.NewCA("self-sign-ca")
|
2016-11-10 22:56:29 +00:00
|
|
|
if err != nil {
|
2017-08-05 21:59:10 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := certutil.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to create a server private key: %v", err)
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 21:59:10 +00:00
|
|
|
config := certutil.Config{
|
|
|
|
CommonName: host,
|
|
|
|
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
|
|
|
|
}
|
|
|
|
cert, err := certutil.NewSignedCert(config, key, ca.Cert, ca.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to sign the server certificate: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &triple.KeyPair{
|
|
|
|
Key: key,
|
|
|
|
Cert: cert,
|
|
|
|
}, ca, nil
|
|
|
|
}
|
2016-04-01 00:29:36 +00:00
|
|
|
|
2017-08-05 21:59:10 +00:00
|
|
|
func TestAddOrUpdateCertAndKey(t *testing.T) {
|
2018-01-18 19:14:42 +00:00
|
|
|
fs := newFS(t)
|
2016-04-01 00:29:36 +00:00
|
|
|
|
2017-08-05 21:59:10 +00:00
|
|
|
cert, _, err := generateRSACerts("echoheaders")
|
2016-04-01 00:29:36 +00:00
|
|
|
if err != nil {
|
2017-08-05 21:59:10 +00:00
|
|
|
t.Fatalf("unexpected error creating SSL certificate: %v", err)
|
2016-04-01 00:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("test-%v", time.Now().UnixNano())
|
2017-08-05 21:59:10 +00:00
|
|
|
|
|
|
|
c := certutil.EncodeCertPEM(cert.Cert)
|
|
|
|
k := certutil.EncodePrivateKeyPEM(cert.Key)
|
|
|
|
|
2018-01-18 19:14:42 +00:00
|
|
|
ngxCert, err := AddOrUpdateCertAndKey(name, c, k, []byte{}, fs)
|
2016-04-01 00:29:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error checking SSL certificate: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:02:29 +00:00
|
|
|
if ngxCert.PemFileName == "" {
|
2016-04-01 00:29:36 +00:00
|
|
|
t.Fatalf("expected path to pem file but returned empty")
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:02:29 +00:00
|
|
|
if len(ngxCert.CN) == 0 {
|
2016-04-01 00:29:36 +00:00
|
|
|
t.Fatalf("expected at least one cname but none returned")
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:02:29 +00:00
|
|
|
if ngxCert.CN[0] != "echoheaders" {
|
|
|
|
t.Fatalf("expected cname echoheaders but %v returned", ngxCert.CN[0])
|
2016-04-01 00:29:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-05 21:59:10 +00:00
|
|
|
|
|
|
|
func TestCACert(t *testing.T) {
|
2018-01-18 19:14:42 +00:00
|
|
|
fs := newFS(t)
|
2017-08-05 21:59:10 +00:00
|
|
|
|
|
|
|
cert, CA, err := generateRSACerts("echoheaders")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating SSL certificate: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("test-%v", time.Now().UnixNano())
|
|
|
|
|
|
|
|
c := certutil.EncodeCertPEM(cert.Cert)
|
|
|
|
k := certutil.EncodePrivateKeyPEM(cert.Key)
|
|
|
|
ca := certutil.EncodeCertPEM(CA.Cert)
|
|
|
|
|
2018-01-18 19:14:42 +00:00
|
|
|
ngxCert, err := AddOrUpdateCertAndKey(name, c, k, ca, fs)
|
2017-08-05 21:59:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error checking SSL certificate: %v", err)
|
|
|
|
}
|
|
|
|
if ngxCert.CAFileName == "" {
|
|
|
|
t.Fatalf("expected a valid CA file name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFakeSSLCert(t *testing.T) {
|
|
|
|
k, c := GetFakeSSLCert()
|
|
|
|
if len(k) == 0 {
|
|
|
|
t.Fatalf("expected a valid key")
|
|
|
|
}
|
|
|
|
if len(c) == 0 {
|
|
|
|
t.Fatalf("expected a valid certificate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddCertAuth(t *testing.T) {
|
2018-01-18 19:14:42 +00:00
|
|
|
fs, err := file.NewFakeFS()
|
2017-08-05 21:59:10 +00:00
|
|
|
if err != nil {
|
2018-01-18 19:14:42 +00:00
|
|
|
t.Fatalf("unexpected error creating filesystem: %v", err)
|
2017-08-05 21:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cn := "demo-ca"
|
|
|
|
_, ca, err := generateRSACerts(cn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating SSL certificate: %v", err)
|
|
|
|
}
|
|
|
|
c := certutil.EncodeCertPEM(ca.Cert)
|
2018-01-18 19:14:42 +00:00
|
|
|
ic, err := AddCertAuth(cn, c, fs)
|
2017-08-05 21:59:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating SSL certificate: %v", err)
|
|
|
|
}
|
|
|
|
if ic.CAFileName == "" {
|
|
|
|
t.Fatalf("expected a valid CA file name")
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 19:14:42 +00:00
|
|
|
|
|
|
|
func newFS(t *testing.T) file.Filesystem {
|
|
|
|
fs, err := file.NewFakeFS()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating filesystem: %v", err)
|
|
|
|
}
|
|
|
|
return fs
|
|
|
|
}
|