aboutsummaryrefslogtreecommitdiff
path: root/internal/node/primary/endpoint_external.go
blob: 42e8fcb71dc4a456b5f610c9797d8a8a75ade3ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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)
}