Skip to content

Commit

Permalink
wip: failing load cert
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Nov 28, 2024
1 parent e90e686 commit 9bb2f71
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
8 changes: 4 additions & 4 deletions internal/datasource/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func LoadCertificate(certPath string) (*x509.Certificate, error) {
return nil, err
}

certPEMBlock, _ := pem.Decode(fileContents)
if certPEMBlock == nil {
return nil, fmt.Errorf("could not decode: cert was not PEM format")
block, _ := pem.Decode(fileContents)
if block == nil || block.Type != "CERTIFICATE" {
return nil, fmt.Errorf("failed to decode PEM block containing the certificate")
}

cert, err := x509.ParseCertificate(certPEMBlock.Bytes)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
Expand Down
69 changes: 69 additions & 0 deletions internal/file/file_manager_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package file

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -587,3 +588,71 @@ func TestFileManagerService_fileActions(t *testing.T) {
defer helpers.RemoveFileWithErrorCheck(t, updateFile.Name())
defer helpers.RemoveFileWithErrorCheck(t, addFilePath)
}

func TestParseX509Certificates(t *testing.T) {
tests := []struct {
certName string
certContent string
name string
expectedSerial []byte
}{
{
name: "Test 1: generated cert",
certName: "public_cert",
certContent: "",
expectedSerial: []byte{0x1, 0xe0, 0xf3},
},
{
name: "Test 2: open ssl cert",
certName: "open_ssl_cert",
certContent: `-----BEGIN CERTIFICATE-----
MIIDazCCAlOgAwIBAgIUR+YGgRHhYwotFyBOvSc1KD9d45kwDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDExMjcxNTM0MDZaFw0yNDEy
MjcxNTM0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Run(test.name, func(t *testing.T) {
var certBytes []byte

if test.certContent == "" {
_, certBytes = helpers.GenerateSelfSignedCert(t)
} else {
certBytes = []byte(test.certContent)
}

certContents := helpers.Cert{
Name: fmt.Sprintf("%s.pem", test.certName),
Type: "CERTIFICATE",
Contents: certBytes,
}
certFile := helpers.WriteCertFiles(t, tempDir, certContents)

certFileMeta, certFileMetaErr := files.FileMetaWithCertificate(certFile)
require.NoError(t, certFileMetaErr)

assert.Equal(t, test.expectedSerial, certFileMeta.GetCertificateMeta().GetSerialNumber())
})
}
}
16 changes: 13 additions & 3 deletions test/helpers/cert_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"math/big"
"os"
"strings"
"testing"
"time"

Expand All @@ -30,7 +31,7 @@ const (
permission = 0o600
serialNumber = 123123
years, months, days = 5, 0, 0
bits = 4096
bits = 2048
)

func GenerateSelfSignedCert(t testing.TB) (keyBytes, certBytes []byte) {
Expand All @@ -44,14 +45,17 @@ func GenerateSelfSignedCert(t testing.TB) (keyBytes, certBytes []byte) {
keyBytes = x509.MarshalPKCS1PrivateKey(key)

tmpl := x509.Certificate{
NotBefore: time.Now(),
NotBefore: time.Now().Add(-10 * time.Minute), // Allow for clock skew
NotAfter: time.Now().AddDate(years, months, days),
SerialNumber: big.NewInt(serialNumber),
Subject: pkix.Name{
CommonName: "New Name",
Organization: []string{"New Org."},
},
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}
certBytes, err = x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &key.PublicKey, key)
if err != nil {
Expand All @@ -69,7 +73,13 @@ func WriteCertFiles(t *testing.T, location string, cert Cert) string {
Bytes: cert.Contents,
})

certFile := fmt.Sprintf("%s%s%s", location, string(os.PathSeparator), cert.Name)
var certFile string
if strings.HasSuffix(location, string(os.PathSeparator)) {
certFile = fmt.Sprintf("%s%s", location, cert.Name)
} else {
certFile = fmt.Sprintf("%s%s%s", location, string(os.PathSeparator), cert.Name)
}

err := os.WriteFile(certFile, pemContents, permission)
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions test/protos/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func CertMeta(fileName, fileHash string) *mpi.FileMeta {
{Type: "customID", Value: "98765"},
},
},

SignatureAlgorithm: mpi.SignatureAlgorithm_SIGNATURE_ALGORITHM_UNKNOWN,
PublicKeyAlgorithm: "",
},
Expand Down

0 comments on commit 9bb2f71

Please sign in to comment.