Skip to content

Modern Docker Management, Designed for Everyone

fix: ipv6 parsing failing on cert docker api versions (#2100)

Kyle Mendell8ea8acb843cf
diff --git a/backend/internal/services/docker_client_service.go b/backend/internal/services/docker_client_service.go
index cbc5017db..4361844da 100644
--- a/backend/internal/services/docker_client_service.go
+++ b/backend/internal/services/docker_client_service.go
@@ -11,6 +11,7 @@ import (
"github.com/getarcaneapp/arcane/backend/internal/config"
"github.com/getarcaneapp/arcane/backend/internal/database"
docker "github.com/getarcaneapp/arcane/backend/pkg/dockerutil"
+ "github.com/getarcaneapp/arcane/backend/pkg/libarcane"
"github.com/getarcaneapp/arcane/backend/pkg/libarcane/timeouts"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/image"
@@ -200,7 +201,7 @@ func (s *DockerClientService) GetAllNetworks(ctx context.Context) (_ []network.S
}
}
- networkList, err := dockerClient.NetworkList(apiCtx, client.NetworkListOptions{})
+ networkList, err := libarcane.NetworkListWithCompatibility(apiCtx, dockerClient, client.NetworkListOptions{})
if err != nil {
return nil, 0, 0, 0, fmt.Errorf("failed to list Docker networks: %w", err)
}
diff --git a/backend/internal/services/network_service.go b/backend/internal/services/network_service.go
index 6b6456a37..885498c47 100644
--- a/backend/internal/services/network_service.go
+++ b/backend/internal/services/network_service.go
@@ -150,7 +150,7 @@ func (s *NetworkService) ListNetworksPaginated(ctx context.Context, params pagin
inUseByID, inUseByName := s.buildNetworkUsageMaps(containers)
- networkList, err := dockerClient.NetworkList(ctx, client.NetworkListOptions{})
+ networkList, err := libarcane.NetworkListWithCompatibility(ctx, dockerClient, client.NetworkListOptions{})
if err != nil {
return nil, pagination.Response{}, networktypes.UsageCounts{}, fmt.Errorf("failed to list Docker networks: %w", err)
}
diff --git a/backend/pkg/libarcane/inspect_compat.go b/backend/pkg/libarcane/inspect_compat.go
index e8553b106..d0715e403 100644
--- a/backend/pkg/libarcane/inspect_compat.go
+++ b/backend/pkg/libarcane/inspect_compat.go
@@ -47,6 +47,10 @@ func (c *inspectCompatibilityClient) NetworkInspect(ctx context.Context, network
return NetworkInspectWithCompatibility(ctx, c.APIClient, networkID, options)
}
+func (c *inspectCompatibilityClient) NetworkList(ctx context.Context, options client.NetworkListOptions) (client.NetworkListResult, error) {
+ return NetworkListWithCompatibility(ctx, c.APIClient, options)
+}
+
// ContainerInspectWithCompatibility retries inspect decoding against raw daemon
// JSON when the primary typed decode fails on a ParseAddr-style CIDR issue.
func ContainerInspectWithCompatibility(ctx context.Context, apiClient client.APIClient, containerID string, options client.ContainerInspectOptions) (client.ContainerInspectResult, error) {
@@ -126,6 +130,43 @@ func NetworkInspectWithCompatibility(ctx context.Context, apiClient client.APICl
}, nil
}
+// NetworkListWithCompatibility retries list decoding against raw daemon JSON
+// when the primary typed decode fails on a ParseAddr-style CIDR issue.
+func NetworkListWithCompatibility(ctx context.Context, apiClient client.APIClient, options client.NetworkListOptions) (client.NetworkListResult, error) {
+ if apiClient == nil {
+ return client.NetworkListResult{}, fmt.Errorf("docker api client is nil")
+ }
+
+ result, err := apiClient.NetworkList(ctx, options)
+ if err == nil || !isInspectAddressParseErrorInternal(err) {
+ return result, err
+ }
+
+ query, queryErr := buildNetworkListQueryInternal(options)
+ if queryErr != nil {
+ return client.NetworkListResult{}, err
+ }
+
+ raw, fetchErr := fetchDockerAPIJSONInternal(ctx, apiClient, "/networks", query)
+ if fetchErr != nil {
+ return client.NetworkListResult{}, err
+ }
+
+ normalized, changed, normalizeErr := normalizeNetworkListRawJSONInternal(raw)
+ if normalizeErr != nil || !changed {
+ return client.NetworkListResult{}, err
+ }
+
+ var repaired []networktypes.Summary
+ if unmarshalErr := json.Unmarshal(normalized, &repaired); unmarshalErr != nil {
+ return client.NetworkListResult{}, err
+ }
+
+ return client.NetworkListResult{
+ Items: repaired,
+ }, nil
+}
+
func fetchDockerAPIJSONInternal(ctx context.Context, apiClient client.APIClient, resourcePath string, query url.Values) ([]byte, error) {
dialer := apiClient.Dialer()
if dialer == nil {
@@ -228,12 +269,51 @@ func normalizeContainerInspectRawJSONInternal(raw []byte) ([]byte, bool, error)
return normalized, true, nil
}
+func normalizeNetworkListRawJSONInternal(raw []byte) ([]byte, bool, error) {
+ var payload []any
+ if err := json.Unmarshal(raw, &payload); err != nil {
+ return nil, false, err
+ }
+
+ changed := false
+ for _, item := range payload {
+ networkPayload, ok := asMapInternal(item)
+ if !ok {
+ continue
+ }
+ changed = normalizeNetworkObjectInternal(networkPayload) || changed
+ }
+
+ if !changed {
+ return raw, false, nil
+ }
+
+ normalized, err := json.Marshal(payload)
+ if err != nil {
+ return nil, false, err
+ }
+ return normalized, true, nil
+}
+
func normalizeNetworkInspectRawJSONInternal(raw []byte) ([]byte, bool, error) {
payload := map[string]any{}
if err := json.Unmarshal(raw, &payload); err != nil {
return nil, false, err
}
+ changed := normalizeNetworkObjectInternal(payload)
+ if !changed {
+ return raw, false, nil
+ }
+
+ normalized, err := json.Marshal(payload)
+ if err != nil {
+ return nil, false, err
+ }
+ return normalized, true, nil
+}
+
+func normalizeNetworkObjectInternal(payload map[string]any) bool {
changed := false
if ipam, ok := asMapInternal(payload["IPAM"]); ok {
if configs, ok := asSliceInternal(ipam["Config"]); ok {
@@ -260,16 +340,7 @@ func normalizeNetworkInspectRawJSONInternal(raw []byte) ([]byte, bool, error) {
changed = normalizePrefixStringFieldInternal(endpoint, "IPv6Address") || changed
}
}
-
- if !changed {
- return raw, false, nil
- }
-
- normalized, err := json.Marshal(payload)
- if err != nil {
- return nil, false, err
- }
- return normalized, true, nil
+ return changed
}
func normalizeAddressStringFieldInternal(obj map[string]any, key string) bool {
@@ -454,6 +525,20 @@ func asSliceInternal(value any) ([]any, bool) {
return out, ok
}
+func buildNetworkListQueryInternal(options client.NetworkListOptions) (url.Values, error) {
+ query := url.Values{}
+ if len(options.Filters) == 0 {
+ return query, nil
+ }
+
+ filtersJSON, err := json.Marshal(options.Filters)
+ if err != nil {
+ return nil, err
+ }
+ query.Set("filters", string(filtersJSON))
+ return query, nil
+}
+
func isInspectAddressParseErrorInternal(err error) bool {
if err == nil {
return false
diff --git a/backend/pkg/libarcane/inspect_compat_test.go b/backend/pkg/libarcane/inspect_compat_test.go
index 390ae3ff5..a736070f2 100644
--- a/backend/pkg/libarcane/inspect_compat_test.go
+++ b/backend/pkg/libarcane/inspect_compat_test.go
@@ -164,6 +164,41 @@ func TestNormalizeNetworkInspectRawJSONInternal_NormalizesContainerEndpoints(t *
assert.Equal(t, netip.MustParsePrefix("fdd0:0:0:c::10/64"), endpoint.IPv6Address)
}
+func TestNormalizeNetworkListRawJSONInternal(t *testing.T) {
+ raw := []byte(`[
+ {
+ "Name":"test-net",
+ "Id":"net123",
+ "Created":"2026-03-11T00:00:00Z",
+ "Scope":"local",
+ "Driver":"bridge",
+ "IPAM":{
+ "Driver":"default",
+ "Config":[
+ {
+ "Subnet":"fdd0:0:0:c::/64",
+ "Gateway":"fdd0:0:0:c::1/64",
+ "AuxiliaryAddresses":{
+ "router":"fdd0:0:0:c::2/64"
+ }
+ }
+ ]
+ }
+ }
+ ]`)
+
+ normalized, changed, err := normalizeNetworkListRawJSONInternal(raw)
+ require.NoError(t, err)
+ require.True(t, changed)
+
+ var networks []networktypes.Summary
+ require.NoError(t, json.Unmarshal(normalized, &networks))
+ require.Len(t, networks, 1)
+ require.Len(t, networks[0].IPAM.Config, 1)
+ assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::1"), networks[0].IPAM.Config[0].Gateway)
+ assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::2"), networks[0].IPAM.Config[0].AuxAddress["router"])
+}
+
func TestNormalizeAddressStringInternal_TrimsWhitespaceAroundValidAddress(t *testing.T) {
normalized, changed := normalizeAddressStringInternal(" 172.18.0.1 ")
assert.True(t, changed)
@@ -250,6 +285,82 @@ func TestNetworkInspectWithCompatibility(t *testing.T) {
assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::1"), result.Network.IPAM.Config[0].Gateway)
}
+func TestNetworkListWithCompatibility(t *testing.T) {
+ networkJSON := `[
+ {
+ "Name":"test-net",
+ "Id":"net123",
+ "Created":"2026-03-11T00:00:00Z",
+ "Scope":"local",
+ "Driver":"bridge",
+ "IPAM":{
+ "Driver":"default",
+ "Config":[
+ {
+ "Subnet":"fdd0:0:0:c::/64",
+ "Gateway":"fdd0:0:0:c::1/64",
+ "AuxiliaryAddresses":{
+ "router":"fdd0:0:0:c::2/64"
+ }
+ }
+ ]
+ }
+ }
+ ]`
+
+ callCount := 0
+ dockerClient := newTestDockerClient(t, func(w http.ResponseWriter, r *http.Request) {
+ callCount++
+ assert.Equal(t, "/v1.41/networks", r.URL.Path)
+ assert.Equal(t, `{"name":{"test-net":true}}`, r.URL.Query().Get("filters"))
+ _, _ = w.Write([]byte(networkJSON))
+ })
+
+ result, err := NetworkListWithCompatibility(context.Background(), dockerClient, client.NetworkListOptions{
+ Filters: make(client.Filters).Add("name", "test-net"),
+ })
+ require.NoError(t, err)
+ require.Len(t, result.Items, 1)
+ require.Len(t, result.Items[0].IPAM.Config, 1)
+ assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::1"), result.Items[0].IPAM.Config[0].Gateway)
+ assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::2"), result.Items[0].IPAM.Config[0].AuxAddress["router"])
+ assert.Equal(t, 2, callCount)
+}
+
+func TestNetworkListWithCompatibility_SuccessSkipsFallback(t *testing.T) {
+ networkJSON := `[
+ {
+ "Name":"test-net",
+ "Id":"net123",
+ "Created":"2026-03-11T00:00:00Z",
+ "Scope":"local",
+ "Driver":"bridge",
+ "IPAM":{
+ "Driver":"default",
+ "Config":[
+ {
+ "Subnet":"fdd0:0:0:c::/64",
+ "Gateway":"fdd0:0:0:c::1"
+ }
+ ]
+ }
+ }
+ ]`
+
+ callCount := 0
+ dockerClient := newTestDockerClient(t, func(w http.ResponseWriter, r *http.Request) {
+ callCount++
+ assert.Equal(t, "/v1.41/networks", r.URL.Path)
+ _, _ = w.Write([]byte(networkJSON))
+ })
+
+ result, err := NetworkListWithCompatibility(context.Background(), dockerClient, client.NetworkListOptions{})
+ require.NoError(t, err)
+ require.Len(t, result.Items, 1)
+ assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::1"), result.Items[0].IPAM.Config[0].Gateway)
+ assert.Equal(t, 1, callCount)
+}
+
func TestContainerInspectWithCompatibility_TLSRemote(t *testing.T) {
containerJSON := `{
"Id":"abc123",
@@ -305,6 +416,29 @@ func TestInspectCompatibilityLeavesInvalidValuesFailing(t *testing.T) {
assert.True(t, strings.Contains(err.Error(), `ParseAddr("definitely-not-an-ip")`))
}
+func TestNetworkListWithCompatibility_LeavesInvalidValuesFailing(t *testing.T) {
+ callCount := 0
+ dockerClient := newTestDockerClient(t, func(w http.ResponseWriter, r *http.Request) {
+ callCount++
+ assert.Equal(t, "/v1.41/networks", r.URL.Path)
+ _, _ = w.Write([]byte(`[
+ {
+ "Name":"test-net",
+ "Id":"net123",
+ "Created":"2026-03-11T00:00:00Z",
+ "Scope":"local",
+ "Driver":"bridge",
+ "IPAM":{"Driver":"default","Config":[{"Subnet":"fdd0:0:0:c::/64","Gateway":"definitely-not-an-ip"}]}
+ }
+ ]`))
+ })
+
+ _, err := NetworkListWithCompatibility(context.Background(), dockerClient, client.NetworkListOptions{})
+ require.Error(t, err)
+ assert.True(t, strings.Contains(err.Error(), `ParseAddr("definitely-not-an-ip")`))
+ assert.Equal(t, 2, callCount)
+}
+
func TestWrapDockerAPIClientForInspectCompatibility(t *testing.T) {
dockerClient := newTestDockerClient(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{
@@ -322,6 +456,31 @@ func TestWrapDockerAPIClientForInspectCompatibility(t *testing.T) {
assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::1"), result.Container.NetworkSettings.Networks["bridge"].IPv6Gateway)
}
+func TestWrapDockerAPIClientForInspectCompatibility_NetworkList(t *testing.T) {
+ callCount := 0
+ dockerClient := newTestDockerClient(t, func(w http.ResponseWriter, r *http.Request) {
+ callCount++
+ assert.Equal(t, "/v1.41/networks", r.URL.Path)
+ _, _ = w.Write([]byte(`[
+ {
+ "Name":"test-net",
+ "Id":"net123",
+ "Created":"2026-03-11T00:00:00Z",
+ "Scope":"local",
+ "Driver":"bridge",
+ "IPAM":{"Driver":"default","Config":[{"Subnet":"fdd0:0:0:c::/64","Gateway":"fdd0:0:0:c::1/64"}]}
+ }
+ ]`))
+ })
+
+ wrapped := WrapDockerAPIClientForInspectCompatibility(dockerClient)
+ result, err := wrapped.NetworkList(context.Background(), client.NetworkListOptions{})
+ require.NoError(t, err)
+ require.Len(t, result.Items, 1)
+ assert.Equal(t, netip.MustParseAddr("fdd0:0:0:c::1"), result.Items[0].IPAM.Config[0].Gateway)
+ assert.Equal(t, 2, callCount)
+}
+
func TestWrapDockerAPIClientForInspectCompatibility_ContainerCreateLegacyNetworks(t *testing.T) {
type createRequest struct {
NetworkingConfig networktypes.NetworkingConfig `json:"NetworkingConfig"`