Skip to content

Commit

Permalink
trimmed pem files and added validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreburgaud committed Dec 23, 2023
1 parent 7408a33 commit 8cb8039
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/pem"
Expand Down Expand Up @@ -135,6 +136,11 @@ func GetLocalCerts(certFile string) ([]*x509.Certificate, error) {
var certs []*x509.Certificate
// Open and read PEM file
data, err := os.ReadFile(certFile)

// Remove any prefix or trailing new lines
data = bytes.TrimLeft(data, "\n")
data = bytes.TrimRight(data, "\n")

if err != nil {
return nil, err
}
Expand Down
30 changes: 30 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package client

import (
"testing"
)

// Test the GetLocalCerts function to ensure that the resulting certs contains a proper slice of *x509.Certificate
func TestGetLocalCerts(t *testing.T) {
tt := []struct {
certFile string
expected string // serialNumber
}{
{"../../test_data/rsa_2048_exp3.crt", "689353446962534075818318094020409231943178713332"},
{"../../test_data/rsa_4096_exp9.crt", "491857893787078309585312564136279056496696784059"},
{"../../test_data/ecdsa.crt", "161666955289179666643937194263860491865277586603"},
{"../../test_data/ed25519.crt", "170414627618143861399763465404232742787060139745"},
}
for _, tc := range tt {
t.Run(tc.certFile, func(t *testing.T) {
certs, err := GetLocalCerts(tc.certFile)
if err != nil {
t.Errorf("error creating cert from local pem file %s: %s", tc.certFile, err)
}
got := certs[0].SerialNumber.String()
if got != tc.expected {
t.Errorf("got %s, expected %s", got, tc.expected)
}
})
}
}

0 comments on commit 8cb8039

Please sign in to comment.