Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit a7e7b5e

Browse files
add cleanup webhook called on disconnect and shutdown
1 parent 2e330b6 commit a7e7b5e

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

config/appconfig.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type AppConfig struct {
1919
// Auth contains the configuration for user authentication.
2020
// swagger:ignore
2121
Auth AuthConfig `json:"auth" yaml:"auth"`
22+
// CleanupServer contains the settings for fetching the user-specific cleanup webhook.
23+
// swagger:ignore
24+
CleanupServer HTTPClientConfiguration `json:"cleanupserver" yaml:"cleanupserver"`
2225
// Log contains the configuration for the logging level.
2326
// swagger:ignore
2427
Log LogConfig `json:"log" yaml:"log"`
@@ -119,6 +122,9 @@ func (cfg *AppConfig) Validate(dynamic bool) error {
119122
queue.add("geoip", &cfg.GeoIP)
120123
queue.add("audit", &cfg.Audit)
121124
queue.add("health", &cfg.Health)
125+
if cfg.CleanupServer.URL != "" {
126+
queue.add("cleanupserver", &cfg.CleanupServer)
127+
}
122128

123129
if cfg.ConfigServer.URL != "" && !dynamic {
124130
return queue.Validate()

internal/backend/handler.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
auth2 "go.containerssh.io/libcontainerssh/auth"
1111
"go.containerssh.io/libcontainerssh/config"
12+
"go.containerssh.io/libcontainerssh/http"
1213
internalConfig "go.containerssh.io/libcontainerssh/internal/config"
1314
"go.containerssh.io/libcontainerssh/internal/docker"
1415
"go.containerssh.io/libcontainerssh/internal/kubernetes"
@@ -27,6 +28,7 @@ type handler struct {
2728

2829
config config.AppConfig
2930
configLoader internalConfig.Loader
31+
cleanupClient http.Client
3032
authResponse sshserver.AuthResponse
3133
metricsCollector metrics.Collector
3234
logger log.Logger
@@ -219,6 +221,7 @@ func (n *networkHandler) OnDisconnect() {
219221
n.lock.Lock()
220222
defer n.lock.Unlock()
221223
if n.backend != nil {
224+
n.cleanupWebhook()
222225
n.backend.OnDisconnect()
223226
n.backend = nil
224227
}
@@ -227,10 +230,26 @@ func (n *networkHandler) OnDisconnect() {
227230
func (n *networkHandler) OnShutdown(shutdownContext context.Context) {
228231
n.lock.Lock()
229232
if n.backend != nil {
233+
n.cleanupWebhook()
230234
backend := n.backend
231235
n.lock.Unlock()
232236
backend.OnShutdown(shutdownContext)
233237
} else {
234238
n.lock.Unlock()
235239
}
236240
}
241+
242+
func (n *networkHandler) cleanupWebhook() {
243+
if n.rootHandler.cleanupClient != nil {
244+
_, err := n.rootHandler.cleanupClient.Post("", map[string]interface{}{
245+
"connectionId": n.connectionID,
246+
}, nil)
247+
if err != nil {
248+
n.logger.Error(message.Wrap(
249+
err,
250+
message.ECleanupWebhook,
251+
"Got unexpected response from cleanup webhook",
252+
))
253+
}
254+
}
255+
}

internal/backend/handler_factory.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sync"
55

66
"go.containerssh.io/libcontainerssh/config"
7+
"go.containerssh.io/libcontainerssh/http"
78
internalConfig "go.containerssh.io/libcontainerssh/internal/config"
89
"go.containerssh.io/libcontainerssh/internal/metrics"
910
"go.containerssh.io/libcontainerssh/internal/sshserver"
@@ -26,7 +27,16 @@ func New(
2627
if err != nil {
2728
return nil, err
2829
}
29-
30+
var cleanupClient http.Client
31+
if config.CleanupServer.URL != "" {
32+
cleanupClient, err = http.NewClient(
33+
config.CleanupServer,
34+
logger,
35+
)
36+
if err != nil {
37+
return nil, err
38+
}
39+
}
3040
backendRequestsCounter := metricsCollector.MustCreateCounter(
3141
MetricNameBackendRequests,
3242
MetricUnitBackendRequests,
@@ -41,6 +51,7 @@ func New(
4151
return &handler{
4252
config: config,
4353
configLoader: loader,
54+
cleanupClient: cleanupClient,
4455
authResponse: defaultAuthResponse,
4556
metricsCollector: metricsCollector,
4657
logger: logger,

message/backend.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ package message
22

33
// EBackendConfig indicates that there is an error in the backend configuration.
44
const EBackendConfig = "BACKEND_CONFIG_ERROR"
5+
6+
// ECleanupWebhook indicates that in calling the cleanup webhook
7+
const ECleanupWebhook = "BACKEND_CLEANUP_WEBHOOK_ERROR"

0 commit comments

Comments
 (0)