From a25faccfce61a8b021e67ada363aad38565c858f Mon Sep 17 00:00:00 2001 From: kayra1 Date: Tue, 4 Jun 2024 04:04:03 +0300 Subject: [PATCH] first try kinda confused about this --- .github/workflows/build-rock.yaml | 5 +++++ internal/api/handlers.go | 21 +++++++++++++++------ internal/certdb/certdb.go | 7 +++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-rock.yaml b/.github/workflows/build-rock.yaml index 713d738..966c52f 100644 --- a/.github/workflows/build-rock.yaml +++ b/.github/workflows/build-rock.yaml @@ -38,6 +38,11 @@ jobs: run: | sleep 30 curl -k https://localhost:3000 2>&1 | grep GoCert + + - name: Test if pebble notify fires correctly + id: notify_test + run : | + echo placeholder - uses: actions/upload-artifact@v4 if: steps.test_image.outcome == 'success' diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 5764198..78ef257 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -168,9 +168,12 @@ func PostCertificate(env *Environment) http.HandlerFunc { strings.Contains(err.Error(), "cert validation failed") { logErrorAndWriteResponse(err.Error(), http.StatusBadRequest, w) return + }else if strings.HasPrefix(err.Error(), "couldn't execute a pebble notify"){ + log.Println("pebble notify failed. Silently continuing.") + }else { + logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w) + return } - logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w) - return } w.WriteHeader(http.StatusCreated) if _, err := w.Write([]byte(strconv.FormatInt(insertId, 10))); err != nil { @@ -187,9 +190,12 @@ func RejectCertificate(env *Environment) http.HandlerFunc { if err.Error() == "csr id not found" { logErrorAndWriteResponse(err.Error(), http.StatusBadRequest, w) return + }else if strings.HasPrefix(err.Error(), "couldn't execute a pebble notify"){ + log.Println("pebble notify failed. Silently continuing.") + } else { + logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w) + return } - logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w) - return } w.WriteHeader(http.StatusAccepted) if _, err := w.Write([]byte(strconv.FormatInt(insertId, 10))); err != nil { @@ -208,9 +214,12 @@ func DeleteCertificate(env *Environment) http.HandlerFunc { if err.Error() == "csr id not found" { logErrorAndWriteResponse(err.Error(), http.StatusBadRequest, w) return + }else if strings.HasPrefix(err.Error(), "couldn't execute a pebble notify"){ + log.Println("pebble notify failed. Silently continuing.") + }else{ + logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w) + return } - logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w) - return } w.WriteHeader(http.StatusAccepted) if _, err := w.Write([]byte(strconv.FormatInt(insertId, 10))); err != nil { diff --git a/internal/certdb/certdb.go b/internal/certdb/certdb.go index 17e262d..cb558b0 100644 --- a/internal/certdb/certdb.go +++ b/internal/certdb/certdb.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "os/exec" _ "github.com/mattn/go-sqlite3" ) @@ -106,6 +107,12 @@ func (db *CertificateRequestsRepository) Update(id string, cert string) (int64, if err != nil { return 0, err } + if insertId != 0{ + cmd := exec.Command("pebble", "notify", "gocert/certificate/create", fmt.Sprintf("request_id=%d", insertId)) + if err := cmd.Run(); err != nil { + return insertId, errors.Join(errors.New("couldn't execute a pebble notify: "), err) + } + } return insertId, nil }