Skip to content

Commit 61a9d65

Browse files
committed
update
1 parent 8bd83d9 commit 61a9d65

File tree

2 files changed

+66
-60
lines changed

2 files changed

+66
-60
lines changed

pkg/ckecli/cmd/scp.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ func detectSCPNode(args []string) (string, error) {
2828
}
2929

3030
func scpSubMain(ctx context.Context, args []string) error {
31-
//pipeFilename, err := createFifo()
32-
//if err != nil {
33-
// return err
34-
//}
31+
pipeFilename, err := createFifo2()
32+
if err != nil {
33+
return err
34+
}
3535

3636
node, err := detectSCPNode(args)
3737
if err != nil {
3838
return err
3939
}
4040

41-
pipeFilename, pirvateKey, err := getPrivateKey(node)
41+
pirvateKey, err := getPrivateKey(node)
4242
if err != nil {
4343
return err
4444
}
@@ -56,7 +56,6 @@ func scpSubMain(ctx context.Context, args []string) error {
5656
defer os.Remove(pipeFilename)
5757
defer killSshAgent(ctx)
5858

59-
//return scp(ctx, args)
6059
scpArgs := []string{
6160
"-o", "UserKnownHostsFile=/dev/null",
6261
"-o", "StrictHostKeyChecking=no",
@@ -74,6 +73,26 @@ func scpSubMain(ctx context.Context, args []string) error {
7473
return c.Run()
7574
}
7675

76+
/*
77+
func scp(ctx context.Context, args []string) error {
78+
scpArgs := []string{
79+
"-o", "UserKnownHostsFile=/dev/null",
80+
"-o", "StrictHostKeyChecking=no",
81+
"-o", "ConnectTimeout=60",
82+
}
83+
if scpParams.recursive {
84+
scpArgs = append(scpArgs, "-r")
85+
}
86+
87+
scpArgs = append(scpArgs, args...)
88+
c := exec.CommandContext(ctx, "scp", scpArgs...)
89+
c.Stdin = os.Stdin
90+
c.Stdout = os.Stdout
91+
c.Stderr = os.Stderr
92+
return c.Run()
93+
}
94+
*/
95+
7796
var scpParams struct {
7897
recursive bool
7998
}

pkg/ckecli/cmd/ssh.go

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ func detectSSHNode(arg string) string {
2626
return nodeName
2727
}
2828

29-
/*
30-
func createFifo() (string, error) {
29+
func createFifo2() (string, error) {
3130
usr, err := user.Current()
3231
if err != nil {
3332
return "", err
@@ -54,64 +53,39 @@ func createFifo() (string, error) {
5453

5554
return fifoFilePath, err
5655
}
57-
*/
58-
59-
func getPrivateKey(nodeName string) (string, []byte, error) {
60-
usr, err := user.Current()
61-
if err != nil {
62-
return "", nil, err
63-
}
64-
65-
fifoFilePath := filepath.Join(usr.HomeDir, ".ssh", "ckecli-ssh-key-"+strconv.Itoa(os.Getpid()))
66-
_, err = os.Stat(fifoFilePath)
67-
//if os.IsExist(err) {
68-
// return fifoFilePath, nil
69-
//}
70-
if !os.IsNotExist(err) {
71-
return "", nil, err
72-
}
73-
74-
err = os.MkdirAll(filepath.Join(usr.HomeDir, ".ssh"), 0700)
75-
if err != nil {
76-
return "", nil, err
77-
}
78-
79-
err = syscall.Mkfifo(fifoFilePath, 0600)
80-
if err != nil {
81-
return "", nil, err
82-
}
8356

57+
func getPrivateKey(nodeName string) ([]byte, error) {
8458
vc, err := inf.Vault()
8559
if err != nil {
86-
return "", nil, err
60+
return nil, err
8761
}
8862
secret, err := vc.Logical().Read(cke.SSHSecret)
8963
if err != nil {
90-
return "", nil, err
64+
return nil, err
9165
}
9266
if secret == nil {
93-
return "", nil, errors.New("no ssh private keys")
67+
return nil, errors.New("no ssh private keys")
9468
}
9569
privKeys := secret.Data
96-
mykey, ok := privKeys[nodeName]
97-
fmt.Println("nodeName=", nodeName)
98-
//fmt.Println("mykey (string)=", mykey.(string))
99-
//fmt.Println("mykey (bin)=", mykey)
100-
//fmt.Println("ok=", ok)
101-
//fmt.Printf("mykey type= %T\n", mykey)
10270

71+
mykey, ok := privKeys[nodeName]
10372
if !ok {
10473
mykey = privKeys[""]
10574
}
10675
if mykey == nil {
107-
return "", nil, errors.New("no ssh private key for " + nodeName)
108-
}
109-
if b, ok := mykey.([]byte); ok {
110-
return "", b, nil
111-
}
112-
return "", nil, errors.New("no []byte key for " + nodeName)
113-
//return mykey.([]byte), nil
114-
//return []byte(mykey), nil
76+
return nil, errors.New("no ssh private key for " + nodeName)
77+
}
78+
/*
79+
go func() {
80+
// OpenSSH reads the private key file three times, it need to write key three times.
81+
writeToFifo(fifo, mykey.(string))
82+
time.Sleep(100 * time.Millisecond)
83+
writeToFifo(fifo, mykey.(string))
84+
time.Sleep(100 * time.Millisecond)
85+
writeToFifo(fifo, mykey.(string))
86+
}()
87+
*/
88+
return mykey.([]byte), nil
11589
}
11690

11791
func sshAgent(ctx context.Context, privateKeyFile string) (map[string]string, error) {
@@ -190,20 +164,16 @@ func writeToFifo(fifo string, data []byte) error {
190164
}
191165

192166
func sshSubMain(ctx context.Context, args []string) error {
193-
//pipeFilename, err := createFifo()
194-
//if err != nil {
195-
// return err
196-
//}
197-
//fmt.Println("pipeFilename=",pipeFilename)
167+
pipeFilename, err := createFifo2()
168+
if err != nil {
169+
return err
170+
}
198171

199172
node := detectSSHNode(args[0])
200-
fmt.Println("node=", node)
201-
202-
pipeFilename, pirvateKey, err := getPrivateKey(node)
173+
pirvateKey, err := getPrivateKey(node)
203174
if err != nil {
204175
return err
205176
}
206-
fmt.Println("privateKey=", pirvateKey)
207177

208178
go func() {
209179
if _, err := sshAgent(ctx, pipeFilename); err != nil {
@@ -219,6 +189,7 @@ func sshSubMain(ctx context.Context, args []string) error {
219189
defer os.Remove(pipeFilename)
220190
defer killSshAgent(ctx)
221191

192+
//return ssh(ctx, args)
222193
sshArgs := []string{
223194
"-o", "UserKnownHostsFile=/dev/null",
224195
"-o", "StrictHostKeyChecking=no",
@@ -232,6 +203,22 @@ func sshSubMain(ctx context.Context, args []string) error {
232203
return c.Run()
233204
}
234205

206+
/*
207+
func ssh(ctx context.Context, args []string) (error, string) {
208+
sshArgs := []string{
209+
"-o", "UserKnownHostsFile=/dev/null",
210+
"-o", "StrictHostKeyChecking=no",
211+
"-o", "ConnectTimeout=60",
212+
}
213+
sshArgs = append(sshArgs, args...)
214+
c := exec.CommandContext(ctx, "ssh", sshArgs...)
215+
c.Stdin = os.Stdin
216+
c.Stdout = os.Stdout
217+
c.Stderr = os.Stderr
218+
return c.Run(), "OK"
219+
}
220+
*/
221+
235222
// sshCmd represents the ssh command
236223
var sshCmd = &cobra.Command{
237224
Use: "ssh [user@]NODE [COMMAND...]",

0 commit comments

Comments
 (0)