From 752bf9d8e1f78b54aaefdfb90f9f1f0391cd4b6d Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 26 Apr 2022 20:18:19 +0200 Subject: use http get for get-* endpoints with input https://git.sigsum.org/sigsum/tree/doc/proposals/2022-01-get-endpoints XXX: fix go.mod after merge in sigsum-go, now rgdd's local path --- pkg/instance/handler_test.go | 56 +++++++++++++++----------------------------- pkg/instance/instance.go | 12 +++++----- 2 files changed, 25 insertions(+), 43 deletions(-) (limited to 'pkg/instance') diff --git a/pkg/instance/handler_test.go b/pkg/instance/handler_test.go index 46a99d5..b3af666 100644 --- a/pkg/instance/handler_test.go +++ b/pkg/instance/handler_test.go @@ -420,15 +420,9 @@ func TestGetTreeCosigned(t *testing.T) { } func TestGetConsistencyProof(t *testing.T) { - buf := func(oldSize, newSize int) io.Reader { - return bytes.NewBufferString(fmt.Sprintf("%s=%d\n%s=%d\n", - "old_size", oldSize, - "new_size", newSize, - )) - } for _, table := range []struct { description string - ascii io.Reader // buffer used to populate HTTP request + params string // params is the query's url params expect bool // set if a mock answer is expected rsp *types.ConsistencyProof // consistency proof from Trillian client err error // error from Trillian client @@ -436,29 +430,29 @@ func TestGetConsistencyProof(t *testing.T) { }{ { description: "invalid: bad request (parser error)", - ascii: bytes.NewBufferString("key=value\n"), + params: "a/1", wantCode: http.StatusBadRequest, }, { description: "invalid: bad request (OldSize is zero)", - ascii: buf(0, 1), + params: "0/1", wantCode: http.StatusBadRequest, }, { description: "invalid: bad request (OldSize > NewSize)", - ascii: buf(2, 1), + params: "2/1", wantCode: http.StatusBadRequest, }, { description: "invalid: backend failure", - ascii: buf(1, 2), + params: "1/2", expect: true, err: fmt.Errorf("something went wrong"), wantCode: http.StatusInternalServerError, }, { description: "valid", - ascii: buf(1, 2), + params: "1/2", expect: true, rsp: &types.ConsistencyProof{ OldSize: 1, @@ -485,7 +479,7 @@ func TestGetConsistencyProof(t *testing.T) { // Create HTTP request url := types.EndpointGetConsistencyProof.Path("http://example.com", i.Prefix) - req, err := http.NewRequest("POST", url, table.ascii) + req, err := http.NewRequest(http.MethodGet, url+table.params, nil) if err != nil { t.Fatalf("must create http request: %v", err) } @@ -501,15 +495,9 @@ func TestGetConsistencyProof(t *testing.T) { } func TestGetInclusionProof(t *testing.T) { - buf := func(hash *types.Hash, treeSize int) io.Reader { - return bytes.NewBufferString(fmt.Sprintf("%s=%x\n%s=%d\n", - "leaf_hash", hash[:], - "tree_size", treeSize, - )) - } for _, table := range []struct { description string - ascii io.Reader // buffer used to populate HTTP request + params string // params is the query's url params expect bool // set if a mock answer is expected rsp *types.InclusionProof // inclusion proof from Trillian client err error // error from Trillian client @@ -517,24 +505,24 @@ func TestGetInclusionProof(t *testing.T) { }{ { description: "invalid: bad request (parser error)", - ascii: bytes.NewBufferString("key=value\n"), + params: "a/0000000000000000000000000000000000000000000000000000000000000000", wantCode: http.StatusBadRequest, }, { description: "invalid: bad request (no proof for tree size)", - ascii: buf(types.HashFn([]byte{}), 1), + params: "1/0000000000000000000000000000000000000000000000000000000000000000", wantCode: http.StatusBadRequest, }, { description: "invalid: backend failure", - ascii: buf(types.HashFn([]byte{}), 2), + params: "2/0000000000000000000000000000000000000000000000000000000000000000", expect: true, err: fmt.Errorf("something went wrong"), wantCode: http.StatusInternalServerError, }, { description: "valid", - ascii: buf(types.HashFn([]byte{}), 2), + params: "2/0000000000000000000000000000000000000000000000000000000000000000", expect: true, rsp: &types.InclusionProof{ TreeSize: 2, @@ -561,7 +549,7 @@ func TestGetInclusionProof(t *testing.T) { // Create HTTP request url := types.EndpointGetInclusionProof.Path("http://example.com", i.Prefix) - req, err := http.NewRequest("POST", url, table.ascii) + req, err := http.NewRequest(http.MethodGet, url+table.params, nil) if err != nil { t.Fatalf("must create http request: %v", err) } @@ -577,15 +565,9 @@ func TestGetInclusionProof(t *testing.T) { } func TestGetLeaves(t *testing.T) { - buf := func(startSize, endSize int64) io.Reader { - return bytes.NewBufferString(fmt.Sprintf("%s=%d\n%s=%d\n", - "start_size", startSize, - "end_size", endSize, - )) - } for _, table := range []struct { description string - ascii io.Reader // buffer used to populate HTTP request + params string // params is the query's url params expect bool // set if a mock answer is expected rsp *types.Leaves // list of leaves from Trillian client err error // error from Trillian client @@ -593,24 +575,24 @@ func TestGetLeaves(t *testing.T) { }{ { description: "invalid: bad request (parser error)", - ascii: bytes.NewBufferString("key=value\n"), + params: "a/1", wantCode: http.StatusBadRequest, }, { description: "invalid: bad request (StartSize > EndSize)", - ascii: buf(1, 0), + params: "1/0", wantCode: http.StatusBadRequest, }, { description: "invalid: backend failure", - ascii: buf(0, 0), + params: "0/0", expect: true, err: fmt.Errorf("something went wrong"), wantCode: http.StatusInternalServerError, }, { description: "valid: one more entry than the configured MaxRange", - ascii: buf(0, testConfig.MaxRange), // query will be pruned + params: fmt.Sprintf("%d/%d", 0, testConfig.MaxRange), // query will be pruned expect: true, rsp: func() *types.Leaves { var list types.Leaves @@ -644,7 +626,7 @@ func TestGetLeaves(t *testing.T) { // Create HTTP request url := types.EndpointGetLeaves.Path("http://example.com", i.Prefix) - req, err := http.NewRequest("POST", url, table.ascii) + req, err := http.NewRequest(http.MethodGet, url+table.params, nil) if err != nil { t.Fatalf("must create http request: %v", err) } diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index fe05b6a..77f2e5b 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -45,9 +45,9 @@ func (i *Instance) Handlers() []Handler { Handler{Instance: i, Handler: getTreeHeadToCosign, Endpoint: types.EndpointGetTreeHeadToSign, Method: http.MethodGet}, // XXX: ToCosign Handler{Instance: i, Handler: getTreeHeadCosigned, Endpoint: types.EndpointGetTreeHeadCosigned, Method: http.MethodGet}, Handler{Instance: i, Handler: getCheckpoint, Endpoint: types.Endpoint("get-checkpoint"), Method: http.MethodGet}, - Handler{Instance: i, Handler: getConsistencyProof, Endpoint: types.EndpointGetConsistencyProof, Method: http.MethodPost}, - Handler{Instance: i, Handler: getInclusionProof, Endpoint: types.EndpointGetInclusionProof, Method: http.MethodPost}, - Handler{Instance: i, Handler: getLeaves, Endpoint: types.EndpointGetLeaves, Method: http.MethodPost}, + Handler{Instance: i, Handler: getConsistencyProof, Endpoint: types.EndpointGetConsistencyProof, Method: http.MethodGet}, + Handler{Instance: i, Handler: getInclusionProof, Endpoint: types.EndpointGetInclusionProof, Method: http.MethodGet}, + Handler{Instance: i, Handler: getLeaves, Endpoint: types.EndpointGetLeaves, Method: http.MethodGet}, } } @@ -94,7 +94,7 @@ func (i *Instance) cosignatureRequestFromHTTP(r *http.Request) (*requests.Cosign func (i *Instance) consistencyProofRequestFromHTTP(r *http.Request) (*requests.ConsistencyProof, error) { var req requests.ConsistencyProof - if err := req.FromASCII(r.Body); err != nil { + if err := req.FromURL(r.URL.Path); err != nil { return nil, fmt.Errorf("FromASCII: %v", err) } if req.OldSize < 1 { @@ -108,7 +108,7 @@ func (i *Instance) consistencyProofRequestFromHTTP(r *http.Request) (*requests.C func (i *Instance) inclusionProofRequestFromHTTP(r *http.Request) (*requests.InclusionProof, error) { var req requests.InclusionProof - if err := req.FromASCII(r.Body); err != nil { + if err := req.FromURL(r.URL.Path); err != nil { return nil, fmt.Errorf("FromASCII: %v", err) } if req.TreeSize < 2 { @@ -121,7 +121,7 @@ func (i *Instance) inclusionProofRequestFromHTTP(r *http.Request) (*requests.Inc func (i *Instance) leavesRequestFromHTTP(r *http.Request) (*requests.Leaves, error) { var req requests.Leaves - if err := req.FromASCII(r.Body); err != nil { + if err := req.FromURL(r.URL.Path); err != nil { return nil, fmt.Errorf("FromASCII: %v", err) } -- cgit v1.2.3