Skip to content

Commit 2300b8b

Browse files
authored
cmd/go-cache-plugin: install signing cert when --revproxy is enabled (#6)
After generating a signing certificate, try to install it in the list of system trusted certificates. Right now this only works on Ubuntu, and requires that the program have write access to the cert file. It logs a warning but does not fail the program if this doesn't succeed.
1 parent b82b7d6 commit 2300b8b

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

cmd/go-cache-plugin/addca_default.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Tailscale Inc & AUTHORS
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build !linux
5+
6+
package main
7+
8+
import (
9+
"errors"
10+
11+
"github.com/creachadair/command"
12+
"github.com/creachadair/tlsutil"
13+
)
14+
15+
func installSigningCert(env *command.Env, cert tlsutil.Certificate) error {
16+
// TODO(creachadair): Maybe crib some other cases from mkcert, if we need
17+
// them, for example:
18+
// https://github.com/FiloSottile/mkcert/blob/master/truststore_darwin.go
19+
20+
return errors.New("unable to install a certificate on this system")
21+
}

cmd/go-cache-plugin/addca_linux.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Tailscale Inc & AUTHORS
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
package main
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"os"
10+
11+
"github.com/creachadair/command"
12+
"github.com/creachadair/tlsutil"
13+
"golang.org/x/sys/unix"
14+
)
15+
16+
func installSigningCert(env *command.Env, cert tlsutil.Certificate) error {
17+
const ubuntuCertFile = "/etc/ssl/certs/ca-certificates.crt"
18+
return lockAndAppend(ubuntuCertFile, cert.CertPEM())
19+
}
20+
21+
// lockAndAppend acquires an exclusive advisory lock on path, if possible, and
22+
// appends data to the end of it. It reports an error if path does not exist,
23+
// or if the lock could not be acquired. The lock is automatically released
24+
// before returning.
25+
func lockAndAppend(path string, data []byte) error {
26+
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0)
27+
if err != nil {
28+
return err
29+
}
30+
fd := int(f.Fd())
31+
if err := unix.Flock(fd, unix.LOCK_EX); err != nil {
32+
f.Close()
33+
return fmt.Errorf("lock: %w", err)
34+
}
35+
defer unix.Flock(fd, unix.LOCK_UN)
36+
_, werr := f.Write(data)
37+
cerr := f.Close()
38+
return errors.Join(werr, cerr)
39+
}

cmd/go-cache-plugin/setup.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,14 @@ func initServerCert(env *command.Env, hosts []string) (tls.Certificate, error) {
200200
if err != nil {
201201
return tls.Certificate{}, fmt.Errorf("generate signing cert: %w", err)
202202
}
203-
// TODO(creachadair): Install the CA someplace
203+
if err := installSigningCert(env, ca); err != nil {
204+
vprintf("WARNING: %v", err)
205+
} else {
206+
vprintf("installed signing cert in system store")
207+
208+
// TODO(creachadair): We should probably clean up old expired certs.
209+
// This is OK for ephemeral build/CI workers, though.
210+
}
204211

205212
sc, err := tlsutil.NewServerCert(&x509.Certificate{
206213
Subject: pkix.Name{Organization: []string{"Go cache plugin reverse proxy"}},

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
1717
github.com/goproxy/goproxy v0.17.2
1818
golang.org/x/sync v0.8.0
19+
golang.org/x/sys v0.22.0
1920
honnef.co/go/tools v0.5.1
2021
tailscale.com v1.72.1
2122
)
@@ -51,7 +52,6 @@ require (
5152
golang.org/x/crypto v0.25.0 // indirect
5253
golang.org/x/exp/typeparams v0.0.0-20240119083558-1b970713d09a // indirect
5354
golang.org/x/mod v0.19.0 // indirect
54-
golang.org/x/sys v0.22.0 // indirect
5555
golang.org/x/tools v0.23.0 // indirect
5656
google.golang.org/protobuf v1.33.0 // indirect
5757
)

0 commit comments

Comments
 (0)