@@ -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
11791func sshAgent (ctx context.Context , privateKeyFile string ) (map [string ]string , error ) {
@@ -190,20 +164,16 @@ func writeToFifo(fifo string, data []byte) error {
190164}
191165
192166func 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
236223var sshCmd = & cobra.Command {
237224 Use : "ssh [user@]NODE [COMMAND...]" ,
0 commit comments