-
Notifications
You must be signed in to change notification settings - Fork 78
/
export.go
98 lines (88 loc) · 2.62 KB
/
export.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
package sshkey
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/cnrancher/autok3s/pkg/common"
pkgsshkey "github.com/cnrancher/autok3s/pkg/sshkey"
"github.com/cnrancher/autok3s/pkg/utils"
"github.com/spf13/cobra"
)
var exportCmd = &cobra.Command{
Use: "export <name>",
Args: cobra.ExactArgs(1),
Short: "export the specificed ssh key pair to files",
Long: `export the specificed ssh key pair as files to path.
private key would be id_rsa, public key would be id_rsa.pub and publci key certificate would be pub.cert`,
PreRunE: validateFiles,
Run: utils.CommandExitWithoutHelpInfo(export),
}
func init() {
exportCmd.Flags().StringVarP(&sshKeyFlags.OutputPath, "output", "o", ".", "The path to write key pair files, will write to id_rsa, id_rsa.pub and pub.cert under the output path")
}
func validateFiles(_ *cobra.Command, args []string) error {
if err := pathExists(sshKeyFlags.OutputPath); err != nil {
return err
}
toValidate := []string{}
name := args[0]
rtn, err := common.DefaultDB.ListSSHKey(&name)
if err != nil {
return err
}
if len(rtn) == 0 {
return fmt.Errorf("ssh key %s doesn't exist", name)
}
target := rtn[0]
checkmap := map[string]string{
pkgsshkey.PrivateKeyFilename: target.SSHKey,
pkgsshkey.PublicKeyFilename: target.SSHPublicKey,
pkgsshkey.CertificateFilename: target.SSHCert,
}
for filename, toCheck := range checkmap {
if toCheck == "" {
continue
}
toValidate = append(toValidate, filename)
}
return pathsNotExists(sshKeyFlags.OutputPath, toValidate...)
}
func export(cmd *cobra.Command, args []string) error {
target, err := common.DefaultDB.ListSSHKey(&args[0])
if err != nil {
return err
}
if err := exportKeyFiles(sshKeyFlags.OutputPath, target[0]); err != nil {
return err
}
cmd.Printf("ssh key %s is written to directory %s\n", target[0].Name, sshKeyFlags.OutputPath)
return nil
}
func pathsNotExists(basePath string, paths ...string) error {
for _, path := range paths {
if basePath != "" {
path = filepath.Join(basePath, path)
}
if err := pathNotExists(path); err != nil {
return err
}
}
return nil
}
func exportKeyFiles(path string, target *common.SSHKey) error {
dataMap := map[string]string{
pkgsshkey.PrivateKeyFilename: target.SSHKey,
pkgsshkey.PublicKeyFilename: target.SSHPublicKey,
pkgsshkey.CertificateFilename: target.SSHCert,
}
for filename, data := range dataMap {
if data == "" {
continue
}
targetPath := filepath.Join(path, filename)
if err := ioutil.WriteFile(targetPath, []byte(data), 0600); err != nil {
return fmt.Errorf("failed to write ssh key pair %s to files, %v", target.Name, err)
}
}
return nil
}