diff options
author | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2021-06-01 21:11:34 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2021-06-01 21:11:34 +0200 |
commit | 2c2c6936a93ce4f18302426a665ed5782910ab85 (patch) | |
tree | 93dbaa64e7c85990464adc625ffdcd5d48aa098a | |
parent | 17a1cf31764541daa509cf59f1bbb32c25b28c8c (diff) |
re-added all endpoints
To be tested in greater detail.
-rw-r--r-- | endpoint.go | 113 | ||||
-rw-r--r-- | instance.go | 4 | ||||
-rw-r--r-- | request.go | 61 | ||||
-rw-r--r-- | util.go | 15 |
4 files changed, 82 insertions, 111 deletions
diff --git a/endpoint.go b/endpoint.go index 98a5ce8..19ea388 100644 --- a/endpoint.go +++ b/endpoint.go @@ -127,61 +127,58 @@ func getConsistencyProof(ctx context.Context, i *Instance, w http.ResponseWriter return http.StatusOK, nil } -//func getProofByHash(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { -// glog.V(3).Info("handling get-proof-by-hash request") -// req, err := i.LogParameters.parseGetProofByHashV1Request(r) -// if err != nil { -// return http.StatusBadRequest, err -// } -// -// trsp, err := i.Client.GetInclusionProofByHash(ctx, &trillian.GetInclusionProofByHashRequest{ -// LogId: i.LogParameters.TreeId, -// LeafHash: req.Hash[:], -// TreeSize: int64(req.TreeSize), -// OrderBySequence: true, -// }) -// if errInner := checkGetInclusionProofByHash(i.LogParameters, trsp, err); errInner != nil { -// return http.StatusInternalServerError, fmt.Errorf("bad GetInclusionProofByHashResponse: %v", errInner) -// } -// -// if err := writeOctetResponse(w, *types.NewInclusionProofV1(i.LogParameters.LogId, req.TreeSize, uint64(trsp.Proof[0].LeafIndex), NewNodePathFromHashPath(trsp.Proof[0].Hashes))); err != nil { -// return http.StatusInternalServerError, fmt.Errorf("writeOctetResponse: %v", err) -// } -// return http.StatusOK, nil -//} -// -//func getEntries(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { -// glog.V(3).Info("handling get-entries request") -// req, err := i.LogParameters.parseGetEntriesV1Request(r) -// if err != nil { -// return http.StatusBadRequest, err -// } -// -// trsp, err := i.Client.GetLeavesByRange(ctx, &trillian.GetLeavesByRangeRequest{ -// LogId: i.LogParameters.TreeId, -// StartIndex: int64(req.Start), -// Count: int64(req.End-req.Start) + 1, -// }) -// if errInner := checkGetLeavesByRange(req, trsp, err); errInner != nil { -// return http.StatusInternalServerError, fmt.Errorf("checkGetLeavesByRangeResponse: %v", errInner) // there is one StatusBadRequest in here tho.. -// } -// -// if rsp, err := NewStItemListFromLeaves(trsp.Leaves); err != nil { -// return http.StatusInternalServerError, fmt.Errorf("NewStItemListFromLeaves: %v", err) // should never happen -// } else if err := writeOctetResponse(w, *rsp); err != nil { -// return http.StatusInternalServerError, fmt.Errorf("writeOctetResponse: %v", err) -// } -// return http.StatusOK, nil -//} -// -//func writeOctetResponse(w http.ResponseWriter, i interface{}) error { -// b, err := types.Marshal(i) -// if err != nil { -// return fmt.Errorf("Marshal: %v", err) -// } -// w.Header().Set("Content-Type", "application/octet-stream") -// if _, err := w.Write(b); err != nil { -// return fmt.Errorf("Write: %v", err) -// } -// return nil -//} +func getProofByHash(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { + glog.V(3).Info("handling get-proof-by-hash request") + req, err := i.LogParameters.parseGetProofByHashRequest(r) + if err != nil { + return http.StatusBadRequest, err + } + + trsp, err := i.Client.GetInclusionProofByHash(ctx, &trillian.GetInclusionProofByHashRequest{ + LogId: i.LogParameters.TreeId, + LeafHash: req.LeafHash[:], + TreeSize: int64(req.TreeSize), + OrderBySequence: true, + }) + if errInner := checkGetInclusionProofByHash(i.LogParameters, trsp, err); errInner != nil { + return http.StatusInternalServerError, fmt.Errorf("bad GetInclusionProofByHashResponse: %v", errInner) + } + + proof := &types.InclusionProof{ + TreeSize: req.TreeSize, + LeafIndex: uint64(trsp.Proof[0].LeafIndex), + Path: NodePathFromHashes(trsp.Proof[0].Hashes), + } + if err := proof.MarshalASCII(w); err != nil { + return http.StatusInternalServerError, fmt.Errorf("MarshalASCII: %v", err) + } + return http.StatusOK, nil +} + +func getEntries(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { + glog.V(3).Info("handling get-entries request") + req, err := i.LogParameters.parseGetEntriesRequest(r) + if err != nil { + return http.StatusBadRequest, err + } + + trsp, err := i.Client.GetLeavesByRange(ctx, &trillian.GetLeavesByRangeRequest{ + LogId: i.LogParameters.TreeId, + StartIndex: int64(req.StartSize), + Count: int64(req.EndSize-req.StartSize) + 1, + }) + if errInner := checkGetLeavesByRange(req, trsp, err); errInner != nil { + return http.StatusInternalServerError, fmt.Errorf("checkGetLeavesByRangeResponse: %v", errInner) // there is one StatusBadRequest in here tho.. + } + + for _, serialized := range trsp.Leaves { + var leaf types.Leaf + if err := leaf.Unmarshal(serialized.LeafValue); err != nil { + return http.StatusInternalServerError, fmt.Errorf("Unmarshal: %v", err) + } + if err := leaf.MarshalASCII(w); err != nil { + return http.StatusInternalServerError, fmt.Errorf("MarshalASCII: %v", err) + } + } + return http.StatusOK, nil +} diff --git a/instance.go b/instance.go index d11032e..5d358c1 100644 --- a/instance.go +++ b/instance.go @@ -27,9 +27,9 @@ func (i *Instance) Handlers() []Handler { Handler{Instance: i, Handler: getLatestSth, Endpoint: EndpointGetLatestSth, Method: http.MethodGet}, Handler{Instance: i, Handler: getStableSth, Endpoint: EndpointGetStableSth, Method: http.MethodGet}, Handler{Instance: i, Handler: getCosignedSth, Endpoint: EndpointGetCosignedSth, Method: http.MethodGet}, - //Handler{Instance: i, Handler: getProofByHash, Endpoint: EndpointGetProofByHash, Method: http.MethodPost}, + Handler{Instance: i, Handler: getProofByHash, Endpoint: EndpointGetProofByHash, Method: http.MethodPost}, Handler{Instance: i, Handler: getConsistencyProof, Endpoint: EndpointGetConsistencyProof, Method: http.MethodPost}, - //Handler{Instance: i, Handler: getEntries, Endpoint: EndpointGetEntries, Method: http.MethodPost}, + Handler{Instance: i, Handler: getEntries, Endpoint: EndpointGetEntries, Method: http.MethodPost}, } } @@ -54,39 +54,28 @@ func (lp *LogParameters) parseGetConsistencyProofRequest(r *http.Request) (*type return &req, nil } -//func (lp *LogParameters) parseGetProofByHashV1Request(r *http.Request) (*types.GetProofByHashV1, error) { -// var item types.GetProofByHashV1 -// if err := unpackOctetPost(r, &item); err != nil { -// return nil, fmt.Errorf("unpackOctetPost: %v", err) -// } -// if item.TreeSize < 1 { -// return nil, fmt.Errorf("TreeSize(%d) must be larger than zero", item.TreeSize) -// } -// return &item, nil -//} -// -//func (lp *LogParameters) parseGetEntriesV1Request(r *http.Request) (*types.GetEntriesV1, error) { -// var item types.GetEntriesV1 -// if err := unpackOctetPost(r, &item); err != nil { -// return nil, fmt.Errorf("unpackOctetPost: %v", err) -// } -// -// if item.Start > item.End { -// return nil, fmt.Errorf("start(%v) must be less than or equal to end(%v)", item.Start, item.End) -// } -// if item.End-item.Start+1 > uint64(lp.MaxRange) { -// item.End = item.Start + uint64(lp.MaxRange) - 1 -// } -// return &item, nil -//} -// -//func unpackOctetPost(r *http.Request, out interface{}) error { -// body, err := ioutil.ReadAll(r.Body) -// if err != nil { -// return fmt.Errorf("failed reading request body: %v", err) -// } -// if err := types.Unmarshal(body, out); err != nil { -// return fmt.Errorf("Unmarshal: %v", err) -// } -// return nil -//} +func (lp *LogParameters) parseGetProofByHashRequest(r *http.Request) (*types.InclusionProofRequest, error) { + var req types.InclusionProofRequest + if err := req.UnmarshalASCII(r.Body); err != nil { + return nil, fmt.Errorf("UnmarshalASCII: %v", err) + } + if req.TreeSize < 1 { + return nil, fmt.Errorf("TreeSize(%d) must be larger than zero", req.TreeSize) + } + return &req, nil +} + +func (lp *LogParameters) parseGetEntriesRequest(r *http.Request) (*types.LeavesRequest, error) { + var req types.LeavesRequest + if err := req.UnmarshalASCII(r.Body); err != nil { + return nil, fmt.Errorf("UnmarshalASCII: %v", err) + } + + if req.StartSize > req.EndSize { + return nil, fmt.Errorf("StartSize(%d) must be less than or equal to EndSize(%d)", req.StartSize, req.EndSize) + } + if req.EndSize-req.StartSize+1 > uint64(lp.MaxRange) { + req.EndSize = req.StartSize + uint64(lp.MaxRange) - 1 + } + return &req, nil +} @@ -1,9 +1,6 @@ package stfe import ( - //"fmt" - - //"github.com/google/trillian" ttypes "github.com/google/trillian/types" "github.com/system-transparency/stfe/types" ) @@ -28,15 +25,3 @@ func NodePathFromHashes(hashes [][]byte) []*[types.HashSize]byte { } return path } - -//func NewStItemListFromLeaves(leaves []*trillian.LogLeaf) (*types.StItemList, error) { -// items := make([]types.StItem, 0, len(leaves)) -// for _, leaf := range leaves { -// var item types.StItem -// if err := types.Unmarshal(leaf.LeafValue, &item); err != nil { -// return nil, fmt.Errorf("Unmarshal failed: %v", err) -// } -// items = append(items, item) -// } -// return &types.StItemList{items}, nil -//} |