-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: route OCI artifact pulls through Docker Desktop HTTP proxy #13770
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
Open
glours
wants to merge
1
commit into
main
Choose a base branch
from
fix/oci-resolver-dd-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| Copyright 2026 Docker Compose CLI authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package desktop | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/moby/moby/client" | ||
| "github.com/sirupsen/logrus" | ||
|
|
||
| "github.com/docker/compose/v5/internal/memnet" | ||
| ) | ||
|
|
||
| // Endpoint returns the Docker Desktop API socket endpoint advertised via the | ||
| // engine info labels, or "" when the active engine is not Docker Desktop. | ||
| func Endpoint(ctx context.Context, apiClient client.APIClient) (string, error) { | ||
| res, err := apiClient.Info(ctx, client.InfoOptions{}) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| for _, l := range res.Info.Labels { | ||
| if k, v, ok := strings.Cut(l, "="); ok && k == EngineLabel { | ||
| return v, nil | ||
| } | ||
| } | ||
| return "", nil | ||
| } | ||
|
|
||
| // httpProxySocketEndpoint derives Docker Desktop's HTTP proxy socket endpoint | ||
| // from a Docker Desktop socket endpoint in the same directory. Returns "" | ||
| // when the input is not a recognized form or when the derived unix socket | ||
| // does not exist (older DD versions or non-DD installs). | ||
| // | ||
| // On macOS/Linux: unix:///path/to/Data/docker-cli.sock → unix:///path/to/Data/httpproxy.sock | ||
| // On Windows: npipe://./pipe/dockerDesktopLinuxEngine → npipe://./pipe/dockerHttpProxy | ||
| func httpProxySocketEndpoint(endpoint string) string { | ||
| if sockPath, ok := strings.CutPrefix(endpoint, "unix://"); ok { | ||
| proxyPath := filepath.Join(filepath.Dir(sockPath), "httpproxy.sock") | ||
| if _, err := os.Stat(proxyPath); err != nil { | ||
| return "" | ||
| } | ||
| return "unix://" + proxyPath | ||
| } | ||
| if strings.HasPrefix(endpoint, "npipe://") { | ||
| return "npipe://./pipe/dockerHttpProxy" | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // ProxyTransport returns an http.RoundTripper that routes traffic through | ||
| // Docker Desktop's PAC-aware HTTP proxy when DD exposes the proxy socket, | ||
| // or nil when no override is needed (callers should use their own default | ||
| // transport in that case — for the OCI resolver this means containerd's | ||
| // built-in transport). Pass "" for endpoint when DD is not the active | ||
| // engine. | ||
| // | ||
| // When DD is available, the returned transport is a clone of | ||
| // http.DefaultTransport with only Proxy and DialContext overridden, so it | ||
| // preserves stdlib timeout, pooling, and HTTP/2 defaults. | ||
| func ProxyTransport(endpoint string) http.RoundTripper { | ||
| proxyEndpoint := httpProxySocketEndpoint(endpoint) | ||
| if proxyEndpoint == "" { | ||
| logrus.Debug("Docker Desktop HTTP proxy not available; deferring to caller's default transport") | ||
| return nil | ||
| } | ||
| logrus.Debugf("routing OCI traffic through Docker Desktop HTTP proxy at %s", proxyEndpoint) | ||
| // Clone http.DefaultTransport to inherit stdlib timeout, pool, and | ||
| // HTTP/2 defaults. Type-assertion is guarded since a process may have | ||
| // replaced http.DefaultTransport with a wrapping RoundTripper (e.g. | ||
| // instrumentation libraries); fall back to a fresh transport in that | ||
| // case rather than panicking. | ||
| var tr *http.Transport | ||
| if defaultTr, ok := http.DefaultTransport.(*http.Transport); ok { | ||
| tr = defaultTr.Clone() | ||
| } else { | ||
| tr = &http.Transport{} | ||
| } | ||
| tr.Proxy = http.ProxyURL(&url.URL{Scheme: "http"}) | ||
| tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
| return memnet.DialEndpoint(ctx, proxyEndpoint) | ||
|
glours marked this conversation as resolved.
|
||
| } | ||
| return tr | ||
| } | ||
|
|
||
| // ProxyTransportFor discovers the Docker Desktop endpoint via apiClient and | ||
| // returns the matching transport, or nil when DD is not active or discovery | ||
| // fails (so callers fall back to their own default transport). | ||
| func ProxyTransportFor(ctx context.Context, apiClient client.APIClient) http.RoundTripper { | ||
| endpoint, err := Endpoint(ctx, apiClient) | ||
| if err != nil { | ||
| logrus.Debugf("could not detect Docker Desktop endpoint, deferring to caller's default transport: %v", err) | ||
| return nil | ||
| } | ||
| return ProxyTransport(endpoint) | ||
| } | ||
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,107 @@ | ||
| /* | ||
| Copyright 2026 Docker Compose CLI authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package desktop | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestHTTPProxySocketEndpoint_UnixSocketExists(t *testing.T) { | ||
| dir := t.TempDir() | ||
| cliSock := filepath.Join(dir, "docker-cli.sock") | ||
| proxySock := filepath.Join(dir, "httpproxy.sock") | ||
| mustTouch(t, cliSock) | ||
| mustTouch(t, proxySock) | ||
|
|
||
| got := httpProxySocketEndpoint("unix://" + cliSock) | ||
| assert.Equal(t, got, "unix://"+proxySock) | ||
| } | ||
|
|
||
| func TestHTTPProxySocketEndpoint_UnixSocketMissing(t *testing.T) { | ||
| // httpproxy.sock deliberately not created — older DD or partial install. | ||
| dir := t.TempDir() | ||
| cliSock := filepath.Join(dir, "docker-cli.sock") | ||
| mustTouch(t, cliSock) | ||
|
|
||
| got := httpProxySocketEndpoint("unix://" + cliSock) | ||
| assert.Equal(t, got, "", "stat miss must fall back so callers do not dial a non-existent socket") | ||
| } | ||
|
|
||
| func TestHTTPProxySocketEndpoint_WindowsNamedPipe(t *testing.T) { | ||
| got := httpProxySocketEndpoint("npipe://./pipe/dockerCli") | ||
| assert.Equal(t, got, "npipe://./pipe/dockerHttpProxy") | ||
| } | ||
|
|
||
| func TestHTTPProxySocketEndpoint_EmptyOrUnknown(t *testing.T) { | ||
| assert.Equal(t, httpProxySocketEndpoint(""), "") | ||
| assert.Equal(t, httpProxySocketEndpoint("tcp://localhost:1234"), "") | ||
| } | ||
|
|
||
| func TestProxyTransport_NilWhenNoDockerDesktop(t *testing.T) { | ||
| assert.Assert(t, ProxyTransport("") == nil, | ||
| "must return nil so callers fall back to their own (e.g. containerd's) default transport") | ||
| } | ||
|
|
||
| func TestProxyTransport_NilWhenSocketMissing(t *testing.T) { | ||
| // no httpproxy.sock created | ||
| dir := t.TempDir() | ||
| cliSock := filepath.Join(dir, "docker-cli.sock") | ||
| mustTouch(t, cliSock) | ||
|
|
||
| assert.Assert(t, ProxyTransport("unix://"+cliSock) == nil, | ||
| "must return nil when DD endpoint is set but proxy socket is missing, not a transport that would dial a dead socket") | ||
| } | ||
|
|
||
| func TestProxyTransport_RoutesThroughDockerDesktop(t *testing.T) { | ||
| if runtime.GOOS == "windows" { | ||
| t.Skip("unix sockets test path; Windows uses named pipes which os.Stat handles differently") | ||
| } | ||
| dir := t.TempDir() | ||
| cliSock := filepath.Join(dir, "docker-cli.sock") | ||
| proxySock := filepath.Join(dir, "httpproxy.sock") | ||
| mustTouch(t, cliSock) | ||
| mustTouch(t, proxySock) | ||
|
|
||
| got := ProxyTransport("unix://" + cliSock) | ||
| tr, ok := got.(*http.Transport) | ||
| assert.Assert(t, ok, "expected *http.Transport when DD endpoint is set and socket exists") | ||
| assert.Assert(t, tr != http.DefaultTransport, "must be a clone, not DefaultTransport itself") | ||
|
|
||
| // Verify the clone preserved http.DefaultTransport's production | ||
| // settings (timeouts, idle pool, HTTP/2). Compare to the source | ||
| // fields rather than asserting fixed values so this test follows | ||
| // stdlib changes. | ||
| src := http.DefaultTransport.(*http.Transport) | ||
| assert.Equal(t, tr.MaxIdleConns, src.MaxIdleConns) | ||
| assert.Equal(t, tr.IdleConnTimeout, src.IdleConnTimeout) | ||
| assert.Equal(t, tr.TLSHandshakeTimeout, src.TLSHandshakeTimeout) | ||
| assert.Equal(t, tr.ExpectContinueTimeout, src.ExpectContinueTimeout) | ||
| assert.Equal(t, tr.ForceAttemptHTTP2, src.ForceAttemptHTTP2) | ||
| } | ||
|
|
||
| func mustTouch(t *testing.T, path string) { | ||
| t.Helper() | ||
| f, err := os.Create(path) | ||
| assert.NilError(t, err) | ||
| assert.NilError(t, f.Close()) | ||
| } |
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,70 @@ | ||
| /* | ||
| Copyright 2026 Docker Compose CLI authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package oci | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/docker/cli/cli/config/configfile" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| // recordingRoundTripper counts RoundTrip invocations on a delegate so tests | ||
| // can verify a supplied transport is actually used by the resolver. | ||
| type recordingRoundTripper struct { | ||
| delegate http.RoundTripper | ||
| calls atomic.Int32 | ||
| } | ||
|
|
||
| func (r *recordingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| r.calls.Add(1) | ||
| return r.delegate.RoundTrip(req) | ||
| } | ||
|
|
||
| // TestNewResolver_UsesProvidedTransport guards that the transport passed to | ||
| // NewResolver actually carries OCI traffic. The httptest server returns 401 | ||
| // so the resolver fails fast without real network access. | ||
| func TestNewResolver_UsesProvidedTransport(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusUnauthorized) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| host := server.Listener.Addr().String() | ||
| // Bare *http.Transport (Proxy: nil) keeps the test hermetic — delegating | ||
| // to http.DefaultTransport would honor HTTP[S]_PROXY env vars in CI or | ||
| // dev shells and route requests away from our local httptest server. | ||
| rec := &recordingRoundTripper{delegate: &http.Transport{}} | ||
|
|
||
| // Mark the test host insecure so the resolver uses HTTP scheme; this | ||
| // avoids needing a TLS cert chain just to exercise plumbing. | ||
| resolver := NewResolver(&configfile.ConfigFile{}, rec, host) | ||
|
|
||
|
glours marked this conversation as resolved.
|
||
| // We expect 401, but only care that the request reached our transport. | ||
| _, _, _ = resolver.Resolve(t.Context(), host+"/test/image:latest") | ||
|
|
||
| assert.Assert(t, rec.calls.Load() > 0, | ||
| "resolver did not invoke the supplied transport — wiring is broken") | ||
| } | ||
|
|
||
| func TestNewResolver_NilTransportIsValid(t *testing.T) { | ||
| resolver := NewResolver(&configfile.ConfigFile{}, nil) | ||
| assert.Assert(t, resolver != nil, "NewResolver must return a non-nil resolver when transport is nil") | ||
| } | ||
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
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.