forked from containers/virtcontainers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshd.go
196 lines (158 loc) · 4.17 KB
/
sshd.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Copyright (c) 2016 Intel Corporation
//
// 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 virtcontainers
import (
"fmt"
"io/ioutil"
"strings"
"syscall"
"time"
"golang.org/x/crypto/ssh"
)
// SshdConfig is a structure storing information needed for
// sshd agent initialization.
type SshdConfig struct {
Username string
PrivKeyFile string
Server string
Port string
Protocol string
Spawner SpawnerType
}
// sshd is an Agent interface implementation for the sshd agent.
type sshd struct {
config SshdConfig
client *ssh.Client
spawner spawner
}
func (c SshdConfig) validate() bool {
return true
}
func publicKeyAuth(file string) (ssh.AuthMethod, error) {
if file == "" {
return nil, errNeedFile
}
privateBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Failed to load private key")
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
return nil, fmt.Errorf("Failed to parse private key")
}
return ssh.PublicKeys(private), nil
}
func execCmd(session *ssh.Session, cmd string) error {
if session == nil {
return fmt.Errorf("session cannot be empty")
}
stdout, err := session.CombinedOutput(cmd)
if err != nil {
return fmt.Errorf("Failed to run %s", cmd)
}
fmt.Printf("%s\n", stdout)
return nil
}
// init is the agent initialization implementation for sshd.
func (s *sshd) init(pod *Pod, config interface{}) error {
if pod == nil {
return errNeedPod
}
if config == nil {
return fmt.Errorf("config cannot be empty")
}
c := config.(SshdConfig)
if c.validate() == false {
return fmt.Errorf("Invalid configuration")
}
s.config = c
s.spawner = newSpawner(c.Spawner)
return nil
}
// exec is the agent command execution implementation for sshd.
func (s *sshd) exec(pod *Pod, c Container, process Process, cmd Cmd) error {
if pod == nil {
return errNeedPod
}
session, err := s.client.NewSession()
if err != nil {
return fmt.Errorf("Failed to create session (Pod ID %s, Container ID %s)",
pod.id, c.id)
}
defer session.Close()
if s.spawner != nil {
cmd.Args, err = s.spawner.formatArgs(cmd.Args)
if err != nil {
return err
}
}
strCmd := strings.Join(cmd.Args, " ")
return execCmd(session, strCmd)
}
// startPod is the agent Pod starting implementation for sshd.
func (s *sshd) startPod(pod Pod) error {
if s.client != nil {
session, err := s.client.NewSession()
if err == nil {
session.Close()
return nil
}
}
sshAuthMethod, err := publicKeyAuth(s.config.PrivKeyFile)
if err != nil {
return err
}
sshConfig := &ssh.ClientConfig{
User: s.config.Username,
Auth: []ssh.AuthMethod{
sshAuthMethod,
},
}
for i := 0; i < 1000; i++ {
s.client, err = ssh.Dial(s.config.Protocol, s.config.Server+":"+s.config.Port, sshConfig)
if err == nil {
break
}
select {
case <-time.After(100 * time.Millisecond):
break
}
}
if err != nil {
return fmt.Errorf("Failed to dial: %s", err)
}
return nil
}
// stopPod is the agent Pod stopping implementation for sshd.
func (s *sshd) stopPod(pod Pod) error {
return nil
}
// createContainer is the agent Container creation implementation for sshd.
func (s *sshd) createContainer(pod *Pod, c *Container) error {
return nil
}
// startContainer is the agent Container starting implementation for sshd.
func (s *sshd) startContainer(pod Pod, c Container) error {
return nil
}
// stopContainer is the agent Container stopping implementation for sshd.
func (s *sshd) stopContainer(pod Pod, c Container) error {
return nil
}
// killContainer is the agent Container signaling implementation for sshd.
func (s *sshd) killContainer(pod Pod, c Container, signal syscall.Signal, all bool) error {
return nil
}