diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index 0c0a1356d..5914e4fe1 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -37,6 +37,7 @@ const ( var ( errAPIKeyNotSet = errors.New("HEADSCALE_CLI_API_KEY environment variable needs to be set") + errUnixSocketNotSet = errors.New("unix_socket is empty: set it, or cli.address and cli.api_key for a remote server") errMissingParameter = errors.New("missing parameters") errResponseStatus = errors.New("unexpected response status") ) @@ -250,6 +251,11 @@ func newSocketClient(socketPath string) (*clientv1.ClientWithResponses, error) { // starts can race its creation; retrying preserves the old gRPC client's // blocking-dial tolerance rather than failing on a not-yet-present socket. func dialHeadscaleSocket(ctx context.Context, socketPath string) (net.Conn, error) { + // Retrying cannot help: no socket ever appears at "". + if socketPath == "" { + return nil, errUnixSocketNotSet + } + b := backoff.NewExponentialBackOff() b.InitialInterval = 50 * time.Millisecond b.MaxInterval = 1 * time.Second diff --git a/cmd/headscale/cli/utils_test.go b/cmd/headscale/cli/utils_test.go index 3a822a830..1222c0018 100644 --- a/cmd/headscale/cli/utils_test.go +++ b/cmd/headscale/cli/utils_test.go @@ -1,9 +1,11 @@ package cli import ( + "context" "os" "path/filepath" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -99,6 +101,16 @@ func TestCLIAPIKey(t *testing.T) { } } +// TestDialEmptySocket: an empty unix_socket fails at once, naming the key, +// instead of retrying until the CLI timeout. +func TestDialEmptySocket(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), time.Second) + defer cancel() + + _, err := dialHeadscaleSocket(ctx, "") + require.ErrorIs(t, err, errUnixSocketNotSet) +} + // TestServerConfigDropsCLIKey: the server never holds the CLI's key, which // may be a file only the CLI can read. func TestServerConfigDropsCLIKey(t *testing.T) {