This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
admin_server.go
384 lines (346 loc) · 12.8 KB
/
admin_server.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2017 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 adminserver contains the KeyTransparencyAdmin implementation
package adminserver
import (
"context"
"fmt"
"time"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/google/keytransparency/core/crypto/vrf/p256"
"github.com/google/keytransparency/core/domain"
"github.com/google/trillian/client"
"github.com/google/trillian/crypto/keys"
"github.com/google/trillian/crypto/keys/der"
"github.com/google/trillian/crypto/keyspb"
"github.com/google/trillian/crypto/sigpb"
"github.com/google/trillian/types"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
pb "github.com/google/keytransparency/core/api/v1/keytransparency_go_proto"
tpb "github.com/google/trillian"
)
var (
logArgs = &tpb.CreateTreeRequest{
Tree: &tpb.Tree{
TreeState: tpb.TreeState_ACTIVE,
TreeType: tpb.TreeType_PREORDERED_LOG,
// Clients that verify output from the log need to import
// _ "github.com/google/trillian/merkle/rfc6962"
HashStrategy: tpb.HashStrategy_RFC6962_SHA256,
SignatureAlgorithm: sigpb.DigitallySigned_ECDSA,
HashAlgorithm: sigpb.DigitallySigned_SHA256,
MaxRootDuration: ptypes.DurationProto(0 * time.Millisecond),
},
}
mapArgs = &tpb.CreateTreeRequest{
Tree: &tpb.Tree{
TreeState: tpb.TreeState_ACTIVE,
TreeType: tpb.TreeType_MAP,
// Clients that verify output from the map need to import
// _ "github.com/google/trillian/merkle/coniks"
HashStrategy: tpb.HashStrategy_CONIKS_SHA256,
SignatureAlgorithm: sigpb.DigitallySigned_ECDSA,
HashAlgorithm: sigpb.DigitallySigned_SHA256,
MaxRootDuration: ptypes.DurationProto(0 * time.Millisecond),
},
}
keyspec = &keyspb.Specification{
Params: &keyspb.Specification_EcdsaParams{
EcdsaParams: &keyspb.Specification_ECDSA{
Curve: keyspb.Specification_ECDSA_P256,
},
},
}
)
// Server implements pb.KeyTransparencyAdminServer
type Server struct {
tlog tpb.TrillianLogClient
tmap tpb.TrillianMapClient
logAdmin tpb.TrillianAdminClient
mapAdmin tpb.TrillianAdminClient
domains domain.Storage
keygen keys.ProtoGenerator
}
// New returns a KeyTransparencyAdmin implementation.
func New(
tlog tpb.TrillianLogClient,
tmap tpb.TrillianMapClient,
logAdmin, mapAdmin tpb.TrillianAdminClient,
domains domain.Storage,
keygen keys.ProtoGenerator,
) *Server {
return &Server{
tlog: tlog,
tmap: tmap,
logAdmin: logAdmin,
mapAdmin: mapAdmin,
domains: domains,
keygen: keygen,
}
}
// ListDomains produces a list of the configured domains
func (s *Server) ListDomains(ctx context.Context, in *pb.ListDomainsRequest) (*pb.ListDomainsResponse, error) {
domains, err := s.domains.List(ctx, in.GetShowDeleted())
if err != nil {
return nil, err
}
resp := make([]*pb.Domain, 0, len(domains))
for _, d := range domains {
info, err := s.fetchDomain(ctx, d)
if err != nil {
return nil, err
}
resp = append(resp, info)
}
return &pb.ListDomainsResponse{
Domains: resp,
}, nil
}
// fetchDomain converts an adminstorage.Domain object into a pb.Domain object
// by fetching the relevant info from Trillian.
func (s *Server) fetchDomain(ctx context.Context, d *domain.Domain) (*pb.Domain, error) {
logTree, err := s.logAdmin.GetTree(ctx, &tpb.GetTreeRequest{TreeId: d.LogID})
if err != nil {
return nil, err
}
mapTree, err := s.mapAdmin.GetTree(ctx, &tpb.GetTreeRequest{TreeId: d.MapID})
if err != nil {
return nil, err
}
return &pb.Domain{
DomainId: d.DomainID,
Log: logTree,
Map: mapTree,
Vrf: d.VRF,
MinInterval: ptypes.DurationProto(d.MinInterval),
MaxInterval: ptypes.DurationProto(d.MaxInterval),
Deleted: d.Deleted,
}, nil
}
// GetDomain retrieves the domain info for a given domain.
func (s *Server) GetDomain(ctx context.Context, in *pb.GetDomainRequest) (*pb.Domain, error) {
domain, err := s.domains.Read(ctx, in.GetDomainId(), in.GetShowDeleted())
if err != nil {
return nil, err
}
info, err := s.fetchDomain(ctx, domain)
if err != nil {
return nil, err
}
return info, nil
}
// privKeyOrGen returns the message inside privKey if privKey is not nil,
// otherwise, it generates a new key with keygen.
func privKeyOrGen(ctx context.Context, privKey *any.Any, keygen keys.ProtoGenerator) (proto.Message, error) {
if privKey != nil {
var keyProto ptypes.DynamicAny
if err := ptypes.UnmarshalAny(privKey, &keyProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal privatekey: %v", err)
}
return keyProto.Message, nil
}
return keygen(ctx, keyspec)
}
// treeConfig returns a CreateTreeRequest
// - with a set PrivateKey is not nil, otherwise KeySpec is set.
// - with a tree description of "KT domain %v"
func treeConfig(treeTemplate *tpb.CreateTreeRequest, privKey *any.Any, domainID string) *tpb.CreateTreeRequest {
config := *treeTemplate
if privKey != nil {
config.Tree.PrivateKey = privKey
} else {
config.KeySpec = keyspec
}
config.Tree.Description = fmt.Sprintf("KT domain %s", domainID)
maxDisplayNameLen := 20
if len(domainID) < maxDisplayNameLen {
config.Tree.DisplayName = domainID
} else {
config.Tree.DisplayName = domainID[:maxDisplayNameLen]
}
return &config
}
// CreateDomain reachs out to Trillian to produce new trees.
func (s *Server) CreateDomain(ctx context.Context, in *pb.CreateDomainRequest) (*pb.Domain, error) {
glog.Infof("Begin CreateDomain(%v)", in.GetDomainId())
if _, err := s.domains.Read(ctx, in.GetDomainId(), true); status.Code(err) != codes.NotFound {
// Domain already exists.
return nil, status.Errorf(codes.AlreadyExists, "Domain %v already exists or is soft deleted.", in.GetDomainId())
}
// Generate VRF key.
wrapped, err := privKeyOrGen(ctx, in.GetVrfPrivateKey(), s.keygen)
if err != nil {
return nil, fmt.Errorf("adminserver: keygen(): %v", err)
}
vrfPriv, err := p256.NewFromWrappedKey(ctx, wrapped)
if err != nil {
return nil, fmt.Errorf("adminserver: NewFromWrappedKey(): %v", err)
}
vrfPublicPB, err := der.ToPublicProto(vrfPriv.Public())
if err != nil {
return nil, err
}
// Create Trillian keys.
logTreeArgs := treeConfig(logArgs, in.GetLogPrivateKey(), in.GetDomainId())
logTree, err := client.CreateAndInitTree(ctx, logTreeArgs, s.logAdmin, s.tmap, s.tlog)
if err != nil {
return nil, fmt.Errorf("adminserver: CreateTree(log): %v", err)
}
mapTreeArgs := treeConfig(mapArgs, in.GetMapPrivateKey(), in.GetDomainId())
mapTree, err := client.CreateAndInitTree(ctx, mapTreeArgs, s.mapAdmin, s.tmap, s.tlog)
if err != nil {
// Delete log if map creation fails.
if _, delErr := s.logAdmin.DeleteTree(ctx, &tpb.DeleteTreeRequest{TreeId: logTree.TreeId}); delErr != nil {
return nil, status.Errorf(codes.Internal, "adminserver: CreateAndInitTree(map): %v, DeleteTree(%v): %v ", err, logTree.TreeId, delErr)
}
return nil, status.Errorf(codes.Internal, "adminserver: CreateAndInitTree(map): %v", err)
}
minInterval, err := ptypes.Duration(in.MinInterval)
if err != nil {
return nil, fmt.Errorf("adminserver: Duration(%v): %v", in.MinInterval, err)
}
maxInterval, err := ptypes.Duration(in.MaxInterval)
if err != nil {
return nil, fmt.Errorf("adminserver: Duration(%v): %v", in.MaxInterval, err)
}
// Initialize log with first map root.
if err := s.initialize(ctx, logTree, mapTree); err != nil {
// Delete log and map if initialization fails.
_, delLogErr := s.logAdmin.DeleteTree(ctx, &tpb.DeleteTreeRequest{TreeId: logTree.TreeId})
_, delMapErr := s.mapAdmin.DeleteTree(ctx, &tpb.DeleteTreeRequest{TreeId: mapTree.TreeId})
return nil, status.Errorf(codes.Internal, "adminserver: init of log with first map root failed: %v. Cleanup: delete log %v: %v, delete map %v: %v",
err, logTree.TreeId, delLogErr, mapTree.TreeId, delMapErr)
}
if err := s.domains.Write(ctx, &domain.Domain{
DomainID: in.GetDomainId(),
MapID: mapTree.TreeId,
LogID: logTree.TreeId,
VRF: vrfPublicPB,
VRFPriv: wrapped,
MinInterval: minInterval,
MaxInterval: maxInterval,
}); err != nil {
return nil, fmt.Errorf("adminserver: domains.Write(): %v", err)
}
d := &pb.Domain{
DomainId: in.GetDomainId(),
Log: logTree,
Map: mapTree,
Vrf: vrfPublicPB,
MinInterval: in.MinInterval,
MaxInterval: in.MaxInterval,
}
glog.Infof("Created domain: %v", d)
return d, nil
}
// initialize inserts the first (empty) SignedMapRoot into the log if it is empty.
// This keeps the log leaves in-sync with the map which starts off with an
// empty log root at map revision 0.
func (s *Server) initialize(ctx context.Context, logTree, mapTree *tpb.Tree) error {
logID := logTree.GetTreeId()
mapID := mapTree.GetTreeId()
// TODO(gbelvin): Store and track trusted root.
trustedRoot := types.LogRootV1{} // Automatically trust the first observed log root.
logClient, err := client.NewFromTree(s.tlog, logTree, trustedRoot)
if err != nil {
return fmt.Errorf("adminserver: could not create log client: %v", err)
}
// Wait for the latest log root to become available.
logRoot, err := logClient.UpdateRoot(ctx)
if err != nil {
return fmt.Errorf("adminserver: UpdateRoot(): %v", err)
}
// TODO(gbelvin): does this need to be in a retry loop?
resp, err := s.tmap.GetSignedMapRootByRevision(ctx, &tpb.GetSignedMapRootByRevisionRequest{
MapId: mapID,
Revision: 0,
})
if err != nil {
return fmt.Errorf("adminserver: GetSignedMapRootByRevision(%v,0): %v", mapID, err)
}
mapVerifier, err := client.NewMapVerifierFromTree(mapTree)
if err != nil {
return fmt.Errorf("adminserver: NewMapVerifierFromTree(): %v", err)
}
mapRoot, err := mapVerifier.VerifySignedMapRoot(resp.GetMapRoot())
if err != nil {
return fmt.Errorf("adminserver: VerifySignedMapRoot(): %v", err)
}
// If the tree is empty and the map is empty,
// add the empty map root to the log.
if logRoot.TreeSize != 0 || mapRoot.Revision != 0 {
return nil // Init not needed.
}
glog.Infof("Initializing Trillian Log %v with empty map root", logID)
if err := logClient.AddSequencedLeafAndWait(ctx, resp.GetMapRoot().GetMapRoot(), int64(mapRoot.Revision)); err != nil {
return fmt.Errorf("adminserver: log.AddSequencedLeaf(%v): %v", mapRoot.Revision, err)
}
return nil
}
// DeleteDomain marks a domain as deleted, but does not immediately delete it.
func (s *Server) DeleteDomain(ctx context.Context, in *pb.DeleteDomainRequest) (*google_protobuf.Empty, error) {
d, err := s.GetDomain(ctx, &pb.GetDomainRequest{DomainId: in.GetDomainId()})
if err != nil {
return nil, err
}
if err := s.domains.SetDelete(ctx, in.GetDomainId(), true); err != nil {
return nil, err
}
_, delLogErr := s.logAdmin.DeleteTree(ctx, &tpb.DeleteTreeRequest{TreeId: d.Log.TreeId})
_, delMapErr := s.mapAdmin.DeleteTree(ctx, &tpb.DeleteTreeRequest{TreeId: d.Map.TreeId})
if delLogErr != nil || delMapErr != nil {
return nil, status.Errorf(codes.Internal, "adminserver: delete log %v: %v, delete map %v: %v",
d.Log.TreeId, delLogErr, d.Map.TreeId, delMapErr)
}
return &google_protobuf.Empty{}, nil
}
// UndeleteDomain reactivates a deleted domain - provided that UndeleteDomain is called sufficiently soon after DeleteDomain.
func (s *Server) UndeleteDomain(ctx context.Context, in *pb.UndeleteDomainRequest) (*google_protobuf.Empty, error) {
return nil, status.Errorf(codes.Unimplemented, "not implemented")
}
// GarbageCollect looks for domains that have been deleted before the specified timestamp and fully deletes them.
func (s *Server) GarbageCollect(ctx context.Context, in *pb.GarbageCollectRequest) (*pb.GarbageCollectResponse, error) {
before, err := ptypes.Timestamp(in.GetBefore())
if err != nil {
return nil, err
}
showDeleted := true
domains, err := s.domains.List(ctx, showDeleted)
if err != nil {
return nil, err
}
// Search for domains deleted before in.Before.
deleted := make([]*pb.Domain, 0)
for _, d := range domains {
if d.Deleted && d.DeletedTimestamp.Before(before) {
dproto, err := s.GetDomain(ctx, &pb.GetDomainRequest{
DomainId: d.DomainID,
ShowDeleted: true,
})
if err != nil {
return nil, err
}
if err := s.domains.Delete(ctx, d.DomainID); err != nil {
return nil, err
}
deleted = append(deleted, dproto)
}
}
return &pb.GarbageCollectResponse{Domains: deleted}, nil
}