forked from YahooArchive/coname
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (167 loc) · 5.54 KB
/
main.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
// Copyright 2014-2015 The Dename Authors.
//
// 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 keyserver
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/signal"
"path"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/agl/ed25519"
"github.com/andres-erbsen/clock"
"github.com/syndtr/goleveldb/leveldb"
"github.com/yahoo/coname/keyserver/kv/leveldbkv"
"github.com/yahoo/coname/keyserver/replication/raftlog"
raftproto "github.com/yahoo/coname/keyserver/replication/raftlog/proto"
"github.com/yahoo/coname/proto"
"github.com/yahoo/coname/vrf"
)
func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
// Copied from the parsePrivateKey function in crypto/tls
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
switch key := key.(type) {
case *rsa.PrivateKey, *ecdsa.PrivateKey:
return key, nil
default:
return nil, fmt.Errorf("found unknown private key type in PKCS#8 wrapping")
}
}
if key, err := x509.ParseECPrivateKey(der); err == nil {
return key, nil
}
return nil, fmt.Errorf("failed to parse private key")
}
// This getKey interprets key IDs as paths, and loads private keys from the
// specified file
func getKey(keyid string) (crypto.PrivateKey, error) {
fileContents, err := ioutil.ReadFile(keyid)
if err != nil {
return nil, err
}
if path.Ext(keyid) == ".ed25519secret" {
if got, want := len(fileContents), ed25519.PrivateKeySize; got != want {
return nil, fmt.Errorf("ed25519 private key %s has wrong size %d (want %d)", keyid, got, want)
}
var keyArray [ed25519.PrivateKeySize]uint8
copy(keyArray[:], fileContents)
return &keyArray, nil
} else if path.Ext(keyid) == ".vrfsecret" {
if got, want := len(fileContents), vrf.SecretKeySize; got != want {
return nil, fmt.Errorf("VRF private key %s has wrong size %d (want %d)", keyid, got, want)
}
var keyArray [vrf.SecretKeySize]uint8
copy(keyArray[:], fileContents)
return &keyArray, nil
} else {
keyPEM := fileContents
var keyDER *pem.Block
for {
keyDER, keyPEM = pem.Decode(keyPEM)
if keyDER == nil {
return nil, fmt.Errorf("failed to parse key PEM in %s", keyid)
}
if keyDER.Type == "PRIVATE KEY" || strings.HasSuffix(keyDER.Type, " PRIVATE KEY") {
break
}
}
return parsePrivateKey(keyDER.Bytes)
}
}
func majority(nReplicas int) int {
return nReplicas/2 + 1
}
func RunWithConfig(cfg *proto.ReplicaConfig) {
// TODO: since we only want to support precisely this ratification policy,
// this should be moved into server.go
ratificationPolicy := &proto.AuthorizationPolicy{
PublicKeys: make(map[uint64]*proto.PublicKey),
PolicyType: &proto.AuthorizationPolicy_Quorum{&proto.QuorumExpr{
Threshold: uint32(majority(len(cfg.KeyserverConfig.InitialReplicas)))},
},
}
replicaIDs := []uint64{}
for _, replica := range cfg.KeyserverConfig.InitialReplicas {
replicaIDs = append(replicaIDs, replica.ID)
replicaExpr := &proto.QuorumExpr{
Threshold: 1,
}
for _, pk := range replica.PublicKeys {
pkid := proto.KeyID(pk)
ratificationPolicy.PublicKeys[pkid] = pk
replicaExpr.Candidates = append(replicaExpr.Candidates, pkid)
}
ratificationPolicy.PolicyType.(*proto.AuthorizationPolicy_Quorum).Quorum.Subexpressions = append(ratificationPolicy.PolicyType.(*proto.AuthorizationPolicy_Quorum).Quorum.Subexpressions, replicaExpr)
}
leveldb, err := leveldb.OpenFile(cfg.LevelDBPath, nil)
if err != nil {
log.Fatalf("Couldn't open DB in directory %s: %s", cfg.LevelDBPath, err)
}
db := leveldbkv.Wrap(leveldb)
clk := clock.New()
raftListener, err := net.Listen("tcp", cfg.RaftAddr)
if err != nil {
log.Fatalf("Couldn't bind to Raft node address %s: %s", cfg.RaftAddr, err)
}
defer raftListener.Close()
raftTLS, err := cfg.RaftTLS.Config(getKey)
if err != nil {
log.Fatalf("Bad Raft TLS configuration: %s", err)
}
raftCreds := credentials.NewTLS(raftTLS)
raftServer := grpc.NewServer(grpc.Creds(raftCreds))
go raftServer.Serve(raftListener)
defer raftServer.Stop()
dialRaftPeer := func(id uint64) raftproto.RaftClient {
// TODO use current, not initial, config
for _, replica := range cfg.KeyserverConfig.InitialReplicas {
if replica.ID == id {
conn, err := grpc.Dial(replica.RaftAddr, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{RootCAs: raftTLS.RootCAs})))
if err != nil {
log.Panicf("Raft GRPC dial failed: %s", err)
}
return raftproto.NewRaftClient(conn)
}
}
log.Panicf("No raft peer %x in configuration", id)
return nil
}
raft := raftlog.New(
cfg.ReplicaID, replicaIDs, db, []byte{tableReplicationLogPrefix},
clk, cfg.RaftHeartbeat.Duration(), raftServer, dialRaftPeer,
)
defer raft.Stop()
server, err := Open(cfg, db, raft, ratificationPolicy, clk, getKey, net.LookupTXT)
if err != nil {
log.Fatalf("Failed to initialize keyserver: %s", err)
}
server.Start()
defer server.Stop()
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch
}