-
Notifications
You must be signed in to change notification settings - Fork 164
/
ranges.go
200 lines (178 loc) · 5.35 KB
/
ranges.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
//
// Copyright 2021 The Sigstore 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 sharding
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"strconv"
"strings"
"github.com/ghodss/yaml"
"github.com/google/trillian"
"github.com/google/trillian/types"
"github.com/sigstore/rekor/pkg/log"
)
type LogRanges struct {
inactive Ranges
active int64
}
type Ranges []LogRange
type LogRange struct {
TreeID int64 `json:"treeID"`
TreeLength int64 `json:"treeLength"`
EncodedPublicKey string `json:"encodedPublicKey"`
decodedPublicKey string
}
func NewLogRanges(ctx context.Context, logClient trillian.TrillianLogClient, path string, treeID uint) (LogRanges, error) {
if path == "" {
log.Logger.Info("No config file specified, skipping init of logRange map")
return LogRanges{}, nil
}
if treeID == 0 {
return LogRanges{}, errors.New("non-zero tlog_id required when passing in shard config filepath; please set the active tree ID via the `--trillian_log_server.tlog_id` flag")
}
// otherwise, try to read contents of the sharding config
ranges, err := logRangesFromPath(path)
if err != nil {
return LogRanges{}, fmt.Errorf("log ranges from path: %w", err)
}
for i, r := range ranges {
r, err := updateRange(ctx, logClient, r)
if err != nil {
return LogRanges{}, fmt.Errorf("updating range for tree id %d: %w", r.TreeID, err)
}
ranges[i] = r
}
log.Logger.Info("Ranges: %v", ranges)
return LogRanges{
inactive: ranges,
active: int64(treeID),
}, nil
}
func logRangesFromPath(path string) (Ranges, error) {
var ranges Ranges
contents, err := ioutil.ReadFile(path)
if err != nil {
return Ranges{}, err
}
if string(contents) == "" {
log.Logger.Info("Sharding config file contents empty, skipping init of logRange map")
return Ranges{}, nil
}
if err := yaml.Unmarshal(contents, &ranges); err != nil {
return Ranges{}, err
}
return ranges, nil
}
// updateRange fills in any missing information about the range
func updateRange(ctx context.Context, logClient trillian.TrillianLogClient, r LogRange) (LogRange, error) {
// If a tree length wasn't passed in, get it ourselves
if r.TreeLength == 0 {
resp, err := logClient.GetLatestSignedLogRoot(ctx, &trillian.GetLatestSignedLogRootRequest{LogId: r.TreeID})
if err != nil {
return LogRange{}, fmt.Errorf("getting signed log root for tree %d: %w", r.TreeID, err)
}
var root types.LogRootV1
if err := root.UnmarshalBinary(resp.SignedLogRoot.LogRoot); err != nil {
return LogRange{}, err
}
r.TreeLength = int64(root.TreeSize)
}
// If a public key was provided, decode it
if r.EncodedPublicKey != "" {
decoded, err := base64.StdEncoding.DecodeString(r.EncodedPublicKey)
if err != nil {
return LogRange{}, err
}
r.decodedPublicKey = string(decoded)
}
return r, nil
}
func (l *LogRanges) ResolveVirtualIndex(index int) (int64, int64) {
indexLeft := index
for _, l := range l.inactive {
if indexLeft < int(l.TreeLength) {
return l.TreeID, int64(indexLeft)
}
indexLeft -= int(l.TreeLength)
}
// If index not found in inactive trees, return the active tree
return l.active, int64(indexLeft)
}
func (l *LogRanges) ActiveTreeID() int64 {
return l.active
}
func (l *LogRanges) NoInactive() bool {
return l.inactive == nil
}
// TotalInactiveLength returns the total length across all inactive shards;
// we don't know the length of the active shard.
func (l *LogRanges) TotalInactiveLength() int64 {
var total int64
for _, r := range l.inactive {
total += r.TreeLength
}
return total
}
func (l *LogRanges) SetInactive(r []LogRange) {
l.inactive = r
}
func (l *LogRanges) GetInactive() []LogRange {
return l.inactive
}
func (l *LogRanges) AppendInactive(r LogRange) {
l.inactive = append(l.inactive, r)
}
func (l *LogRanges) SetActive(i int64) {
l.active = i
}
func (l *LogRanges) GetActive() int64 {
return l.active
}
func (l *LogRanges) String() string {
ranges := []string{}
for _, r := range l.inactive {
ranges = append(ranges, fmt.Sprintf("%d=%d", r.TreeID, r.TreeLength))
}
ranges = append(ranges, fmt.Sprintf("active=%d", l.active))
return strings.Join(ranges, ",")
}
// PublicKey returns the associated public key for the given Tree ID
// and returns the active public key by default
func (l *LogRanges) PublicKey(activePublicKey, treeID string) (string, error) {
// if no tree ID is specified, assume the active tree
if treeID == "" {
return activePublicKey, nil
}
tid, err := strconv.Atoi(treeID)
if err != nil {
return "", err
}
for _, i := range l.inactive {
if int(i.TreeID) == tid {
if i.decodedPublicKey != "" {
return i.decodedPublicKey, nil
}
// assume the active public key if one wasn't provided
return activePublicKey, nil
}
}
if tid == int(l.active) {
return activePublicKey, nil
}
return "", fmt.Errorf("%d is not a valid tree ID and doesn't have an associated public key", tid)
}