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
|
package api
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"git.sigsum.org/sigsum-go/pkg/requests"
"git.sigsum.org/sigsum-go/pkg/types"
)
type NetworkClient struct {
http.Client
LogURL string
}
func NewNetworkClient(httpClient http.Client, logURL string) *NetworkClient {
return &NetworkClient{
Client: httpClient,
LogURL: logURL,
}
}
func (c *NetworkClient) GetToCosignTreeHead() (*types.SignedTreeHead, error) {
return nil, fmt.Errorf("TODO")
}
func (c *NetworkClient) GetCosignedTreeHead() (*types.CosignedTreeHead, error) {
rsp, err := c.get(types.EndpointGetTreeHeadCosigned.Path(c.LogURL))
if err != nil {
return nil, fmt.Errorf("client: HTTP %d: %s", rsp.StatusCode, errorMessage(rsp.Body))
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("client: HTTP %d: %s", rsp.StatusCode, errorMessage(rsp.Body))
}
var cth types.CosignedTreeHead
if err := cth.FromASCII(rsp.Body); err != nil {
return nil, fmt.Errorf("client: HTTP %d: %s", rsp.StatusCode, errorMessage(rsp.Body))
}
return &cth, nil
}
func (c *NetworkClient) GetInclusionProof(treeSize uint64, leafHash *types.Hash) (*types.InclusionProof, error) {
data := requests.InclusionProof{
TreeSize: treeSize,
LeafHash: *leafHash,
}
buf := bytes.NewBuffer(nil)
if err := data.ToASCII(buf); err != nil {
return nil, fmt.Errorf("client: invalid request data: %v", err)
}
rsp, err := c.post(types.EndpointGetInclusionProof.Path(c.LogURL), buf)
if err != nil {
return nil, fmt.Errorf("client: invalid proof request: %v", err)
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("client: HTTP %d: %s", rsp.StatusCode, errorMessage(rsp.Body))
}
var proof types.InclusionProof
if err := proof.FromASCII(rsp.Body, treeSize); err != nil {
return nil, fmt.Errorf("client: failed parsing response: %v", err)
}
return &proof, nil
}
func (c *NetworkClient) GetConsistencyProof(oldSize, newSize uint64) (*types.ConsistencyProof, error) {
return nil, fmt.Errorf("TODO")
}
func (c *NetworkClient) GetLeaves(startSize, endSize uint64) (*types.Leaves, error) {
return nil, fmt.Errorf("TODO")
}
func (c *NetworkClient) AddLeaf(leaf *requests.Leaf) error {
buf := bytes.NewBuffer(nil)
if err := leaf.ToASCII(buf); err != nil {
return fmt.Errorf("client: invalid request data: %v", err)
}
rsp, err := c.post(types.EndpointAddLeaf.Path(c.LogURL), buf)
if err != nil {
return fmt.Errorf("client: HTTP %d: %s", rsp.StatusCode, errorMessage(rsp.Body))
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("client: HTTP %d: %s", rsp.StatusCode, errorMessage(rsp.Body))
}
return nil
}
func (c *NetworkClient) AddCosignature(*requests.Cosignature) error {
return fmt.Errorf("TODO")
}
func (c *NetworkClient) get(url string) (*http.Response, error) {
log.Printf("GET %s", url)
return c.Get(url)
}
func (c *NetworkClient) post(url string, buf *bytes.Buffer) (*http.Response, error) {
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return nil, fmt.Errorf("client: invalid leaf request: %v", err)
}
log.Printf("POST %s", url)
return c.Do(req)
}
func errorMessage(r io.Reader) string {
b, _ := ioutil.ReadAll(r)
return string(b)
// TODO: the below doesn't work because log error messages are malformed
//msg := struct {
// Message string `ascii:"Error"`
//}{}
//if err := ascii.StdEncoding.Deserialize(r, &msg); err != nil {
// return fmt.Sprintf("malformed error message: %v", err)
//}
//return msg.Message
}
|