This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
/
history.go
107 lines (95 loc) · 3.01 KB
/
history.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
// Copyright 2016 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 cmd
import (
"context"
"fmt"
"os"
"sort"
"text/tabwriter"
"time"
"github.com/google/keytransparency/core/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
start, end int64
)
// histCmd fetches the account history for a user
var histCmd = &cobra.Command{
Use: "history [user email] [app]",
Short: "Retrieve and verify all keys used for this account",
Long: `Retrieve all user profiles for this account from the key server
and verify that the results are consistent.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("user email needs to be provided")
}
userID := args[0]
timeout := viper.GetDuration("timeout")
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
c, err := GetClient(ctx)
if err != nil {
return fmt.Errorf("error connecting: %v", err)
}
if end == 0 {
// Get the current revision.
slr, smr, err := c.VerifiedGetLatestRevision(ctx)
if err != nil {
return fmt.Errorf("failed to get user: %v", err)
}
if verbose {
fmt.Printf("Got current revision: %v\n", slr.TreeSize-1)
}
end = int64(smr.Revision)
}
roots, profiles, err := c.PaginateHistory(ctx, userID, start, end)
if err != nil {
return fmt.Errorf("failed fetching history: %v", err)
}
compressed, err := client.CompressHistory(profiles)
if err != nil {
return fmt.Errorf("failed compressing history: %v", err)
}
// Sort map heads.
keys := make(uint64Slice, 0, len(compressed))
for k := range compressed {
keys = append(keys, k)
}
sort.Sort(keys)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
fmt.Fprintln(w, "Revision\tTimestamp\tProfile")
for _, e := range keys {
mapRoot := roots[e]
t := time.Unix(0, int64(mapRoot.TimestampNanos))
data := compressed[e]
fmt.Fprintf(w, "%v\t%v\t%v\n", mapRoot.Revision, t.Format(time.UnixDate), data)
}
if err := w.Flush(); err != nil {
return nil
}
return nil
},
}
// uint64Slice satisfies sort.Interface.
type uint64Slice []uint64
func (m uint64Slice) Len() int { return len(m) }
func (m uint64Slice) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m uint64Slice) Less(i, j int) bool { return m[i] < m[j] }
func init() {
RootCmd.AddCommand(histCmd)
histCmd.PersistentFlags().Int64Var(&start, "start", 1, "Start revision")
histCmd.PersistentFlags().Int64Var(&end, "end", 0, "End revision")
}