From 237fcdde13c66b48805c5f9857aabbe9bf6ada8b Mon Sep 17 00:00:00 2001 From: Al Cutter Date: Tue, 28 Jan 2025 10:59:32 +0000 Subject: [PATCH] Remove local HTTP Update support for now --- internal/http/server.go | 52 --------------------- internal/http/server_test.go | 90 ------------------------------------ 2 files changed, 142 deletions(-) diff --git a/internal/http/server.go b/internal/http/server.go index 5423e60..3834288 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -18,7 +18,6 @@ package http import ( "encoding/json" "fmt" - "io" "net/http" "github.com/gorilla/mux" @@ -41,56 +40,6 @@ func NewServer(witness *witness.Witness) *Server { } } -// update handles requests to update checkpoints. -// It expects a POSTed body containing a JSON-formatted api.UpdateRequest -// statement and returns a JSON-formatted api.UpdateResponse statement. -func (s *Server) update(w http.ResponseWriter, r *http.Request) { - v := mux.Vars(r) - logID := v["logid"] - body, err := io.ReadAll(r.Body) - if err != nil { - http.Error(w, fmt.Sprintf("cannot read request body: %v", err.Error()), http.StatusBadRequest) - return - } - var req api.UpdateRequest - if err := json.Unmarshal(body, &req); err != nil { - http.Error(w, fmt.Sprintf("cannot parse request body as proper JSON struct: %v", err.Error()), http.StatusBadRequest) - return - } - // Get the output from the witness. - chkpt, err := s.w.Update(r.Context(), logID, req.OldSize, req.Checkpoint, req.Proof) - if err != nil { - switch err { - case witness.ErrCheckpointStale: - w.Header().Set("X-Content-Type-Options", "nosniff") - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - w.WriteHeader(http.StatusConflict) - if _, err := w.Write(chkpt); err != nil { - klog.Warningf("Error writing response: %v", err) - } - case witness.ErrUnknownLog: - http.Error(w, err.Error(), http.StatusNotFound) - case witness.ErrNoValidSignature: - http.Error(w, err.Error(), http.StatusForbidden) - case witness.ErrOldSizeInvalid: - http.Error(w, err.Error(), http.StatusBadRequest) - case witness.ErrInvalidProof: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - case witness.ErrRootMismatch: - http.Error(w, err.Error(), http.StatusConflict) - default: - http.Error(w, err.Error(), http.StatusInternalServerError) - } - return - } - - w.Header().Set("X-Content-Type-Options", "nosniff") - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - if _, err := w.Write(chkpt); err != nil { - klog.Warningf("Error writing response: %v", err) - } -} - // getCheckpoint returns a checkpoint stored for a given log. func (s *Server) getCheckpoint(w http.ResponseWriter, r *http.Request) { v := mux.Vars(r) @@ -129,7 +78,6 @@ func (s *Server) getLogs(w http.ResponseWriter, r *http.Request) { func (s *Server) RegisterHandlers(r *mux.Router) { logStr := "{logid:[a-zA-Z0-9-]+}" r.HandleFunc(fmt.Sprintf(api.HTTPGetCheckpoint, logStr), s.getCheckpoint).Methods(http.MethodGet) - r.HandleFunc(fmt.Sprintf(api.HTTPUpdate, logStr), s.update).Methods(http.MethodPut) r.HandleFunc(api.HTTPGetLogs, s.getLogs).Methods(http.MethodGet) } diff --git a/internal/http/server_test.go b/internal/http/server_test.go index 0510e94..317c859 100644 --- a/internal/http/server_test.go +++ b/internal/http/server_test.go @@ -15,7 +15,6 @@ package http import ( - "bytes" "context" "encoding/hex" "encoding/json" @@ -24,7 +23,6 @@ import ( "net/http" "net/http/httptest" "sort" - "strings" "testing" "github.com/gorilla/mux" @@ -262,91 +260,3 @@ func TestGetChkpt(t *testing.T) { }) } } - -func TestUpdate(t *testing.T) { - for _, test := range []struct { - desc string - initC []byte - body api.UpdateRequest - wantStatus int - wantCP []byte - }{ - { - desc: "vanilla consistency happy path", - initC: mInit, - body: api.UpdateRequest{OldSize: 5, Checkpoint: mNext, Proof: consProof}, - wantStatus: http.StatusOK, - }, { - desc: "oldSize > submitted checkpoint size", - initC: mInit, - body: api.UpdateRequest{OldSize: 10, Checkpoint: mNext, Proof: consProof}, - wantStatus: http.StatusBadRequest, - }, { - desc: "client smaller oldSize than witness", - initC: mInit, - body: api.UpdateRequest{OldSize: 4, Checkpoint: mNext, Proof: consProof}, - wantStatus: http.StatusConflict, - wantCP: mInit, - }, { - desc: "vanilla consistency garbage proof", - initC: mInit, - body: api.UpdateRequest{OldSize: 5, Checkpoint: mNext, Proof: [][]byte{ - dh("aaaa", 2), - dh("bbbb", 2), - dh("cccc", 2), - dh("dddd", 2), - }}, - wantStatus: http.StatusUnprocessableEntity, - }, { - desc: "vanilla consistency garbage checkpoint", - initC: mInit, - body: api.UpdateRequest{OldSize: 5, Checkpoint: []byte("aaa"), Proof: consProof}, - wantStatus: http.StatusForbidden, - }, - } { - t.Run(test.desc, func(t *testing.T) { - ctx := context.Background() - logID := "monkeys" - // Set up witness. - w := newWitness(t, []logOpts{{ - ID: logID, - origin: logOrigin, - PK: mPK, - }}) - // Set an initial checkpoint for the log. - if _, err := w.Update(ctx, logID, 0, test.initC, nil); err != nil { - t.Errorf("failed to set checkpoint: %v", err) - } - // Now set up the http server. - ts, tsCloseFn := createTestEnv(w) - defer tsCloseFn() - // Update to a newer checkpoint. - client := ts.Client() - reqBody, err := json.Marshal(test.body) - if err != nil { - t.Fatalf("couldn't parse request: %v", err) - } - url := fmt.Sprintf("%s%s", ts.URL, fmt.Sprintf(api.HTTPUpdate, logID)) - req, err := http.NewRequest(http.MethodPut, url, strings.NewReader(string(reqBody))) - if err != nil { - t.Fatalf("couldn't form http request: %v", err) - } - resp, err := client.Do(req) - if err != nil { - t.Errorf("error response: %v", err) - } - if got, want := resp.StatusCode, test.wantStatus; got != want { - body, _ := io.ReadAll(resp.Body) - t.Errorf("status code got %s, want %d (%s)", resp.Status, want, body) - } - defer resp.Body.Close() - respBody, err := io.ReadAll(resp.Body) - if err != nil { - t.Errorf("failed to read response body: %v", err) - } - if test.wantCP != nil && !bytes.HasPrefix(respBody, test.wantCP) { - t.Errorf("got body:\n%s\nbut want:\n%s", respBody, test.wantCP) - } - }) - } -}