Skip to content

Commit

Permalink
add cleanup webhook called on disconnect and shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
devinchristianson committed Mar 3, 2024
1 parent 2e330b6 commit a7e7b5e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type AppConfig struct {
// Auth contains the configuration for user authentication.
// swagger:ignore
Auth AuthConfig `json:"auth" yaml:"auth"`
// CleanupServer contains the settings for fetching the user-specific cleanup webhook.
// swagger:ignore
CleanupServer HTTPClientConfiguration `json:"cleanupserver" yaml:"cleanupserver"`
// Log contains the configuration for the logging level.
// swagger:ignore
Log LogConfig `json:"log" yaml:"log"`
Expand Down Expand Up @@ -119,6 +122,9 @@ func (cfg *AppConfig) Validate(dynamic bool) error {
queue.add("geoip", &cfg.GeoIP)
queue.add("audit", &cfg.Audit)
queue.add("health", &cfg.Health)
if cfg.CleanupServer.URL != "" {
queue.add("cleanupserver", &cfg.CleanupServer)
}

Check warning on line 127 in config/appconfig.go

View check run for this annotation

Codecov / codecov/patch

config/appconfig.go#L125-L127

Added lines #L125 - L127 were not covered by tests

if cfg.ConfigServer.URL != "" && !dynamic {
return queue.Validate()
Expand Down
19 changes: 19 additions & 0 deletions internal/backend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

auth2 "go.containerssh.io/libcontainerssh/auth"
"go.containerssh.io/libcontainerssh/config"
"go.containerssh.io/libcontainerssh/http"
internalConfig "go.containerssh.io/libcontainerssh/internal/config"
"go.containerssh.io/libcontainerssh/internal/docker"
"go.containerssh.io/libcontainerssh/internal/kubernetes"
Expand All @@ -27,6 +28,7 @@ type handler struct {

config config.AppConfig
configLoader internalConfig.Loader
cleanupClient http.Client
authResponse sshserver.AuthResponse
metricsCollector metrics.Collector
logger log.Logger
Expand Down Expand Up @@ -219,6 +221,7 @@ func (n *networkHandler) OnDisconnect() {
n.lock.Lock()
defer n.lock.Unlock()
if n.backend != nil {
n.cleanupWebhook()
n.backend.OnDisconnect()
n.backend = nil
}
Expand All @@ -227,10 +230,26 @@ func (n *networkHandler) OnDisconnect() {
func (n *networkHandler) OnShutdown(shutdownContext context.Context) {
n.lock.Lock()
if n.backend != nil {
n.cleanupWebhook()
backend := n.backend
n.lock.Unlock()
backend.OnShutdown(shutdownContext)
} else {
n.lock.Unlock()
}
}

func (n *networkHandler) cleanupWebhook() {
if n.rootHandler.cleanupClient != nil {
_, err := n.rootHandler.cleanupClient.Post("", map[string]interface{}{
"connectionId": n.connectionID,
}, nil)
if err != nil {
n.logger.Error(message.Wrap(
err,
message.ECleanupWebhook,
"Got unexpected response from cleanup webhook",
))
}

Check warning on line 253 in internal/backend/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/backend/handler.go#L244-L253

Added lines #L244 - L253 were not covered by tests
}
}
13 changes: 12 additions & 1 deletion internal/backend/handler_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sync"

"go.containerssh.io/libcontainerssh/config"
"go.containerssh.io/libcontainerssh/http"
internalConfig "go.containerssh.io/libcontainerssh/internal/config"
"go.containerssh.io/libcontainerssh/internal/metrics"
"go.containerssh.io/libcontainerssh/internal/sshserver"
Expand All @@ -26,7 +27,16 @@ func New(
if err != nil {
return nil, err
}

var cleanupClient http.Client
if config.CleanupServer.URL != "" {
cleanupClient, err = http.NewClient(
config.CleanupServer,
logger,
)
if err != nil {
return nil, err
}

Check warning on line 38 in internal/backend/handler_factory.go

View check run for this annotation

Codecov / codecov/patch

internal/backend/handler_factory.go#L32-L38

Added lines #L32 - L38 were not covered by tests
}
backendRequestsCounter := metricsCollector.MustCreateCounter(
MetricNameBackendRequests,
MetricUnitBackendRequests,
Expand All @@ -41,6 +51,7 @@ func New(
return &handler{
config: config,
configLoader: loader,
cleanupClient: cleanupClient,
authResponse: defaultAuthResponse,
metricsCollector: metricsCollector,
logger: logger,
Expand Down
3 changes: 3 additions & 0 deletions message/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ package message

// EBackendConfig indicates that there is an error in the backend configuration.
const EBackendConfig = "BACKEND_CONFIG_ERROR"

// ECleanupWebhook indicates that in calling the cleanup webhook
const ECleanupWebhook = "BACKEND_CLEANUP_WEBHOOK_ERROR"

0 comments on commit a7e7b5e

Please sign in to comment.