Skip to content

Commit

Permalink
removed pointers all around
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Mar 28, 2024
1 parent 700a1f4 commit de22f27
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
14 changes: 7 additions & 7 deletions internal/certdb/certdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ type CertificateRequest struct {
Certificate *string
}

func (db *CertificateRequests) RetrieveAll() ([]*CertificateRequest, error) {
func (db *CertificateRequests) RetrieveAll() ([]CertificateRequest, error) {
rows, err := db.conn.Query(fmt.Sprintf(queryGetAllCSRs, db.table))
if err != nil {
return nil, err
}

var allCsrs []*CertificateRequest
var allCsrs []CertificateRequest
defer rows.Close()
for rows.Next() {
var csr CertificateRequest
if err := rows.Scan(&csr.ID, &csr.CSR, &csr.Certificate); err != nil {
return nil, err
}
allCsrs = append(allCsrs, &csr)
allCsrs = append(allCsrs, csr)
}
return allCsrs, nil
}

func (db *CertificateRequests) Retrieve(csr *string) (*CertificateRequest, error) {
func (db *CertificateRequests) Retrieve(csr string) (*CertificateRequest, error) {
var newCSR CertificateRequest
row := db.conn.QueryRow(fmt.Sprintf(queryGetCSR, db.table), csr)
if err := row.Scan(&newCSR.ID, &newCSR.CSR, &newCSR.Certificate); err != nil {
Expand All @@ -53,7 +53,7 @@ func (db *CertificateRequests) Retrieve(csr *string) (*CertificateRequest, error
return &newCSR, nil
}

func (db *CertificateRequests) Create(csr *string) (int64, error) {
func (db *CertificateRequests) Create(csr string) (int64, error) {
if err := ValidateCertificateRequest(csr); err != nil {
return 0, err
}
Expand All @@ -68,7 +68,7 @@ func (db *CertificateRequests) Create(csr *string) (int64, error) {
return id, nil
}

func (db *CertificateRequests) Update(csr *string, cert *string) (int64, error) {
func (db *CertificateRequests) Update(csr string, cert string) (int64, error) {
if err := ValidateCertificate(cert, csr); err != nil {
return 0, err
}
Expand All @@ -83,7 +83,7 @@ func (db *CertificateRequests) Update(csr *string, cert *string) (int64, error)
return id, nil
}

func (db *CertificateRequests) Delete(csr *string) error {
func (db *CertificateRequests) Delete(csr string) error {
_, err := db.conn.Exec(fmt.Sprintf(queryDeleteCSR, db.table), csr)
if err != nil {
return err
Expand Down
32 changes: 16 additions & 16 deletions internal/certdb/certdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func TestEndToEnd(t *testing.T) {
defer db.Disconnect()
db.Connect(":memory:", "CertificateRequests")

if _, err := db.Create(&ValidCSR1); err != nil {
if _, err := db.Create(ValidCSR1); err != nil {
t.Fatalf("Couldn't complete Create: %s", err)
}
if _, err := db.Create(&ValidCSR2); err != nil {
if _, err := db.Create(ValidCSR2); err != nil {
t.Fatalf("Couldn't complete Create: %s", err)
}
if _, err := db.Create(&ValidCSR3); err != nil {
if _, err := db.Create(ValidCSR3); err != nil {
t.Fatalf("Couldn't complete Create: %s", err)
}

Expand All @@ -38,27 +38,27 @@ func TestEndToEnd(t *testing.T) {
if len(res) != 3 {
t.Fatalf("One or more CSRs weren't found in DB")
}
retrievedCSR, err := db.Retrieve(&ValidCSR1)
retrievedCSR, err := db.Retrieve(ValidCSR1)
if err != nil {
t.Fatalf("Couldn't complete Retrieve: %s", err)
}
if retrievedCSR.CSR != ValidCSR1 {
t.Fatalf("The CSR from the database doesn't match the CSR that was given")
}

if err = db.Delete(&ValidCSR1); err != nil {
if err = db.Delete(ValidCSR1); err != nil {
t.Fatalf("Couldn't complete Delete: %s", err)
}
res, _ = db.RetrieveAll()
if len(res) != 2 {
t.Fatalf("CSR's weren't deleted from the DB properly")
}

_, err = db.Update(&ValidCSR2, &ValidCert2)
_, err = db.Update(ValidCSR2, ValidCert2)
if err != nil {
t.Fatalf("Couldn't complete Update: %s", err)
}
retrievedCSR, _ = db.Retrieve(&ValidCSR2)
retrievedCSR, _ = db.Retrieve(ValidCSR2)
if *retrievedCSR.Certificate != ValidCert2 {
t.Fatalf("The certificate that was uploaded does not match the certificate that was given: Retrieved: %s\nGiven: %s", *retrievedCSR.Certificate, ValidCert2)
}
Expand All @@ -70,12 +70,12 @@ func TestCreateFails(t *testing.T) {
defer db.Disconnect()

InvalidCSR := strings.ReplaceAll(ValidCSR1, "/", "+")
if _, err := db.Create(&InvalidCSR); err == nil {
if _, err := db.Create(InvalidCSR); err == nil {
t.Fatalf("Expected error due to invalid CSR")
}

db.Create(&ValidCSR1)
if _, err := db.Create(&ValidCSR1); err == nil {
db.Create(ValidCSR1)
if _, err := db.Create(ValidCSR1); err == nil {
t.Fatalf("Expected error due to duplicate CSR")
}
}
Expand All @@ -85,13 +85,13 @@ func TestUpdateFails(t *testing.T) {
defer db.Disconnect()
db.Connect(":memory:", "CertificateRequests")

db.Create(&ValidCSR1)
db.Create(&ValidCSR2)
db.Create(ValidCSR1)
db.Create(ValidCSR2)
InvalidCert := strings.ReplaceAll(ValidCert2, "/", "+")
if _, err := db.Update(&ValidCSR2, &InvalidCert); err == nil {
if _, err := db.Update(ValidCSR2, InvalidCert); err == nil {
t.Fatalf("Expected updating with invalid cert to fail")
}
if _, err := db.Update(&ValidCSR1, &ValidCert2); err == nil {
if _, err := db.Update(ValidCSR1, ValidCert2); err == nil {
t.Fatalf("Expected updating with mismatched cert to fail")
}
}
Expand All @@ -101,8 +101,8 @@ func TestRetrieve(t *testing.T) {
defer db.Disconnect()
db.Connect(":memory:", "CertificateRequests")

db.Create(&ValidCSR1)
if _, err := db.Retrieve(&ValidCSR2); err == nil {
db.Create(ValidCSR1)
if _, err := db.Retrieve(ValidCSR2); err == nil {
t.Fatalf("Expected failure looking for nonexistent CSR")
}

Expand Down
10 changes: 5 additions & 5 deletions internal/certdb/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"errors"
)

func ValidateCertificateRequest(csrString *string) error {
block, _ := pem.Decode([]byte(*csrString))
func ValidateCertificateRequest(csrString string) error {
block, _ := pem.Decode([]byte(csrString))
if block == nil {
return errors.New("PEM Certificate Request string not found or malformed")
}
Expand All @@ -22,13 +22,13 @@ func ValidateCertificateRequest(csrString *string) error {
return nil
}

func ValidateCertificate(certString *string, csrString *string) error {
func ValidateCertificate(certString string, csrString string) error {
if err := ValidateCertificateRequest(csrString); err != nil {
return err
}
csrBlock, _ := pem.Decode([]byte(*csrString))
csrBlock, _ := pem.Decode([]byte(csrString))
csr, _ := x509.ParseCertificateRequest(csrBlock.Bytes)
certBlock, _ := pem.Decode([]byte(*certString))
certBlock, _ := pem.Decode([]byte(certString))
if certBlock == nil {
return errors.New("PEM Certificate string not found or malformed")
}
Expand Down
26 changes: 13 additions & 13 deletions internal/certdb/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,35 @@ Mvo/+PAJHkBciR5Xn+Wg2a+7vrZvT6CBoRSOTozlLSM=
-----END CERTIFICATE-----`

func TestCSRValidationSuccess(t *testing.T) {
if err := certdb.ValidateCertificateRequest(&ValidCSR1); err != nil {
if err := certdb.ValidateCertificateRequest(ValidCSR1); err != nil {
t.Fatalf("Couldn't verify valid CSR: %s", err)
}
if err := certdb.ValidateCertificateRequest(&ValidCSR2); err != nil {
if err := certdb.ValidateCertificateRequest(ValidCSR2); err != nil {
t.Fatalf("Couldn't verify valid CSR: %s", err)
}
if err := certdb.ValidateCertificateRequest(&ValidCSR3); err != nil {
if err := certdb.ValidateCertificateRequest(ValidCSR3); err != nil {
t.Fatalf("Couldn't verify valid CSR: %s", err)
}
}

func TestCSRValidationFail(t *testing.T) {
var wrongString string = "this is a real csr!!!"
err := certdb.ValidateCertificateRequest(&wrongString)
err := certdb.ValidateCertificateRequest(wrongString)
if err.Error() != "PEM Certificate Request string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var ValidCSRWithoutWhitespace = strings.ReplaceAll(ValidCSR1, "\n", "")
err = certdb.ValidateCertificateRequest(&ValidCSRWithoutWhitespace)
err = certdb.ValidateCertificateRequest(ValidCSRWithoutWhitespace)
if err.Error() != "PEM Certificate Request string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var wrongPemType string = strings.ReplaceAll(ValidCSR1, "CERTIFICATE REQUEST", "SOME RANDOM PEM TYPE")
err = certdb.ValidateCertificateRequest(&wrongPemType)
err = certdb.ValidateCertificateRequest(wrongPemType)
if err.Error() != "given PEM string not a certificate request" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var InvalidCSR = strings.ReplaceAll(ValidCSR1, "/", "p")
err = certdb.ValidateCertificateRequest(&InvalidCSR)
err = certdb.ValidateCertificateRequest(InvalidCSR)
if err == nil {
t.Fatalf("Expected CSR to fail validation")
}
Expand All @@ -124,33 +124,33 @@ func TestCSRValidationFail(t *testing.T) {
// Fuzz test

func TestCertValidationSuccess(t *testing.T) {
if err := certdb.ValidateCertificate(&ValidCert2, &ValidCSR2); err != nil {
if err := certdb.ValidateCertificate(ValidCert2, ValidCSR2); err != nil {
t.Fatalf("Expected cert to be valid")
}
}

func TestCertValidationFail(t *testing.T) {
var wrongString string = "this is a real cert!!!"
err := certdb.ValidateCertificate(&wrongString, &ValidCSR2)
err := certdb.ValidateCertificate(wrongString, ValidCSR2)
if err.Error() != "PEM Certificate string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var ValidCertWithoutWhitespace = strings.ReplaceAll(ValidCert2, "\n", "")
err = certdb.ValidateCertificate(&ValidCertWithoutWhitespace, &ValidCSR2)
err = certdb.ValidateCertificate(ValidCertWithoutWhitespace, ValidCSR2)
if err.Error() != "PEM Certificate string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var wrongPemType string = strings.ReplaceAll(ValidCert2, "CERTIFICATE", "SOME RANDOM PEM TYPE")
err = certdb.ValidateCertificate(&wrongPemType, &ValidCSR2)
err = certdb.ValidateCertificate(wrongPemType, ValidCSR2)
if err.Error() != "given PEM string not a certificate" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var InvalidCert = strings.ReplaceAll(ValidCert2, "M", "i")
err = certdb.ValidateCertificate(&InvalidCert, &ValidCSR2)
err = certdb.ValidateCertificate(InvalidCert, ValidCSR2)
if err == nil {
t.Fatalf("Expected cert to fail validation")
}
err = certdb.ValidateCertificate(&ValidCert2, &ValidCSR1)
err = certdb.ValidateCertificate(ValidCert2, ValidCSR1)
if err == nil || err.Error() != "certificate does not match CSR" {
t.Fatalf("Expected cert to not match CSR")
}
Expand Down

0 comments on commit de22f27

Please sign in to comment.