-
Notifications
You must be signed in to change notification settings - Fork 23
/
factory.go
212 lines (187 loc) · 5.12 KB
/
factory.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
package libcontainerssh
import (
"context"
"go.containerssh.io/libcontainerssh/config"
"go.containerssh.io/libcontainerssh/internal/auditlogintegration"
"go.containerssh.io/libcontainerssh/internal/authintegration"
"go.containerssh.io/libcontainerssh/internal/backend"
"go.containerssh.io/libcontainerssh/internal/geoip"
"go.containerssh.io/libcontainerssh/internal/geoip/geoipprovider"
"go.containerssh.io/libcontainerssh/internal/health"
"go.containerssh.io/libcontainerssh/internal/metrics"
"go.containerssh.io/libcontainerssh/internal/metricsintegration"
"go.containerssh.io/libcontainerssh/internal/sshserver"
"go.containerssh.io/libcontainerssh/log"
"go.containerssh.io/libcontainerssh/message"
"go.containerssh.io/libcontainerssh/service"
)
// New creates a new instance of ContainerSSH.
func New(cfg config.AppConfig, factory log.LoggerFactory) (Service, service.Lifecycle, error) {
if err := cfg.Validate(false); err != nil {
return nil, nil, message.Wrap(err, message.ECoreConfig, "invalid ContainerSSH configuration")
}
logger, err := factory.Make(cfg.Log)
if err != nil {
return nil, nil, err
}
pool := service.NewPool(
service.NewLifecycleFactory(),
logger.WithLabel("module", "service"),
)
healthService, err := health.New(cfg.Health, logger.WithLabel("module", "health"))
if err != nil {
return nil, nil, err
}
pool.Add(healthService)
geoIPLookupProvider, err := geoip.New(cfg.GeoIP)
if err != nil {
return nil, nil, err
}
metricsCollector := metrics.New(geoIPLookupProvider)
if err := createMetricsServer(cfg, logger, metricsCollector, pool); err != nil {
return nil, nil, err
}
containerBackend, err := createBackend(cfg, logger, metricsCollector)
if err != nil {
return nil, nil, err
}
authHandler, err := createAuthHandler(cfg, logger, containerBackend, metricsCollector, pool)
if err != nil {
return nil, nil, err
}
auditLogHandler, err := createAuditLogHandler(cfg, logger, authHandler, geoIPLookupProvider)
if err != nil {
return nil, nil, err
}
metricsHandler, err := createMetricsBackend(cfg, metricsCollector, auditLogHandler)
if err != nil {
return nil, nil, err
}
if err := createSSHServer(cfg, logger, metricsHandler, pool); err != nil {
return nil, nil, err
}
return setUpService(pool, logger, healthService)
}
func setUpService(pool service.Pool, logger log.Logger, healthService health.Service) (
Service,
service.Lifecycle,
error,
) {
poolWrapper := &servicePool{
pool,
logger,
}
lifecycle := service.NewLifecycle(poolWrapper)
lifecycle.OnRunning(
func(s service.Service, l service.Lifecycle) {
healthService.ChangeStatus(true)
},
).OnStopping(
func(s service.Service, l service.Lifecycle, shutdownContext context.Context) {
healthService.ChangeStatus(false)
},
).OnCrashed(
func(s service.Service, l service.Lifecycle, err error) {
healthService.ChangeStatus(false)
},
)
return poolWrapper, lifecycle, nil
}
type servicePool struct {
service.Pool
logger log.Logger
}
func (s servicePool) RotateLogs() error {
return s.logger.Rotate()
}
func createMetricsBackend(
cfg config.AppConfig,
collector metrics.Collector,
handler sshserver.Handler,
) (sshserver.Handler, error) {
return metricsintegration.NewHandler(
cfg.Metrics,
collector,
handler,
)
}
func createMetricsServer(
cfg config.AppConfig,
logger log.Logger,
metricsCollector metrics.Collector,
pool service.Pool,
) error {
metricsLogger := logger.WithLabel("module", "metrics")
metricsServer, err := metrics.NewServer(cfg.Metrics, metricsCollector, metricsLogger)
if err != nil {
return err
}
if metricsServer == nil {
return nil
}
pool.Add(metricsServer)
return nil
}
func createSSHServer(
cfg config.AppConfig,
logger log.Logger,
auditLogHandler sshserver.Handler,
pool service.Pool,
) error {
sshLogger := logger.WithLabel("module", "ssh")
sshServer, err := sshserver.New(
cfg.SSH,
auditLogHandler,
sshLogger,
)
if err != nil {
return err
}
pool.Add(sshServer)
return nil
}
func createAuditLogHandler(
cfg config.AppConfig,
logger log.Logger,
authHandler sshserver.Handler,
geoIPLookupProvider geoipprovider.LookupProvider,
) (sshserver.Handler, error) {
auditLogger := logger.WithLabel("module", "audit")
return auditlogintegration.New(
cfg.Audit,
authHandler,
geoIPLookupProvider,
auditLogger,
)
}
func createAuthHandler(
cfg config.AppConfig,
logger log.Logger,
backend sshserver.Handler,
metricsCollector metrics.Collector,
pool service.Pool,
) (sshserver.Handler, error) {
authLogger := logger.WithLabel("module", "auth")
handler, services, err := authintegration.New(
cfg.Auth,
backend,
authLogger,
metricsCollector,
authintegration.BehaviorNoPassthrough,
)
if err != nil {
return nil, err
}
for _, svc := range services {
pool.Add(svc)
}
return handler, nil
}
func createBackend(cfg config.AppConfig, logger log.Logger, metricsCollector metrics.Collector) (sshserver.Handler, error) {
backendLogger := logger.WithLabel("module", "backend")
containerBackend, err := backend.New(cfg, backendLogger, metricsCollector, sshserver.AuthResponseUnavailable)
if err != nil {
return nil, err
}
return containerBackend, nil
}