forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets_get.go
216 lines (181 loc) · 5.44 KB
/
secrets_get.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
213
214
215
216
package main
import (
"context"
"errors"
"flag"
"fmt"
"path/filepath"
"strings"
"github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/privval"
"github.com/gnolang/gno/tm2/pkg/commands"
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/p2p"
)
var errInvalidSecretsGetArgs = errors.New("invalid number of secrets get arguments provided")
type secretsGetCfg struct {
commonAllCfg
raw bool
}
// newSecretsGetCmd creates the secrets get command
func newSecretsGetCmd(io commands.IO) *commands.Command {
cfg := &secretsGetCfg{}
cmd := commands.NewCommand(
commands.Metadata{
Name: "get",
ShortUsage: "secrets get [flags] [<key>]",
ShortHelp: "shows the Gno secrets present in a common directory",
LongHelp: "shows the validator private key, the node p2p key and the validator's last sign state at the given path " +
"by fetching the option specified at <key>",
},
cfg,
func(_ context.Context, args []string) error {
return execSecretsGet(cfg, args, io)
},
)
// Add subcommand helpers
helperGen := metadataHelperGenerator{
MetaUpdate: func(meta *commands.Metadata, inputType string) {
meta.ShortUsage = fmt.Sprintf("secrets get %s <%s>", meta.Name, inputType)
},
TagNameSelector: "json",
TreeDisplay: false,
}
cmd.AddSubCommands(generateSubCommandHelper(helperGen, secrets{}, func(_ context.Context, args []string) error {
return execSecretsGet(cfg, args, io)
})...)
return cmd
}
func (c *secretsGetCfg) RegisterFlags(fs *flag.FlagSet) {
c.commonAllCfg.RegisterFlags(fs)
fs.BoolVar(
&c.raw,
"raw",
false,
"output raw string values, rather than as JSON strings",
)
}
func execSecretsGet(cfg *secretsGetCfg, args []string, io commands.IO) error {
// Make sure the directory is there
if cfg.dataDir == "" || !isValidDirectory(cfg.dataDir) {
return errInvalidDataDir
}
// Make sure the get arguments are valid
if len(args) > 1 {
return errInvalidSecretsGetArgs
}
// Load the secrets from the dir
loadedSecrets, err := loadSecrets(cfg.dataDir)
if err != nil {
return err
}
// Find and print the secrets value, if any
if err := printKeyValue(loadedSecrets, cfg.raw, io, args...); err != nil {
return fmt.Errorf("unable to get secrets value, %w", err)
}
return nil
}
// loadSecrets loads the secrets from the specified data directory
func loadSecrets(dirPath string) (*secrets, error) {
// Construct the file paths
var (
validatorKeyPath = filepath.Join(dirPath, defaultValidatorKeyName)
validatorStatePath = filepath.Join(dirPath, defaultValidatorStateName)
nodeKeyPath = filepath.Join(dirPath, defaultNodeKeyName)
)
var (
vkInfo *validatorKeyInfo
vsInfo *validatorStateInfo
niInfo *nodeIDInfo
err error
)
// Load the secrets
if osm.FileExists(validatorKeyPath) {
vkInfo, err = readValidatorKey(validatorKeyPath)
if err != nil {
return nil, fmt.Errorf("unable to load secrets, %w", err)
}
}
if osm.FileExists(validatorStatePath) {
vsInfo, err = readValidatorState(validatorStatePath)
if err != nil {
return nil, fmt.Errorf("unable to load secrets, %w", err)
}
}
if osm.FileExists(nodeKeyPath) {
niInfo, err = readNodeID(nodeKeyPath)
if err != nil {
return nil, fmt.Errorf("unable to load secrets, %w", err)
}
}
return &secrets{
ValidatorKeyInfo: vkInfo,
ValidatorStateInfo: vsInfo,
NodeIDInfo: niInfo,
}, nil
}
// readValidatorKey reads the validator key from the given path
func readValidatorKey(path string) (*validatorKeyInfo, error) {
validatorKey, err := readSecretData[privval.FilePVKey](path)
if err != nil {
return nil, fmt.Errorf("unable to read validator key, %w", err)
}
return &validatorKeyInfo{
Address: validatorKey.Address.String(),
PubKey: validatorKey.PubKey.String(),
}, nil
}
// readValidatorState reads the validator state from the given path
func readValidatorState(path string) (*validatorStateInfo, error) {
validatorState, err := readSecretData[privval.FilePVLastSignState](path)
if err != nil {
return nil, fmt.Errorf("unable to read validator state, %w", err)
}
return &validatorStateInfo{
Height: validatorState.Height,
Round: validatorState.Round,
Step: validatorState.Step,
Signature: validatorState.Signature,
SignBytes: validatorState.SignBytes,
}, nil
}
// readNodeID reads the node p2p info from the given path
func readNodeID(path string) (*nodeIDInfo, error) {
nodeKey, err := readSecretData[p2p.NodeKey](path)
if err != nil {
return nil, fmt.Errorf("unable to read node key, %w", err)
}
// Construct the config path
var (
nodeDir = filepath.Join(filepath.Dir(path), "..")
configPath = constructConfigPath(nodeDir)
cfg = config.DefaultConfig()
)
// Check if there is an existing config file
if osm.FileExists(configPath) {
// Attempt to grab the config from disk
cfg, err = config.LoadConfig(nodeDir)
if err != nil {
return nil, fmt.Errorf("unable to load config file, %w", err)
}
}
return &nodeIDInfo{
ID: nodeKey.ID().String(),
P2PAddress: constructP2PAddress(nodeKey.ID(), cfg.P2P.ListenAddress),
}, nil
}
// constructP2PAddress constructs the P2P address other nodes can use
// to connect directly
func constructP2PAddress(nodeID p2p.ID, listenAddress string) string {
var (
address string
parts = strings.SplitN(listenAddress, "://", 2)
)
switch len(parts) {
case 2:
address = parts[1]
default:
address = listenAddress
}
return fmt.Sprintf("%s@%s", nodeID, address)
}