-
Notifications
You must be signed in to change notification settings - Fork 504
/
secrets_init.go
151 lines (120 loc) · 3.43 KB
/
secrets_init.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
package init
import (
"fmt"
"github.com/spf13/cobra"
"github.com/0xPolygon/polygon-edge/command"
)
const (
// maxInitNum is the maximum value for "num" flag
maxInitNum = 30
)
var (
errInvalidNum = fmt.Errorf("num flag value should be between 1 and %d", maxInitNum)
basicParams initParams
initNumber int
)
func GetCommand() *cobra.Command {
secretsInitCmd := &cobra.Command{
Use: "init",
Short: "Initializes private keys for the Polygon Edge (Validator + Networking) " +
"to the specified Secrets Manager",
PreRunE: runPreRun,
Run: runCommand,
}
setFlags(secretsInitCmd)
return secretsInitCmd
}
func setFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(
&basicParams.dataDir,
dataDirFlag,
"",
"the directory for the Polygon Edge data if the local FS is used",
)
cmd.Flags().StringVar(
&basicParams.configPath,
configFlag,
"",
"the path to the SecretsManager config file, "+
"if omitted, the local FS secrets manager is used",
)
cmd.Flags().IntVar(
&initNumber,
numFlag,
1,
"the flag indicating how many secrets should be created, only for the local FS",
)
// Don't accept data-dir and config flags because they are related to different secrets managers.
// data-dir is about the local FS as secrets storage, config is about remote secrets manager.
cmd.MarkFlagsMutuallyExclusive(dataDirFlag, configFlag)
// num flag should be used with data-dir flag only so it should not be used with config flag.
cmd.MarkFlagsMutuallyExclusive(numFlag, configFlag)
cmd.Flags().BoolVar(
&basicParams.generatesECDSA,
ecdsaFlag,
true,
"the flag indicating whether new ECDSA key is created",
)
cmd.Flags().BoolVar(
&basicParams.generatesNetwork,
networkFlag,
true,
"the flag indicating whether new Network key is created",
)
cmd.Flags().BoolVar(
&basicParams.generatesBLS,
blsFlag,
true,
"the flag indicating whether new BLS key is created",
)
cmd.Flags().BoolVar(
&basicParams.insecureLocalStore,
insecureLocalStoreFlag,
false,
"the flag indicating should the secrets stored on the local storage be encrypted",
)
}
func runPreRun(_ *cobra.Command, _ []string) error {
if initNumber < 1 || initNumber > maxInitNum {
return errInvalidNum
}
return basicParams.validateFlags()
}
func runCommand(cmd *cobra.Command, _ []string) {
outputter := command.InitializeOutputter(cmd)
defer outputter.WriteOutput()
paramsList := getParamsList()
results := make(command.Results, len(paramsList))
for i, params := range paramsList {
if err := params.initSecrets(); err != nil {
outputter.SetError(err)
return
}
res, err := params.getResult()
if err != nil {
outputter.SetError(err)
return
}
results[i] = res
}
outputter.SetCommandResult(results)
}
// getParamsList creates a list of initParams with num elements.
// This function basically copies the given initParams but updating dataDir by applying an index.
func getParamsList() []initParams {
if initNumber == 1 {
return []initParams{basicParams}
}
paramsList := make([]initParams, initNumber)
for i := 1; i <= initNumber; i++ {
paramsList[i-1] = initParams{
dataDir: fmt.Sprintf("%s%d", basicParams.dataDir, i),
configPath: basicParams.configPath,
generatesECDSA: basicParams.generatesECDSA,
generatesBLS: basicParams.generatesBLS,
generatesNetwork: basicParams.generatesNetwork,
insecureLocalStore: basicParams.insecureLocalStore,
}
}
return paramsList
}