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

Adds a cleanup webhook called on disconnect and shutdown #567

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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 @@
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 @@

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 @@

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 @@
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) 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 @@
"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 @@
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 @@
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"
Loading