Skip to content

Commit

Permalink
Remove local HTTP Update support for now
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Jan 28, 2025
1 parent 3fb247c commit 237fcdd
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 142 deletions.
52 changes: 0 additions & 52 deletions internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package http
import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/gorilla/mux"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
90 changes: 0 additions & 90 deletions internal/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package http

import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
Expand All @@ -24,7 +23,6 @@ import (
"net/http"
"net/http/httptest"
"sort"
"strings"
"testing"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 237fcdd

Please sign in to comment.