-
Notifications
You must be signed in to change notification settings - Fork 11
/
ssh.go
128 lines (111 loc) · 2.99 KB
/
ssh.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
/*
Copyright 2017 Balena Ltd.
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 main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/balena-io-modules/gexpect/pty"
"github.com/gliderlabs/ssh"
"github.com/prometheus/client_golang/prometheus"
gossh "golang.org/x/crypto/ssh"
)
func addHostKey(s *ssh.Server, dir string, keyType string) error {
keyPath := filepath.Join(dir, fmt.Sprintf("id_%s", keyType))
raw, err := ioutil.ReadFile(keyPath)
if err != nil {
return err
}
pkey, err := gossh.ParsePrivateKey(raw)
if err != nil {
return err
}
s.AddHostKey(pkey)
return nil
}
func makeHandler(
shell string,
creds *syscall.Credential,
envKeysWhitelist []string,
verbosity int,
) ssh.Handler {
return func(session ssh.Session) {
log.Printf("Handling command '%s' from %s@%s", session.RawCommand(), session.User(), session.RemoteAddr())
cmd := exec.Command(shell)
if creds != nil {
cmd.SysProcAttr = &syscall.SysProcAttr{Credential: creds}
}
activeSessions.Inc()
defer activeSessions.Dec()
remoteAddrParts := strings.Split(session.RemoteAddr().String(), ":")
ip := strings.Join(remoteAddrParts[0:len(remoteAddrParts)-1], ":")
totalSessions.With(prometheus.Labels{
"user": session.User(),
"ip": ip,
}).Inc()
if len(envKeysWhitelist) > 0 {
envWhitelist := make(map[string]bool)
for _, key := range envKeysWhitelist {
envWhitelist[key] = true
}
for _, env := range session.Environ() {
key := strings.Split(env, "=")[0]
if envWhitelist[key] {
cmd.Env = append(cmd.Env, env)
}
}
}
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_USER=%s", session.User()))
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_ORIGINAL_COMMAND=%s", session.RawCommand()))
if ptqReq, winCh, isPty := session.Pty(); isPty {
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptqReq.Term))
terminal, err := pty.Start(cmd)
if err != nil {
return
}
go func() {
for win := range winCh {
terminal.SetWinSize(win.Width, win.Height)
}
}()
go io.Copy(terminal, session)
go io.Copy(session, terminal)
defer terminal.Close()
} else {
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
return
}
stdin, err := cmd.StdinPipe()
if err != nil {
return
}
if err := cmd.Start(); err != nil {
return
}
go io.Copy(stdin, session)
go io.Copy(session, stdout)
go io.Copy(session.Stderr(), stderr)
}
cmd.Wait()
}
}