-
Notifications
You must be signed in to change notification settings - Fork 14
/
authenticator_test_server.go
118 lines (100 loc) · 2.94 KB
/
authenticator_test_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package common
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"math/big"
"net/http"
"net/http/httptest"
"os"
"strings"
"time"
)
type TestAuthServer struct {
Server *httptest.Server
ClientCertPath string
CertLogPath string
ExpectedTokenValue string
SkipWritingCSRFile bool
HandleLogin func(
loginCsr *x509.CertificateRequest,
loginCsrErr error,
)
}
// testServer creates, for testing purposes, an http server on a random port that mocks conjur's
// login and authenticate endpoints.
func NewTestAuthServer(clientCertPath, certLogPath, expectedTokenValue string, skipWritingCSRfile bool) *TestAuthServer {
ts := &TestAuthServer{
ClientCertPath: clientCertPath,
CertLogPath: certLogPath,
ExpectedTokenValue: expectedTokenValue,
SkipWritingCSRFile: skipWritingCSRfile,
}
authnCACertificate := &x509.Certificate{
SerialNumber: big.NewInt(2021),
}
authnCAPrivKey, _ := rsa.GenerateKey(rand.Reader, 4096)
ts.Server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/authenticate") {
// Peer certificate from mutual auth
// Respond with a dummy token
w.WriteHeader(201)
w.Write([]byte(ts.ExpectedTokenValue))
}
if strings.HasPrefix(r.URL.Path, "/inject_client_cert") {
// Login request
body, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
csrBlock, _ := pem.Decode(body)
loginCsr, err := x509.ParseCertificateRequest(csrBlock.Bytes)
if ts.HandleLogin != nil {
ts.HandleLogin(loginCsr, err)
}
w.WriteHeader(202)
if ts.SkipWritingCSRFile {
err := ioutil.WriteFile(ts.CertLogPath, []byte("error writing csr file\n"), os.ModePerm)
if err != nil {
panic(err)
}
return
}
// Create client certificate template
clientCRTTemplate := x509.Certificate{
Signature: loginCsr.Signature,
SignatureAlgorithm: loginCsr.SignatureAlgorithm,
PublicKeyAlgorithm: loginCsr.PublicKeyAlgorithm,
PublicKey: loginCsr.PublicKey,
SerialNumber: big.NewInt(2),
Issuer: authnCACertificate.Subject,
Subject: loginCsr.Subject,
NotBefore: time.Now(),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
// Create client certificate from template and CA public key
clientCRTRaw, _ := x509.CreateCertificate(
rand.Reader,
&clientCRTTemplate,
authnCACertificate,
loginCsr.PublicKey,
authnCAPrivKey,
)
// Save the certificate
err = ioutil.WriteFile(
ts.ClientCertPath,
pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: clientCRTRaw}),
os.ModePerm,
)
if err != nil {
panic(err)
}
}
}))
ts.Server.StartTLS()
ts.Server.TLS.ClientAuth = tls.RequestClientCert
return ts
}