-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh.go
164 lines (147 loc) · 5.09 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
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
package main
import (
"fmt"
"io"
"net/http"
"strings"
"github.com/gliderlabs/ssh"
"github.com/miekg/pgo/conf"
"github.com/miekg/pgo/osutil"
"go.science.ru.nl/log"
)
// pgoctl machine dhz//status
func newRouter(c *conf.Config) ssh.Handler {
return func(ses ssh.Session) {
pub := ses.PublicKey()
if pub == nil {
warnSession(ses, fmt.Sprintf("Connection denied for user %q because no public key supplied", ses.User()), http.StatusUnauthorized)
return
}
if len(ses.Command()) == 0 {
warnSession(ses, fmt.Sprintf("No commands in connection for user %q", ses.User()), http.StatusBadRequest)
return
}
name, command, args, err := parseCommand(ses.Command())
if err != nil {
warnSession(ses, fmt.Sprintf("No correct commands in connection for user %q", ses.User()), http.StatusBadRequest)
return
}
var s *conf.Service
for i := range c.Services {
if c.Services[i].Name == name {
s = c.Services[i]
break
}
}
if s == nil {
warnSession(ses, fmt.Sprintf("No service found with name %q", name), http.StatusNotFound)
return
}
// Get the keys and chose *those*
pubkeys, err := s.PublicKeys()
if err != nil || len(pubkeys) == 0 {
warnSession(ses, fmt.Sprintf("No public keys found for %q", name), http.StatusNotFound)
return
}
key := -1
for i := range pubkeys {
if ssh.KeysEqual(pubkeys[i], ses.PublicKey()) {
key = i
break
}
}
if key == -1 {
warnSession(ses, fmt.Sprintf("Key for user %q does not match any for name %s", ses.User(), s.Name), http.StatusUnauthorized)
return
}
route, ok := routes[command]
if !ok {
warnSession(ses, fmt.Sprintf("Command %q does not match any route", command), http.StatusNotAcceptable)
return
}
log.Infof("[%s]: Routing for user %q, running %q %v", name, ses.User(), command, args)
out, err := route(s, args)
exitSession(ses, out, err)
return
}
}
var routes = map[string]func(s *conf.Service, args []string) ([]byte, error){
"up": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Up(args) },
"down": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Down(args) },
"stop": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Stop(args) },
"start": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Start(args) },
"restart": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.ReStart(args) },
"ps": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Ps(args) },
"pull": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Pull(args) },
"exec": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Exec(args) },
"load": func(c *conf.Service, args []string) ([]byte, error) { return c.Compose.Load(args) },
"logs": func(c *conf.Service, args []string) ([]byte, error) {
for i := range args {
if args[i] == "-f" || args[i] == "--follow" {
return nil, fmt.Errorf("logs: following logs is not possible")
}
}
return c.Compose.Logs(args)
},
"git": func(c *conf.Service, args []string) ([]byte, error) {
if len(args) == 0 {
return nil, fmt.Errorf("expected git command, got nothing")
}
switch args[0] {
case "pull":
_, err := c.Git.Pull(nil)
return nil, err
case "hash":
hash := c.Git.Hash()
return []byte(hash), nil
default:
return nil, fmt.Errorf("unrecognized command: %s", args[0])
}
},
"journal": func(c *conf.Service, args []string) ([]byte, error) {
return nil, fmt.Errorf("disabled")
/*
for i := range args {
if args[i] == "-f" || args[i] == "--follow" {
return nil, fmt.Errorf("journal: following logs is not possible")
}
}
uid, _ := osutil.User(c.User)
args = append([]string{fmt.Sprintf("_UID=%d", uid)}, args...)
cmd := exec.Command("journalctl", args...)
log.Debugf("[%s]: running in %q as %q %v", c.Name, cmd.Dir, c.User, cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
metric.CmdErrorCount.WithLabelValues(c.Name, "journal", args[0]).Inc()
}
return out, err
*/
},
"ping": func(c *conf.Service, _ []string) ([]byte, error) {
return []byte("pong! - " + osutil.Hostname() + "\n"), nil
},
}
// parseCommand parses: dhz//ps in name (dhz) and command (status) and optional args after it, split on space.
func parseCommand(s []string) (name, command string, args []string, error error) {
// see conf.go ParseCommand which does more, but also has this code.
items := strings.Split(s[0], "//")
if len(items) != 2 {
return "", "", nil, fmt.Errorf("expected name//command, got %s", s[0])
}
name = items[0]
command = items[1]
return name, command, s[1:], nil
}
func exitSession(ses ssh.Session, data []byte, err error) {
if err != nil {
warnSession(ses, fmt.Sprintf("An error occurred in command in connection for user %q: %s\nCaptured output:\n%s", ses.User(), err, data), http.StatusInternalServerError)
return
}
ses.Write(data)
ses.Exit(0)
}
func warnSession(ses ssh.Session, warn string, status int) {
log.Warning(warn)
io.WriteString(ses, http.StatusText(status)+": "+warn+"\n")
ses.Exit(status)
}