This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
/
trillian_client.go
174 lines (152 loc) · 6.11 KB
/
trillian_client.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sequencer
import (
"context"
"github.com/golang/glog"
"github.com/google/trillian/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/google/keytransparency/core/directory"
tpb "github.com/google/trillian"
tclient "github.com/google/trillian/client"
)
// trillianFactory creates verifying clients for Trillian
type trillianFactory interface {
MapClient(ctx context.Context, dirID string) (trillianMap, error)
MapWriteClient(ctx context.Context, dirID string) (*MapWriteClient, error)
LogClient(ctx context.Context, dirID string) (trillianLog, error)
}
// trillianMap communicates with the Trilian map and verifies the responses.
type trillianMap interface {
GetAndVerifyLatestMapRoot(ctx context.Context) (*tpb.SignedMapRoot, *types.MapRootV1, error)
GetAndVerifyMapRootByRevision(ctx context.Context, rev int64) (*tpb.SignedMapRoot, *types.MapRootV1, error)
}
// trillianLog communicates with the Trillian log and verifies the responses.
type trillianLog interface {
WaitForInclusion(ctx context.Context, data []byte) error
UpdateRoot(ctx context.Context) (*types.LogRootV1, error)
AddSequencedLeaves(ctx context.Context, dataByIndex map[int64][]byte) error
}
// Trillian contains Trillian gRPC clients and metadata about them.
type Trillian struct {
directories directory.Storage
tmap tpb.TrillianMapClient
tlog tpb.TrillianLogClient
twrite tpb.TrillianMapWriteClient
}
// MapWriteClient returns a connection to the map write API.
func (t *Trillian) MapWriteClient(ctx context.Context, dirID string) (*MapWriteClient, error) {
directory, err := t.directories.Read(ctx, dirID, false)
if err != nil {
glog.Errorf("directories.Read(%v): %v", dirID, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch directory info for %v", dirID)
}
return &MapWriteClient{
MapID: directory.Map.TreeId,
twrite: t.twrite,
}, nil
}
// MapClient returns a verifying MapClient
func (t *Trillian) MapClient(ctx context.Context, dirID string) (trillianMap, error) { // nolint
directory, err := t.directories.Read(ctx, dirID, false)
if err != nil {
glog.Errorf("directories.Read(%v): %v", dirID, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch directory info for %v", dirID)
}
c, err := tclient.NewMapClientFromTree(t.tmap, directory.Map)
if err != nil {
return nil, err
}
return &MapClient{MapClient: c}, nil
}
// LogClient returns a verifying LogClient.
func (t *Trillian) LogClient(ctx context.Context, dirID string) (trillianLog, error) { // nolint
directory, err := t.directories.Read(ctx, dirID, false)
if err != nil {
glog.Errorf("directories.Read(%v): %v", dirID, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch directory info for %v", dirID)
}
// Create verifying log client.
trustedRoot := types.LogRootV1{} // TODO(gbelvin): Store and track trustedRoot.
return tclient.NewFromTree(t.tlog, directory.Log, trustedRoot)
}
type MapWriteClient struct {
MapID int64
twrite tpb.TrillianMapWriteClient
}
func (c *MapWriteClient) GetLeavesByRevision(ctx context.Context, rev int64, indexes [][]byte) ([]*tpb.MapLeaf, error) {
mapLeaves, err := c.twrite.GetLeavesByRevision(ctx, &tpb.GetMapLeavesByRevisionRequest{
MapId: c.MapID,
Revision: rev,
Index: indexes,
})
return mapLeaves.GetLeaves(), err
}
func (c *MapWriteClient) WriteLeaves(ctx context.Context, rev int64, leaves []*tpb.MapLeaf) error {
_, err := c.twrite.WriteLeaves(ctx, &tpb.WriteMapLeavesRequest{
MapId: c.MapID,
Leaves: leaves,
ExpectRevision: rev,
})
return err
}
// MapClient interacts with the Trillian Map and verifies its responses.
type MapClient struct {
*tclient.MapClient
}
// GetAndVerifyLatestMapRoot verifies and returns the latest map root.
func (c *MapClient) GetAndVerifyLatestMapRoot(ctx context.Context) (*tpb.SignedMapRoot, *types.MapRootV1, error) {
rootResp, err := c.Conn.GetSignedMapRoot(ctx, &tpb.GetSignedMapRootRequest{MapId: c.MapID})
if err != nil {
return nil, nil, status.Errorf(status.Code(err), "GetSignedMapRoot(%v): %v", c.MapID, err)
}
mapRoot, err := c.VerifySignedMapRoot(rootResp.GetMapRoot())
if err != nil {
return nil, nil, status.Errorf(codes.Internal, "VerifySignedMapRoot(%v): %v", c.MapID, err)
}
return rootResp.GetMapRoot(), mapRoot, nil
}
// GetAndVerifyMapRootByRevision verifies and returns a specific map root.
func (c *MapClient) GetAndVerifyMapRootByRevision(ctx context.Context,
rev int64) (*tpb.SignedMapRoot, *types.MapRootV1, error) {
req := &tpb.GetSignedMapRootByRevisionRequest{
MapId: c.MapID,
Revision: rev,
}
resp, err := c.Conn.GetSignedMapRootByRevision(ctx, req)
if err != nil {
return nil, nil, status.Errorf(status.Code(err), "GetSignedMapRootByRevision(%v, %v): %v", c.MapID, rev, err)
}
rawMapRoot := resp.GetMapRoot()
mapRoot, err := c.VerifySignedMapRoot(rawMapRoot)
if err != nil {
return nil, nil, status.Errorf(codes.Internal, "VerifySignedMapRoot(): %v", err)
}
return rawMapRoot, mapRoot, nil
}
// GetMapLeavesByRevisionNoProof returns the requested map leaves at a specific revision.
// indexes may not contain duplicates.
func (c *MapClient) GetMapLeavesByRevisionNoProof(ctx context.Context, revision int64, indexes [][]byte) ([]*tpb.MapLeaf, error) {
getResp, err := c.Conn.GetLeavesByRevisionNoProof(ctx, &tpb.GetMapLeavesByRevisionRequest{
MapId: c.MapID,
Index: indexes,
Revision: revision,
})
if err != nil {
s := status.Convert(err)
return nil, status.Errorf(s.Code(), "GetLeavesByRevisionNoProof(): %v", s.Message())
}
return getResp.Leaves, nil
}