This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Mirantis/virtlet
/
ssh_interface.go
147 lines (120 loc) · 3.09 KB
/
ssh_interface.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
/*
Copyright 2017 Mirantis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package framework
import (
"errors"
"fmt"
"io"
"strings"
"golang.org/x/crypto/ssh"
"github.com/Mirantis/virtlet/pkg/tools"
)
type sshInterface struct {
vmInterface *VMInterface
client *ssh.Client
fwStopCh chan struct{}
}
func newSSHInterface(vmInterface *VMInterface, user, secret string) (*sshInterface, error) {
var authMethod ssh.AuthMethod
key := trimBlock(secret)
signer, err := ssh.ParsePrivateKey([]byte(key))
if err != nil {
authMethod = ssh.Password(secret)
} else {
authMethod = ssh.PublicKeys(signer)
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{authMethod},
}
vmPod, err := vmInterface.Pod()
if err != nil {
return nil, err
}
ports := []*tools.ForwardedPort{
{RemotePort: 22},
}
fwStopCh, err := vmPod.PortForward(ports)
if err != nil {
return nil, err
}
sshClient, err := ssh.Dial("tcp", fmt.Sprintf("localhost:%d", ports[0].LocalPort), config)
if err != nil {
defer close(fwStopCh)
return nil, err
}
return &sshInterface{
vmInterface: vmInterface,
client: sshClient,
fwStopCh: fwStopCh,
}, nil
}
func (si *sshInterface) Run(stdin io.Reader, stdout, stderr io.Writer, command ...string) error {
cmd, err := si.Start(stdin, stdout, stderr, command...)
if err != nil {
return err
}
return cmd.Wait()
}
func (si *sshInterface) Start(stdin io.Reader, stdout, stderr io.Writer, command ...string) (Command, error) {
session, err := si.client.NewSession()
if err != nil {
return nil, err
}
session.Stdout = stdout
session.Stderr = stderr
session.Stdin = stdin
for i, arg := range command {
if i == 0 {
continue
}
command[i] = fmt.Sprintf("'%s'", strings.Replace(arg, "'", "\\'", -1))
}
if err := session.Start(strings.Join(command, " ")); err != nil {
defer session.Close()
return nil, err
}
return sshCommand{session: session}, err
}
func (si *sshInterface) Close() error {
if si.client != nil {
defer close(si.fwStopCh)
err := si.client.Close()
si.client = nil
return err
}
return nil
}
// Logs is a placeholder for fulfilling Executor interface
func (*sshInterface) Logs() (string, error) {
return "", errors.New("not implemented")
}
type sshCommand struct {
session *ssh.Session
}
var _ Command = &sshCommand{}
func (sc sshCommand) Wait() error {
err := sc.session.Wait()
defer sc.session.Close()
if err != nil {
if s, ok := err.(*ssh.ExitError); ok {
return CommandError{ExitCode: s.ExitStatus()}
}
return err
}
return nil
}
func (sc sshCommand) Kill() error {
defer sc.session.Close()
return sc.session.Signal(ssh.SIGKILL)
}