mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 17:07:47 +02:00
fix(agent): don't warn about unset HUB_URL in SSH-only mode (#2316)
This commit is contained in:
@@ -30,6 +30,11 @@ const (
|
|||||||
wsDeadline = 120 * time.Second
|
wsDeadline = 120 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// errNoHubURL is returned when HUB_URL is unset. This is not a failure
|
||||||
|
// condition: an agent configured with only a public key runs in SSH-only mode,
|
||||||
|
// where the hub dials the agent and no outbound WebSocket client is expected.
|
||||||
|
var errNoHubURL = errors.New("HUB_URL environment variable not set")
|
||||||
|
|
||||||
type caCertFileError struct {
|
type caCertFileError struct {
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
@@ -63,7 +68,7 @@ type WebSocketClient struct {
|
|||||||
func newWebSocketClient(agent *Agent) (client *WebSocketClient, err error) {
|
func newWebSocketClient(agent *Agent) (client *WebSocketClient, err error) {
|
||||||
hubURLStr, exists := utils.GetEnv("HUB_URL")
|
hubURLStr, exists := utils.GetEnv("HUB_URL")
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, errors.New("HUB_URL environment variable not set")
|
return nil, errNoHubURL
|
||||||
}
|
}
|
||||||
|
|
||||||
client = &WebSocketClient{}
|
client = &WebSocketClient{}
|
||||||
|
|||||||
@@ -32,6 +32,28 @@ import (
|
|||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestNewWebSocketClientNoHubURL verifies that an unset HUB_URL returns the
|
||||||
|
// errNoHubURL sentinel rather than an opaque error. Callers rely on this to
|
||||||
|
// distinguish SSH-only mode -- a supported configuration in which the hub dials
|
||||||
|
// the agent -- from an actual misconfiguration.
|
||||||
|
func TestNewWebSocketClientNoHubURL(t *testing.T) {
|
||||||
|
agent := createTestAgent(t)
|
||||||
|
|
||||||
|
// t.Setenv registers restoration of the original value; unset afterwards so
|
||||||
|
// GetEnv's LookupEnv reports the variable as absent rather than empty.
|
||||||
|
t.Setenv("BESZEL_AGENT_HUB_URL", "")
|
||||||
|
os.Unsetenv("BESZEL_AGENT_HUB_URL")
|
||||||
|
t.Setenv("HUB_URL", "")
|
||||||
|
os.Unsetenv("HUB_URL")
|
||||||
|
t.Setenv("BESZEL_AGENT_TOKEN", "test-token")
|
||||||
|
|
||||||
|
client, err := newWebSocketClient(agent)
|
||||||
|
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Nil(t, client)
|
||||||
|
assert.ErrorIs(t, err, errNoHubURL)
|
||||||
|
}
|
||||||
|
|
||||||
// TestNewWebSocketClient tests WebSocket client creation
|
// TestNewWebSocketClient tests WebSocket client creation
|
||||||
func TestNewWebSocketClient(t *testing.T) {
|
func TestNewWebSocketClient(t *testing.T) {
|
||||||
agent := createTestAgent(t)
|
agent := createTestAgent(t)
|
||||||
|
|||||||
@@ -91,8 +91,16 @@ func (c *ConnectionManager) Start(serverOptions ServerOptions) error {
|
|||||||
if errors.As(err, &caCertErr) {
|
if errors.As(err, &caCertErr) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
disableSSH, _ := utils.GetEnv("DISABLE_SSH")
|
||||||
|
if errors.Is(err, errNoHubURL) && disableSSH != "true" {
|
||||||
|
// SSH-only mode: the hub dials the agent, so there is nothing to warn
|
||||||
|
// about. With SSH also disabled there is no connection method at all,
|
||||||
|
// so that case still warns.
|
||||||
|
slog.Debug("WebSocket client not configured", "err", err)
|
||||||
|
} else {
|
||||||
slog.Warn("Error creating WebSocket client", "err", err)
|
slog.Warn("Error creating WebSocket client", "err", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
c.wsClient = wsClient
|
c.wsClient = wsClient
|
||||||
|
|
||||||
c.serverOptions = serverOptions
|
c.serverOptions = serverOptions
|
||||||
|
|||||||
Reference in New Issue
Block a user