2019-03-14 08:30:04 +00:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2019-09-13 17:28:04 +00:00
|
|
|
"context"
|
2019-03-14 08:30:04 +00:00
|
|
|
"crypto/tls"
|
2019-07-12 15:50:04 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2019-03-14 08:30:04 +00:00
|
|
|
"testing"
|
2019-06-21 15:18:05 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-12 15:50:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-14 08:30:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LocalhostCert is a PEM-encoded TLS cert with SAN IPs
|
|
|
|
// "127.0.0.1" and "[::1]", expiring at Jan 29 16:00:00 2084 GMT.
|
|
|
|
// generated from src/crypto/tls:
|
|
|
|
// go run generate_cert.go --rsa-bits 1024 --host 127.0.0.1,::1,example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h
|
|
|
|
var (
|
|
|
|
localhostCert = FileOrContent(`-----BEGIN CERTIFICATE-----
|
2022-02-14 13:08:07 +00:00
|
|
|
MIIDOTCCAiGgAwIBAgIQSRJrEpBGFc7tNb1fb5pKFzANBgkqhkiG9w0BAQsFADAS
|
2019-03-14 08:30:04 +00:00
|
|
|
MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw
|
2022-02-14 13:08:07 +00:00
|
|
|
MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
|
|
|
|
MIIBCgKCAQEA6Gba5tHV1dAKouAaXO3/ebDUU4rvwCUg/CNaJ2PT5xLD4N1Vcb8r
|
|
|
|
bFSW2HXKq+MPfVdwIKR/1DczEoAGf/JWQTW7EgzlXrCd3rlajEX2D73faWJekD0U
|
|
|
|
aUgz5vtrTXZ90BQL7WvRICd7FlEZ6FPOcPlumiyNmzUqtwGhO+9ad1W5BqJaRI6P
|
|
|
|
YfouNkwR6Na4TzSj5BrqUfP0FwDizKSJ0XXmh8g8G9mtwxOSN3Ru1QFc61Xyeluk
|
|
|
|
POGKBV/q6RBNklTNe0gI8usUMlYyoC7ytppNMW7X2vodAelSu25jgx2anj9fDVZu
|
|
|
|
h7AXF5+4nJS4AAt0n1lNY7nGSsdZas8PbQIDAQABo4GIMIGFMA4GA1UdDwEB/wQE
|
|
|
|
AwICpDATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MB0GA1Ud
|
|
|
|
DgQWBBStsdjh3/JCXXYlQryOrL4Sh7BW5TAuBgNVHREEJzAlggtleGFtcGxlLmNv
|
|
|
|
bYcEfwAAAYcQAAAAAAAAAAAAAAAAAAAAATANBgkqhkiG9w0BAQsFAAOCAQEAxWGI
|
|
|
|
5NhpF3nwwy/4yB4i/CwwSpLrWUa70NyhvprUBC50PxiXav1TeDzwzLx/o5HyNwsv
|
|
|
|
cxv3HdkLW59i/0SlJSrNnWdfZ19oTcS+6PtLoVyISgtyN6DpkKpdG1cOkW3Cy2P2
|
|
|
|
+tK/tKHRP1Y/Ra0RiDpOAmqn0gCOFGz8+lqDIor/T7MTpibL3IxqWfPrvfVRHL3B
|
|
|
|
grw/ZQTTIVjjh4JBSW3WyWgNo/ikC1lrVxzl4iPUGptxT36Cr7Zk2Bsg0XqwbOvK
|
|
|
|
5d+NTDREkSnUbie4GeutujmX3Dsx88UiV6UY/4lHJa6I5leHUNOHahRbpbWeOfs/
|
|
|
|
WkBKOclmOV2xlTVuPw==
|
2019-03-14 08:30:04 +00:00
|
|
|
-----END CERTIFICATE-----`)
|
|
|
|
|
|
|
|
// LocalhostKey is the private key for localhostCert.
|
|
|
|
localhostKey = FileOrContent(`-----BEGIN RSA PRIVATE KEY-----
|
2022-02-14 13:08:07 +00:00
|
|
|
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDoZtrm0dXV0Aq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po8cZGQywKHUWsxi23ozHoxo
|
|
|
|
+bGqeQb9U661TnfdDspIXia+xilZt3mm5BPzOUuRqlh4Y9SOBpSWRmEhyw76w4ZP
|
|
|
|
OPxjWYAgwQKBgA/FehSYxeJgRjSdo+MWnK66tjHgDJE8bYpUZsP0JC4R9DL5oiaA
|
|
|
|
brd2fI6Y+SbyeNBallObt8LSgzdtnEAbjIH8uDJqyOmknNePRvAvR6mP4xyuR+Bv
|
|
|
|
m+Lgp0DMWTw5J9CKpydZDItc49T/mJ5tPhdFVd+am0NAQnmr1MCZ6nHxAoGABS3Y
|
|
|
|
LkaC9FdFUUqSU8+Chkd/YbOkuyiENdkvl6t2e52jo5DVc1T7mLiIrRQi4SI8N9bN
|
|
|
|
/3oJWCT+uaSLX2ouCtNFunblzWHBrhxnZzTeqVq4SLc8aESAnbslKL4i8/+vYZlN
|
|
|
|
s8xtiNcSvL+lMsOBORSXzpj/4Ot8WwTkn1qyGgECgYBKNTypzAHeLE6yVadFp3nQ
|
|
|
|
Ckq9yzvP/ib05rvgbvrne00YeOxqJ9gtTrzgh7koqJyX1L4NwdkEza4ilDWpucn0
|
|
|
|
xiUZS4SoaJq6ZvcBYS62Yr1t8n09iG47YL8ibgtmH3L+svaotvpVxVK+d7BLevA/
|
|
|
|
ZboOWVe3icTy64BT3OQhmg==
|
2019-03-14 08:30:04 +00:00
|
|
|
-----END RSA PRIVATE KEY-----`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTLSInStore(t *testing.T) {
|
2019-06-27 21:58:03 +00:00
|
|
|
dynamicConfigs := []*CertAndStores{{
|
|
|
|
Certificate: Certificate{
|
|
|
|
CertFile: localhostCert,
|
|
|
|
KeyFile: localhostKey,
|
|
|
|
},
|
|
|
|
}}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
tlsManager := NewManager()
|
2019-09-13 17:28:04 +00:00
|
|
|
tlsManager.UpdateConfigs(context.Background(), nil, nil, dynamicConfigs)
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
certs := tlsManager.GetStore("default").DynamicCerts.Get().(map[string]*tls.Certificate)
|
|
|
|
if len(certs) == 0 {
|
|
|
|
t.Fatal("got error: default store must have TLS certificates.")
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 14:32:04 +00:00
|
|
|
|
|
|
|
func TestTLSInvalidStore(t *testing.T) {
|
2019-06-27 21:58:03 +00:00
|
|
|
dynamicConfigs := []*CertAndStores{{
|
|
|
|
Certificate: Certificate{
|
|
|
|
CertFile: localhostCert,
|
|
|
|
KeyFile: localhostKey,
|
|
|
|
},
|
|
|
|
}}
|
2019-06-21 14:32:04 +00:00
|
|
|
|
|
|
|
tlsManager := NewManager()
|
2019-09-13 17:28:04 +00:00
|
|
|
tlsManager.UpdateConfigs(context.Background(),
|
|
|
|
map[string]Store{
|
|
|
|
"default": {
|
|
|
|
DefaultCertificate: &Certificate{
|
|
|
|
CertFile: "/wrong",
|
|
|
|
KeyFile: "/wrong",
|
|
|
|
},
|
2019-06-21 14:32:04 +00:00
|
|
|
},
|
2019-09-13 17:28:04 +00:00
|
|
|
}, nil, dynamicConfigs)
|
2019-06-21 14:32:04 +00:00
|
|
|
|
|
|
|
certs := tlsManager.GetStore("default").DynamicCerts.Get().(map[string]*tls.Certificate)
|
|
|
|
if len(certs) == 0 {
|
|
|
|
t.Fatal("got error: default store must have TLS certificates.")
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 15:18:05 +00:00
|
|
|
|
|
|
|
func TestManager_Get(t *testing.T) {
|
2019-06-27 21:58:03 +00:00
|
|
|
dynamicConfigs := []*CertAndStores{{
|
|
|
|
Certificate: Certificate{
|
|
|
|
CertFile: localhostCert,
|
|
|
|
KeyFile: localhostKey,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
tlsConfigs := map[string]Options{
|
2019-06-21 15:18:05 +00:00
|
|
|
"foo": {MinVersion: "VersionTLS12"},
|
|
|
|
"bar": {MinVersion: "VersionTLS11"},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
tlsOptionsName string
|
|
|
|
expectedMinVersion uint16
|
|
|
|
expectedError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Get a tls config from a valid name",
|
|
|
|
tlsOptionsName: "foo",
|
|
|
|
expectedMinVersion: uint16(tls.VersionTLS12),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Get another tls config from a valid name",
|
|
|
|
tlsOptionsName: "bar",
|
|
|
|
expectedMinVersion: uint16(tls.VersionTLS11),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Get an tls config from an invalid name",
|
|
|
|
tlsOptionsName: "unknown",
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Get an tls config from unexisting 'default' name",
|
|
|
|
tlsOptionsName: "default",
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsManager := NewManager()
|
2019-09-13 17:28:04 +00:00
|
|
|
tlsManager.UpdateConfigs(context.Background(), nil, tlsConfigs, dynamicConfigs)
|
2019-06-21 15:18:05 +00:00
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
config, err := tlsManager.Get("default", test.tlsOptionsName)
|
|
|
|
if test.expectedError {
|
|
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, config.MinVersion, test.expectedMinVersion)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 15:50:04 +00:00
|
|
|
|
|
|
|
func TestClientAuth(t *testing.T) {
|
|
|
|
tlsConfigs := map[string]Options{
|
2019-07-19 09:52:04 +00:00
|
|
|
"eca": {
|
|
|
|
ClientAuth: ClientAuth{},
|
|
|
|
},
|
|
|
|
"ecat": {
|
|
|
|
ClientAuth: ClientAuth{ClientAuthType: ""},
|
|
|
|
},
|
|
|
|
"ncc": {
|
|
|
|
ClientAuth: ClientAuth{ClientAuthType: "NoClientCert"},
|
|
|
|
},
|
|
|
|
"rcc": {
|
|
|
|
ClientAuth: ClientAuth{ClientAuthType: "RequestClientCert"},
|
|
|
|
},
|
|
|
|
"racc": {
|
|
|
|
ClientAuth: ClientAuth{ClientAuthType: "RequireAnyClientCert"},
|
|
|
|
},
|
2019-07-12 15:50:04 +00:00
|
|
|
"vccig": {
|
|
|
|
ClientAuth: ClientAuth{
|
|
|
|
CAFiles: []FileOrContent{localhostCert},
|
|
|
|
ClientAuthType: "VerifyClientCertIfGiven",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"vccigwca": {
|
|
|
|
ClientAuth: ClientAuth{ClientAuthType: "VerifyClientCertIfGiven"},
|
|
|
|
},
|
2019-07-19 09:52:04 +00:00
|
|
|
"ravcc": {
|
|
|
|
ClientAuth: ClientAuth{ClientAuthType: "RequireAndVerifyClientCert"},
|
|
|
|
},
|
2019-07-12 15:50:04 +00:00
|
|
|
"ravccwca": {
|
|
|
|
ClientAuth: ClientAuth{
|
|
|
|
CAFiles: []FileOrContent{localhostCert},
|
|
|
|
ClientAuthType: "RequireAndVerifyClientCert",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"ravccwbca": {
|
|
|
|
ClientAuth: ClientAuth{
|
|
|
|
CAFiles: []FileOrContent{"Bad content"},
|
|
|
|
ClientAuthType: "RequireAndVerifyClientCert",
|
|
|
|
},
|
|
|
|
},
|
2019-07-19 09:52:04 +00:00
|
|
|
"ucat": {
|
|
|
|
ClientAuth: ClientAuth{ClientAuthType: "Unknown"},
|
|
|
|
},
|
2019-07-12 15:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block, _ := pem.Decode([]byte(localhostCert))
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
tlsOptionsName string
|
|
|
|
expectedClientAuth tls.ClientAuthType
|
|
|
|
expectedRawSubject []byte
|
2019-07-19 09:52:04 +00:00
|
|
|
expectedError bool
|
2019-07-12 15:50:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Empty ClientAuth option should get a tls.NoClientCert (default value)",
|
|
|
|
tlsOptionsName: "eca",
|
|
|
|
expectedClientAuth: tls.NoClientCert,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Empty ClientAuthType option should get a tls.NoClientCert (default value)",
|
|
|
|
tlsOptionsName: "ecat",
|
|
|
|
expectedClientAuth: tls.NoClientCert,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "NoClientCert option should get a tls.NoClientCert as ClientAuthType",
|
|
|
|
tlsOptionsName: "ncc",
|
|
|
|
expectedClientAuth: tls.NoClientCert,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "RequestClientCert option should get a tls.RequestClientCert as ClientAuthType",
|
|
|
|
tlsOptionsName: "rcc",
|
|
|
|
expectedClientAuth: tls.RequestClientCert,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "RequireAnyClientCert option should get a tls.RequireAnyClientCert as ClientAuthType",
|
|
|
|
tlsOptionsName: "racc",
|
|
|
|
expectedClientAuth: tls.RequireAnyClientCert,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "VerifyClientCertIfGiven option should get a tls.VerifyClientCertIfGiven as ClientAuthType",
|
|
|
|
tlsOptionsName: "vccig",
|
|
|
|
expectedClientAuth: tls.VerifyClientCertIfGiven,
|
|
|
|
},
|
|
|
|
{
|
2019-07-19 09:52:04 +00:00
|
|
|
desc: "VerifyClientCertIfGiven option without CAFiles yields a default ClientAuthType (NoClientCert)",
|
2019-07-12 15:50:04 +00:00
|
|
|
tlsOptionsName: "vccigwca",
|
|
|
|
expectedClientAuth: tls.NoClientCert,
|
2019-07-19 09:52:04 +00:00
|
|
|
expectedError: true,
|
2019-07-12 15:50:04 +00:00
|
|
|
},
|
|
|
|
{
|
2019-07-19 09:52:04 +00:00
|
|
|
desc: "RequireAndVerifyClientCert option without CAFiles yields a default ClientAuthType (NoClientCert)",
|
2019-07-12 15:50:04 +00:00
|
|
|
tlsOptionsName: "ravcc",
|
|
|
|
expectedClientAuth: tls.NoClientCert,
|
2019-07-19 09:52:04 +00:00
|
|
|
expectedError: true,
|
2019-07-12 15:50:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "RequireAndVerifyClientCert option should get a tls.RequireAndVerifyClientCert as ClientAuthType with CA files",
|
|
|
|
tlsOptionsName: "ravccwca",
|
|
|
|
expectedClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
expectedRawSubject: cert.RawSubject,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Unknown option yields a default ClientAuthType (NoClientCert)",
|
|
|
|
tlsOptionsName: "ucat",
|
|
|
|
expectedClientAuth: tls.NoClientCert,
|
2019-07-19 09:52:04 +00:00
|
|
|
expectedError: true,
|
2019-07-12 15:50:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Bad CA certificate content yields a default ClientAuthType (NoClientCert)",
|
|
|
|
tlsOptionsName: "ravccwbca",
|
|
|
|
expectedClientAuth: tls.NoClientCert,
|
2019-07-19 09:52:04 +00:00
|
|
|
expectedError: true,
|
2019-07-12 15:50:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsManager := NewManager()
|
2019-09-13 17:28:04 +00:00
|
|
|
tlsManager.UpdateConfigs(context.Background(), nil, tlsConfigs, nil)
|
2019-07-12 15:50:04 +00:00
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
config, err := tlsManager.Get("default", test.tlsOptionsName)
|
2019-07-19 09:52:04 +00:00
|
|
|
|
|
|
|
if test.expectedError {
|
|
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:50:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if test.expectedRawSubject != nil {
|
|
|
|
subjects := config.ClientCAs.Subjects()
|
|
|
|
assert.Len(t, subjects, 1)
|
|
|
|
assert.Equal(t, subjects[0], test.expectedRawSubject)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ClientAuth, test.expectedClientAuth)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|