-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathediag.go
More file actions
213 lines (187 loc) · 6.71 KB
/
ediag.go
File metadata and controls
213 lines (187 loc) · 6.71 KB
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
package veracity
import (
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
"reflect"
"strings"
"time"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/datatrails/go-datatrails-merklelog/massifs/snowflakeid"
"github.com/datatrails/go-datatrails-merklelog/mmr"
"github.com/datatrails/go-datatrails-simplehash/simplehash"
veracityapp "github.com/datatrails/veracity/app"
"github.com/urfave/cli/v2"
)
// NewEventDiagCmd provides diagnostic support for event verification
//
//nolint:gocognit
func NewEventDiagCmd() *cli.Command {
return &cli.Command{Name: "event-log-info",
Aliases: []string{"ediag"},
Usage: "print diagnostics about an events entry in the log",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "bendump", Aliases: []string{"b"}, Value: false},
&cli.StringFlag{
Name: "json", Aliases: []string{"j"},
},
},
Action: func(cCtx *cli.Context) error {
tenantIdentity := cCtx.String("tenant")
appData, err := veracityapp.ReadAppData(cCtx.Args().Len() == 0, cCtx.Args().Get(0))
if err != nil {
return err
}
appEntries, err := veracityapp.AppDataToVerifiableLogEntries(appData, tenantIdentity)
if err != nil {
return err
}
cmd := &CmdCtx{}
if err = cfgMassifReader(cmd, cCtx); err != nil {
return err
}
cmpPrint := func(fmtEq, fmtNe string, a, b any) bool {
if reflect.DeepEqual(a, b) {
fmt.Printf(fmtEq, a)
return true
}
fmt.Printf(fmtNe, a, b)
return false
}
for _, appEntry := range appEntries {
if appEntry.MMRIndex() == 0 {
continue
}
// Get the mmrIndex from the request and then compute the massif
// it implies based on the massifHeight command line option.
mmrIndex := appEntry.MMRIndex()
massifIndex := massifs.MassifIndexFromMMRIndex(cmd.massifHeight, mmrIndex)
tenantIdentity := cCtx.String("tenant")
if tenantIdentity == "" {
// The tenant identity on the event is the original tenant
// that created the event. For public assets and shared
// assets, this is true regardless of which tenancy the
// record is fetched from. Those same events will appear in
// the logs of all tenants they were shared with.
tenantIdentity, err = appEntry.LogTenant()
if err != nil {
return err
}
}
// read the massif blob
cmd.massif, err = cmd.massifReader.GetMassif(context.Background(), tenantIdentity, massifIndex)
if err != nil {
return err
}
// Get the human time from the idtimestamp committed on the event.
idTimestamp, err := appEntry.IDTimestamp(&cmd.massif)
if err != nil {
return err
}
idTimestampWithEpoch := make([]byte, len(idTimestamp)+1)
idTimestampWithEpoch[0] = byte(0) // 0 epoch
copy(idTimestampWithEpoch[1:], idTimestamp)
eventIDTimestamp, _, err := massifs.SplitIDTimestampBytes(idTimestampWithEpoch)
if err != nil {
return err
}
eventIDTimestampMS, err := snowflakeid.IDUnixMilli(eventIDTimestamp, uint8(cmd.massif.Start.CommitmentEpoch))
if err != nil {
return err
}
leafIndex := mmr.LeafIndex(mmrIndex)
// Note that the banner info is all from the event response
fmt.Printf("%d %s %s\n", leafIndex, time.UnixMilli(eventIDTimestampMS).Format(time.RFC3339Nano), appEntry.AppID())
leafIndexMassif, err := cmd.massif.GetMassifLeafIndex(leafIndex)
if err != nil {
return fmt.Errorf("when expecting %d for %d: %v", leafIndexMassif, mmrIndex, err)
}
fmt.Printf(" |%8d leaf-index-massif\n", leafIndexMassif)
// Read the trie entry from the log
logTrieEntry := massifs.GetTrieEntry(cmd.massif.Data, cmd.massif.IndexStart(), leafIndexMassif)
logNodeValue, err := cmd.massif.Get(mmrIndex)
if err != nil {
return err
}
logTrieKey := massifs.GetTrieKey(cmd.massif.Data, cmd.massif.IndexStart(), leafIndexMassif)
logTrieIDTimestampBytes := logTrieEntry[massifs.TrieEntryIdTimestampStart:massifs.TrieEntryIdTimestampEnd]
logTrieIDTimestamp := binary.BigEndian.Uint64(logTrieIDTimestampBytes)
unixMS, err := snowflakeid.IDUnixMilli(logTrieIDTimestamp, uint8(cmd.massif.Start.CommitmentEpoch))
if err != nil {
return err
}
idTime := time.UnixMilli(unixMS)
// TODO: log version 1 uses the uuid bytes of the log tenant
// so we need to handle that here as well as log version 0.
trieKey := massifs.NewTrieKey(
massifs.KeyTypeApplicationContent,
[]byte(tenantIdentity),
[]byte(strings.TrimPrefix(appEntry.AppID(), "public")))
if len(trieKey) != massifs.TrieKeyBytes {
return massifs.ErrIndexEntryBadSize
}
cmpPrint(
" |%x trie-key\n",
" |%x != log-trie-key %x\n", trieKey[:32], logTrieKey[:32])
fmt.Printf(" |%x %s log-idtimestamp\n", logTrieIDTimestampBytes, idTime.Format(time.DateTime))
cmpPrint(
" |%x idtimestamp\n",
" |%x != log-idtimestamp %x\n", eventIDTimestamp, logTrieIDTimestamp)
// Compute the event data hash, independent of domain and idtimestamp
v3Event, err := simplehash.V3FromEventJSON(appEntry.SerializedBytes()) // NOTE for assetsv2 the serialized bytes is actually the event json API response
if err != nil {
return err
}
eventHasher := sha256.New()
if err = simplehash.V3HashEvent(eventHasher, v3Event); err != nil {
return err
}
eventHash := eventHasher.Sum(nil)
fmt.Printf(" |%x v3hash (just the schema fields hashed)\n", eventHash)
if cCtx.Bool("bendump") {
bencode, err2 := bencodeEvent(v3Event)
if err2 != nil {
return err2
}
fmt.Printf(" |%s\n", string(bencode))
}
leafHasher := simplehash.NewHasherV3()
err = leafHasher.HashEventFromV3(
v3Event,
simplehash.WithPrefix([]byte{LeafTypePlain}),
simplehash.WithIDCommitted(eventIDTimestamp))
if err != nil {
return err
}
leafHash := leafHasher.Sum(nil)
ok := cmpPrint(
" |%x leaf\n",
" |%x leaf != log-leaf %x\n", leafHash, logNodeValue)
if !ok {
// if the leaf doesn't match we definitely cant verify it
continue
}
// Generate the proof for the mmrIndex and get the root. We use
// the mmrSize from the end of the blob in which the leaf entry
// was recorded. Any size > than the leaf index would work.
mmrSize := cmd.massif.RangeCount()
proof, err := mmr.InclusionProof(&cmd.massif, mmrSize, mmrIndex)
if err != nil {
return err
}
verified, err := mmr.VerifyInclusion(&cmd.massif, eventHasher, mmrSize, logNodeValue, mmrIndex, proof)
if verified {
fmt.Printf("OK|%d %d\n", mmrIndex, leafIndex)
continue
}
if err != nil {
fmt.Printf("XX|%d %d|%s\n", mmrIndex, leafIndex, err.Error())
continue
}
fmt.Printf("XX|%d %d\n", mmrIndex, leafIndex)
}
return nil
},
}
}