This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
236 lines (211 loc) · 6.05 KB
/
handler.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package backend
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/containerssh/configuration/v2"
"github.com/containerssh/docker/v2"
"github.com/containerssh/kubernetes/v2"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
"github.com/containerssh/security"
"github.com/containerssh/sshproxy"
"github.com/containerssh/sshserver"
"github.com/containerssh/structutils"
)
type handler struct {
sshserver.AbstractHandler
config configuration.AppConfig
configLoader configuration.Loader
authResponse sshserver.AuthResponse
metricsCollector metrics.Collector
logger log.Logger
backendRequestsCounter metrics.Counter
backendErrorCounter metrics.Counter
lock *sync.Mutex
}
func (h *handler) OnNetworkConnection(
remoteAddr net.TCPAddr,
connectionID string,
) (sshserver.NetworkConnectionHandler, error) {
return &networkHandler{
logger: h.logger.
WithLabel("connectionId", connectionID).
WithLabel("remoteAddr", remoteAddr.IP.String()),
rootHandler: h,
remoteAddr: remoteAddr,
connectionID: connectionID,
lock: &sync.Mutex{},
}, nil
}
type networkHandler struct {
sshserver.AbstractNetworkConnectionHandler
rootHandler *handler
remoteAddr net.TCPAddr
connectionID string
backend sshserver.NetworkConnectionHandler
lock *sync.Mutex
logger log.Logger
}
func (n *networkHandler) OnAuthPassword(_ string, _ []byte) (response sshserver.AuthResponse, reason error) {
return n.authResponse()
}
func (n *networkHandler) authResponse() (sshserver.AuthResponse, error) {
switch n.rootHandler.authResponse {
case sshserver.AuthResponseUnavailable:
return sshserver.AuthResponseUnavailable, fmt.Errorf("the backend handler does not support authentication")
default:
return n.rootHandler.authResponse, nil
}
}
func (n *networkHandler) OnAuthPubKey(_ string, _ string) (response sshserver.AuthResponse, reason error) {
return n.authResponse()
}
func (n *networkHandler) OnHandshakeFailed(_ error) {
}
func (n *networkHandler) OnHandshakeSuccess(username string) (
connection sshserver.SSHConnectionHandler,
failureReason error,
) {
appConfig, err := n.loadConnectionSpecificConfig(username)
if err != nil {
return nil, err
}
backendLogger := n.logger.WithLevel(appConfig.Log.Level).WithLabel("username", username)
return n.initBackend(username, appConfig, backendLogger)
}
func (n *networkHandler) initBackend(
username string,
appConfig configuration.AppConfig,
backendLogger log.Logger,
) (sshserver.SSHConnectionHandler, error) {
backend, failureReason := n.getConfiguredBackend(
appConfig,
backendLogger,
n.rootHandler.backendRequestsCounter.WithLabels(metrics.Label(MetricLabelBackend, appConfig.Backend)),
n.rootHandler.backendErrorCounter.WithLabels(metrics.Label(MetricLabelBackend, appConfig.Backend)),
)
if failureReason != nil {
return nil, failureReason
}
// Inject security overlay
backend, failureReason = security.New(appConfig.Security, backend, n.logger)
if failureReason != nil {
return nil, failureReason
}
n.backend = backend
return backend.OnHandshakeSuccess(username)
}
func (n *networkHandler) getConfiguredBackend(
appConfig configuration.AppConfig,
backendLogger log.Logger,
backendRequestsCounter metrics.Counter,
backendErrorCounter metrics.Counter,
) (backend sshserver.NetworkConnectionHandler, failureReason error) {
switch appConfig.Backend {
case "docker":
backend, failureReason = docker.New(
n.remoteAddr,
n.connectionID,
appConfig.Docker,
backendLogger.WithLabel("backend", "docker"),
backendRequestsCounter,
backendErrorCounter,
)
case "dockerrun":
//goland:noinspection GoDeprecation
backend, failureReason = docker.NewDockerRun(
n.remoteAddr,
n.connectionID,
appConfig.DockerRun,
backendLogger.WithLabel("backend", "dockerrun"),
backendRequestsCounter,
backendErrorCounter,
)
case "kubernetes":
backend, failureReason = kubernetes.New(
n.remoteAddr,
n.connectionID,
appConfig.Kubernetes,
backendLogger.WithLabel("backend", "kubernetes"),
backendRequestsCounter,
backendErrorCounter,
)
case "kuberun":
//goland:noinspection GoDeprecation
backend, failureReason = kubernetes.NewKubeRun(
n.remoteAddr,
n.connectionID,
appConfig.KubeRun,
backendLogger.WithLabel("backend", "kuberun"),
backendRequestsCounter,
backendErrorCounter,
)
case "sshproxy":
backend, failureReason = sshproxy.New(
n.remoteAddr,
n.connectionID,
appConfig.SSHProxy,
backendLogger.WithLabel("backend", "sshproxy"),
backendRequestsCounter,
backendErrorCounter,
)
default:
failureReason = fmt.Errorf("invalid backend: %s", appConfig.Backend)
}
return backend, failureReason
}
func (n *networkHandler) loadConnectionSpecificConfig(
username string,
) (
configuration.AppConfig,
error,
) {
ctx, cancelFunc := context.WithTimeout(context.Background(), 60*time.Second)
defer cancelFunc()
appConfig := configuration.AppConfig{}
if err := structutils.Copy(&appConfig, &n.rootHandler.config); err != nil {
return appConfig, fmt.Errorf("failed to copy application configuration (%w)", err)
}
if err := n.rootHandler.configLoader.LoadConnection(
ctx,
username,
n.remoteAddr,
n.connectionID,
&appConfig,
); err != nil {
return appConfig, fmt.Errorf("failed to load connections-specific configuration (%w)", err)
}
if err := appConfig.Validate(true); err != nil {
newErr := fmt.Errorf("configuration server returned invalid configuration (%w)", err)
n.rootHandler.logger.Error(
log.Wrap(
err,
EConfig,
"configuration server returned invalid configuration",
),
)
return appConfig, newErr
}
return appConfig, nil
}
func (n *networkHandler) OnDisconnect() {
n.lock.Lock()
defer n.lock.Unlock()
if n.backend != nil {
n.backend.OnDisconnect()
n.backend = nil
}
}
func (n *networkHandler) OnShutdown(shutdownContext context.Context) {
n.lock.Lock()
if n.backend != nil {
backend := n.backend
n.lock.Unlock()
backend.OnShutdown(shutdownContext)
} else {
n.lock.Unlock()
}
}