From 5ba3cec231989ba1deb5926f8fdcd8caec169821 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Thu, 5 Nov 2020 18:17:23 +0100 Subject: added client-side get-entries --- client/client.go | 20 ++++++++++++++-- client/get-entries/main.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 client/get-entries/main.go diff --git a/client/client.go b/client/client.go index 5f24fdc..43386b0 100644 --- a/client/client.go +++ b/client/client.go @@ -193,8 +193,24 @@ func (c *Client) GetProofByHash(ctx context.Context, treeSize uint64, rootHash, return item, nil } -func (c *Client) GetEntries(ctx context.Context, start, end uint64) (*stfe.StItem, error) { - return nil, fmt.Errorf("TODO: Client.GetEntries()") +func (c *Client) GetEntries(ctx context.Context, start, end uint64) ([]*stfe.GetEntryResponse, error) { + req, err := http.NewRequest("GET", c.protocol()+c.Log.BaseUrl+"/get-entries", nil) + if err != nil { + return nil, fmt.Errorf("failed creating http request: %v", err) + } + req.Header.Set("Content-Type", "application/json") + q := req.URL.Query() + q.Add("start", fmt.Sprintf("%d", start)) + q.Add("end", fmt.Sprintf("%d", end)) + req.URL.RawQuery = q.Encode() + glog.V(2).Infof("created http request: %s %s", req.Method, req.URL) + + var rsp []*stfe.GetEntryResponse + if err := c.doRequest(ctx, req, &rsp); err != nil { + return nil, err + } + // TODO: verify signature over leaf data + return rsp, nil } // GetAnchors fetches the log's trust anchors. Safe to use without a client diff --git a/client/get-entries/main.go b/client/get-entries/main.go new file mode 100644 index 0000000..6a45707 --- /dev/null +++ b/client/get-entries/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "context" + "flag" + "fmt" + + "net/http" + + "github.com/golang/glog" + "github.com/system-transparency/stfe" + "github.com/system-transparency/stfe/client" +) + +var ( + operators = flag.String("operators", "../../descriptor/stfe.json", "path to json-encoded list of log operators") + logId = flag.String("log_id", "B9oCJk4XIOMXba8dBM5yUj+NLtqTE6xHwbvR9dYkHPM=", "base64-encoded log identifier") + start = flag.Uint64("start", 50, "inclusive start index to download") + end = flag.Uint64("end", 60, "inclusive stop index to download") +) + +func main() { + flag.Parse() + + client, err := client.NewClientFromPath(*logId, "", "", *operators, &http.Client{}, true) + if err != nil { + glog.Fatal(err) + } + + items := make([]*stfe.StItem, 0, *end-*start+1) + i := *start + for len(items) != cap(items) { + rsps, err := client.GetEntries(context.Background(), i, *end) + if err != nil { + glog.Fatal(err) + } + + for _, rsp := range rsps { + var item stfe.StItem + if err := item.Unmarshal(rsp.Leaf); err != nil { + glog.Fatalf("bad StItem: unmarshal failed: %v", err) + } else if item.Format != stfe.StFormatChecksumV1 { + glog.Fatalf("bad StFormat: %v", item.Format) + } + items = append(items, &item) + } + i += uint64(len(rsps)) + } + + for i, item := range items { + glog.V(2).Infof("Index(%d): %s", *start+uint64(i), item) + str, err := item.MarshalB64() + if err != nil { + glog.Fatalf("bad StItem: marshal failed: %v", err) + } + fmt.Printf("Index(%d): %s\n", *start+uint64(i), str) + } + + glog.Flush() +} -- cgit v1.2.3