forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd_dump_common.go
191 lines (159 loc) · 4.93 KB
/
md_dump_common.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
package main
import (
"fmt"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/keybase/kbfs/kbfscodec"
"github.com/keybase/kbfs/kbfscrypto"
"github.com/keybase/kbfs/kbfsmd"
"github.com/keybase/kbfs/libkbfs"
"golang.org/x/net/context"
)
// replacementMap is a map from a string to its replacement, intended
// to provide human-readable strings to UIDs and KIDs. It is usually
// created once per invocation and plumbed through everything that
// uses it, filling it on-demand so as to avoid needless calls to the
// service.
type replacementMap map[string]string
func mdDumpGetDeviceStringForCryptPublicKey(k kbfscrypto.CryptPublicKey, ui libkbfs.UserInfo) (
string, bool) {
deviceName, ok := ui.KIDNames[k.KID()]
if !ok {
return "", false
}
if revokedTime, ok := ui.RevokedCryptPublicKeys[k]; ok {
return fmt.Sprintf("%s (revoked %s) (kid:%s)",
deviceName, revokedTime.Time(), k), true
}
return fmt.Sprintf("%s (kid:%s)", deviceName, k), true
}
func mdDumpGetDeviceStringForVerifyingKey(k kbfscrypto.VerifyingKey, ui libkbfs.UserInfo) (
string, bool) {
deviceName, ok := ui.KIDNames[k.KID()]
if !ok {
return "", false
}
if revokedTime, ok := ui.RevokedVerifyingKeys[k]; ok {
return fmt.Sprintf("%s (revoked %s) (kid:%s)",
deviceName, revokedTime.Time(), k), true
}
return fmt.Sprintf("%s (kid:%s)", deviceName, k), true
}
func mdDumpFillReplacements(ctx context.Context, codec kbfscodec.Codec,
service libkbfs.KeybaseService, prefix string,
rmd kbfsmd.RootMetadata, extra kbfsmd.ExtraMetadata,
replacements replacementMap) error {
writers, readers, err := rmd.GetUserDevicePublicKeys(extra)
if err != nil {
return err
}
for _, userKeys := range []kbfsmd.UserDevicePublicKeys{writers, readers} {
for u := range userKeys {
// Make sure to only make one Resolve and one
// LoadUserPlusKeys call per user for a single
// replacements map.
if _, ok := replacements[u.String()]; ok {
continue
}
username, _, err := service.Resolve(
ctx, fmt.Sprintf("uid:%s", u))
if err == nil {
replacements[u.String()] = fmt.Sprintf(
"%s (uid:%s)", username, u)
} else {
replacements[u.String()] = fmt.Sprintf(
"<unknown username> (uid:%s)", u)
printError(prefix, err)
}
ui, err := service.LoadUserPlusKeys(ctx, u, "")
if err != nil {
printError(prefix, err)
continue
}
for _, k := range ui.CryptPublicKeys {
if _, ok := replacements[k.String()]; ok {
continue
}
if deviceStr, ok := mdDumpGetDeviceStringForCryptPublicKey(k, ui); ok {
replacements[k.String()] = deviceStr
}
}
for k := range ui.RevokedCryptPublicKeys {
if _, ok := replacements[k.String()]; ok {
continue
}
if deviceStr, ok := mdDumpGetDeviceStringForCryptPublicKey(k, ui); ok {
replacements[k.String()] = deviceStr
}
}
for _, k := range ui.VerifyingKeys {
if _, ok := replacements[k.String()]; ok {
continue
}
if deviceStr, ok := mdDumpGetDeviceStringForVerifyingKey(k, ui); ok {
replacements[k.String()] = deviceStr
}
}
for k := range ui.RevokedVerifyingKeys {
if _, ok := replacements[k.String()]; ok {
continue
}
if deviceStr, ok := mdDumpGetDeviceStringForVerifyingKey(k, ui); ok {
replacements[k.String()] = deviceStr
}
}
}
}
return nil
}
func mdDumpReplaceAll(s string, replacements replacementMap) string {
for old, new := range replacements {
s = strings.Replace(s, old, new, -1)
}
return s
}
func mdDumpReadOnlyRMDWithReplacements(
ctx context.Context, codec kbfscodec.Codec,
replacements replacementMap, rmd libkbfs.ReadOnlyRootMetadata) error {
c := spew.NewDefaultConfig()
c.Indent = " "
c.DisablePointerAddresses = true
c.DisableCapacities = true
c.SortKeys = true
fmt.Print("Root metadata\n")
fmt.Print("-------------\n")
brmdDump, err := kbfsmd.DumpRootMetadata(codec, rmd.GetBareRootMetadata())
if err != nil {
return err
}
fmt.Printf("%s\n", mdDumpReplaceAll(brmdDump, replacements))
fmt.Print("Extra metadata\n")
fmt.Print("--------------\n")
extraDump, err := kbfsmd.DumpExtraMetadata(codec, rmd.Extra())
if err != nil {
return err
}
fmt.Printf("%s\n", mdDumpReplaceAll(extraDump, replacements))
fmt.Print("Private metadata\n")
fmt.Print("----------------\n")
pmdDump, err := libkbfs.DumpPrivateMetadata(
codec, len(rmd.GetSerializedPrivateMetadata()), *rmd.Data())
if err != nil {
return err
}
// Let the caller provide a trailing newline (if desired).
fmt.Printf("%s", mdDumpReplaceAll(pmdDump, replacements))
return nil
}
func mdDumpReadOnlyRMD(ctx context.Context, config libkbfs.Config,
prefix string, replacements replacementMap,
rmd libkbfs.ReadOnlyRootMetadata) error {
err := mdDumpFillReplacements(
ctx, config.Codec(), config.KeybaseService(),
prefix, rmd.GetBareRootMetadata(), rmd.Extra(), replacements)
if err != nil {
printError(prefix, err)
}
return mdDumpReadOnlyRMDWithReplacements(
ctx, config.Codec(), replacements, rmd)
}