aboutsummaryrefslogtreecommitdiff
path: root/internal/node/primary/endpoint_external.go
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2022-05-24 23:33:38 +0200
committerRasmus Dahlberg <rasmus@mullvad.net>2022-06-23 11:33:17 +0200
commit559bccccd40d028e412d9f11709ded0250ba6dcd (patch)
tree50f3193dbe70fec21357963c11e5f663013f4b4c /internal/node/primary/endpoint_external.go
parent4b20ef0c1732bcef633c0ed7104501898aa84e2c (diff)
implement primary and secondary role, for replicationv0.5.0
Diffstat (limited to 'internal/node/primary/endpoint_external.go')
-rw-r--r--internal/node/primary/endpoint_external.go147
1 files changed, 147 insertions, 0 deletions
diff --git a/internal/node/primary/endpoint_external.go b/internal/node/primary/endpoint_external.go
new file mode 100644
index 0000000..42e8fcb
--- /dev/null
+++ b/internal/node/primary/endpoint_external.go
@@ -0,0 +1,147 @@
+package primary
+
+// This file implements external HTTP handler callbacks for primary nodes.
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+
+ "git.sigsum.org/log-go/internal/node/handler"
+ "git.sigsum.org/log-go/internal/requests"
+ "git.sigsum.org/sigsum-go/pkg/log"
+)
+
+func addLeaf(ctx context.Context, c handler.Config, w http.ResponseWriter, r *http.Request) (int, error) {
+ p := c.(Primary)
+ log.Debug("handling add-leaf request")
+ req, err := requests.LeafRequestFromHTTP(r, p.Config.ShardStart, ctx, p.DNS)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ sth := p.Stateman.ToCosignTreeHead()
+ sequenced, err := p.TrillianClient.AddLeaf(ctx, req, sth.TreeSize)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ if sequenced {
+ return http.StatusOK, nil
+ } else {
+ return http.StatusAccepted, nil
+ }
+}
+
+func addCosignature(ctx context.Context, c handler.Config, w http.ResponseWriter, r *http.Request) (int, error) {
+ p := c.(Primary)
+ log.Debug("handling add-cosignature request")
+ req, err := requests.CosignatureRequestFromHTTP(r, p.Witnesses)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+ vk := p.Witnesses[req.KeyHash]
+ if err := p.Stateman.AddCosignature(ctx, &vk, &req.Cosignature); err != nil {
+ return http.StatusBadRequest, err
+ }
+ return http.StatusOK, nil
+}
+
+func getTreeHeadToCosign(ctx context.Context, c handler.Config, w http.ResponseWriter, _ *http.Request) (int, error) {
+ p := c.(Primary)
+ log.Debug("handling get-tree-head-to-cosign request")
+ sth := p.Stateman.ToCosignTreeHead()
+ if err := sth.ToASCII(w); err != nil {
+ return http.StatusInternalServerError, err
+ }
+ return http.StatusOK, nil
+}
+
+func getTreeHeadCosigned(ctx context.Context, c handler.Config, w http.ResponseWriter, _ *http.Request) (int, error) {
+ p := c.(Primary)
+ log.Debug("handling get-tree-head-cosigned request")
+ cth, err := p.Stateman.CosignedTreeHead(ctx)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ if err := cth.ToASCII(w); err != nil {
+ return http.StatusInternalServerError, err
+ }
+ return http.StatusOK, nil
+}
+
+func getConsistencyProof(ctx context.Context, c handler.Config, w http.ResponseWriter, r *http.Request) (int, error) {
+ p := c.(Primary)
+ log.Debug("handling get-consistency-proof request")
+ req, err := requests.ConsistencyProofRequestFromHTTP(r)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ curTree := p.Stateman.ToCosignTreeHead()
+ if req.NewSize > curTree.TreeHead.TreeSize {
+ return http.StatusBadRequest, fmt.Errorf("new_size outside of current tree")
+ }
+
+ proof, err := p.TrillianClient.GetConsistencyProof(ctx, req)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ if err := proof.ToASCII(w); err != nil {
+ return http.StatusInternalServerError, err
+ }
+ return http.StatusOK, nil
+}
+
+func getInclusionProof(ctx context.Context, c handler.Config, w http.ResponseWriter, r *http.Request) (int, error) {
+ p := c.(Primary)
+ log.Debug("handling get-inclusion-proof request")
+ req, err := requests.InclusionProofRequestFromHTTP(r)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ curTree := p.Stateman.ToCosignTreeHead()
+ if req.TreeSize > curTree.TreeHead.TreeSize {
+ return http.StatusBadRequest, fmt.Errorf("tree_size outside of current tree")
+ }
+
+ proof, err := p.TrillianClient.GetInclusionProof(ctx, req)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ if err := proof.ToASCII(w); err != nil {
+ return http.StatusInternalServerError, err
+ }
+ return http.StatusOK, nil
+}
+
+func getLeavesGeneral(ctx context.Context, c handler.Config, w http.ResponseWriter, r *http.Request, doLimitToCurrentTree bool) (int, error) {
+ p := c.(Primary)
+ log.Debug("handling get-leaves request")
+ req, err := requests.LeavesRequestFromHTTP(r, uint64(p.MaxRange))
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ if doLimitToCurrentTree {
+ curTree := p.Stateman.ToCosignTreeHead()
+ if req.EndSize >= curTree.TreeHead.TreeSize {
+ return http.StatusBadRequest, fmt.Errorf("end_size outside of current tree")
+ }
+ }
+
+ leaves, err := p.TrillianClient.GetLeaves(ctx, req)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ for _, leaf := range *leaves {
+ if err := leaf.ToASCII(w); err != nil {
+ return http.StatusInternalServerError, err
+ }
+ }
+ return http.StatusOK, nil
+}
+
+func getLeavesExternal(ctx context.Context, c handler.Config, w http.ResponseWriter, r *http.Request) (int, error) {
+ return getLeavesGeneral(ctx, c, w, r, true)
+}