forked from nats-io/nsc
/
listkeys.go
212 lines (190 loc) · 5.99 KB
/
listkeys.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
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2018-2019 The NATS 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 cmd
import (
"errors"
"fmt"
"os"
"github.com/kbehouse/nsc/cmd/store"
"github.com/nats-io/nkeys"
"github.com/spf13/cobra"
"github.com/xlab/tablewriter"
)
func createListKeysCmd() *cobra.Command {
var params ListKeysParams
cmd := &cobra.Command{
Use: "keys",
Short: "List operator, account and user keys in the current operator and account context",
Long: `List operator, account and user keys in the current operator and account context.
Additional flags allow you to specify which types of keys to display. For example
the --operator shows the operator key, the --accounts show account keys, etc.
You can further limit the account and user displayed by specifying the
--account and --user flags respectively. To list all keys specify
the --all flag.
The --not-referenced flag displays all keys not relevant to the current
operator, accounts and users. These keys may be referenced in a different
operator context.
The --filter flag allows you to specify a few letters in a public key
and display only those keys that match provided the --operator,
--accounts, and --user or --all flags match the key type.
`,
Example: `nsc list keys
nsc list keys --all (same as specifying --operator --accounts --users)
nsc list keys --operator --not-referenced (shows all other operator keys)
nsc list keys --all --filter VSVMGA (shows all keys containing the filter)
nsc list keys --account A (changes the account context to the specified account)
`,
Args: MaxArgs(0),
SilenceUsage: false,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunMaybeStorelessAction(cmd, args, ¶ms); err != nil {
return err
}
return nil
},
}
cmd.Flags().BoolVarP(¶ms.Operator, "operator", "o", false, "show operator keys")
cmd.Flags().BoolVarP(¶ms.Accounts, "accounts", "a", false, "show account keys")
cmd.Flags().BoolVarP(¶ms.Users, "users", "u", false, "show user keys")
cmd.Flags().StringVarP(¶ms.Account, "account", "", "", "show specified account keys")
cmd.Flags().StringVarP(¶ms.User, "user", "", "", "show specified user key")
cmd.Flags().BoolVarP(¶ms.All, "all", "A", false, "show operator, accounts and users")
cmd.Flags().StringVarP(¶ms.Filter, "filter", "f", "", "filter keys containing string")
cmd.Flags().BoolVarP(¶ms.Unreferenced, "not-referenced", "", false, "shows keys that are not referenced in the current operator context")
cmd.Flags().BoolVarP(¶ms.Seeds, "show-seeds", "S", false, "shows seed keys value")
return cmd
}
func init() {
listCmd.AddCommand(createListKeysCmd())
}
type ListKeysParams struct {
Seeds bool
KeyCollectorParams
KS store.KeyStore
}
func (p *ListKeysParams) SetDefaults(ctx ActionCtx) error {
p.KS = ctx.StoreCtx().KeyStore
return p.KeyCollectorParams.SetDefaults(ctx)
}
func (p *ListKeysParams) PreInteractive(ctx ActionCtx) error {
return nil
}
func (p *ListKeysParams) Load(ctx ActionCtx) error {
return nil
}
func (p *ListKeysParams) PostInteractive(ctx ActionCtx) error {
return nil
}
func (p *ListKeysParams) Validate(ctx ActionCtx) error {
kdir := store.GetKeysDir()
_, err := os.Stat(kdir)
if os.IsNotExist(err) {
return fmt.Errorf("keystore %#q does not exist", kdir)
}
if ctx.StoreCtx().Operator.Name == "" && !p.Unreferenced {
return errors.New("operator is not set -- set an operator first or try --not-referenced to list all keys not in the context")
}
if p.Unreferenced && p.Seeds {
return errors.New("specify one of --show-seeds or --not-referenced")
}
return nil
}
func (p *ListKeysParams) Run(ctx ActionCtx) (store.Status, error) {
var err error
var keys Keys
keys.KeyList, err = p.KeyCollectorParams.Run(ctx)
keys.MessageFn = p.Report
return keys, err
}
func (p *ListKeysParams) Report(ks Keys) string {
if ks.Len() == 0 {
return "no keys matched query"
}
if p.Seeds {
return p.ReportSeeds(ks)
}
var hasUnreferenced bool
table := tablewriter.CreateTable()
table.AddTitle("Keys")
table.AddHeaders("Entity", "Key", "Signing Key", "Stored")
for _, k := range ks.KeyList {
unreferenced := false
if k.Name == "?" {
hasUnreferenced = true
unreferenced = true
}
sk := ""
if k.Signing {
sk = "*"
}
stored := ""
if k.HasKey() {
stored = "*"
}
if k.Invalid {
stored = "BAD"
}
pad := ""
if !unreferenced {
switch k.ExpectedKind {
case nkeys.PrefixByteAccount:
pad = " "
case nkeys.PrefixByteUser:
pad = " "
}
}
n := fmt.Sprintf("%s%s", pad, k.Name)
table.AddRow(n, k.Pub, sk, stored)
}
s := table.Render()
if hasUnreferenced {
s = fmt.Sprintf("%s[?] unreferenced key - may belong to a different operator context", s)
}
return s
}
func (p *ListKeysParams) ReportSeeds(ks Keys) string {
table := tablewriter.CreateTable()
table.AddTitle("Seeds Keys")
table.AddHeaders("Entity", "Private Key", "Signing Key")
for _, k := range ks.KeyList {
sk := ""
if k.Signing {
sk = "*"
}
pad := ""
switch k.ExpectedKind {
case nkeys.PrefixByteAccount:
pad = " "
case nkeys.PrefixByteUser:
pad = " "
}
n := fmt.Sprintf("%s%s", pad, k.Name)
if k.Invalid || k.KeyPath == "" {
k.Pub = fmt.Sprintf("%s [!]", k.Pub)
} else {
seed, err := p.KS.GetSeed(k.Pub)
if err != nil {
k.Pub = fmt.Sprintf("%s [ERR]", k.Pub)
} else {
k.Pub = seed
}
}
table.AddRow(n, k.Pub, sk)
}
s := table.Render()
s = fmt.Sprintf("%s[ ! ] seed is not stored\n", s)
s = fmt.Sprintf("%s[ERR] error reading seed\n", s)
return s
}