mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 21:12:07 +00:00
Setup test for verifying by checking certificate fingerprints
This commit is contained in:
parent
7ade8261f6
commit
64bc96baf9
@ -18,8 +18,10 @@ package vclib_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha1"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@ -30,7 +32,7 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib/fixtures"
|
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib/fixtures"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTestServer(t *testing.T, caCertPath, serverCertPath, serverKeyPath string, handler http.HandlerFunc) *httptest.Server {
|
func createTestServer(t *testing.T, caCertPath, serverCertPath, serverKeyPath string, handler http.HandlerFunc) (*httptest.Server, string) {
|
||||||
caCertPEM, err := ioutil.ReadFile(caCertPath)
|
caCertPEM, err := ioutil.ReadFile(caCertPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Could not read ca cert from file")
|
t.Fatalf("Could not read ca cert from file")
|
||||||
@ -54,22 +56,20 @@ func createTestServer(t *testing.T, caCertPath, serverCertPath, serverKeyPath st
|
|||||||
RootCAs: certPool,
|
RootCAs: certPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
return server
|
// calculate the leaf certificate's fingerprint
|
||||||
|
x509LeafCert := server.TLS.Certificates[0].Certificate[0]
|
||||||
|
tpBytes := sha1.Sum(x509LeafCert)
|
||||||
|
tpString := fmt.Sprintf("%x", tpBytes)
|
||||||
|
|
||||||
|
return server, tpString
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithValidCaCert(t *testing.T) {
|
func TestWithValidCaCert(t *testing.T) {
|
||||||
gotRequest := false
|
handler, verify := getRequestVerifier(t)
|
||||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
gotRequest = true
|
|
||||||
}
|
|
||||||
|
|
||||||
server := createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
server, _ := createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
||||||
server.StartTLS()
|
server.StartTLS()
|
||||||
|
u := mustParseUrl(t, server.URL)
|
||||||
u, err := url.Parse(server.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Cannot parse URL: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
connection := &vclib.VSphereConnection{
|
connection := &vclib.VSphereConnection{
|
||||||
Hostname: u.Hostname(),
|
Hostname: u.Hostname(),
|
||||||
@ -80,9 +80,26 @@ func TestWithValidCaCert(t *testing.T) {
|
|||||||
// Ignoring error here, because we only care about the TLS connection
|
// Ignoring error here, because we only care about the TLS connection
|
||||||
connection.NewClient(context.Background())
|
connection.NewClient(context.Background())
|
||||||
|
|
||||||
if !gotRequest {
|
verify()
|
||||||
t.Fatalf("Never saw a request, TLS connection could not be established")
|
}
|
||||||
|
|
||||||
|
func TestWithValidThumbprint(t *testing.T) {
|
||||||
|
handler, verify := getRequestVerifier(t)
|
||||||
|
|
||||||
|
server, serverThumbprint := createTestServer(t, fixtures.CaCertPath, fixtures.ServerCertPath, fixtures.ServerKeyPath, handler)
|
||||||
|
server.StartTLS()
|
||||||
|
u := mustParseUrl(t, server.URL)
|
||||||
|
|
||||||
|
connection := &vclib.VSphereConnection{
|
||||||
|
Hostname: u.Hostname(),
|
||||||
|
Port: u.Port(),
|
||||||
|
Thumbprint: serverThumbprint,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ignoring error here, because we only care about the TLS connection
|
||||||
|
connection.NewClient(context.Background())
|
||||||
|
|
||||||
|
verify()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithInvalidCaCertPath(t *testing.T) {
|
func TestWithInvalidCaCertPath(t *testing.T) {
|
||||||
@ -133,3 +150,27 @@ type fakeTransport struct{}
|
|||||||
func (ft fakeTransport) RoundTrip(*http.Request) (*http.Response, error) {
|
func (ft fakeTransport) RoundTrip(*http.Request) (*http.Response, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRequestVerifier(t *testing.T) (http.HandlerFunc, func()) {
|
||||||
|
gotRequest := false
|
||||||
|
|
||||||
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
gotRequest = true
|
||||||
|
}
|
||||||
|
|
||||||
|
checker := func() {
|
||||||
|
if !gotRequest {
|
||||||
|
t.Fatalf("Never saw a request, maybe TLS connection could not be established?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler, checker
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustParseUrl(t *testing.T, i string) *url.URL {
|
||||||
|
u, err := url.Parse(i)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Cannot parse URL: %v", err)
|
||||||
|
}
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user