switch ipfs key import/export to use files instead of strings
Adin Schmahmann committed
Aug 3, 2020 at 15:46 UTC
7f15be2ae570fea8f7a9b019b6863cd8adfb7798
3 files changed
+113
-63
core/commands/keystore.go
+52
-27
@@ -1,17 +1,20 @@
1
package commands
2
3
import (
4
+ "bytes"
5
"fmt"
6
"io"
7
+ "io/ioutil"
8
+ "os"
9
"text/tabwriter"
10
11
cmds "github.com/ipfs/go-ipfs-cmds"
12
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
13
+ "github.com/ipfs/go-ipfs/core/commands/e"
14
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
15
options "github.com/ipfs/interface-go-ipfs-core/options"
16
"github.com/libp2p/go-libp2p-core/crypto"
17
peer "github.com/libp2p/go-libp2p-core/peer"
14
- "github.com/mr-tron/base58/base58"
18
mbase "github.com/multiformats/go-multibase"
19
)
20
@@ -47,10 +50,6 @@ type KeyOutput struct {
50
Id string
51
}
52
50
-type ExportKeyOutput struct {
51
- Sk string
52
-}
53
-
53
type KeyOutputList struct {
54
Keys []KeyOutput
55
}
@@ -152,22 +151,22 @@ func formatID(id peer.ID, formatLabel string) string {
151
}
152
}
153
155
-func encodeSKForExport(sk crypto.PrivKey) (string, error) {
156
- data, err := crypto.MarshalPrivateKey(sk)
157
- if err != nil {
158
- return "", err
159
- }
160
- return base58.Encode(data), nil
161
-}
162
-
154
var keyExportCmd = &cmds.Command{
155
Helptext: cmds.HelpText{
156
Tagline: "Export a keypair",
157
+ ShortDescription: `
158
+Exports a named libp2p key to disk.
159
+
160
+By default, the output will be stored at './<key-name>', but an alternate
161
+path can be specified with '--output=<path>' or '-o=<path>'.
162
+`,
163
},
164
Arguments: []cmds.Argument{
165
cmds.StringArg("name", true, false, "name of key to export").EnableStdin(),
166
},
170
- Options: []cmds.Option{},
167
+ Options: []cmds.Option{
168
+ cmds.StringOption(outputOptionName, "o", "The path where the output should be stored."),
169
+ },
170
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
171
name := req.Arguments[0]
172
@@ -191,22 +190,44 @@ var keyExportCmd = &cmds.Command{
190
return fmt.Errorf("key with name '%s' doesn't exist", name)
191
}
192
194
- encoded, err := encodeSKForExport(sk)
193
+ encoded, err := crypto.MarshalPrivateKey(sk)
194
if err != nil {
195
return err
196
}
197
199
- return cmds.EmitOnce(res, &ExportKeyOutput{
200
- Sk: encoded,
201
- })
198
+ return res.Emit(bytes.NewReader(encoded))
199
},
203
- Encoders: cmds.EncoderMap{
204
- cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ko *ExportKeyOutput) error {
205
- _, err := w.Write([]byte(ko.Sk + "\n"))
206
- return err
207
- }),
200
+ PostRun: cmds.PostRunMap{
201
+ cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
202
+ req := res.Request()
203
+
204
+ v, err := res.Next()
205
+ if err != nil {
206
+ return err
207
+ }
208
+
209
+ outReader, ok := v.(io.Reader)
210
+ if !ok {
211
+ return e.New(e.TypeErr(outReader, v))
212
+ }
213
+
214
+ outPath := getOutPath(req)
215
+
216
+ // create file
217
+ file, err := os.Create(outPath)
218
+ if err != nil {
219
+ return err
220
+ }
221
+ defer file.Close()
222
+
223
+ _, err = io.Copy(file, outReader)
224
+ if err != nil {
225
+ return err
226
+ }
227
+
228
+ return nil
229
+ },
230
},
209
- Type: ExportKeyOutput{},
231
}
232
233
var keyImportCmd = &cmds.Command{
@@ -218,7 +239,7 @@ var keyImportCmd = &cmds.Command{
239
},
240
Arguments: []cmds.Argument{
241
cmds.StringArg("name", true, false, "name to associate with key in keychain"),
221
- cmds.StringArg("key", true, false, "key provided by generate or export"),
242
+ cmds.FileArg("key", true, false, "key provided by generate or export"),
243
},
244
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
245
name := req.Arguments[0]
@@ -227,9 +248,13 @@ var keyImportCmd = &cmds.Command{
248
return fmt.Errorf("cannot import key with name 'self'")
249
}
250
230
- encoded := req.Arguments[1]
251
+ file, err := cmdenv.GetFileArg(req.Files.Entries())
252
+ if err != nil {
253
+ return err
254
+ }
255
+ defer file.Close()
256
232
- data, err := base58.Decode(encoded)
257
+ data, err := ioutil.ReadAll(file)
258
if err != nil {
259
return err
260
}
test/sharness/lib/test-lib.sh
-16
@@ -483,22 +483,6 @@ test_check_ed25519_b36cid_peerid() {
483
}
484
}
485
486
-test_check_rsa2048_sk() {
487
- sklen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
488
- if (($sklen < 1600)); then
489
- echo "Bad RSA2048 sk '$1' with len '$sklen'"
490
- return 1
491
- fi
492
-}
493
-
494
-test_check_ed25519_sk() {
495
- sklen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
496
- test "$sklen" = "93" || {
497
- echo "Bad ED25519 sk '$1' with len '$sklen'"
498
- return 1
499
- }
500
-}
501
-
486
convert_tcp_maddr() {
487
echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }'
488
}
test/sharness/t0165-keystore.sh
+61
-20
@@ -18,8 +18,9 @@ test_check_rsa2048_b58mh_peerid $PEERID
18
'
19
20
test_expect_success "test RSA key sk export format" '
21
-SK=$(ipfs key export key_rsa) &&
22
-test_check_rsa2048_sk $SK
21
+ipfs key export key_rsa &&
22
+test_check_rsa2048_sk key_rsa &&
23
+rm key_rsa
24
'
25
26
test_expect_success "test RSA key B36CID multihash format" '
@@ -34,8 +35,9 @@ test_check_ed25519_b36cid_peerid $PEERID
35
'
36
37
test_expect_success "test ED25519 key sk export format" '
37
-SK=$(ipfs key export key_ed25519) &&
38
-test_check_ed25519_sk $SK
38
+ipfs key export key_ed25519 &&
39
+test_check_ed25519_sk key_ed25519 &&
40
+rm key_ed25519
41
'
42
43
test_expect_success "test ED25519 key B36CID multihash format" '
@@ -47,36 +49,59 @@ ipfs key rm key_ed25519
49
50
51
test_expect_success "create a new rsa key" '
50
- rsahash=$(ipfs key gen -f=b58mh foobarsa --type=rsa --size=2048)
52
+ rsahash=$(ipfs key gen -f=b58mh generated_rsa_key --type=rsa --size=2048)
53
+ echo $rsahash > rsa_key_id
54
'
55
56
test_expect_success "create a new ed25519 key" '
54
- edhash=$(ipfs key gen -f=b58mh bazed --type=ed25519)
57
+ edhash=$(ipfs key gen -f=b58mh generated_ed25519_key --type=ed25519)
58
+ echo $edhash > ed25519_key_id
59
'
56
-
57
- test_expect_success "import an rsa key" '
58
- echo "B9bLmHeKLQU1hX23meSn2kJiNW7AZ31C6PBNSYumejXB13vxSVvViZDkEnchAH4BTs9yVnBNZKZYLwykaCohzxTntesTCVaBhZR2Br2Swav2NXwVBhfrUbaBTrR2248KfbVxSiUdkpFn8kcAmUGwp2KGMGRmq85WreGFDdAvzz8ruN2EFfWSHLc1YeUxHeUgKsQm3N13uF7q5x4qvjWM6yvMWNY7JtZrihT8BZQhgb2ezR4iYPjXZTtPZWsLbrrhUvHhxSn1NsTw6NZ7Jbs84qMXXnH56BmT8J9LugRhQQvgBquSoS1m7aeD2y1L1A7mueVDYGLDzgxRSt5CohY7VVMdheUpPiq44CicuYt5YgbbuE3wMHntn6sd9QSYw7f4SjjKdw7Jhy5fNW29SkHvpLfZKfqzBNhSfHsofXVEASDpfr5mqws1eQTztqvvZhHXQxAoxxP3PK6TfDnATdLdV3Cy5v6nLt7ppsBj54hif5EZneHMLeYP8bYLbELQ2fZdoprpnKVBsMY1nWvgrMKUTpLjoKB6bzAZYuXaYVtmrdhESmKCyE12yEyvCcD8DJQU6JjqaD1DyVSPNkL3ze26Mm3ZyiFEH7M4XirUsPLrsj41Qt1xGaVGNEdkdthihytTZxxnmgyAptZMUNfSviBfH1tVbfoXFtBGU8eYMLdSHFqxSktT1mqeiWatxMQZ8pTeA9VCvAp9RTSRFkTQR9uP6w6qTzRD9cFcH4HyCEc5TJpiZdP7u9RjaEo2S3P9VkHfmqCH4McpLgw7He9nm7rf9JA2Gh7ubTKy5e6dJUWojgYhGS4KGe3yKGFhLaNgRiME63fUEFSnN2ZvCSM9qsrj34q2h8962xBod9hCVEDfk4tfmHu1UHX5AGaW6mk3pzqKKVYTTWXi84JSH7vzKPmQuhwaAR9Ye3Jbdzehp4xhrT5aFCjnz3r5qNv2zz48Fq5bGc1RUh88PUMT3z6kuzv6B1eXTLYpeu9gGdjc5C9DQDTYPfcHWn7dSHr4AGV1sN6SwVy8W5LZdMAZaeXCDn9iXDwbeD2DYd2ozVCEzceygVzpVdnueNx5FmG6zHtGzfuStr4Jj85sbd2jUGh4ES2bMU41jw2gJ6ujjf6CrxZpCWhXz6NJpAS9njcDFXuspf7otbMjCB6TzwokJwEse31nGUZQdhQgXn23vnZtxwCV621uXFbm7xVACRZKeuXgw8VdEVaXGvf2V4DdhjZnjmePBbTeJ7WjABavLcpMqZJH7FgaLxazFqk9RXtnfUEbVAAhuZzxz6L8Z6axHwz3a4EZtALRjfFjn6xjaUtsWXYW8P6F7femM6UHx3qXMo43hKC7oxnd6Tfta972dgyQfSoBwWkWzB8cvaJreNh4bdLNkw6mty86NXGKyijv83LR1HjbnUoTwPbEMX8JyzLfMf3qiWzwf6MHrXprwygmEpNc6w8tNNivmcWyCX3wmPkMKK1bmi5TCHoUtRrxcyXKhmuyo6zzag8KyK6iZaRbMiFiUJBi5VYwidMutkexWo8SRqfSV5yp2kxswknmpeVTnXBhVEy3anMEiD6bV48AnbF6SfKAi2DGBFqxBFfpFEbYtPauHiYYzZX1epqvKxY23xA9J8FosMk4yYN4Ps7Rh" >> importkey
59
- imphash=$(ipfs key import -f=b58mh quxel $(cat importkey))
60
+
61
+ test_expect_success "export and import rsa key" '
62
+ ipfs key export generated_rsa_key &&
63
+ ipfs key rm generated_rsa_key &&
64
+ ipfs key import generated_rsa_key generated_rsa_key > roundtrip_rsa_key_id &&
65
+ test_cmp rsa_key_id roundtrip_rsa_key_id
66
'
67
62
- test_expect_success "exported key matches imported" '
63
- ipfs key export quxel >> exportkey &&
64
- test_cmp importkey exportkey
68
+ test_expect_success "export and import ed25519 key" '
69
+ ipfs key export generated_ed25519_key &&
70
+ ipfs key rm generated_ed25519_key &&
71
+ ipfs key import generated_ed25519_key generated_ed25519_key > roundtrip_ed25519_key_id &&
72
+ test_cmp ed25519_key_id roundtrip_ed25519_key_id
73
+ '
74
+
75
+ test_expect_success "test export file option" '
76
+ ipfs key export generated_rsa_key -o=named_rsa_export_file &&
77
+ test_cmp generated_rsa_key named_rsa_export_file &&
78
+ ipfs key export generated_ed25519_key -o=named_ed25519_export_file &&
79
+ test_cmp generated_ed25519_key named_ed25519_export_file
80
'
81
82
test_expect_success "key export can't export self" '
83
test_must_fail ipfs key export self 2>&1 | tee key_exp_out &&
84
+ grep -q "Error: cannot export key with name" key_exp_out &&
85
+ test_must_fail ipfs key export self -o=selfexport 2>&1 | tee key_exp_out &&
86
grep -q "Error: cannot export key with name" key_exp_out
87
'
88
89
test_expect_success "key import can't import self" '
73
- test_must_fail ipfs key import self $(cat importkey) 2>&1 | tee key_imp_out &&
74
- grep -q "Error: cannot import key with name" key_imp_out
90
+ ipfs key gen overwrite_self_import &&
91
+ ipfs key export overwrite_self_import &&
92
+ test_must_fail ipfs key import self overwrite_self_import 2>&1 | tee key_imp_out &&
93
+ grep -q "Error: cannot import key with name" key_imp_out &&
94
+ ipfs key rm overwrite_self_import &&
95
+ rm overwrite_self_import
96
+ '
97
+
98
+ test_expect_success "add a default key" '
99
+ ipfs key gen quxel
100
'
101
102
test_expect_success "all keys show up in list output" '
78
- echo bazed > list_exp &&
79
- echo foobarsa >> list_exp &&
103
+ echo generated_ed25519_key > list_exp &&
104
+ echo generated_rsa_key >> list_exp &&
105
echo quxel >> list_exp &&
106
echo self >> list_exp
107
ipfs key list -f=b58mh > list_out &&
@@ -94,8 +119,8 @@ ipfs key rm key_ed25519
119
'
120
121
test_expect_success "key rm remove a key" '
97
- ipfs key rm foobarsa
98
- echo bazed > list_exp &&
122
+ ipfs key rm generated_rsa_key
123
+ echo generated_ed25519_key > list_exp &&
124
echo quxel >> list_exp &&
125
echo self >> list_exp
126
ipfs key list -f=b58mh > list_out &&
@@ -108,7 +133,7 @@ ipfs key rm key_ed25519
133
'
134
135
test_expect_success "key rename rename a key" '
111
- ipfs key rename bazed fooed
136
+ ipfs key rename generated_ed25519_key fooed
137
echo fooed > list_exp &&
138
echo quxel >> list_exp &&
139
echo self >> list_exp
@@ -134,6 +159,22 @@ ipfs key rm key_ed25519
159
'
160
}
161
162
+test_check_rsa2048_sk() {
163
+ sklen=$(ls -l $1 | awk '{print $5}') &&
164
+ test "$sklen" -lt "1600" && test "$sklen" -gt "1000" || {
165
+ echo "Bad RSA2048 sk '$1' with len '$sklen'"
166
+ return 1
167
+ }
168
+}
169
+
170
+test_check_ed25519_sk() {
171
+ sklen=$(ls -l $1 | awk '{print $5}') &&
172
+ test "$sklen" -lt "100" && test "$sklen" -gt "30" || {
173
+ echo "Bad ED25519 sk '$1' with len '$sklen'"
174
+ return 1
175
+ }
176
+}
177
+
178
test_key_cmd
179
180
test_done