Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlay common certificate authority paths with the httptap certificate #39

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions httptap.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,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 @@ -412,6 +418,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