Skip to content

Commit

Permalink
Merge pull request #39 from monasticacademy/overlay-certs
Browse files Browse the repository at this point in the history
Overlay common certificate authority paths with the httptap certificate
  • Loading branch information
alexflint authored Feb 23, 2025
2 parents c484ebd + 9547c19 commit 4e8ba06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
23 changes: 21 additions & 2 deletions httptap.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,23 @@ func Main() error {
}
defer os.RemoveAll(tempdir)

// marshal certificate authority to PEM format
caPEM, err := certfile.MarshalPEM(ca.Certificate)
if err != nil {
return fmt.Errorf("error marshaling certificate authority to PEM format: %w", err)
}

// write certificate authority to PEM file
caPath := filepath.Join(tempdir, "ca-certificates.crt")
err = certfile.WritePEM(caPath, ca.Certificate)
err = os.WriteFile(caPath, caPEM, 0666)
if err != nil {
return fmt.Errorf("error writing certificate authority to temporary PEM file: %w", err)
}
verbosef("created %v", caPath)

// write certificate authority to another common PEM file
caPath2 := filepath.Join(tempdir, "ca-bundle.crt")
err = certfile.WritePEM(caPath2, ca.Certificate)
err = os.WriteFile(caPath2, caPEM, 0666)
if err != nil {
return fmt.Errorf("error writing certificate authority to temporary PEM file: %w", err)
}
Expand Down Expand Up @@ -422,6 +428,19 @@ func Main() error {
defer mount.Remove()
}

// overlay common certificate authority file locations
var caLocations = []string{"/etc/ssl/certs/ca-certificates.crt"}
for _, path := range caLocations {
if st, err := os.Lstat(path); err == nil && st.Mode().IsRegular() && !args.NoOverlay {
verbosef("overlaying %v...", path)
mount, err := overlay.Mount(filepath.Dir(path), overlay.File(filepath.Base(path), caPEM))
if err != nil {
return fmt.Errorf("error setting up overlay: %w", err)
}
defer mount.Remove()
}
}

// switch user and group if requested
if args.User != "" {
u, err := user.Lookup(args.User)
Expand Down
14 changes: 14 additions & 0 deletions pkg/certfile/certfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package certfile

import (
"bytes"
"crypto/x509"
"encoding/pem"
"fmt"
Expand All @@ -9,6 +10,19 @@ import (
"software.sslmate.com/src/go-pkcs12"
)

// MarshalPEM encodes an x509 certficate to bytes in PEM format
func MarshalPEM(certificate *x509.Certificate) ([]byte, error) {
var b bytes.Buffer
err := pem.Encode(&b, &pem.Block{
Type: "CERTIFICATE",
Bytes: certificate.Raw,
})
if err != nil {
return nil, err
}
return b.Bytes(), nil
}

// WritePEM writes an x509 certificate to a PEM file
func WritePEM(path string, certificate *x509.Certificate) (err error) {
var f *os.File
Expand Down

0 comments on commit 4e8ba06

Please sign in to comment.