From 2dcaf038655426c572c5d292973001c581f0a1a2 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 1 Jun 2021 21:37:54 +0200 Subject: moved endpoint type into types --- endpoint.go | 21 --------------------- endpoint_test.go | 48 ------------------------------------------------ instance.go | 18 +++++++++--------- types/types.go | 41 +++++++++++++++++++---------------------- types/types_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 100 deletions(-) create mode 100644 types/types_test.go diff --git a/endpoint.go b/endpoint.go index 19ea388..9be55b4 100644 --- a/endpoint.go +++ b/endpoint.go @@ -5,33 +5,12 @@ import ( "crypto/ed25519" "fmt" "net/http" - "strings" "github.com/golang/glog" "github.com/google/trillian" "github.com/system-transparency/stfe/types" ) -// Endpoint is a named HTTP API endpoint -type Endpoint string - -const ( - EndpointAddEntry = Endpoint("add-leaf") - EndpointAddCosignature = Endpoint("add-cosignature") - EndpointGetLatestSth = Endpoint("get-tree-head-latest") - EndpointGetStableSth = Endpoint("get-tree-head-to-sign") - EndpointGetCosignedSth = Endpoint("get-tree-head-cosigned") - EndpointGetProofByHash = Endpoint("get-proof-by-hash") - EndpointGetConsistencyProof = Endpoint("get-consistency-proof") - EndpointGetEntries = Endpoint("get-leaves") -) - -// Path joins a number of components to form a full endpoint path, e.g., base -// ("example.com"), prefix ("st/v1"), and the endpoint itself ("get-sth"). -func (e Endpoint) Path(components ...string) string { - return strings.Join(append(components, string(e)), "/") -} - func addEntry(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.V(3).Info("handling add-entry request") leaf, err := i.LogParameters.parseAddEntryV1Request(r) diff --git a/endpoint_test.go b/endpoint_test.go index aab2c54..e515635 100644 --- a/endpoint_test.go +++ b/endpoint_test.go @@ -452,54 +452,6 @@ func TestEndpointGetEntriesV1(t *testing.T) { } } -func TestEndpointPath(t *testing.T) { - base, prefix, proto := "http://example.com", "test", "st/v1" - for _, table := range []struct { - endpoint Endpoint - want string - }{ - { - endpoint: EndpointAddEntry, - want: "http://example.com/test/st/v1/add-entry", - }, - { - endpoint: EndpointAddCosignature, - want: "http://example.com/test/st/v1/add-cosignature", - }, - { - endpoint: EndpointGetLatestSth, - want: "http://example.com/test/st/v1/get-latest-sth", - }, - { - endpoint: EndpointGetStableSth, - want: "http://example.com/test/st/v1/get-stable-sth", - }, - { - endpoint: EndpointGetCosignedSth, - want: "http://example.com/test/st/v1/get-cosigned-sth", - }, - { - endpoint: EndpointGetConsistencyProof, - want: "http://example.com/test/st/v1/get-consistency-proof", - }, - { - endpoint: EndpointGetProofByHash, - want: "http://example.com/test/st/v1/get-proof-by-hash", - }, - { - endpoint: EndpointGetEntries, - want: "http://example.com/test/st/v1/get-entries", - }, - } { - if got, want := table.endpoint.Path(base+"/"+prefix+"/"+proto), table.want; got != want { - t.Errorf("got endpoint\n%s\n\tbut wanted\n%s\n\twith one component", got, want) - } - if got, want := table.endpoint.Path(base, prefix, proto), table.want; got != want { - t.Errorf("got endpoint\n%s\n\tbut wanted\n%s\n\tmultiple components", got, want) - } - } -} - // TODO: TestWriteOctetResponse func TestWriteOctetResponse(t *testing.T) { } diff --git a/instance.go b/instance.go index 5d358c1..4425770 100644 --- a/instance.go +++ b/instance.go @@ -22,14 +22,14 @@ type Instance struct { // Handlers returns a list of STFE handlers func (i *Instance) Handlers() []Handler { return []Handler{ - Handler{Instance: i, Handler: addEntry, Endpoint: EndpointAddEntry, Method: http.MethodPost}, - Handler{Instance: i, Handler: addCosignature, Endpoint: EndpointAddCosignature, Method: http.MethodPost}, - 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: getConsistencyProof, Endpoint: EndpointGetConsistencyProof, Method: http.MethodPost}, - Handler{Instance: i, Handler: getEntries, Endpoint: EndpointGetEntries, Method: http.MethodPost}, + Handler{Instance: i, Handler: addEntry, Endpoint: types.EndpointAddLeaf, Method: http.MethodPost}, + Handler{Instance: i, Handler: addCosignature, Endpoint: types.EndpointAddCosignature, Method: http.MethodPost}, + Handler{Instance: i, Handler: getLatestSth, Endpoint: types.EndpointGetTreeHeadLatest, Method: http.MethodGet}, + Handler{Instance: i, Handler: getStableSth, Endpoint: types.EndpointGetTreeHeadToSign, Method: http.MethodGet}, + Handler{Instance: i, Handler: getCosignedSth, Endpoint: types.EndpointGetTreeHeadCosigned, Method: http.MethodGet}, + Handler{Instance: i, Handler: getProofByHash, Endpoint: types.EndpointGetProofByHash, Method: http.MethodPost}, + Handler{Instance: i, Handler: getConsistencyProof, Endpoint: types.EndpointGetConsistencyProof, Method: http.MethodPost}, + Handler{Instance: i, Handler: getEntries, Endpoint: types.EndpointGetLeaves, Method: http.MethodPost}, } } @@ -37,7 +37,7 @@ func (i *Instance) Handlers() []Handler { // to an STFE server instance as well as a function that uses it. type Handler struct { Instance *Instance - Endpoint Endpoint + Endpoint types.Endpoint Method string Handler func(context.Context, *Instance, http.ResponseWriter, *http.Request) (int, error) } diff --git a/types/types.go b/types/types.go index 2da40da..d031b29 100644 --- a/types/types.go +++ b/types/types.go @@ -3,17 +3,34 @@ package types import ( "crypto/ed25519" "crypto/sha256" + "strings" ) const ( HashSize = sha256.Size SignatureSize = ed25519.SignatureSize VerificationKeySize = ed25519.PublicKeySize + + EndpointAddLeaf = Endpoint("add-leaf") + EndpointAddCosignature = Endpoint("add-cosignature") + EndpointGetTreeHeadLatest = Endpoint("get-tree-head-latest") + EndpointGetTreeHeadToSign = Endpoint("get-tree-head-to-sign") + EndpointGetTreeHeadCosigned = Endpoint("get-tree-head-cosigned") + EndpointGetProofByHash = Endpoint("get-proof-by-hash") + EndpointGetConsistencyProof = Endpoint("get-consistency-proof") + EndpointGetLeaves = Endpoint("get-leaves") ) +// Endpoint is a named HTTP API endpoint +type Endpoint string + +// Path joins a number of components to form a full endpoint path. For example, +// EndpointAddLeaf.Path("example.com", "st/v0") -> example.com/st/v0/add-leaf. +func (e Endpoint) Path(components ...string) string { + return strings.Join(append(components, string(e)), "/") +} + // Leaf is the log's Merkle tree leaf. -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#merkle-tree-leaf type Leaf struct { Message SigIdent @@ -35,18 +52,12 @@ type SigIdent struct { // SignedTreeHead is composed of a tree head and a list of signature-signer // pairs. Each signature is computed over the Trunnel-serialized tree head. -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-tree-head-cosigned -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-tree-head-to-sign -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-tree-head-latest type SignedTreeHead struct { TreeHead SigIdent []*SigIdent } // TreeHead is the log's tree head. -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#merkle-tree-head type TreeHead struct { Timestamp uint64 TreeSize uint64 @@ -55,8 +66,6 @@ type TreeHead struct { // ConsistencyProof is a consistency proof that proves the log's append-only // property. -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-consistency-proof type ConsistencyProof struct { NewSize uint64 OldSize uint64 @@ -65,8 +74,6 @@ type ConsistencyProof struct { // InclusionProof is an inclusion proof that proves a leaf is included in the // log. -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-proof-by-hash type InclusionProof struct { TreeSize uint64 LeafIndex uint64 @@ -77,32 +84,24 @@ type InclusionProof struct { type LeafList []*Leaf // ConsistencyProofRequest is a get-consistency-proof request -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-consistency-proof type ConsistencyProofRequest struct { NewSize uint64 OldSize uint64 } // InclusionProofRequest is a get-proof-by-hash request -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-proof-by-hash type InclusionProofRequest struct { LeafHash *[HashSize]byte TreeSize uint64 } // LeavesRequest is a get-leaves request -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#get-leaves type LeavesRequest struct { StartSize uint64 EndSize uint64 } // LeafRequest is an add-leaf request -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#add-leaf type LeafRequest struct { Message Signature *[SignatureSize]byte @@ -111,8 +110,6 @@ type LeafRequest struct { } // CosignatureRequest is an add-cosignature request -// -// Ref: https://github.com/system-transparency/stfe/blob/design/doc/api.md#add-cosignature type CosignatureRequest struct { SigIdent } diff --git a/types/types_test.go b/types/types_test.go new file mode 100644 index 0000000..9d76c73 --- /dev/null +++ b/types/types_test.go @@ -0,0 +1,53 @@ +package types + +import ( + "testing" +) + +func TestEndpointPath(t *testing.T) { + base, prefix, proto := "example.com", "log", "st/v0" + for _, table := range []struct { + endpoint Endpoint + want string + }{ + { + endpoint: EndpointAddLeaf, + want: "example.com/log/st/v0/add-leaf", + }, + { + endpoint: EndpointAddCosignature, + want: "example.com/log/st/v0/add-cosignature", + }, + { + endpoint: EndpointGetTreeHeadLatest, + want: "example.com/log/st/v0/get-tree-head-latest", + }, + { + endpoint: EndpointGetTreeHeadToSign, + want: "example.com/log/st/v0/get-tree-head-to-sign", + }, + { + endpoint: EndpointGetTreeHeadCosigned, + want: "example.com/log/st/v0/get-tree-head-cosigned", + }, + { + endpoint: EndpointGetConsistencyProof, + want: "example.com/log/st/v0/get-consistency-proof", + }, + { + endpoint: EndpointGetProofByHash, + want: "example.com/log/st/v0/get-proof-by-hash", + }, + { + endpoint: EndpointGetLeaves, + want: "example.com/log/st/v0/get-leaves", + }, + } { + if got, want := table.endpoint.Path(base+"/"+prefix+"/"+proto), table.want; got != want { + t.Errorf("got endpoint\n%s\n\tbut wanted\n%s\n\twith one component", got, want) + } + if got, want := table.endpoint.Path(base, prefix, proto), table.want; got != want { + t.Errorf("got endpoint\n%s\n\tbut wanted\n%s\n\tmultiple components", got, want) + } + } +} -- cgit v1.2.3