Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more ssl test cases #1076

Merged
merged 2 commits into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 101 additions & 14 deletions core/pkg/net/ssl/ssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,67 @@ limitations under the License.
package ssl

import (
"encoding/base64"
"crypto/x509"
"fmt"
"io/ioutil"
"testing"
"time"

certutil "k8s.io/client-go/util/cert"
"k8s.io/client-go/util/cert/triple"
"k8s.io/ingress/core/pkg/ingress"
)

func TestAddOrUpdateCertAndKey(t *testing.T) {
td, err := ioutil.TempDir("", "ssl")
const (
rsaBits = 2048
validFor = 365 * 24 * time.Hour
)

// 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")
if err != nil {
t.Fatalf("Unexpected error creating temporal directory: %v", err)
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)
}
ingress.DefaultSSLDirectory = td

// openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /tmp/tls.key -out /tmp/tls.crt -subj "/CN=echoheaders/O=echoheaders"
tlsCrt := "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURhakNDQWxLZ0F3SUJBZ0lKQUxHUXR5VVBKTFhYTUEwR0NTcUdTSWIzRFFFQkJRVUFNQ3d4RkRBU0JnTlYKQkFNVEMyVmphRzlvWldGa1pYSn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3Y0YjBOOFRjbW1oSy9CbDdiRXBOd0dFczNoNGs3TVdNOEF4QU15c3VxCmZmQ1pJM0tkNVJYNk0zbGwyV2QyRjhFQ2dZQlQ5RU9oTG0vVmhWMUVjUVR0cVZlMGJQTXZWaTVLSGozZm5UZkUKS0FEUVIvYVZncElLR3RLN0xUdGxlbVpPbi8yeU5wUS91UnpHZ3pDUUtldzNzU1RFSmMzYVlzbFVudzdhazJhZAp2ZTdBYXowMU84YkdHTk1oamNmdVBIS05LN2Nsc3pKRHJzcys4SnRvb245c0JHWEZYdDJuaWlpTTVPWVN5TTg4CkNJMjFEUUtCZ0hEQVRZbE84UWlDVWFBQlVqOFBsb1BtMDhwa3cyc1VmQW0xMzJCY00wQk9BN1hqYjhtNm1ManQKOUlteU5kZ2ZiM080UjlKVUxTb1pZSTc1dUxIL3k2SDhQOVlpWHZOdzMrTXl6VFU2b2d1YU8xSTNya2pna29NeAo5cU5pYlJFeGswS1A5MVZkckVLSEdHZEFwT05ES1N4VzF3ektvbUxHdmtYSTVKV05KRXFkCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg=="
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
}

dCrt, err := base64.StdEncoding.DecodeString(tlsCrt)
func TestAddOrUpdateCertAndKey(t *testing.T) {
td, err := ioutil.TempDir("", "ssl")
if err != nil {
t.Fatalf("Unexpected error: %+v", err)
return
t.Fatalf("Unexpected error creating temporal directory: %v", err)
}
ingress.DefaultSSLDirectory = td

dKey, err := base64.StdEncoding.DecodeString(tlsKey)
cert, _, err := generateRSACerts("echoheaders")
if err != nil {
t.Fatalf("Unexpected error: %+v", err)
t.Fatalf("unexpected error creating SSL certificate: %v", err)
}

name := fmt.Sprintf("test-%v", time.Now().UnixNano())
ngxCert, err := AddOrUpdateCertAndKey(name, dCrt, dKey, []byte{})

c := certutil.EncodeCertPEM(cert.Cert)
k := certutil.EncodePrivateKeyPEM(cert.Key)

ngxCert, err := AddOrUpdateCertAndKey(name, c, k, []byte{})
if err != nil {
t.Fatalf("unexpected error checking SSL certificate: %v", err)
}
Expand All @@ -66,3 +94,62 @@ func TestAddOrUpdateCertAndKey(t *testing.T) {
t.Fatalf("expected cname echoheaders but %v returned", ngxCert.CN[0])
}
}

func TestCACert(t *testing.T) {
td, err := ioutil.TempDir("", "ssl")
if err != nil {
t.Fatalf("Unexpected error creating temporal directory: %v", err)
}
ingress.DefaultSSLDirectory = td

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)

ngxCert, err := AddOrUpdateCertAndKey(name, c, k, ca)
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) {
td, err := ioutil.TempDir("", "ssl")
if err != nil {
t.Fatalf("Unexpected error creating temporal directory: %v", err)
}
ingress.DefaultSSLDirectory = td

cn := "demo-ca"
_, ca, err := generateRSACerts(cn)
if err != nil {
t.Fatalf("unexpected error creating SSL certificate: %v", err)
}
c := certutil.EncodeCertPEM(ca.Cert)
ic, err := AddCertAuth(cn, c)
if err != nil {
t.Fatalf("unexpected error creating SSL certificate: %v", err)
}
if ic.CAFileName == "" {
t.Fatalf("expected a valid CA file name")
}
}
15 changes: 15 additions & 0 deletions vendor/k8s.io/client-go/util/cert/triple/BUILD

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions vendor/k8s.io/client-go/util/cert/triple/triple.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading