Skip to content

Commit a49a2f6

Browse files
committed
cmd/litewitness: generate the bastion certificate once at startup
Closes #56 Fixes #55
1 parent f0348e1 commit a49a2f6

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

cmd/litewitness/litewitness.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ func main() {
122122
})
123123

124124
signer := connectToSSHAgent()
125+
bastionCertX509, err := selfSignedCertificate(signer)
126+
if err != nil {
127+
fatal("generating self-signed certificate", "err", err)
128+
}
129+
bastionCert := tls.Certificate{
130+
Certificate: [][]byte{bastionCertX509},
131+
PrivateKey: signer,
132+
}
125133

126134
w, err := witness.NewWitness(*dbFlag, *nameFlag, signer, slog.Default())
127135
if err != nil {
@@ -196,7 +204,7 @@ func main() {
196204
retry := 0
197205
for {
198206
startTime := time.Now()
199-
err := connectToBastion(ctx, addr, signer, srv, true)
207+
err := connectToBastion(ctx, addr, bastionCert, srv, true)
200208
duration := time.Since(startTime)
201209
slog.Warn("bastion connection failed", "bastion", addr, "duration", duration, "err", err)
202210

@@ -251,7 +259,7 @@ func main() {
251259
if *bastionFlag != "" {
252260
go func() {
253261
for _, bastion := range strings.Split(*bastionFlag, ",") {
254-
err := connectToBastion(ctx, bastion, signer, srv, false)
262+
err := connectToBastion(ctx, bastion, bastionCert, srv, false)
255263
if err == errBastionDisconnected {
256264
// Connection succeeded and then was interrupted. Restart to
257265
// let the scheduler apply any backoff, and then retry all bastions.
@@ -410,12 +418,8 @@ func indexHandler(w *witness.Witness) http.HandlerFunc {
410418

411419
var errBastionDisconnected = errors.New("connection to bastion interrupted")
412420

413-
func connectToBastion(ctx context.Context, bastion string, signer *signer, srv *http.Server, logSpecific bool) error {
421+
func connectToBastion(ctx context.Context, bastion string, cert tls.Certificate, srv *http.Server, logSpecific bool) error {
414422
slog.Info("connecting to bastion", "bastion", bastion)
415-
cert, err := selfSignedCertificate(signer)
416-
if err != nil {
417-
fatal("generating self-signed certificate", "err", err)
418-
}
419423
dialCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
420424
defer cancel()
421425
var roots *x509.CertPool
@@ -429,14 +433,11 @@ func connectToBastion(ctx context.Context, bastion string, signer *signer, srv *
429433
}
430434
conn, err := (&tls.Dialer{
431435
Config: &tls.Config{
432-
Certificates: []tls.Certificate{{
433-
Certificate: [][]byte{cert},
434-
PrivateKey: signer,
435-
}},
436-
MinVersion: tls.VersionTLS13,
437-
MaxVersion: tls.VersionTLS13,
438-
NextProtos: []string{"bastion/0"},
439-
RootCAs: roots,
436+
Certificates: []tls.Certificate{cert},
437+
MinVersion: tls.VersionTLS13,
438+
MaxVersion: tls.VersionTLS13,
439+
NextProtos: []string{"bastion/0"},
440+
RootCAs: roots,
440441
},
441442
}).DialContext(dialCtx, "tcp", bastion)
442443
if err != nil {
@@ -475,7 +476,7 @@ func selfSignedCertificate(key crypto.Signer) ([]byte, error) {
475476
SerialNumber: big.NewInt(1),
476477
Subject: pkix.Name{CommonName: "litewitness"},
477478
NotBefore: time.Now().Add(-1 * time.Hour),
478-
NotAfter: time.Now().Add(24 * time.Hour),
479+
NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour),
479480
KeyUsage: x509.KeyUsageDigitalSignature,
480481
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
481482
}

0 commit comments

Comments
 (0)