Add tests. This actually adds the initial tests for the TLS listener,

then layers reloading tests on top.
This commit is contained in:
Jeff Mitchell 2016-03-10 01:40:36 -05:00
parent baf0763b3c
commit c6066af4c1
7 changed files with 211 additions and 12 deletions

View File

@ -1,12 +1,22 @@
package server package server
import ( import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"math/rand"
"net" "net"
"os"
"testing" "testing"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/vault"
) )
func TestTCPListener(t *testing.T) { func TestTCPListener(t *testing.T) {
ln, _, err := tcpListenerFactory(map[string]string{ ln, _, _, err := tcpListenerFactory(map[string]string{
"address": "127.0.0.1:0", "address": "127.0.0.1:0",
"tls_disable": "1", "tls_disable": "1",
}) })
@ -18,24 +28,83 @@ func TestTCPListener(t *testing.T) {
return net.Dial("tcp", ln.Addr().String()) return net.Dial("tcp", ln.Addr().String())
} }
testListenerImpl(t, ln, connFn) testListenerImpl(t, ln, connFn, "")
} }
// TestTCPListener_tls tests both TLS generally and also the reload capability
// of core, system backend, and the listener logic
func TestTCPListener_tls(t *testing.T) { func TestTCPListener_tls(t *testing.T) {
// TODO wd, _ := os.Getwd()
t.Skip() wd += "/test-fixtures/reload/"
ln, _, err := tcpListenerFactory(map[string]string{ td, err := ioutil.TempDir("", fmt.Sprintf("vault-test-%d", rand.New(rand.NewSource(time.Now().Unix())).Int63))
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
// Setup initial certs
inBytes, _ := ioutil.ReadFile(wd + "reload_foo.pem")
ioutil.WriteFile(td+"reload_curr.pem", inBytes, 0777)
inBytes, _ = ioutil.ReadFile(wd + "reload_foo.key")
ioutil.WriteFile(td+"reload_curr.key", inBytes, 0777)
inBytes, _ = ioutil.ReadFile(wd + "reload_ca.pem")
certPool := x509.NewCertPool()
ok := certPool.AppendCertsFromPEM(inBytes)
if !ok {
t.Fatal("not ok when appending CA cert")
}
ln, _, reloadFunc, err := tcpListenerFactory(map[string]string{
"address": "127.0.0.1:0", "address": "127.0.0.1:0",
"tls_disable": "1", "tls_cert_file": td + "reload_curr.pem",
"tls_key_file": td + "reload_curr.key",
}) })
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
connFn := func(lnReal net.Listener) (net.Conn, error) { core, _, root := vault.TestCoreUnsealed(t)
return net.Dial("tcp", ln.Addr().String()) bc := &logical.BackendConfig{
Logger: nil,
System: logical.StaticSystemView{
DefaultLeaseTTLVal: time.Hour * 24,
MaxLeaseTTLVal: time.Hour * 24 * 30,
},
} }
testListenerImpl(t, ln, connFn) vault.NewSystemBackend(core, bc)
core.AddReloadFunc("listentest", reloadFunc)
connFn := func(lnReal net.Listener) (net.Conn, error) {
conn, err := tls.Dial("tcp", ln.Addr().String(), &tls.Config{
RootCAs: certPool,
})
if err != nil {
return nil, err
}
if err = conn.Handshake(); err != nil {
return nil, err
}
return conn, nil
}
testListenerImpl(t, ln, connFn, "foo.example.com")
inBytes, _ = ioutil.ReadFile(wd + "reload_bar.pem")
ioutil.WriteFile(td+"reload_curr.pem", inBytes, 0777)
inBytes, _ = ioutil.ReadFile(wd + "reload_bar.key")
ioutil.WriteFile(td+"reload_curr.key", inBytes, 0777)
req := logical.TestRequest(t, logical.UpdateOperation, "sys/reload")
req.ClientToken = root
resp, err := core.HandleRequest(req)
if err != nil {
t.Fatalf("err: %s", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
testListenerImpl(t, ln, connFn, "bar.example.com")
} }

View File

@ -2,6 +2,7 @@ package server
import ( import (
"bytes" "bytes"
"crypto/tls"
"io" "io"
"net" "net"
"testing" "testing"
@ -9,14 +10,17 @@ import (
type testListenerConnFn func(net.Listener) (net.Conn, error) type testListenerConnFn func(net.Listener) (net.Conn, error)
func testListenerImpl(t *testing.T, ln net.Listener, connFn testListenerConnFn) { func testListenerImpl(t *testing.T, ln net.Listener, connFn testListenerConnFn, certName string) {
serverCh := make(chan net.Conn, 1) serverCh := make(chan net.Conn, 1)
go func() { go func() {
server, err := ln.Accept() server, err := ln.Accept()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if certName != "" {
tlsConn := server.(*tls.Conn)
tlsConn.Handshake()
}
serverCh <- server serverCh <- server
}() }()
@ -24,6 +28,18 @@ func testListenerImpl(t *testing.T, ln net.Listener, connFn testListenerConnFn)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if certName != "" {
tlsConn := client.(*tls.Conn)
if len(tlsConn.ConnectionState().PeerCertificates) != 1 {
t.Fatalf("err: number of certs too long")
}
peerName := tlsConn.ConnectionState().PeerCertificates[0].Subject.CommonName
if peerName != certName {
t.Fatalf("err: bad cert name %s, expected %s", peerName, certName)
}
}
server := <-serverCh server := <-serverCh
defer client.Close() defer client.Close()
defer server.Close() defer server.Close()

View File

@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAwF7sRAyUiLcd6es6VeaTRUBOusFFGkmKJ5lU351waCJqXFju
Z6i/SQYNAAnnRgotXSTE1fIPjE2kZNH1hvqE5IpTGgAwy50xpjJrrBBI6e9lyKqj
7T8gLVNBvtC0cpQi+pGrszEI0ckDQCSZHqi/PAzcpmLUgh2KMrgagT+YlN35KHtl
/bQ/Fsn+kqykVqNw69n/CDKNKdDHn1qPwiX9q/fTMj3EG6g+3ntKrUOh8V/gHKPz
q8QGP/wIud2K+tTSorVXr/4zx7xgzlbJkCakzcQQiP6K+paPnDRlE8fK+1gRRyR7
XCzyp0irUl8G1NjYAR/tVWxiUhlk/jZutb8PpwIDAQABAoIBAEOzJELuindyujxQ
ZD9G3h1I/GwNCFyv9Mbq10u7BIwhUH0fbwdcA7WXQ4v38ERd4IkfH4aLoZ0m1ewF
V/sgvxQO+h/0YTfHImny5KGxOXfaoF92bipYROKuojydBmQsbgLwsRRm9UufCl3Q
g3KewG5JuH112oPQEYq379v8nZ4FxC3Ano1OFBTm9UhHIAX1Dn22kcHOIIw8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