-
Notifications
You must be signed in to change notification settings - Fork 213
Add Windows named-pipe support to the API listener #5201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+515
−52
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba34215
Support npipe URLs in discovery health check
samuv 9eeceab
Add Windows named-pipe support to API listener
samuv bdfd53e
Test npipe URL parsing and discovery health
samuv 261fcce
Test API listener split and pipe URL forms
samuv 785ddc3
Tighten Windows named-pipe handling per review
samuv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build !windows | ||
|
|
||
| package api | ||
|
samuv marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "log/slog" | ||
| "net" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| // supportsNamedPipe reports whether the current build target can host a | ||
| // Windows named-pipe listener. Used by createListener to reject pipe addresses | ||
| // before reaching the per-platform setupUnixSocket implementation. | ||
| func supportsNamedPipe() bool { return false } | ||
|
|
||
| // setupUnixSocket creates a UNIX domain socket listener at the given path. | ||
| // On non-Windows platforms named-pipe addresses are not supported; callers | ||
| // guard against that in createListener. | ||
| func setupUnixSocket(address string) (net.Listener, error) { | ||
| if err := os.Remove(address); err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| return nil, fmt.Errorf("failed to remove existing socket: %w", err) | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(address), 0750); err != nil { | ||
| return nil, fmt.Errorf("failed to create socket directory: %w", err) | ||
| } | ||
|
|
||
| listener, err := net.Listen("unix", address) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create UNIX socket listener: %w", err) | ||
| } | ||
|
|
||
| if err := os.Chmod(address, socketPermissions); err != nil { | ||
|
samuv marked this conversation as resolved.
|
||
| // Roll back the bound listener and the socket file rather than leaking | ||
| // either: the listener owns the AF_UNIX file and net.Listen will not | ||
| // rebind the same path while the file exists. | ||
| _ = listener.Close() | ||
| _ = os.Remove(address) | ||
| return nil, fmt.Errorf("failed to set socket permissions: %w", err) | ||
| } | ||
|
|
||
| return listener, nil | ||
| } | ||
|
|
||
| // cleanupUnixSocket removes the socket file at address. Missing files are not | ||
| // an error since cleanup may run after a partial startup. | ||
| func cleanupUnixSocket(address string) { | ||
| if err := os.Remove(address); err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| slog.Warn("failed to remove socket file", "error", err) | ||
| } | ||
| } | ||
|
|
||
| // socketURL returns the URL form of a Unix-socket address for the discovery | ||
| // file. Non-Windows platforms only ever produce unix:// URLs. | ||
| func socketURL(address string) string { | ||
| return "unix://" + address | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build !windows | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSocketURL_Unix(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, "unix:///tmp/test.sock", socketURL("/tmp/test.sock")) | ||
| } | ||
|
|
||
| func TestIsNamedPipeAddress(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| name string | ||
| address string | ||
| want bool | ||
| }{ | ||
| {"plain socket", "/tmp/thv.sock", false}, | ||
| {"named pipe", `\\.\pipe\thv-api`, true}, | ||
| {"named pipe mixed case", `\\.\Pipe\thv-api`, true}, | ||
| {"empty", "", false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, tt.want, isNamedPipeAddress(tt.address)) | ||
| }) | ||
| } | ||
| } | ||
|
samuv marked this conversation as resolved.
|
||
|
|
||
| // TestCreateListener_NamedPipe_Unsupported asserts that createListener rejects | ||
| // pipe addresses on non-Windows up front, mirroring the dialer-side guard | ||
| // covered by TestCheckHealth_NamedPipe_Unsupported_OnNonWindows. | ||
| func TestCreateListener_NamedPipe_Unsupported(t *testing.T) { | ||
| t.Parallel() | ||
| _, _, err := createListener(`\\.\pipe\thv-api`, true) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "only supported on Windows") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build windows | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "log/slog" | ||
| "net" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/Microsoft/go-winio" | ||
| ) | ||
|
|
||
| // namedPipeBufferSize is the size of the input/output buffers winio allocates | ||
| // per pipe instance. 64 KiB matches what go-winio uses in similar consumers | ||
| // (Docker, containerd, Podman) and is well above any single HTTP header chunk. | ||
| const namedPipeBufferSize = 64 * 1024 | ||
|
|
||
| // supportsNamedPipe reports whether the current build target can host a | ||
| // Windows named-pipe listener. Used by createListener to choose between the | ||
| // pipe and AF_UNIX paths without dragging the runtime package into server.go. | ||
| func supportsNamedPipe() bool { return true } | ||
|
|
||
| // setupUnixSocket creates either a Windows named-pipe listener (when address | ||
| // has the \\.\pipe\ prefix) or an AF_UNIX listener at a filesystem path. | ||
| // | ||
| // Named pipes are kernel objects rather than files, so the os.Stat / os.Remove | ||
| // precheck, os.MkdirAll, and os.Chmod steps are skipped: the pipe namespace | ||
| // has no parent directory, and access control is governed by the security | ||
| // descriptor on the listener (winio's default restricts access to the | ||
| // creating user, which matches the toolhive-studio same-user use case). | ||
| // | ||
| // AF_UNIX is supported on Windows 10 1803+. The chmod step is dropped on this | ||
| // path because POSIX file modes do not apply on Windows. | ||
| func setupUnixSocket(address string) (net.Listener, error) { | ||
| if isNamedPipeAddress(address) { | ||
| // MessageMode is left at false (byte stream) explicitly because HTTP | ||
| // requires byte-oriented framing. | ||
| listener, err := winio.ListenPipe(address, &winio.PipeConfig{ | ||
|
samuv marked this conversation as resolved.
|
||
| MessageMode: false, | ||
| InputBufferSize: namedPipeBufferSize, | ||
|
samuv marked this conversation as resolved.
|
||
| OutputBufferSize: namedPipeBufferSize, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create named pipe listener: %w", err) | ||
| } | ||
| return listener, nil | ||
| } | ||
|
|
||
| if err := os.Remove(address); err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| return nil, fmt.Errorf("failed to remove existing socket: %w", err) | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(address), 0750); err != nil { | ||
| return nil, fmt.Errorf("failed to create socket directory: %w", err) | ||
| } | ||
|
|
||
| listener, err := net.Listen("unix", address) | ||
|
samuv marked this conversation as resolved.
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create UNIX socket listener: %w", err) | ||
| } | ||
|
|
||
| return listener, nil | ||
| } | ||
|
|
||
| // cleanupUnixSocket removes the AF_UNIX socket file at address, or no-ops for | ||
| // named pipes (the pipe is destroyed when the listener closes). | ||
| func cleanupUnixSocket(address string) { | ||
| if isNamedPipeAddress(address) { | ||
| return | ||
| } | ||
| if err := os.Remove(address); err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| slog.Warn("failed to remove socket file", "error", err) | ||
| } | ||
| } | ||
|
|
||
| // socketURL returns the URL form of a Unix-socket or named-pipe address for | ||
| // the discovery file. Named pipes are emitted as npipe://<name> where <name> | ||
| // is everything after the \\.\pipe\ prefix. | ||
| func socketURL(address string) string { | ||
|
samuv marked this conversation as resolved.
|
||
| if isNamedPipeAddress(address) { | ||
| return "npipe://" + address[len(namedPipePrefix):] | ||
| } | ||
| return "unix://" + address | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.