Skip to content

Commit

Permalink
temp: rename functions and add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Nov 19, 2024
1 parent c723e89 commit 84c87b0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
26 changes: 13 additions & 13 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func (db *Database) RetrieveAllCSRs() ([]CertificateRequest, error) {
return csrs, nil
}

// RetrieveCSRbyID gets a given CSR from the repository.
// It returns the row id and matching certificate alongside the CSR in a CertificateRequest object.
// RetrieveCSRbyID gets a CSR row from the repository from a given ID.
func (db *Database) RetrieveCSRbyID(id int) (*CertificateRequest, error) {
csr := CertificateRequest{
ID: id,
Expand All @@ -115,8 +114,7 @@ func (db *Database) RetrieveCSRbyID(id int) (*CertificateRequest, error) {
return &csr, nil
}

// RetrieveCSRbyCSR gets a given CSR from the repository TODO.
// It returns the row id and matching certificate alongside the CSR in a CertificateRequest object.
// RetrieveCSRbyCSR gets a given CSR row from the repository using the CSR text.
func (db *Database) RetrieveCSRbyCSR(csr string) (*CertificateRequest, error) {
row := CertificateRequest{
CSR: csr,
Expand All @@ -132,8 +130,7 @@ func (db *Database) RetrieveCSRbyCSR(csr string) (*CertificateRequest, error) {
return &row, nil
}

// CreateCSR creates a new entry in the repository.
// The given CSR must be valid and unique
// CreateCSR creates a new CSR entry in the repository. The string must be a valid CSR and unique.
func (db *Database) CreateCSR(csr string) error {
if err := ValidateCertificateRequest(csr); err != nil {
return errors.New("csr validation failed: " + err.Error())
Expand All @@ -152,9 +149,8 @@ func (db *Database) CreateCSR(csr string) error {
return nil
}

// UpdateCSR adds a new cert to the given CSR in the repository.
// The given certificate must share the public key of the CSR and must be valid.
func (db *Database) AddCertificateToCSRbyCSR(csr string, cert string) error {
// AddCertificateChainToCSRbyCSR adds a new certificate chain to a row for a given CSR string.
func (db *Database) AddCertificateChainToCSRbyCSR(csr string, cert string) error {
err := ValidateCertificate(cert)
if err != nil {
return errors.New("cert validation failed: " + err.Error())
Expand All @@ -180,6 +176,7 @@ func (db *Database) AddCertificateToCSRbyCSR(csr string, cert string) error {
return nil
}

// AddCertificateChainToCSRbyID adds a new certificate chain to a row for a given row ID.
func (db *Database) AddCertificateToCSRbyID(id int, cert string) error {
csr, err := db.RetrieveCSRbyID(id)
if err != nil {
Expand Down Expand Up @@ -210,6 +207,7 @@ func (db *Database) AddCertificateToCSRbyID(id int, cert string) error {
return nil
}

// RejectCSRbyCSR updates input CSR's row by setting the certificate bundle to "" and moving the row status to "Rejected".
func (db *Database) RejectCSRbyCSR(csr string) error {
oldRow, err := db.RetrieveCSRbyCSR(csr)
if err != nil {
Expand All @@ -232,6 +230,7 @@ func (db *Database) RejectCSRbyCSR(csr string) error {
return nil
}

// RejectCSRbyCSR updates input ID's row by setting the certificate bundle to "" and sets the row status to "Rejected".
func (db *Database) RejectCSRbyID(id int) error {
oldRow, err := db.RetrieveCSRbyID(id)
if err != nil {
Expand All @@ -254,6 +253,7 @@ func (db *Database) RejectCSRbyID(id int) error {
return nil
}

// RevokeCSR updates the input CSR's row by setting the certificate bundle to "" and sets the row status to "Revoked".
func (db *Database) RevokeCSR(csr string) error {
oldRow, err := db.RetrieveCSRbyCSR(csr)
if err != nil {
Expand All @@ -276,8 +276,8 @@ func (db *Database) RevokeCSR(csr string) error {
return nil
}

// DeleteCSR removes a CSR from the database alongside the certificate that may have been generated for it.
func (db *Database) DeleteCSR(csr string) error {
// DeleteCSRbyCSR removes a CSR from the database alongside the certificate that may have been generated for it.
func (db *Database) DeleteCSRbyCSR(csr string) error {
stmt, err := sqlair.Prepare(fmt.Sprintf(deleteCSRStmt, db.certificateTable), CertificateRequest{})
if err != nil {
return err
Expand Down Expand Up @@ -404,8 +404,8 @@ func (db *Database) UpdateUserPassword(id int, password string) error {
return nil
}

// DeleteUser removes a user from the table.
func (db *Database) DeleteUser(id int) error {
// DeleteUserByID removes a user from the table.
func (db *Database) DeleteUserByID(id int) error {
_, err := db.RetrieveUserByID(id)
if err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions internal/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func TestCSRsEndToEnd(t *testing.T) {
t.Fatalf("The CSR from the database doesn't match the CSR that was given")
}

if err = db.DeleteCSR(AppleCSR); err != nil {
if err = db.DeleteCSRbyCSR(AppleCSR); err != nil {
t.Fatalf("Couldn't complete Delete: %s", err)
}
res, _ = db.RetrieveAllCSRs()
if len(res) != 2 {
t.Fatalf("CSR's weren't deleted from the DB properly")
}
BananaCertBundle := strings.TrimSpace(fmt.Sprintf("%s%s", BananaCert, IssuerCert))
err = db.AddCertificateToCSRbyCSR(BananaCSR, BananaCertBundle)
err = db.AddCertificateChainToCSRbyCSR(BananaCSR, BananaCertBundle)
if err != nil {
t.Fatalf("Couldn't complete Update: %s", err)
}
Expand Down Expand Up @@ -108,10 +108,10 @@ func TestUpdateFails(t *testing.T) {
db.CreateCSR(AppleCSR) //nolint:errcheck
db.CreateCSR(BananaCSR) //nolint:errcheck
InvalidCert := strings.ReplaceAll(BananaCert, "/", "+")
if err := db.AddCertificateToCSRbyCSR(BananaCSR, InvalidCert); err == nil {
if err := db.AddCertificateChainToCSRbyCSR(BananaCSR, InvalidCert); err == nil {
t.Fatalf("Expected updating with invalid cert to fail")
}
if err := db.AddCertificateToCSRbyCSR(AppleCSR, BananaCert); err == nil {
if err := db.AddCertificateChainToCSRbyCSR(AppleCSR, BananaCert); err == nil {
t.Fatalf("Expected updating with mismatched cert to fail")
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestUsersEndToEnd(t *testing.T) {
if err := bcrypt.CompareHashAndPassword([]byte(retrievedUser.HashedPassword), []byte("pw123")); err != nil {
t.Fatalf("The user's password doesn't match the one stored in the database")
}
if err = db.DeleteUser(1); err != nil {
if err = db.DeleteUserByID(1); err != nil {
t.Fatalf("Couldn't complete Delete: %s", err)
}
res, _ = db.RetrieveAllUsers()
Expand All @@ -202,7 +202,7 @@ func Example() {
if err != nil {
log.Fatalln(err)
}
err = db.AddCertificateToCSRbyCSR(BananaCSR, BananaCert)
err = db.AddCertificateChainToCSRbyCSR(BananaCSR, BananaCert)
if err != nil {
log.Fatalln(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func initializeTestDB(t *testing.T, db *db.Database) {
if err != nil {
t.Fatalf("couldn't create test csr: %s", err)
}
err = db.AddCertificateToCSRbyCSR(csr, fmt.Sprintf("%s%s", cert, ca))
err = db.AddCertificateChainToCSRbyCSR(csr, fmt.Sprintf("%s%s", cert, ca))
if err != nil {
t.Fatalf("couldn't create test cert: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/handlers_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func DeleteAccount(env *HandlerConfig) http.HandlerFunc {
writeError(w, http.StatusBadRequest, "deleting an Admin account is not allowed.")
return
}
err = env.DB.DeleteUser(idInt)
err = env.DB.DeleteUserByID(idInt)
if err != nil {
log.Println(err)
if errors.Is(err, sqlair.ErrNoRows) {
Expand Down

0 comments on commit 84c87b0

Please sign in to comment.