From dd19521190f39a8b1704adb724f5f812040f91e4 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 27 Oct 2020 15:16:24 +0100 Subject: decoupled log instance and info Makes things a bit more modular. As part of this process I also replaced ct/x509 with crypto/x509, which already suits our needs. --- handler.go | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'handler.go') diff --git a/handler.go b/handler.go index 33c6979..6e5fe49 100644 --- a/handler.go +++ b/handler.go @@ -3,6 +3,7 @@ package stfe import ( "context" "fmt" + "time" "net/http" @@ -13,26 +14,26 @@ import ( // appHandler implements the http.Handler interface, and contains a reference // to an STFE server instance as well as a function that uses it. type appHandler struct { - instance *instance // STFE server instance + instance *Instance // STFE server instance endpoint string // e.g., add-entry method string // e.g., GET - handler func(context.Context, *instance, http.ResponseWriter, *http.Request) (int, error) + handler func(context.Context, *Instance, http.ResponseWriter, *http.Request) (int, error) } // ServeHTTP docdoc func (a appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - ctx, cancel := context.WithDeadline(r.Context(), a.instance.timesource.Now().Add(a.instance.deadline)) + ctx, cancel := context.WithDeadline(r.Context(), time.Now().Add(a.instance.Deadline)) defer cancel() if r.Method != a.method { - glog.Warningf("%s: got HTTP %s, wanted HTTP %s", a.instance.prefix+a.endpoint, r.Method, a.method) + glog.Warningf("%s: got HTTP %s, wanted HTTP %s", a.instance.LogParameters.Prefix+a.endpoint, r.Method, a.method) a.sendHTTPError(w, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed: %s", r.Method)) return } statusCode, err := a.handler(ctx, a.instance, w, r) if err != nil { - glog.Warningf("handler error %s/%s: %v", a.instance.prefix, a.endpoint, err) + glog.Warningf("handler error %s/%s: %v", a.instance.LogParameters.Prefix, a.endpoint, err) a.sendHTTPError(w, statusCode, err) } } @@ -42,26 +43,26 @@ func (a appHandler) sendHTTPError(w http.ResponseWriter, statusCode int, err err http.Error(w, http.StatusText(statusCode), statusCode) } -func addEntry(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) { +func addEntry(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.Info("in addEntry") request, err := NewAddEntryRequest(r) if err != nil { return http.StatusBadRequest, err } // request can be decoded - leaf, appendix, err := VerifyAddEntryRequest(i.anchors, request) + leaf, appendix, err := VerifyAddEntryRequest(i.LogParameters, request) if err != nil { return http.StatusBadRequest, err } // valid add-entry request trillianRequest := trillian.QueueLeafRequest{ - LogId: i.logID, + LogId: i.LogParameters.TreeId, Leaf: &trillian.LogLeaf{ LeafValue: leaf, ExtraData: appendix, }, } - trillianResponse, err := i.client.QueueLeaf(ctx, &trillianRequest) + trillianResponse, err := i.Client.QueueLeaf(ctx, &trillianRequest) if err != nil { return http.StatusInternalServerError, fmt.Errorf("backend QueueLeaf request failed: %v", err) } // note: more detail could be provided here, see addChainInternal in ctfe @@ -72,7 +73,7 @@ func addEntry(ctx context.Context, i *instance, w http.ResponseWriter, r *http.R } // getEntries provides a list of entries from the Trillian backend -func getEntries(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) { +func getEntries(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.Info("in getEntries") request, err := NewGetEntriesRequest(r) if err != nil { @@ -80,11 +81,11 @@ func getEntries(ctx context.Context, i *instance, w http.ResponseWriter, r *http } // request can be decoded and is valid trillianRequest := trillian.GetLeavesByRangeRequest{ - LogId: i.logID, + LogId: i.LogParameters.TreeId, StartIndex: request.Start, Count: request.End - request.Start + 1, } - trillianResponse, err := i.client.GetLeavesByRange(ctx, &trillianRequest) + trillianResponse, err := i.Client.GetLeavesByRange(ctx, &trillianRequest) if err != nil { return http.StatusInternalServerError, fmt.Errorf("backend GetLeavesByRange request failed: %v", err) } @@ -113,9 +114,9 @@ func getEntries(ctx context.Context, i *instance, w http.ResponseWriter, r *http } // getAnchors provides a list of configured trust anchors -func getAnchors(_ context.Context, i *instance, w http.ResponseWriter, _ *http.Request) (int, error) { +func getAnchors(_ context.Context, i *Instance, w http.ResponseWriter, _ *http.Request) (int, error) { glog.Info("in getAnchors") - data := NewGetAnchorsResponse(i.anchorsPool.RawCertificates()) + data := NewGetAnchorsResponse(i.LogParameters.AnchorList) if err := WriteJsonResponse(data, w); err != nil { return http.StatusInternalServerError, err } @@ -123,7 +124,7 @@ func getAnchors(_ context.Context, i *instance, w http.ResponseWriter, _ *http.R } // getProofByHash provides an inclusion proof based on a given leaf hash -func getProofByHash(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) { +func getProofByHash(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.Info("in getProofByHash") request, err := NewGetProofByHashRequest(r) if err != nil { @@ -131,12 +132,12 @@ func getProofByHash(ctx context.Context, i *instance, w http.ResponseWriter, r * } // request can be decoded and is valid trillianRequest := trillian.GetInclusionProofByHashRequest{ - LogId: i.logID, + LogId: i.LogParameters.TreeId, LeafHash: request.Hash, TreeSize: request.TreeSize, OrderBySequence: true, } - trillianResponse, err := i.client.GetInclusionProofByHash(ctx, &trillianRequest) + trillianResponse, err := i.Client.GetInclusionProofByHash(ctx, &trillianRequest) if err != nil { return http.StatusInternalServerError, fmt.Errorf("failed fetching inclusion proof from Trillian backend: %v", err) } @@ -159,13 +160,13 @@ func getProofByHash(ctx context.Context, i *instance, w http.ResponseWriter, r * } // getConsistencyProof provides a consistency proof between two STHs -func getConsistencyProof(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) { +func getConsistencyProof(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.Info("in getConsistencyProof") return http.StatusOK, nil // TODO } // getSth provides the most recent STH -func getSth(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) { +func getSth(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.Info("in getSth") return http.StatusOK, nil // TODO } -- cgit v1.2.3