Skip to content

Modern Docker Management, Designed for Everyone

refactor: merge all-environments reosurce lists on the server (#4419)

Kyle Mendellb6979fc389e7
diff --git a/backend/internal/bootstrap/router_bootstrap.go b/backend/internal/bootstrap/router_bootstrap.go
index cbbb3dffe..eeda2b605 100644
--- a/backend/internal/bootstrap/router_bootstrap.go
+++ b/backend/internal/bootstrap/router_bootstrap.go
@@ -11,6 +11,7 @@ import (
"strings"
"github.com/getarcaneapp/arcane/types/v2"
+ "github.com/getarcaneapp/arcane/types/v2/base"
usertypes "github.com/getarcaneapp/arcane/types/v2/user"
"github.com/labstack/echo-otel/v5"
"github.com/labstack/echo/v5"
@@ -24,6 +25,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/apikey"
"github.com/getarcaneapp/arcane/backend/v2/internal/auth"
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/environment"
"github.com/getarcaneapp/arcane/backend/v2/internal/federated"
"github.com/getarcaneapp/arcane/backend/v2/internal/middleware"
"github.com/getarcaneapp/arcane/backend/v2/internal/telemetry"
@@ -269,6 +271,15 @@ func newRouter(p RouterParams) (*echo.Echo, *edge.TunnelServer) {
p.TunnelRegistry,
httpx.ValidateWebSocketOrigin(cfg.GetAppURL()),
)
+ apiGroup.Use(middleware.NewEnvironmentScopeMiddleware(func(ctx context.Context, id string) (base.EnvironmentRef, error) {
+ env, err := deps.Environment.Service().GetEnvironmentByIDCached(ctx, id)
+ if err != nil {
+ return base.EnvironmentRef{}, err
+ }
+ return base.EnvironmentRef{ID: id, Name: environment.DisplayName(id, env.Name)}, nil
+ }, permissionMatcher, func(ctx context.Context) int {
+ return deps.Settings.Service().GetIntSetting(ctx, "allEnvironmentsLimit", 64)
+ }))
apiGroup.Use(envProxyMiddleware)
humaAPI := api.SetupAPI(e, apiGroup, handlerAppCtx, cfg, deps)
diff --git a/backend/internal/build/service.go b/backend/internal/build/service.go
index d1a358885..2bcd70d9f 100644
--- a/backend/internal/build/service.go
+++ b/backend/internal/build/service.go
@@ -13,6 +13,7 @@ import (
"strings"
"time"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/image"
usertypes "github.com/getarcaneapp/arcane/types/v2/user"
"go.getarcane.app/builds/api"
@@ -33,6 +34,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/settings"
"github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
type BuildService struct {
@@ -256,10 +259,10 @@ func sanitizeBuildContextForEventInternal(raw string) string {
return ""
}
- base, fragment, hasFragment := strings.Cut(trimmed, "#")
- parsed, err := url.Parse(base)
+ address, fragment, hasFragment := strings.Cut(trimmed, "#")
+ parsed, err := url.Parse(address)
if err != nil {
- return kit.Ternary(strings.Contains(base, "@"), "[unparseable URL]", trimmed)
+ return kit.Ternary(strings.Contains(address, "@"), "[unparseable URL]", trimmed)
}
if parsed.User == nil {
return trimmed
@@ -460,6 +463,48 @@ func (s *BuildService) ListImageBuildsByEnvironmentPaginated(ctx context.Context
return records, paginationResp, nil
}
+func (s *BuildService) AggregateImageBuilds(ctx context.Context, params pagination.QueryParams) (base.Paginated[image.BuildRecord], error) {
+ collection, err := httpx.CollectEnvironments[base.Paginated[image.BuildRecord]](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return base.Paginated[image.BuildRecord]{}, err
+ }
+
+ pages := make([]pagination.EnvironmentPage[image.BuildRecord], 0, len(collection.Results))
+ for _, result := range collection.Results {
+ pages = append(pages, pagination.EnvironmentPage[image.BuildRecord]{
+ Environment: result.Environment,
+ Items: result.Value.Data,
+ TotalAvailable: cmp.Or(result.Value.Pagination.GrandTotalItems, result.Value.Pagination.TotalItems),
+ })
+ }
+
+ config := pagination.Config[image.BuildRecord]{SortBindings: []pagination.SortBinding[image.BuildRecord]{
+ {
+ Key: "createdAt",
+ Fn: func(a, b image.BuildRecord) int {
+ return cmp.Or(a.CreatedAt.Compare(b.CreatedAt), strings.Compare(a.ID, b.ID))
+ },
+ DescFn: func(a, b image.BuildRecord) int {
+ return cmp.Or(b.CreatedAt.Compare(a.CreatedAt), strings.Compare(a.ID, b.ID))
+ },
+ },
+ {Key: "completedAt", Fn: func(a, b image.BuildRecord) int {
+ return kit.FromPtr(a.CompletedAt).Compare(kit.FromPtr(b.CompletedAt))
+ }},
+ {Key: "status", Fn: func(a, b image.BuildRecord) int {
+ return strings.Compare(a.Status, b.Status)
+ }},
+ }}
+ data, resp := pagination.MergeEnvironmentPages(config, pages, params)
+
+ return base.Paginated[image.BuildRecord]{
+ Success: true,
+ Data: data,
+ Pagination: handlerutil.PaginationResponse(resp),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
func (s *BuildService) GetImageBuildByID(ctx context.Context, environmentID, buildID string) (*image.BuildRecord, error) {
if s.db == nil {
return nil, errors.New("build history not available")
diff --git a/backend/internal/build/service_test.go b/backend/internal/build/service_test.go
index f8aae912e..a56e8ece5 100644
--- a/backend/internal/build/service_test.go
+++ b/backend/internal/build/service_test.go
@@ -6,12 +6,19 @@ import (
"encoding/json/v2"
"errors"
"io"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
"os"
"path/filepath"
"reflect"
"strings"
+ "sync"
"testing"
+ "time"
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ "github.com/getarcaneapp/arcane/types/v2/image"
usertypes "github.com/getarcaneapp/arcane/types/v2/user"
"github.com/libtnb/sqlite"
"github.com/stretchr/testify/assert"
@@ -26,6 +33,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/gitrepo"
"github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func TestBuildService_ResolveBuildRequest_PassesThroughLocalContext(t *testing.T) {
@@ -490,3 +498,91 @@ func encryptSecretForTest(t *testing.T, value string) string {
require.NoError(t, err)
return encrypted
}
+
+func buildScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ _ = json.MarshalWrite(w, body)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregateImageBuildsMergesEnvironments(t *testing.T) {
+ start := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
+ bodies := map[string]any{
+ "a": base.Paginated[image.BuildRecord]{Success: true, Data: []image.BuildRecord{
+ {ID: "a1", EnvironmentID: "0", Status: "success", CreatedAt: start.Add(time.Hour)},
+ {ID: "a2", EnvironmentID: "0", Status: "failed", CreatedAt: start.Add(3 * time.Hour)},
+ }, Pagination: base.PaginationResponse{TotalItems: 2}},
+ "b": base.Paginated[image.BuildRecord]{Success: true, Data: []image.BuildRecord{
+ {ID: "b1", EnvironmentID: "0", Status: "success", CreatedAt: start.Add(2 * time.Hour)},
+ }, Pagination: base.PaginationResponse{TotalItems: 1}},
+ }
+ rowLabels := func(items []image.BuildRecord) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.ID)
+ }
+ return out
+ }
+ svc := &BuildService{}
+
+ t.Run("defaults to newest first across environments and pages", func(t *testing.T) {
+ ctx, queries := buildScopeContext(t, "/api/environments/a,b,c/images/builds?start=0&limit=2&order=desc", bodies)
+ body, err := svc.AggregateImageBuilds(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Order: pagination.SortDesc}, Params: pagination.Params{Limit: 2}})
+ require.NoError(t, err)
+
+ assert.True(t, body.Success)
+ assert.Equal(t, []string{"a/a2", "b/b1"}, rowLabels(body.Data))
+ assert.Equal(t, "Env b", body.Data[1].EnvironmentName)
+ assert.Equal(t, int64(3), body.Pagination.TotalItems)
+ assert.Equal(t, int64(3), body.Pagination.GrandTotalItems)
+ assert.Equal(t, int64(2), body.Pagination.TotalPages)
+ require.Len(t, body.FailedEnvironments, 1)
+ assert.Equal(t, "c", body.FailedEnvironments[0].Environment.ID)
+ assert.Equal(t, "environment offline", body.FailedEnvironments[0].Message)
+ for _, id := range []string{"a", "b"} {
+ query, ok := queries.Load(id)
+ require.True(t, ok)
+ assert.Equal(t, "0", query.(url.Values).Get("start"))
+ assert.Equal(t, "-1", query.(url.Values).Get("limit"))
+ }
+ })
+
+ t.Run("sorts by status", func(t *testing.T) {
+ ctx, _ := buildScopeContext(t, "/api/environments/a,b,c/images/builds?sort=status&order=asc", bodies)
+ body, err := svc.AggregateImageBuilds(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "status", Order: pagination.SortAsc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ assert.Equal(t, []string{"a/a2", "a/a1", "b/b1"}, rowLabels(body.Data))
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, _ := buildScopeContext(t, "/api/environments/a,b,c/images/builds?sort=environment&order=desc", bodies)
+ body, err := svc.AggregateImageBuilds(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ assert.Equal(t, []string{"b/b1", "a/a1", "a/a2"}, rowLabels(body.Data))
+ })
+
+ t.Run("all environments failing returns empty data", func(t *testing.T) {
+ ctx, _ := buildScopeContext(t, "/api/environments/a,b,c/images/builds", map[string]any{})
+ body, err := svc.AggregateImageBuilds(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}})
+ require.NoError(t, err)
+ assert.Empty(t, body.Data)
+ assert.Len(t, body.FailedEnvironments, 3)
+ })
+}
diff --git a/backend/internal/config/schema/schema_test.go b/backend/internal/config/schema/schema_test.go
index a394463ec..b9de69145 100644
--- a/backend/internal/config/schema/schema_test.go
+++ b/backend/internal/config/schema/schema_test.go
@@ -277,6 +277,7 @@ var expectedEnvConfigVars = []string{
var expectedSettingOverrideKeys = []string{
"activityHistoryMaxEntries",
"activityHistoryRetentionDays",
+ "allEnvironmentsLimit",
"apnsEnabled",
"authLocalEnabled",
"authPasswordPolicy",
diff --git a/backend/internal/container/handler.go b/backend/internal/container/handler.go
index 327a55ebd..dccb77436 100644
--- a/backend/internal/container/handler.go
+++ b/backend/internal/container/handler.go
@@ -33,6 +33,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
type ContainerHandler struct {
@@ -45,12 +46,13 @@ type ContainerHandler struct {
// ContainerPaginatedResponse is the paginated list response for containers.
type ContainerPaginatedResponse struct {
- Success bool `json:"success"`
- Data []container.Summary `json:"data"`
- Groups []container.SummaryGroup `json:"groups,omitempty"`
- Counts container.StatusCounts `json:"counts"`
- Pagination base.PaginationResponse `json:"pagination"`
- ResourceSortSupported bool `json:"resourceSortSupported"`
+ Success bool `json:"success"`
+ Data []container.Summary `json:"data"`
+ Groups []container.SummaryGroup `json:"groups,omitempty"`
+ Counts container.StatusCounts `json:"counts"`
+ Pagination base.PaginationResponse `json:"pagination"`
+ ResourceSortSupported bool `json:"resourceSortSupported"`
+ FailedEnvironments []base.EnvironmentFailure `json:"failedEnvironments,omitempty" doc:"Environments whose results are missing from a multi-environment response"`
}
type ListContainersInput struct {
@@ -137,10 +139,6 @@ type GenerateComposeInput struct {
}
func (h *ContainerHandler) ListContainers(ctx context.Context, input *ListContainersInput) (*ListContainersOutput, error) {
- if ps, _ := middleware.PermissionsFromContext(ctx); stats.ContainerResourceSortPermissionDenied(ps, input.EnvironmentID, input.Sort) {
- return nil, huma.Error403Forbidden("permission denied: " + authz.PermContainersRead)
- }
-
params := handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search)
if input.Updates != "" {
params.Filters["updates"] = input.Updates
@@ -152,6 +150,18 @@ func (h *ContainerHandler) ListContainers(ctx context.Context, input *ListContai
params.Filters["label"] = input.Label
}
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.containerService.AggregateContainers(ctx, params, input.GroupBy)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list containers: " + err.Error())
+ }
+ return &ListContainersOutput{Body: body}, nil
+ }
+
+ if ps, _ := middleware.PermissionsFromContext(ctx); stats.ContainerResourceSortPermissionDenied(ps, input.EnvironmentID, input.Sort) {
+ return nil, huma.Error403Forbidden("permission denied: " + authz.PermContainersRead)
+ }
+
result, err := h.containerService.ListContainersPaginated(ctx, params, true, input.IncludeInternal, input.IncludeHidden, input.GroupBy)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to list containers: " + err.Error())
diff --git a/backend/internal/container/module.go b/backend/internal/container/module.go
index 45238a572..9dd18a039 100644
--- a/backend/internal/container/module.go
+++ b/backend/internal/container/module.go
@@ -77,6 +77,7 @@ func RegisterContainers(
Description: "Paginated list of containers",
Tags: []string{"Containers"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermContainersList, h.ListContainers)
middleware.RegisterWithPermission(api, huma.Operation{
diff --git a/backend/internal/container/service.go b/backend/internal/container/service.go
index be754e51c..b2148eabf 100644
--- a/backend/internal/container/service.go
+++ b/backend/internal/container/service.go
@@ -9,6 +9,7 @@ import (
"log/slog"
"maps"
"net/netip"
+ "net/url"
"strings"
"sync"
"time"
@@ -45,6 +46,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/timeouts"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/iconcatalog"
)
@@ -1653,7 +1656,7 @@ func (s *ContainerService) ListContainersPaginated(
ungroupedParams.Limit = -1
result := config.SearchOrderAndPaginate(items, ungroupedParams)
- groups, paginationResp := paginateContainerProjectGroupsInternal(result, params)
+ groups, paginationResp := paginateContainerProjectGroups(result, params)
// Icons must be resolved before flattening: groups hold value copies,
// so the flattened items only carry icons applied to the groups first.
@@ -1719,11 +1722,54 @@ func (s *ContainerService) listContainersByResourceInternal(
}, nil
}
-func paginateContainerProjectGroupsInternal(
+func (s *ContainerService) AggregateContainers(ctx context.Context, params pagination.QueryParams, groupBy string) (ContainerPaginatedResponse, error) {
+ collection, err := httpx.CollectEnvironments[ContainerPaginatedResponse](ctx, url.Values{"start": {"0"}, "limit": {"-1"}, "groupBy": nil})
+ if err != nil {
+ return ContainerPaginatedResponse{}, err
+ }
+
+ pages := make([]pagination.EnvironmentPage[containertypes.Summary], 0, len(collection.Results))
+ counts := containertypes.StatusCounts{}
+ resourceSortSupported := true
+ for _, result := range collection.Results {
+ page := result.Value
+ pages = append(pages, pagination.EnvironmentPage[containertypes.Summary]{
+ Environment: result.Environment,
+ Items: page.Data,
+ TotalAvailable: cmp.Or(page.Pagination.GrandTotalItems, page.Pagination.TotalItems),
+ })
+ counts.RunningContainers += page.Counts.RunningContainers
+ counts.StoppedContainers += page.Counts.StoppedContainers
+ counts.TotalContainers += page.Counts.TotalContainers
+ resourceSortSupported = resourceSortSupported && page.ResourceSortSupported
+ }
+
+ grouped := groupBy == containerGroupByProject
+ ungroupedParams := params
+ ungroupedParams.Start, ungroupedParams.Limit = 0, -1
+ data, resp := pagination.MergeEnvironmentPages(s.buildContainerPaginationConfig(), pages, kit.Ternary(grouped, ungroupedParams, params))
+ var groups []containertypes.SummaryGroup
+ if grouped {
+ groups, resp = paginateContainerProjectGroups(pagination.FilterResult[containertypes.Summary]{Items: data, TotalCount: resp.TotalItems, TotalAvailable: resp.GrandTotalItems}, params)
+ data = flattenContainerProjectGroupsInternal(groups)
+ }
+
+ return ContainerPaginatedResponse{
+ Success: true,
+ Data: data,
+ Groups: groups,
+ Counts: counts,
+ Pagination: handlerutil.PaginationResponse(resp),
+ ResourceSortSupported: resourceSortSupported,
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
+func paginateContainerProjectGroups(
result pagination.FilterResult[containertypes.Summary],
params pagination.QueryParams,
) ([]containertypes.SummaryGroup, pagination.Response) {
- groups := groupContainersByProjectInternal(result.Items)
+ groups := groupContainersByProject(result.Items)
totalCount := len(result.Items)
if params.Limit <= 0 {
@@ -1773,7 +1819,7 @@ func paginateContainerProjectGroupsInternal(
}
}
-func groupContainersByProjectInternal(items []containertypes.Summary) []containertypes.SummaryGroup {
+func groupContainersByProject(items []containertypes.Summary) []containertypes.SummaryGroup {
groups := make([]containertypes.SummaryGroup, 0)
groupIndexes := make(map[string]int)
diff --git a/backend/internal/container/service_test.go b/backend/internal/container/service_test.go
index 580be1884..67a43a936 100644
--- a/backend/internal/container/service_test.go
+++ b/backend/internal/container/service_test.go
@@ -20,6 +20,7 @@ import (
"time"
"github.com/containerd/errdefs"
+ "github.com/getarcaneapp/arcane/types/v2/base"
containertypes "github.com/getarcaneapp/arcane/types/v2/container"
"github.com/getarcaneapp/arcane/types/v2/image"
"github.com/getarcaneapp/arcane/types/v2/user"
@@ -41,6 +42,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/settings"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func TestPaginateContainerProjectGroupsKeepsProjectWhole(t *testing.T) {
@@ -69,7 +71,7 @@ func TestPaginateContainerProjectGroupsKeepsProjectWhole(t *testing.T) {
newGroupedContainerSummary("immich-postgres", "immich"),
}
- groupedItems, resp := paginateContainerProjectGroupsInternal(
+ groupedItems, resp := paginateContainerProjectGroups(
pagination.FilterResult[containertypes.Summary]{Items: items, TotalCount: int64(len(items)), TotalAvailable: int64(len(items))},
pagination.QueryParams{Start: 0, Limit: 20},
)
@@ -108,14 +110,14 @@ func TestPaginateContainerProjectGroupsSelectsRequestedPage(t *testing.T) {
}
result := pagination.FilterResult[containertypes.Summary]{Items: items, TotalCount: int64(len(items)), TotalAvailable: int64(len(items))}
- pageGroups, resp := paginateContainerProjectGroupsInternal(result, pagination.QueryParams{Start: 4, Limit: 4})
+ pageGroups, resp := paginateContainerProjectGroups(result, pagination.QueryParams{Start: 4, Limit: 4})
require.Equal(t, []string{"proj-b", "solo-1"}, groupNamesOf(pageGroups))
require.Equal(t, 2, resp.CurrentPage)
require.Equal(t, int64(3), resp.TotalPages)
require.Equal(t, int64(11), resp.TotalItems)
- pageGroups, resp = paginateContainerProjectGroupsInternal(result, pagination.QueryParams{Start: 400, Limit: 4})
+ pageGroups, resp = paginateContainerProjectGroups(result, pagination.QueryParams{Start: 400, Limit: 4})
require.Equal(t, []string{"proj-c", "solo-2"}, groupNamesOf(pageGroups))
require.Equal(t, 3, resp.CurrentPage)
@@ -140,7 +142,7 @@ func TestContainerSummaryIconsAppliedAfterGrouping(t *testing.T) {
require.Empty(t, item.IconLightURL, "icons must be deferred until after pagination")
}
- groups, _ := paginateContainerProjectGroupsInternal(
+ groups, _ := paginateContainerProjectGroups(
pagination.FilterResult[containertypes.Summary]{Items: items, TotalCount: int64(len(items)), TotalAvailable: int64(len(items))},
pagination.QueryParams{Start: 0, Limit: 20},
)
@@ -166,7 +168,7 @@ func groupNamesOf(groups []containertypes.SummaryGroup) []string {
}
func TestGroupContainersByProjectUsesNoProjectBucket(t *testing.T) {
- groups := groupContainersByProjectInternal([]containertypes.Summary{
+ groups := groupContainersByProject([]containertypes.Summary{
{ID: "1", Labels: map[string]string{"com.docker.compose.project": "alpha"}},
{ID: "2", Labels: map[string]string{}},
{ID: "3", Labels: nil},
@@ -1267,3 +1269,117 @@ func TestContainerServiceDownloadLogsPassesTTYOutputThroughInternal(t *testing.T
require.Equal(t, "container-0123456789ab-logs.log", filename)
require.Equal(t, "raw tty line\r\nno trailing newline", string(content))
}
+
+func containerScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ _ = json.NewEncoder(w).Encode(body)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregateContainersMergesEnvironments(t *testing.T) {
+ bodies := map[string]any{
+ "a": ContainerPaginatedResponse{
+ Success: true,
+ Data: []containertypes.Summary{newGroupedContainerSummary("alpha", "shop"), newGroupedContainerSummary("charlie", "web")},
+ Counts: containertypes.StatusCounts{RunningContainers: 1, StoppedContainers: 1, TotalContainers: 2},
+ Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 3},
+ ResourceSortSupported: true,
+ },
+ "b": ContainerPaginatedResponse{
+ Success: true,
+ Data: []containertypes.Summary{newGroupedContainerSummary("bravo", "shop"), newGroupedContainerSummary("delta", "")},
+ Counts: containertypes.StatusCounts{RunningContainers: 2, TotalContainers: 2},
+ Pagination: base.PaginationResponse{TotalItems: 2},
+ },
+ }
+ rowLabels := func(items []containertypes.Summary) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.Names[0])
+ }
+ return out
+ }
+ svc := &ContainerService{}
+
+ t.Run("sorts by name across environments and pages", func(t *testing.T) {
+ ctx, queries := containerScopeContext(t, "/api/environments/a,b,c/containers?start=2&limit=2&sort=name&order=asc&groupBy=project", bodies)
+ body, err := svc.AggregateContainers(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "name", Order: pagination.SortAsc}, Params: pagination.Params{Start: 2, Limit: 2}}, "")
+ require.NoError(t, err)
+
+ require.True(t, body.Success)
+ require.Equal(t, []string{"a/charlie", "b/delta"}, rowLabels(body.Data))
+ require.Equal(t, "Env a", body.Data[0].EnvironmentName)
+ require.Empty(t, body.Groups)
+ require.Equal(t, int64(4), body.Pagination.TotalItems)
+ require.Equal(t, int64(5), body.Pagination.GrandTotalItems)
+ require.Equal(t, int64(2), body.Pagination.TotalPages)
+ require.Equal(t, 2, body.Pagination.CurrentPage)
+ require.Equal(t, containertypes.StatusCounts{RunningContainers: 3, StoppedContainers: 1, TotalContainers: 4}, body.Counts)
+ require.False(t, body.ResourceSortSupported)
+ require.Equal(t, []base.EnvironmentFailure{{Environment: base.EnvironmentRef{ID: "c", Name: "c"}, Message: "environment offline"}}, body.FailedEnvironments)
+ for _, id := range []string{"a", "b", "c"} {
+ stored, ok := queries.Load(id)
+ require.True(t, ok)
+ query := stored.(url.Values)
+ require.Equal(t, "0", query.Get("start"))
+ require.Equal(t, "-1", query.Get("limit"))
+ require.Equal(t, "name", query.Get("sort"))
+ require.False(t, query.Has("groupBy"))
+ }
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, _ := containerScopeContext(t, "/api/environments/a,b,c/containers?sort=environment&order=asc", bodies)
+ body, err := svc.AggregateContainers(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortAsc}, Params: pagination.Params{Limit: 20}}, "")
+ require.NoError(t, err)
+ require.Equal(t, []string{"a/alpha", "a/charlie", "b/bravo", "b/delta"}, rowLabels(body.Data))
+ })
+
+ t.Run("project groups merge across environments and stay whole", func(t *testing.T) {
+ ctx, queries := containerScopeContext(t, "/api/environments/a,b,c/containers?start=0&limit=1&sort=name&order=asc&groupBy=project", bodies)
+ body, err := svc.AggregateContainers(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "name", Order: pagination.SortAsc}, Params: pagination.Params{Limit: 1}}, "project")
+ require.NoError(t, err)
+
+ require.Equal(t, []string{"shop"}, groupNamesOf(body.Groups))
+ require.Equal(t, []string{"a/alpha", "b/bravo"}, rowLabels(body.Groups[0].Items))
+ require.Equal(t, []string{"a/alpha", "b/bravo"}, rowLabels(body.Data))
+ require.Equal(t, int64(3), body.Pagination.TotalPages)
+ require.Equal(t, int64(4), body.Pagination.TotalItems)
+ stored, _ := queries.Load("a")
+ require.False(t, stored.(url.Values).Has("groupBy"))
+ })
+
+ t.Run("resource sort is supported only when every environment supports it", func(t *testing.T) {
+ ctx, _ := containerScopeContext(t, "/api/environments/a,b,c/containers", map[string]any{"a": bodies["a"]})
+ body, err := svc.AggregateContainers(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}}, "")
+ require.NoError(t, err)
+ require.True(t, body.ResourceSortSupported)
+ require.Len(t, body.FailedEnvironments, 2)
+ })
+
+ t.Run("all environments failing returns empty data", func(t *testing.T) {
+ ctx, _ := containerScopeContext(t, "/api/environments/a,b,c/containers", map[string]any{})
+ body, err := svc.AggregateContainers(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}}, "")
+ require.NoError(t, err)
+ require.True(t, body.Success)
+ require.Empty(t, body.Data)
+ require.Len(t, body.FailedEnvironments, 3)
+ })
+}
diff --git a/backend/internal/image/children/patch/handler.go b/backend/internal/image/children/patch/handler.go
index 861d9c552..967b00785 100644
--- a/backend/internal/image/children/patch/handler.go
+++ b/backend/internal/image/children/patch/handler.go
@@ -12,6 +12,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
// ImagePatchHandler provides Huma-based image patching endpoints.
@@ -78,6 +79,13 @@ func (h *ImagePatchHandler) PatchImage(ctx context.Context, input *PatchImageInp
// ListPatchTargets returns scanned images with fixable counts and latest patch runs.
func (h *ImagePatchHandler) ListPatchTargets(ctx context.Context, input *ListPatchTargetsInput) (*handlerutil.Page[imagepatch.PatchTarget], error) {
params := handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search)
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.imagePatchService.AggregatePatchTargets(ctx, params)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list image patch targets: " + err.Error())
+ }
+ return &handlerutil.Page[imagepatch.PatchTarget]{Body: body}, nil
+ }
targets, paginationResp, err := h.imagePatchService.ListPatchTargets(ctx, input.EnvironmentID, params)
if err != nil {
diff --git a/backend/internal/image/children/patch/module.go b/backend/internal/image/children/patch/module.go
index 9d00e230b..0dca99064 100644
--- a/backend/internal/image/children/patch/module.go
+++ b/backend/internal/image/children/patch/module.go
@@ -19,16 +19,16 @@ func RegisterRoutes(api huma.API, imagePatchService *Service, appCtx handlerutil
handlerutil.RegisterSecured(api,
handlerutil.Operation("list-image-patches", http.MethodGet, "/environments/{id}/images/patches", "List image patches", "Retrieves the paginated image patch history for the environment", "Images"),
authz.PermImagesList, h.ListImagePatches)
- handlerutil.RegisterSecured(api,
- handlerutil.Operation(
- "list-image-patch-targets",
- http.MethodGet,
- "/environments/{id}/images/patch-targets",
- "List image patch targets",
- "Retrieves scanned images with fixable vulnerability counts and their latest patch run",
- "Images",
- ),
- authz.PermVulnsRead, h.ListPatchTargets)
+ patchTargets := handlerutil.Operation(
+ "list-image-patch-targets",
+ http.MethodGet,
+ "/environments/{id}/images/patch-targets",
+ "List image patch targets",
+ "Retrieves scanned images with fixable vulnerability counts and their latest patch run",
+ "Images",
+ )
+ patchTargets.Metadata = map[string]any{authz.MetaEnvironmentAggregate: true}
+ handlerutil.RegisterSecured(api, patchTargets, authz.PermVulnsRead, h.ListPatchTargets)
handlerutil.RegisterSecured(api,
handlerutil.Operation(
"patch-image",
diff --git a/backend/internal/image/children/patch/service.go b/backend/internal/image/children/patch/service.go
index e90233796..4996d466e 100644
--- a/backend/internal/image/children/patch/service.go
+++ b/backend/internal/image/children/patch/service.go
@@ -9,6 +9,7 @@ import (
"fmt"
"log/slog"
"maps"
+ "net/url"
"os"
"path/filepath"
"slices"
@@ -18,6 +19,7 @@ import (
"github.com/containerd/platforms"
"github.com/distribution/reference"
activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/features"
"github.com/getarcaneapp/arcane/types/v2/imagepatch"
usertypes "github.com/getarcaneapp/arcane/types/v2/user"
@@ -48,6 +50,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/vuln"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/logging"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/tracing"
)
@@ -616,6 +620,42 @@ func (s *Service) PatchedRefs(ctx context.Context, envID string) (map[string]str
return set, nil
}
+func (s *Service) AggregatePatchTargets(ctx context.Context, params pagination.QueryParams) (base.Paginated[imagepatch.PatchTarget], error) {
+ collection, err := httpx.CollectEnvironments[base.Paginated[imagepatch.PatchTarget]](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return base.Paginated[imagepatch.PatchTarget]{}, fmt.Errorf("collect environment patch targets: %w", err)
+ }
+ pages := make([]pagination.EnvironmentPage[imagepatch.PatchTarget], 0, len(collection.Results))
+ for _, result := range collection.Results {
+ pages = append(pages, pagination.EnvironmentPage[imagepatch.PatchTarget]{
+ Environment: result.Environment,
+ Items: result.Value.Data,
+ TotalAvailable: cmp.Or(result.Value.Pagination.GrandTotalItems, result.Value.Pagination.TotalItems),
+ })
+ }
+ items, resp := pagination.MergeEnvironmentPages(pagination.Config[imagepatch.PatchTarget]{
+ SortBindings: []pagination.SortBinding[imagepatch.PatchTarget]{
+ {
+ Key: "scanTime",
+ Fn: func(a, b imagepatch.PatchTarget) int {
+ return cmp.Or(a.ScanTime.Compare(b.ScanTime), strings.Compare(a.ImageID, b.ImageID))
+ },
+ DescFn: func(a, b imagepatch.PatchTarget) int {
+ return cmp.Or(b.ScanTime.Compare(a.ScanTime), strings.Compare(a.ImageID, b.ImageID))
+ },
+ },
+ {Key: "imageName", Fn: func(a, b imagepatch.PatchTarget) int { return strings.Compare(a.ImageRef, b.ImageRef) }},
+ {Key: "fixableCount", Fn: func(a, b imagepatch.PatchTarget) int { return cmp.Compare(a.FixableCount, b.FixableCount) }},
+ },
+ }, pages, params)
+ return base.Paginated[imagepatch.PatchTarget]{
+ Success: true,
+ Data: items,
+ Pagination: handlerutil.PaginationResponse(resp),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
// ListPatchTargets returns scanned images with their fixable-vulnerability
// counts and latest patch run, for the security page's patching view. Images
// that are themselves patch outputs are folded into their original's row: they
diff --git a/backend/internal/image/children/patch/service_test.go b/backend/internal/image/children/patch/service_test.go
index c22a378a4..91aeb6da0 100644
--- a/backend/internal/image/children/patch/service_test.go
+++ b/backend/internal/image/children/patch/service_test.go
@@ -2,13 +2,18 @@ package patch
import (
"context"
+ "encoding/json/v2"
"fmt"
"net/http"
"net/http/httptest"
+ "net/url"
+ "strings"
+ "sync"
"sync/atomic"
"testing"
"time"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/features"
"github.com/getarcaneapp/arcane/types/v2/imagepatch"
"github.com/getarcaneapp/arcane/types/v2/user"
@@ -25,6 +30,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/settings"
"github.com/getarcaneapp/arcane/backend/v2/internal/vulnerability"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func TestResolvePatchedRef(t *testing.T) {
@@ -182,3 +188,72 @@ func TestDisabledFeatureSkipsPatchVerificationScan(t *testing.T) {
})
require.Equal(t, int32(1), inspected.Load())
}
+
+func TestAggregatePatchTargetsMergesEnvironments(t *testing.T) {
+ scanned := time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
+ bodies := map[string]base.Paginated[imagepatch.PatchTarget]{
+ "a": {Success: true, Data: []imagepatch.PatchTarget{
+ {ImageID: "sha256:a1", ImageRef: "nginx:1", FixableCount: 3, ScanTime: scanned.Add(2 * time.Hour)},
+ {ImageID: "sha256:a2", ImageRef: "redis:7", FixableCount: 1, ScanTime: scanned},
+ }, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 4}},
+ "b": {Success: true, Data: []imagepatch.PatchTarget{
+ {ImageID: "sha256:b1", ImageRef: "alpine:3", FixableCount: 5, ScanTime: scanned.Add(time.Hour)},
+ }, Pagination: base.PaginationResponse{TotalItems: 1}},
+ }
+ var queries sync.Map
+ ctx := httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, "/api/environments/a,b,c/images/patch-targets?start=0&limit=20&order=desc", http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ payload, err := json.Marshal(body)
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ _, _ = w.Write(payload)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ })
+ rowLabels := func(items []imagepatch.PatchTarget) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.ImageRef)
+ }
+ return out
+ }
+ svc := &Service{}
+ merge := func(sort string, order pagination.SortOrder, start, limit int) base.Paginated[imagepatch.PatchTarget] {
+ body, err := svc.AggregatePatchTargets(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: sort, Order: order}, Params: pagination.Params{Start: start, Limit: limit}})
+ require.NoError(t, err)
+ return body
+ }
+
+ defaults := merge("", pagination.SortDesc, 0, 20)
+ assert.True(t, defaults.Success)
+ assert.Equal(t, []string{"a/nginx:1", "b/alpine:3", "a/redis:7"}, rowLabels(defaults.Data))
+ assert.Equal(t, "Env a", defaults.Data[0].EnvironmentName)
+ assert.Equal(t, int64(3), defaults.Pagination.TotalItems)
+ assert.Equal(t, int64(5), defaults.Pagination.GrandTotalItems)
+ require.Len(t, defaults.FailedEnvironments, 1)
+ assert.Equal(t, "c", defaults.FailedEnvironments[0].Environment.ID)
+ for _, id := range []string{"a", "b"} {
+ query, ok := queries.Load(id)
+ require.True(t, ok)
+ assert.Equal(t, "0", query.(url.Values).Get("start"))
+ assert.Equal(t, "-1", query.(url.Values).Get("limit"))
+ }
+
+ assert.Equal(t, []string{"b/alpine:3", "a/nginx:1", "a/redis:7"}, rowLabels(merge("imageName", pagination.SortAsc, 0, -1).Data))
+ assert.Equal(t, []string{"b/alpine:3", "a/nginx:1"}, rowLabels(merge("fixableCount", pagination.SortDesc, 0, 2).Data))
+ assert.Equal(t, []string{"a/redis:7", "a/nginx:1", "b/alpine:3"}, rowLabels(merge("environment", pagination.SortAsc, 0, -1).Data))
+}
diff --git a/backend/internal/image/handler.go b/backend/internal/image/handler.go
index 571115ca5..693b89b51 100644
--- a/backend/internal/image/handler.go
+++ b/backend/internal/image/handler.go
@@ -150,6 +150,14 @@ func (h *ImageHandler) ListImages(ctx context.Context, input *ListImagesInput) (
params.Limit = cmp.Or(params.Limit, 20)
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.imageService.AggregateImages(ctx, params)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list images: " + err.Error())
+ }
+ return &ListImagesOutput{Body: body}, nil
+ }
+
images, paginationResp, err := h.imageService.ListImagesPaginated(ctx, params)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to list images: " + err.Error())
@@ -432,6 +440,14 @@ func (h *ImageHandler) ListImageBuilds(ctx context.Context, input *ListImageBuil
params.Filters["provider"] = input.Provider
}
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.buildService.AggregateImageBuilds(ctx, params)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list build history: " + err.Error())
+ }
+ return &handlerutil.Page[image.BuildRecord]{Body: body}, nil
+ }
+
builds, paginationResp, err := h.buildService.ListImageBuildsByEnvironmentPaginated(ctx, input.EnvironmentID, params)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to list build history: " + err.Error())
@@ -542,6 +558,14 @@ func resolvePruneImageUntilInternal(input *PruneImagesInput) string {
// GetImageUsageCounts returns counts of images by usage status.
func (h *ImageHandler) GetImageUsageCounts(ctx context.Context, input *GetImageUsageCountsInput) (*handlerutil.Out[image.UsageCounts], error) {
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.imageService.AggregateImageCounts(ctx)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to get image usage counts: " + err.Error())
+ }
+ return &handlerutil.Out[image.UsageCounts]{Body: body}, nil
+ }
+
_, counts, err := h.dockerService.GetAllImages(ctx)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to get image usage counts: " + err.Error())
diff --git a/backend/internal/image/module.go b/backend/internal/image/module.go
index 12021194f..ab96111af 100644
--- a/backend/internal/image/module.go
+++ b/backend/internal/image/module.go
@@ -107,6 +107,7 @@ func RegisterImages(
Description: "Get a paginated list of Docker images",
Tags: []string{"Images"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermImagesList, h.ListImages)
middleware.RegisterWithPermission(api, huma.Operation{
@@ -117,6 +118,7 @@ func RegisterImages(
Description: "Get counts of images in use, unused, total, and total size",
Tags: []string{"Images"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermImagesList, h.GetImageUsageCounts)
middleware.RegisterWithPermission(api, huma.Operation{
@@ -207,6 +209,7 @@ func RegisterImages(
Description: "Get a paginated list of image build history for an environment",
Tags: []string{"Images"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermImagesList, h.ListImageBuilds)
middleware.RegisterWithPermission(api, huma.Operation{
diff --git a/backend/internal/image/service.go b/backend/internal/image/service.go
index edddafcf2..ded9fa208 100644
--- a/backend/internal/image/service.go
+++ b/backend/internal/image/service.go
@@ -8,6 +8,7 @@ import (
"io"
"log/slog"
"maps"
+ "net/url"
"slices"
"strings"
"time"
@@ -15,6 +16,7 @@ import (
"github.com/distribution/reference"
"github.com/getarcaneapp/arcane/types/v2"
activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
"github.com/getarcaneapp/arcane/types/v2/features"
imagetypes "github.com/getarcaneapp/arcane/types/v2/image"
@@ -50,6 +52,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/flow"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/imageref"
)
@@ -1014,6 +1018,53 @@ func (s *ImageService) ListImagesPaginated(ctx context.Context, params paginatio
return result.Items, paginationResp, nil
}
+func (s *ImageService) AggregateImages(ctx context.Context, params pagination.QueryParams) (imagetypes.ListResponse, error) {
+ collection, err := httpx.CollectEnvironments[imagetypes.ListResponse](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return imagetypes.ListResponse{}, err
+ }
+
+ pages := make([]pagination.EnvironmentPage[imagetypes.Summary], 0, len(collection.Results))
+ for _, result := range collection.Results {
+ pages = append(pages, pagination.EnvironmentPage[imagetypes.Summary]{
+ Environment: result.Environment,
+ Items: result.Value.Data,
+ TotalAvailable: cmp.Or(result.Value.Pagination.GrandTotalItems, result.Value.Pagination.TotalItems),
+ })
+ }
+ data, resp := pagination.MergeEnvironmentPages(s.getImagePaginationConfig(), pages, params)
+
+ return imagetypes.ListResponse{
+ Success: true,
+ Data: data,
+ Pagination: handlerutil.PaginationResponse(resp),
+ MaxImageUploadSize: func() int {
+ if len(collection.Results) == 0 {
+ return 0
+ }
+ return collection.Results[0].Value.MaxImageUploadSize
+ }(),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
+func (s *ImageService) AggregateImageCounts(ctx context.Context) (base.ApiResponse[imagetypes.UsageCounts], error) {
+ collection, err := httpx.CollectEnvironments[base.ApiResponse[imagetypes.UsageCounts]](ctx, nil)
+ if err != nil {
+ return base.ApiResponse[imagetypes.UsageCounts]{}, err
+ }
+
+ counts := imagetypes.UsageCounts{}
+ for _, result := range collection.Results {
+ counts.Inuse += result.Value.Data.Inuse
+ counts.Unused += result.Value.Data.Unused
+ counts.Total += result.Value.Data.Total
+ counts.TotalSize += result.Value.Data.TotalSize
+ }
+
+ return base.ApiResponse[imagetypes.UsageCounts]{Success: true, Data: counts, FailedEnvironments: collection.Failures}, nil
+}
+
func buildImageRefUpdateLookupsInternal(imageRefs []string) []imageRefUpdateLookup {
lookups := make([]imageRefUpdateLookup, 0, len(imageRefs))
seen := make(map[string]struct{}, len(imageRefs))
diff --git a/backend/internal/image/service_test.go b/backend/internal/image/service_test.go
index d2423a797..40ea3a0a3 100644
--- a/backend/internal/image/service_test.go
+++ b/backend/internal/image/service_test.go
@@ -1,6 +1,7 @@
package image
import (
+ "context"
"encoding/json"
"errors"
"io"
@@ -9,9 +10,11 @@ import (
"net/url"
"regexp"
"strings"
+ "sync"
"testing"
"time"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
imagetypes "github.com/getarcaneapp/arcane/types/v2/image"
"github.com/getarcaneapp/arcane/types/v2/user"
@@ -36,6 +39,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/settings"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/flow/flowtest"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/imageref"
)
@@ -750,3 +754,107 @@ func TestPatchWorkflowDefinitions(t *testing.T) {
harness.Start(t)
flowtest.AssertDefinitions(t, harness)
}
+
+func imageScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ _ = json.NewEncoder(w).Encode(body)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregateImagesMergesEnvironments(t *testing.T) {
+ bodies := map[string]any{
+ "a": imagetypes.ListResponse{Success: true, Data: []imagetypes.Summary{
+ {ID: "a1", Repo: "nginx", Tag: "latest"},
+ {ID: "a2", Repo: "redis", Tag: "7"},
+ }, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 4}, MaxImageUploadSize: 500},
+ "b": imagetypes.ListResponse{Success: true, Data: []imagetypes.Summary{
+ {ID: "b1", Repo: "postgres", Tag: "16"},
+ }, Pagination: base.PaginationResponse{TotalItems: 1}, MaxImageUploadSize: 100},
+ }
+ rowLabels := func(items []imagetypes.Summary) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.Repo)
+ }
+ return out
+ }
+ svc := &ImageService{}
+
+ t.Run("sorts by repo across environments and pages", func(t *testing.T) {
+ ctx, queries := imageScopeContext(t, "/api/environments/a,b,c/images?start=0&limit=2&sort=repo&order=asc", bodies)
+ body, err := svc.AggregateImages(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "repo", Order: pagination.SortAsc}, Params: pagination.Params{Limit: 2}})
+ require.NoError(t, err)
+
+ assert.True(t, body.Success)
+ assert.Equal(t, []string{"a/nginx", "b/postgres"}, rowLabels(body.Data))
+ assert.Equal(t, "Env b", body.Data[1].EnvironmentName)
+ assert.Equal(t, int64(3), body.Pagination.TotalItems)
+ assert.Equal(t, int64(5), body.Pagination.GrandTotalItems)
+ assert.Equal(t, int64(2), body.Pagination.TotalPages)
+ assert.Equal(t, 500, body.MaxImageUploadSize)
+ require.Len(t, body.FailedEnvironments, 1)
+ assert.Equal(t, "c", body.FailedEnvironments[0].Environment.ID)
+ assert.Equal(t, "environment offline", body.FailedEnvironments[0].Message)
+ for _, id := range []string{"a", "b"} {
+ query, ok := queries.Load(id)
+ require.True(t, ok)
+ assert.Equal(t, "0", query.(url.Values).Get("start"))
+ assert.Equal(t, "-1", query.(url.Values).Get("limit"))
+ }
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, _ := imageScopeContext(t, "/api/environments/a,b,c/images?sort=environment&order=desc", bodies)
+ body, err := svc.AggregateImages(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ assert.Equal(t, []string{"b/postgres", "a/nginx", "a/redis"}, rowLabels(body.Data))
+ })
+
+ t.Run("upload limit comes from the first answering environment", func(t *testing.T) {
+ ctx, _ := imageScopeContext(t, "/api/environments/a,b,c/images", map[string]any{"b": bodies["b"]})
+ body, err := svc.AggregateImages(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}})
+ require.NoError(t, err)
+ assert.Equal(t, 100, body.MaxImageUploadSize)
+ assert.Len(t, body.FailedEnvironments, 2)
+ })
+
+ t.Run("all environments failing returns empty data", func(t *testing.T) {
+ ctx, _ := imageScopeContext(t, "/api/environments/a,b,c/images", map[string]any{})
+ body, err := svc.AggregateImages(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}})
+ require.NoError(t, err)
+ assert.Empty(t, body.Data)
+ assert.Len(t, body.FailedEnvironments, 3)
+ })
+}
+
+func TestAggregateImageCountsSumsEnvironments(t *testing.T) {
+ ctx, _ := imageScopeContext(t, "/api/environments/a,b,c/images/counts", map[string]any{
+ "a": base.ApiResponse[imagetypes.UsageCounts]{Success: true, Data: imagetypes.UsageCounts{Inuse: 1, Unused: 2, Total: 3, TotalSize: 100}},
+ "b": base.ApiResponse[imagetypes.UsageCounts]{Success: true, Data: imagetypes.UsageCounts{Inuse: 2, Total: 2, TotalSize: 50}},
+ })
+
+ body, err := (&ImageService{}).AggregateImageCounts(ctx)
+ require.NoError(t, err)
+
+ assert.True(t, body.Success)
+ assert.Equal(t, imagetypes.UsageCounts{Inuse: 3, Unused: 2, Total: 5, TotalSize: 150}, body.Data)
+ require.Len(t, body.FailedEnvironments, 1)
+ assert.Equal(t, "c", body.FailedEnvironments[0].Environment.ID)
+}
diff --git a/backend/internal/middleware/aggregate.go b/backend/internal/middleware/aggregate.go
new file mode 100644
index 000000000..77f78cf5e
--- /dev/null
+++ b/backend/internal/middleware/aggregate.go
@@ -0,0 +1,44 @@
+package middleware
+
+import (
+ "context"
+ "net/http"
+ "slices"
+ "strconv"
+ "strings"
+
+ "github.com/labstack/echo/v5"
+ "go.getarcane.app/kit/pkg"
+
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/authz"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
+)
+
+func NewEnvironmentScopeMiddleware(resolve httpx.EnvironmentResolver, matcher *authz.PermissionMatcher, limit func(context.Context) int) echo.MiddlewareFunc {
+ return func(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c *echo.Context) error {
+ req := c.Request()
+ rest, ok := strings.CutPrefix(req.URL.Path, apiEnvironmentsPrefix)
+ segment, suffix, hasSuffix := strings.Cut(rest, "/")
+ if !ok || !strings.Contains(segment, ",") {
+ return next(c)
+ }
+
+ ids := strings.Split(segment, ",")
+ unique := kit.Unique(ids)
+ maxEnvironments := limit(req.Context())
+ switch {
+ case slices.Contains(ids, ""):
+ return writeProxyError(c, http.StatusBadRequest, "Environment scope contains an empty environment ID")
+ case len(unique) > maxEnvironments:
+ return writeProxyError(c, http.StatusBadRequest, "Environment scope is limited to "+strconv.Itoa(maxEnvironments)+" environments")
+ case req.Method != http.MethodGet || matcher == nil || !matcher.SupportsEnvironmentAggregation(http.MethodGet, kit.Ternary(hasSuffix, "/"+suffix, "")):
+ return writeProxyError(c, http.StatusBadRequest, "This operation does not support multiple environments")
+ }
+
+ scope := &httpx.EnvironmentScope{IDs: unique, Request: req.Clone(req.Context()), Handler: c.Echo(), Resolve: resolve}
+ c.SetRequest(req.WithContext(httpx.WithEnvironmentScope(req.Context(), scope)))
+ return next(c)
+ }
+ }
+}
diff --git a/backend/internal/middleware/aggregate_test.go b/backend/internal/middleware/aggregate_test.go
new file mode 100644
index 000000000..d5d21eaaa
--- /dev/null
+++ b/backend/internal/middleware/aggregate_test.go
@@ -0,0 +1,120 @@
+package middleware
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/danielgtaylor/huma/v2"
+ "github.com/danielgtaylor/huma/v2/adapters/humaecho"
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ "github.com/labstack/echo/v5"
+ "github.com/stretchr/testify/require"
+
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/authz"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
+)
+
+type scopeTestInput struct {
+ ID string `path:"id"`
+}
+
+func newScopeTestRouter(t *testing.T, limit int) (*echo.Echo, *[]*httpx.EnvironmentScope) {
+ t.Helper()
+ router := echo.New()
+ group := router.Group("/api")
+ matcher := authz.NewPermissionMatcher()
+ group.Use(NewEnvironmentScopeMiddleware(func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: id}, nil
+ }, matcher, func(context.Context) int { return limit }))
+ api := humaecho.NewWithGroup(router, group, huma.DefaultConfig("test", "1.0.0"))
+
+ handled := []*httpx.EnvironmentScope{}
+ handler := func(ctx context.Context, _ *scopeTestInput) (*struct{}, error) {
+ scope, _ := httpx.EnvironmentScopeFromContext(ctx)
+ handled = append(handled, scope)
+ return &struct{}{}, nil
+ }
+ aggregate := map[string]any{authz.MetaEnvironmentAggregate: true}
+ huma.Register(api, huma.Operation{OperationID: "list-containers", Method: http.MethodGet, Path: "/environments/{id}/containers", Metadata: aggregate}, handler)
+ huma.Register(api, huma.Operation{OperationID: "create-container", Method: http.MethodPost, Path: "/environments/{id}/containers", Metadata: aggregate}, handler)
+ huma.Register(api, huma.Operation{OperationID: "list-images", Method: http.MethodGet, Path: "/environments/{id}/images"}, handler)
+ matcher.CollectFromHumaAPI(api)
+ return router, &handled
+}
+
+func serveScopeTestRequest(router *echo.Echo, method, target string) *httptest.ResponseRecorder {
+ recorder := httptest.NewRecorder()
+ router.ServeHTTP(recorder, httptest.NewRequest(method, target, http.NoBody))
+ return recorder
+}
+
+func TestEnvironmentScopeMiddlewarePassesSingleEnvironmentThrough(t *testing.T) {
+ router, handled := newScopeTestRouter(t, 64)
+
+ recorder := serveScopeTestRequest(router, http.MethodGet, "/api/environments/env-1/containers")
+
+ require.Less(t, recorder.Code, http.StatusBadRequest)
+ require.Equal(t, []*httpx.EnvironmentScope{nil}, *handled)
+}
+
+func TestEnvironmentScopeMiddlewareAttachesDeduplicatedScope(t *testing.T) {
+ router, handled := newScopeTestRouter(t, 64)
+
+ recorder := serveScopeTestRequest(router, http.MethodGet, "/api/environments/0,env-a,0,env-b/containers?search=web")
+
+ require.Less(t, recorder.Code, http.StatusBadRequest)
+ require.Len(t, *handled, 1)
+ scope := (*handled)[0]
+ require.NotNil(t, scope)
+ require.Equal(t, []string{"0", "env-a", "env-b"}, scope.IDs)
+ require.Same(t, router, scope.Handler)
+ require.NotNil(t, scope.Resolve)
+ require.Equal(t, "/api/environments/0,env-a,0,env-b/containers", scope.Request.URL.Path)
+ _, captured := httpx.EnvironmentScopeFromContext(scope.Request.Context())
+ require.False(t, captured, "the captured request must not carry the scope")
+}
+
+func TestEnvironmentScopeMiddlewareRejectsInvalidScopes(t *testing.T) {
+ tooMany := make([]string, 65)
+ for i := range tooMany {
+ tooMany[i] = fmt.Sprintf("env-%d", i)
+ }
+ tests := []struct {
+ name string
+ method string
+ target string
+ }{
+ {name: "unsupported route", method: http.MethodGet, target: "/api/environments/a,b/images"},
+ {name: "non-GET method", method: http.MethodPost, target: "/api/environments/a,b/containers"},
+ {name: "empty component", method: http.MethodGet, target: "/api/environments/a,,b/containers"},
+ {name: "too many environments", method: http.MethodGet, target: "/api/environments/" + strings.Join(tooMany, ",") + "/containers"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ router, handled := newScopeTestRouter(t, 64)
+
+ recorder := serveScopeTestRequest(router, tt.method, tt.target)
+
+ require.Equal(t, http.StatusBadRequest, recorder.Code)
+ require.Contains(t, recorder.Body.String(), `"error"`)
+ require.Empty(t, *handled)
+ })
+ }
+}
+
+func TestEnvironmentScopeMiddlewareEnforcesConfiguredLimit(t *testing.T) {
+ router, handled := newScopeTestRouter(t, 3)
+
+ rejected := serveScopeTestRequest(router, http.MethodGet, "/api/environments/a,b,c,d/containers")
+ accepted := serveScopeTestRequest(router, http.MethodGet, "/api/environments/a,b,c,a/containers")
+
+ require.Equal(t, http.StatusBadRequest, rejected.Code)
+ require.Contains(t, rejected.Body.String(), "limited to 3 environments")
+ require.Less(t, accepted.Code, http.StatusBadRequest)
+ require.Len(t, *handled, 1)
+ require.Equal(t, []string{"a", "b", "c"}, (*handled)[0].IDs)
+}
diff --git a/backend/internal/middleware/environment.go b/backend/internal/middleware/environment.go
index 2be058913..e40296d4a 100644
--- a/backend/internal/middleware/environment.go
+++ b/backend/internal/middleware/environment.go
@@ -38,7 +38,8 @@ const (
environmentsPathMarker = "/environments/"
// proxyTimeout is generous because proxied operations such as image pulls can stream for minutes.
- proxyTimeout = 30 * time.Minute
+ proxyTimeout = 30 * time.Minute
+ proxyDialTimeout = 5 * time.Second
maxProxiedWorkspaceManifestBytes = 1024 * 1024
maxProxiedGitOpsSyncBodyBytes = 1024 * 1024
@@ -94,7 +95,7 @@ func NewEnvProxyMiddlewareWithParamAndRegistry(
}
// Traced so direct agents continue the manager's trace.
- httpClient := httpx.NewHTTPClient(httpxtypes.ClientOptions{Timeout: proxyTimeout, TLSHandshakeTimeout: 10 * time.Second})
+ httpClient := httpx.NewHTTPClient(httpxtypes.ClientOptions{Timeout: proxyTimeout, TLSHandshakeTimeout: 10 * time.Second, DialTimeout: proxyDialTimeout})
httpClient.Transport = otelhttp.NewTransport(httpClient.Transport)
m := &EnvironmentMiddleware{
@@ -118,6 +119,9 @@ func NewEnvProxyMiddlewareWithParamAndRegistry(
func (m *EnvironmentMiddleware) Handle(c *echo.Context, next echo.HandlerFunc) error {
req := c.Request()
ctx := req.Context()
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ return next(c)
+ }
_, rest, hasMarker := strings.Cut(req.URL.Path, environmentsPathMarker)
pathEnvID, _, _ := strings.Cut(rest, "/")
diff --git a/backend/internal/middleware/environment_test.go b/backend/internal/middleware/environment_test.go
index 64f511632..08985490c 100644
--- a/backend/internal/middleware/environment_test.go
+++ b/backend/internal/middleware/environment_test.go
@@ -18,6 +18,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/authz"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func newTestEnvironmentMiddleware() *EnvironmentMiddleware {
@@ -627,3 +628,27 @@ func TestProxyPermissionDeniedResourceSortRequiresRead(t *testing.T) {
require.False(t, m.proxyPermissionDenied(c, withRead, proxyTestEnvID, "/containers"),
"expected resource sort to be allowed with containers:list and containers:read")
}
+
+func TestEnvironmentMiddleware_ServesMultiEnvironmentScopeLocally(t *testing.T) {
+ router := echo.New()
+ router.Pre(func(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c *echo.Context) error {
+ req := c.Request()
+ c.SetRequest(req.WithContext(httpx.WithEnvironmentScope(req.Context(), &httpx.EnvironmentScope{IDs: []string{"env-edge", "0"}})))
+ return next(c)
+ }
+ })
+ api := attachMiddleware(router, newTestEnvironmentMiddleware())
+
+ localHandlerHit := false
+ api.GET("/environments/:id/containers", func(c *echo.Context) error {
+ localHandlerHit = true
+ return c.JSON(http.StatusOK, map[string]any{"success": true})
+ })
+
+ recorder := httptest.NewRecorder()
+ router.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/environments/env-edge/containers", http.NoBody))
+
+ assert.Equal(t, http.StatusOK, recorder.Code)
+ assert.True(t, localHandlerHit)
+}
diff --git a/backend/internal/middleware/role.go b/backend/internal/middleware/role.go
index 3bea8a7d1..581455b50 100644
--- a/backend/internal/middleware/role.go
+++ b/backend/internal/middleware/role.go
@@ -4,12 +4,14 @@ import (
"context"
"log/slog"
"net/http"
+ "slices"
"github.com/danielgtaylor/huma/v2"
"github.com/labstack/echo/v5"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/pkg/authz"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
// RegisterWithPermission registers a Huma operation that requires perm. It
@@ -69,7 +71,11 @@ func RequirePermission(api huma.API, perm string) huma.Middlewares {
if authz.IsEnvScoped(perm) {
envID = authz.EnvIDFromPath(ctx.URL().Path)
}
- if !ps.Allows(perm, envID) {
+ allowed := ps.Allows(perm, envID)
+ if scope, scoped := httpx.EnvironmentScopeFromContext(ctx.Context()); scoped && authz.IsEnvScoped(perm) {
+ allowed = slices.ContainsFunc(scope.IDs, func(id string) bool { return ps.Allows(perm, id) })
+ }
+ if !allowed {
if err := huma.WriteErr(api, ctx, http.StatusForbidden, "permission denied: "+perm); err != nil {
slog.WarnContext(ctx.Context(), "failed to write 403 response", "error", err)
}
diff --git a/backend/internal/middleware/role_test.go b/backend/internal/middleware/role_test.go
index 46e06b5f4..3356761a1 100644
--- a/backend/internal/middleware/role_test.go
+++ b/backend/internal/middleware/role_test.go
@@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/http/httptest"
+ "strings"
"testing"
"github.com/danielgtaylor/huma/v2"
@@ -12,6 +13,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/getarcaneapp/arcane/backend/v2/pkg/authz"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func TestRequirePermission_RejectsCallerMissingPermission(t *testing.T) {
@@ -225,3 +227,46 @@ func TestRequireSudoRejectsHumanAdminAndAllowsAgent(t *testing.T) {
})
}
}
+
+func TestRequirePermissionAllowsScopeWhenAnyEnvironmentAllows(t *testing.T) {
+ tests := []struct {
+ name string
+ ids []string
+ allowed bool
+ }{
+ {name: "one permitted environment", ids: []string{"env-2", "env-1"}, allowed: true},
+ {name: "no permitted environment", ids: []string{"env-2", "env-3"}, allowed: false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ router := echo.New()
+ api := humaecho.NewWithGroup(router, router.Group("/api"), huma.DefaultConfig("test", "1.0.0"))
+ api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
+ ps := authz.NewPermissionSet()
+ ps.AddEnv("env-1", authz.PermContainersList)
+ scoped := httpx.WithEnvironmentScope(context.WithValue(ctx.Context(), ContextKeyUserPermissions, ps), &httpx.EnvironmentScope{IDs: tt.ids})
+ next(huma.WithContext(ctx, scoped))
+ })
+ huma.Register(api, huma.Operation{
+ OperationID: "scoped-list",
+ Method: http.MethodGet,
+ Path: "/environments/{id}/containers",
+ Middlewares: RequirePermission(api, authz.PermContainersList),
+ }, func(_ context.Context, _ *struct {
+ ID string `path:"id"`
+ },
+ ) (*struct{}, error) {
+ return &struct{}{}, nil
+ })
+
+ rec := httptest.NewRecorder()
+ router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/environments/"+strings.Join(tt.ids, ",")+"/containers", http.NoBody))
+
+ if tt.allowed {
+ require.Less(t, rec.Code, http.StatusMultipleChoices)
+ return
+ }
+ require.Equal(t, http.StatusForbidden, rec.Code)
+ })
+ }
+}
diff --git a/backend/internal/middleware/telemetry.go b/backend/internal/middleware/telemetry.go
index efd9e05ca..b511e464d 100644
--- a/backend/internal/middleware/telemetry.go
+++ b/backend/internal/middleware/telemetry.go
@@ -55,6 +55,10 @@ func SpanEnvironment(environmentName func(ctx context.Context, id string) string
if id == "" || !strings.HasPrefix(c.Path(), "/api/environments/:id") || !span.IsRecording() {
return next(c)
}
+ if strings.Contains(id, ",") {
+ span.SetAttributes(attribute.StringSlice("arcane.environment.ids", strings.Split(id, ",")))
+ return next(c)
+ }
span.SetAttributes(attribute.String("arcane.environment.id", id))
err := next(c)
if resp, unwrapErr := echo.UnwrapResponse(c.Response()); err == nil && unwrapErr == nil && resp.Status < http.StatusBadRequest {
diff --git a/backend/internal/network/handler.go b/backend/internal/network/handler.go
index 6954d5eae..bae03e85e 100644
--- a/backend/internal/network/handler.go
+++ b/backend/internal/network/handler.go
@@ -24,6 +24,7 @@ import (
activitylib "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/activity"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
type NetworkHandler struct {
@@ -94,6 +95,14 @@ func (h *NetworkHandler) ListNetworks(ctx context.Context, input *ListNetworksIn
params.Filters["inUse"] = input.InUse
}
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.networkService.AggregateNetworks(ctx, params)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list networks: " + err.Error())
+ }
+ return &ListNetworksOutput{Body: body}, nil
+ }
+
networks, paginationResp, counts, err := h.networkService.ListNetworksPaginated(ctx, params)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to list networks: " + err.Error())
diff --git a/backend/internal/network/module.go b/backend/internal/network/module.go
index 058a2c9ef..e997b1571 100644
--- a/backend/internal/network/module.go
+++ b/backend/internal/network/module.go
@@ -28,6 +28,7 @@ func RegisterNetworks(api huma.API, networkSvc *NetworkService, dockerSvc *docke
Summary: "List networks",
Tags: []string{"Networks"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermNetworksList, h.ListNetworks)
middleware.RegisterWithPermission(api, huma.Operation{
diff --git a/backend/internal/network/service.go b/backend/internal/network/service.go
index cdb94bba6..3b5a5d8c0 100644
--- a/backend/internal/network/service.go
+++ b/backend/internal/network/service.go
@@ -6,10 +6,12 @@ import (
"fmt"
"log/slog"
"net/netip"
+ "net/url"
"slices"
"sort"
"strings"
+ "github.com/getarcaneapp/arcane/types/v2/base"
networktypes "github.com/getarcaneapp/arcane/types/v2/network"
usertypes "github.com/getarcaneapp/arcane/types/v2/user"
"github.com/moby/moby/api/types/container"
@@ -27,6 +29,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/event"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
type NetworkService struct {
@@ -439,6 +443,36 @@ func (s *NetworkService) convertToNetworkSummaries(rawNets []network.Summary, in
return items, nil
}
+func (s *NetworkService) AggregateNetworks(ctx context.Context, params pagination.QueryParams) (base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts], error) {
+ collection, err := httpx.CollectEnvironments[base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{}, err
+ }
+
+ pages := make([]pagination.EnvironmentPage[networktypes.Summary], 0, len(collection.Results))
+ counts := networktypes.UsageCounts{}
+ for _, result := range collection.Results {
+ page := result.Value
+ pages = append(pages, pagination.EnvironmentPage[networktypes.Summary]{
+ Environment: result.Environment,
+ Items: page.Data,
+ TotalAvailable: cmp.Or(page.Pagination.GrandTotalItems, page.Pagination.TotalItems),
+ })
+ counts.Inuse += page.Counts.Inuse
+ counts.Unused += page.Counts.Unused
+ counts.Total += page.Counts.Total
+ }
+ data, resp := pagination.MergeEnvironmentPages(s.buildNetworkPaginationConfig(), pages, params)
+
+ return base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{
+ Success: true,
+ Data: data,
+ Counts: counts,
+ Pagination: handlerutil.PaginationResponse(resp),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
func (s *NetworkService) buildNetworkPaginationConfig() pagination.Config[networktypes.Summary] {
return pagination.Config[networktypes.Summary]{
SearchAccessors: []pagination.SearchAccessor[networktypes.Summary]{
diff --git a/backend/internal/network/service_test.go b/backend/internal/network/service_test.go
new file mode 100644
index 000000000..011b24c28
--- /dev/null
+++ b/backend/internal/network/service_test.go
@@ -0,0 +1,100 @@
+package network
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "sync"
+ "testing"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ networktypes "github.com/getarcaneapp/arcane/types/v2/network"
+ "github.com/stretchr/testify/require"
+
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
+)
+
+func networkScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ _ = json.NewEncoder(w).Encode(body)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregateNetworksMergesEnvironments(t *testing.T) {
+ bodies := map[string]any{
+ "a": base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{Success: true, Data: []networktypes.Summary{
+ {ID: "a1", Name: "bridge", InUse: true},
+ {ID: "a2", Name: "frontend"},
+ }, Counts: networktypes.UsageCounts{Inuse: 1, Unused: 1, Total: 2}, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 3}},
+ "b": base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{Success: true, Data: []networktypes.Summary{
+ {ID: "b1", Name: "backend", InUse: true},
+ {ID: "b2", Name: "bridge", InUse: true},
+ }, Counts: networktypes.UsageCounts{Inuse: 2, Total: 2}, Pagination: base.PaginationResponse{TotalItems: 2}},
+ }
+ rowLabels := func(items []networktypes.Summary) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.Name)
+ }
+ return out
+ }
+ svc := &NetworkService{}
+
+ t.Run("sorts by name across environments and pages", func(t *testing.T) {
+ ctx, queries := networkScopeContext(t, "/api/environments/a,b,c/networks?start=2&limit=2&sort=name&order=asc", bodies)
+ body, err := svc.AggregateNetworks(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "name", Order: pagination.SortAsc}, Params: pagination.Params{Start: 2, Limit: 2}})
+ require.NoError(t, err)
+
+ require.True(t, body.Success)
+ require.Equal(t, []string{"b/bridge", "a/frontend"}, rowLabels(body.Data))
+ require.Equal(t, "Env b", body.Data[0].EnvironmentName)
+ require.Equal(t, int64(4), body.Pagination.TotalItems)
+ require.Equal(t, int64(5), body.Pagination.GrandTotalItems)
+ require.Equal(t, int64(2), body.Pagination.TotalPages)
+ require.Equal(t, 2, body.Pagination.CurrentPage)
+ require.Equal(t, networktypes.UsageCounts{Inuse: 3, Unused: 1, Total: 4}, body.Counts)
+ require.Equal(t, []base.EnvironmentFailure{{Environment: base.EnvironmentRef{ID: "c", Name: "c"}, Message: "environment offline"}}, body.FailedEnvironments)
+ for _, id := range []string{"a", "b"} {
+ stored, ok := queries.Load(id)
+ require.True(t, ok)
+ require.Equal(t, "0", stored.(url.Values).Get("start"))
+ require.Equal(t, "-1", stored.(url.Values).Get("limit"))
+ }
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, _ := networkScopeContext(t, "/api/environments/a,b,c/networks?sort=environment&order=asc", bodies)
+ body, err := svc.AggregateNetworks(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortAsc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ require.Equal(t, []string{"a/bridge", "a/frontend", "b/backend", "b/bridge"}, rowLabels(body.Data))
+ })
+
+ t.Run("all environments failing returns empty data", func(t *testing.T) {
+ ctx, _ := networkScopeContext(t, "/api/environments/a,b,c/networks", map[string]any{})
+ body, err := svc.AggregateNetworks(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}})
+ require.NoError(t, err)
+ require.Empty(t, body.Data)
+ require.Len(t, body.FailedEnvironments, 3)
+ })
+}
diff --git a/backend/internal/port/handler.go b/backend/internal/port/handler.go
index 8c262e12c..400e868a4 100644
--- a/backend/internal/port/handler.go
+++ b/backend/internal/port/handler.go
@@ -8,6 +8,7 @@ import (
"github.com/getarcaneapp/arcane/types/v2/port"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
type PortHandler struct {
@@ -26,6 +27,14 @@ type ListPortsInput struct {
func (h *PortHandler) ListPorts(ctx context.Context, input *ListPortsInput) (*handlerutil.Page[port.PortMapping], error) {
params := handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search)
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.portService.AggregatePorts(ctx, params)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("failed to list ports")
+ }
+ return &handlerutil.Page[port.PortMapping]{Body: body}, nil
+ }
+
items, paginationResp, err := h.portService.ListPortsPaginated(ctx, params)
if err != nil {
return nil, huma.Error500InternalServerError("failed to list ports")
diff --git a/backend/internal/port/module.go b/backend/internal/port/module.go
index 2eb7c9eac..dac8e192e 100644
--- a/backend/internal/port/module.go
+++ b/backend/internal/port/module.go
@@ -20,5 +20,6 @@ func RegisterPorts(api huma.API, portSvc *PortService) {
Summary: "List port mappings",
Tags: []string{"Ports"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermContainersList, h.ListPorts)
}
diff --git a/backend/internal/port/service.go b/backend/internal/port/service.go
index f70119ae2..a3400a3fe 100644
--- a/backend/internal/port/service.go
+++ b/backend/internal/port/service.go
@@ -3,9 +3,11 @@ package port
import (
"context"
"fmt"
+ "net/url"
"strconv"
"strings"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/container"
porttypes "github.com/getarcaneapp/arcane/types/v2/port"
"go.getarcane.app/docker"
@@ -13,6 +15,8 @@ import (
dockerInternal "github.com/getarcaneapp/arcane/backend/v2/internal/docker"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
type PortService struct {
@@ -51,6 +55,31 @@ func (s *PortService) ListPortsPaginated(ctx context.Context, params pagination.
return result.Items, pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params), nil
}
+func (s *PortService) AggregatePorts(ctx context.Context, params pagination.QueryParams) (base.Paginated[porttypes.PortMapping], error) {
+ collection, err := httpx.CollectEnvironments[base.Paginated[porttypes.PortMapping]](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return base.Paginated[porttypes.PortMapping]{}, err
+ }
+
+ pages := make([]pagination.EnvironmentPage[porttypes.PortMapping], 0, len(collection.Results))
+ for _, result := range collection.Results {
+ page := result.Value.Pagination
+ pages = append(pages, pagination.EnvironmentPage[porttypes.PortMapping]{
+ Environment: result.Environment,
+ Items: result.Value.Data,
+ TotalAvailable: kit.Ternary(page.GrandTotalItems != 0, page.GrandTotalItems, page.TotalItems),
+ })
+ }
+ data, resp := pagination.MergeEnvironmentPages(s.buildPortPaginationConfig(), pages, params)
+
+ return base.Paginated[porttypes.PortMapping]{
+ Success: true,
+ Data: data,
+ Pagination: handlerutil.PaginationResponse(resp),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
func (s *PortService) buildPortPaginationConfig() pagination.Config[porttypes.PortMapping] {
return pagination.Config[porttypes.PortMapping]{
SearchAccessors: []pagination.SearchAccessor[porttypes.PortMapping]{
diff --git a/backend/internal/port/service_test.go b/backend/internal/port/service_test.go
index cfe1913c0..c0d9c7d29 100644
--- a/backend/internal/port/service_test.go
+++ b/backend/internal/port/service_test.go
@@ -1,13 +1,18 @@
package port
import (
+ "context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/netip"
+ "net/url"
"strings"
+ "sync"
"testing"
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ porttypes "github.com/getarcaneapp/arcane/types/v2/port"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/client"
"github.com/stretchr/testify/assert"
@@ -15,6 +20,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/docker"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func newPortServiceTestDockerService(t *testing.T, containers []container.Summary) *docker.DockerClientService {
@@ -243,3 +249,83 @@ func newTestDockerClientInternal(t *testing.T, server *httptest.Server) *client.
return cli
}
+
+func portScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ _ = json.NewEncoder(w).Encode(body)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregatePortsMergesEnvironments(t *testing.T) {
+ bodies := map[string]any{
+ "a": base.Paginated[porttypes.PortMapping]{Success: true, Data: []porttypes.PortMapping{
+ {ID: "a1", ContainerName: "web", HostPort: 8080, ContainerPort: 80, Protocol: "tcp", IsPublished: true},
+ {ID: "a2", ContainerName: "db", HostPort: 5432, ContainerPort: 5432, Protocol: "tcp", IsPublished: true},
+ }, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 3}},
+ "b": base.Paginated[porttypes.PortMapping]{Success: true, Data: []porttypes.PortMapping{
+ {ID: "b1", ContainerName: "api", HostPort: 3000, ContainerPort: 3000, Protocol: "tcp", IsPublished: true},
+ }, Pagination: base.PaginationResponse{TotalItems: 1}},
+ }
+ rowLabels := func(items []porttypes.PortMapping) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.ContainerName)
+ }
+ return out
+ }
+ svc := &PortService{}
+
+ t.Run("sorts by host port across environments and pages", func(t *testing.T) {
+ ctx, queries := portScopeContext(t, "/api/environments/a,b,c/ports?start=0&limit=2&sort=hostPort&order=asc", bodies)
+ body, err := svc.AggregatePorts(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "hostPort", Order: pagination.SortAsc}, Params: pagination.Params{Limit: 2}})
+ require.NoError(t, err)
+
+ assert.True(t, body.Success)
+ assert.Equal(t, []string{"b/api", "a/db"}, rowLabels(body.Data))
+ assert.Equal(t, "Env b", body.Data[0].EnvironmentName)
+ assert.Equal(t, int64(3), body.Pagination.TotalItems)
+ assert.Equal(t, int64(4), body.Pagination.GrandTotalItems)
+ assert.Equal(t, int64(2), body.Pagination.TotalPages)
+ require.Len(t, body.FailedEnvironments, 1)
+ assert.Equal(t, "c", body.FailedEnvironments[0].Environment.ID)
+ assert.Equal(t, "environment offline", body.FailedEnvironments[0].Message)
+ for _, id := range []string{"a", "b"} {
+ query, ok := queries.Load(id)
+ require.True(t, ok)
+ assert.Equal(t, "0", query.(url.Values).Get("start"))
+ assert.Equal(t, "-1", query.(url.Values).Get("limit"))
+ }
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, _ := portScopeContext(t, "/api/environments/a,b,c/ports?sort=environment&order=asc", bodies)
+ body, err := svc.AggregatePorts(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortAsc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ assert.Equal(t, []string{"a/db", "a/web", "b/api"}, rowLabels(body.Data))
+ })
+
+ t.Run("all environments failing returns empty data", func(t *testing.T) {
+ ctx, _ := portScopeContext(t, "/api/environments/a,b,c/ports", map[string]any{})
+ body, err := svc.AggregatePorts(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}})
+ require.NoError(t, err)
+ assert.Empty(t, body.Data)
+ assert.Len(t, body.FailedEnvironments, 3)
+ })
+}
diff --git a/backend/internal/project/children/listing/service.go b/backend/internal/project/children/listing/service.go
index 833c8aeac..2b06231b3 100644
--- a/backend/internal/project/children/listing/service.go
+++ b/backend/internal/project/children/listing/service.go
@@ -490,15 +490,8 @@ func GroupComposeContainersByProject(containers []container.Summary) map[string]
return containersByProject
}
-// Page searches, orders and paginates rows by their derived fields and returns
-// the page indexes of tracked rows still needing enrichment.
-func Page(items []project.Details, params pagination.QueryParams, tracked func(id string) bool) (pagination.FilterResult[project.Details], []int) {
- // Tags are filtered in the database query.
- if _, exists := params.Filters["tags"]; exists {
- params.Filters = maps.Clone(params.Filters)
- delete(params.Filters, "tags")
- }
- config := pagination.Config[project.Details]{
+func PaginationConfig() pagination.Config[project.Details] {
+ return pagination.Config[project.Details]{
SearchAccessors: []pagination.SearchAccessor[project.Details]{
func(p project.Details) (string, error) { return p.Name, nil },
func(p project.Details) (string, error) { return p.Path, nil },
@@ -517,6 +510,7 @@ func Page(items []project.Details, params pagination.QueryParams, tracked func(i
{Key: "name", Fn: func(a, b project.Details) int { return strings.Compare(a.Name, b.Name) }},
{Key: "status", Fn: func(a, b project.Details) int { return strings.Compare(a.Status, b.Status) }},
{Key: "serviceCount", Fn: func(a, b project.Details) int { return cmp.Compare(a.ServiceCount, b.ServiceCount) }},
+ {Key: "runningCount", Fn: func(a, b project.Details) int { return cmp.Compare(a.RunningCount, b.RunningCount) }},
{Key: "path", Fn: func(a, b project.Details) int { return strings.Compare(a.RelativePath, b.RelativePath) }},
{
Key: "createdAt",
@@ -576,7 +570,17 @@ func Page(items []project.Details, params pagination.QueryParams, tracked func(i
},
},
}
- result := config.SearchOrderAndPaginate(items, params)
+}
+
+// Page searches, orders and paginates rows by their derived fields and returns
+// the page indexes of tracked rows still needing enrichment.
+func Page(items []project.Details, params pagination.QueryParams, tracked func(id string) bool) (pagination.FilterResult[project.Details], []int) {
+ // Tags are filtered in the database query.
+ if _, exists := params.Filters["tags"]; exists {
+ params.Filters = maps.Clone(params.Filters)
+ delete(params.Filters, "tags")
+ }
+ result := PaginationConfig().SearchOrderAndPaginate(items, params)
pageIndexes := make([]int, 0, len(result.Items))
for i, item := range result.Items {
if tracked(item.ID) {
diff --git a/backend/internal/project/handler.go b/backend/internal/project/handler.go
index 588fe103b..956e8ed33 100644
--- a/backend/internal/project/handler.go
+++ b/backend/internal/project/handler.go
@@ -166,6 +166,13 @@ func (h *ProjectHandler) ListProjects(ctx context.Context, input *ListProjectsIn
if input.Label != "" {
params.Filters["label"] = input.Label
}
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.projectService.AggregateProjects(ctx, params)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list projects: " + err.Error())
+ }
+ return &handlerutil.Page[project.Details]{Body: body}, nil
+ }
projects, paginationResp, err := h.projectService.ListProjects(ctx, params)
if err != nil {
@@ -190,6 +197,13 @@ func (h *ProjectHandler) ListProjects(ctx context.Context, input *ListProjectsIn
// GetProjectStatusCounts returns counts of projects by status.
func (h *ProjectHandler) GetProjectStatusCounts(ctx context.Context, input *GetProjectStatusCountsInput) (*handlerutil.Out[project.StatusCounts], error) {
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.projectService.AggregateProjectCounts(ctx)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to get project status counts: " + err.Error())
+ }
+ return &handlerutil.Out[project.StatusCounts]{Body: body}, nil
+ }
counts, err := h.projectService.GetProjectStatusCounts(ctx)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to get project status counts: " + err.Error())
@@ -205,6 +219,13 @@ func (h *ProjectHandler) GetProjectStatusCounts(ctx context.Context, input *GetP
// ListProjectTags returns the reusable project tag catalog for an environment.
func (h *ProjectHandler) ListProjectTags(ctx context.Context, _ *ListProjectTagsInput) (*handlerutil.Out[[]project.TagOption], error) {
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.projectService.AggregateProjectTags(ctx)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list project tags: " + err.Error())
+ }
+ return &handlerutil.Out[[]project.TagOption]{Body: body}, nil
+ }
options, err := h.projectService.ListProjectTagOptions(ctx)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to list project tags: " + err.Error())
diff --git a/backend/internal/project/module.go b/backend/internal/project/module.go
index 4a1aa58d8..b077e53b2 100644
--- a/backend/internal/project/module.go
+++ b/backend/internal/project/module.go
@@ -54,6 +54,7 @@ func RegisterProjects(api huma.API, projectService *ProjectService, activityServ
Description: "Get a paginated list of Docker Compose projects",
Tags: []string{"Projects"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermProjectsList, h.ListProjects)
middleware.RegisterWithPermission(api, huma.Operation{
@@ -64,6 +65,7 @@ func RegisterProjects(api huma.API, projectService *ProjectService, activityServ
Description: "Get counts of running, stopped, and total projects",
Tags: []string{"Projects"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermProjectsList, h.GetProjectStatusCounts)
middleware.RegisterWithPermission(api, huma.Operation{
@@ -74,6 +76,7 @@ func RegisterProjects(api huma.API, projectService *ProjectService, activityServ
Description: "Get sorted, distinct project tag names",
Tags: []string{"Projects"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermProjectsList, h.ListProjectTags)
middleware.RegisterWithPermission(api, huma.Operation{
diff --git a/backend/internal/project/service.go b/backend/internal/project/service.go
index 1c738533b..65a1f8f4a 100644
--- a/backend/internal/project/service.go
+++ b/backend/internal/project/service.go
@@ -9,6 +9,7 @@ import (
"io"
"log/slog"
"maps"
+ "net/url"
"os"
"path"
"path/filepath"
@@ -19,6 +20,7 @@ import (
"time"
"github.com/compose-spec/compose-go/v2/types"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
imagetypes "github.com/getarcaneapp/arcane/types/v2/image"
lifecycletype "github.com/getarcaneapp/arcane/types/v2/lifecycle"
@@ -69,6 +71,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/concurrency"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/iconcatalog"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/imageref"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/userctx"
@@ -3428,6 +3432,63 @@ func (s *ProjectService) GetProjectStatusCounts(ctx context.Context) (projecttyp
return s.listing.StatusCounts(ctx, projectRecords(projectsList)), nil
}
+func (s *ProjectService) AggregateProjects(ctx context.Context, params pagination.QueryParams) (base.Paginated[projecttypes.Details], error) {
+ collection, err := httpx.CollectEnvironments[base.Paginated[projecttypes.Details]](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return base.Paginated[projecttypes.Details]{}, fmt.Errorf("collect environment projects: %w", err)
+ }
+ pages := make([]pagination.EnvironmentPage[projecttypes.Details], 0, len(collection.Results))
+ for _, result := range collection.Results {
+ pages = append(pages, pagination.EnvironmentPage[projecttypes.Details]{
+ Environment: result.Environment,
+ Items: result.Value.Data,
+ TotalAvailable: cmp.Or(result.Value.Pagination.GrandTotalItems, result.Value.Pagination.TotalItems),
+ })
+ }
+ byID := func(a, b projecttypes.Details) int { return strings.Compare(a.ID, b.ID) }
+ listConfig := listing.PaginationConfig()
+ listConfig.SortBindings = append([]pagination.SortBinding[projecttypes.Details]{{Key: "id", Fn: byID, DescFn: byID}}, listConfig.SortBindings...)
+ items, resp := pagination.MergeEnvironmentPages(listConfig, pages, params)
+ return base.Paginated[projecttypes.Details]{
+ Success: true,
+ Data: items,
+ Pagination: handlerutil.PaginationResponse(resp),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
+func (s *ProjectService) AggregateProjectCounts(ctx context.Context) (base.ApiResponse[projecttypes.StatusCounts], error) {
+ collection, err := httpx.CollectEnvironments[base.ApiResponse[projecttypes.StatusCounts]](ctx, nil)
+ if err != nil {
+ return base.ApiResponse[projecttypes.StatusCounts]{}, fmt.Errorf("collect environment project counts: %w", err)
+ }
+ var counts projecttypes.StatusCounts
+ for _, result := range collection.Results {
+ counts.RunningProjects += result.Value.Data.RunningProjects
+ counts.StoppedProjects += result.Value.Data.StoppedProjects
+ counts.TotalProjects += result.Value.Data.TotalProjects
+ counts.ArchivedProjects += result.Value.Data.ArchivedProjects
+ }
+ return base.ApiResponse[projecttypes.StatusCounts]{Success: true, Data: counts, FailedEnvironments: collection.Failures}, nil
+}
+
+func (s *ProjectService) AggregateProjectTags(ctx context.Context) (base.ApiResponse[[]projecttypes.TagOption], error) {
+ collection, err := httpx.CollectEnvironments[base.ApiResponse[[]projecttypes.TagOption]](ctx, nil)
+ if err != nil {
+ return base.ApiResponse[[]projecttypes.TagOption]{}, fmt.Errorf("collect environment project tags: %w", err)
+ }
+ options := make([]projecttypes.TagOption, 0)
+ for _, result := range collection.Results {
+ for _, option := range result.Value.Data {
+ if !slices.ContainsFunc(options, func(existing projecttypes.TagOption) bool { return existing.Name == option.Name }) {
+ options = append(options, option)
+ }
+ }
+ }
+ slices.SortStableFunc(options, func(a, b projecttypes.TagOption) int { return strings.Compare(a.Name, b.Name) })
+ return base.ApiResponse[[]projecttypes.TagOption]{Success: true, Data: options, FailedEnvironments: collection.Failures}, nil
+}
+
// projectListRows builds list rows and persists live-inferred service counts
// over stored zeros, since SQL sorts and paginates on service_count.
func (s *ProjectService) projectListRows(ctx context.Context, projectsDir string, projectsList []Project, snapshot listing.Snapshot) []projecttypes.Details {
diff --git a/backend/internal/project/service_test.go b/backend/internal/project/service_test.go
index de8a7feb9..d0d378f72 100644
--- a/backend/internal/project/service_test.go
+++ b/backend/internal/project/service_test.go
@@ -18,12 +18,14 @@ import (
"path/filepath"
"runtime"
"strings"
+ "sync"
"sync/atomic"
"testing"
"time"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v5/pkg/api"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
imagetypes "github.com/getarcaneapp/arcane/types/v2/image"
projecttypes "github.com/getarcaneapp/arcane/types/v2/project"
@@ -68,6 +70,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/volumes"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/iconcatalog"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/imageref"
)
@@ -4919,7 +4922,7 @@ func TestProjectService_SyncProjectsFromFileSystem_PreservesGitOpsProjectWithCus
syncProjectID := "proj-custom-compose"
syncID := "sync-custom-compose"
- sync := &GitOpsSync{
+ gitOpsSync := &GitOpsSync{
ID: syncID,
Name: "Radarr Sync",
EnvironmentID: "0",
@@ -4929,7 +4932,7 @@ func TestProjectService_SyncProjectsFromFileSystem_PreservesGitOpsProjectWithCus
ProjectID: &syncProjectID,
SyncDirectory: true,
}
- require.NoError(t, db.Create(sync).Error)
+ require.NoError(t, db.Create(gitOpsSync).Error)
project := &Project{
ID: syncProjectID,
@@ -7359,23 +7362,23 @@ func TestDiscoveredProjectTagUpdatesRemainScoped(t *testing.T) {
}
func TestProjectTagSummaryDoesNotMutateSharedReferenceResults(t *testing.T) {
- base := map[string]*imagetypes.UpdateInfo{"example:3.1.0": {HasUpdate: false, UpdateType: "digest"}}
+ known := map[string]*imagetypes.UpdateInfo{"example:3.1.0": {HasUpdate: false, UpdateType: "digest"}}
scoped := map[string]*imagetypes.UpdateInfo{
"first": {HasUpdate: true, UpdateType: "tag", LatestVersion: "3.2.0"},
"second": {HasUpdate: true, UpdateType: "tag", LatestVersion: "4.0.0"},
}
- merged := projectdetails.MergeProjectContainerUpdateInfo(base, []projecttypes.RuntimeService{{ContainerID: "first", Image: "example:3.1.0"}, {ContainerID: "second", Image: "example:3.1.0"}}, scoped)
+ merged := projectdetails.MergeProjectContainerUpdateInfo(known, []projecttypes.RuntimeService{{ContainerID: "first", Image: "example:3.1.0"}, {ContainerID: "second", Image: "example:3.1.0"}}, scoped)
require.True(t, merged["example:3.1.0"].HasUpdate)
require.Empty(t, merged["example:3.1.0"].LatestVersion)
- require.False(t, base["example:3.1.0"].HasUpdate)
+ require.False(t, known["example:3.1.0"].HasUpdate)
require.Equal(t, "3.2.0", scoped["first"].LatestVersion)
// A shared image result is dropped when every service using the reference
// opted out of update checks, and kept while one still monitors it.
- base = map[string]*imagetypes.UpdateInfo{"example:3.1.0": {HasUpdate: true, UpdateType: "digest"}, "other:1.0": {HasUpdate: true, UpdateType: "digest"}}
+ known = map[string]*imagetypes.UpdateInfo{"example:3.1.0": {HasUpdate: true, UpdateType: "digest"}, "other:1.0": {HasUpdate: true, UpdateType: "digest"}}
unmonitored := map[string]string{imageref.UpdateCheckLabel: "false"}
merged = projectdetails.MergeProjectContainerUpdateInfo(
- base,
+ known,
[]projecttypes.RuntimeService{
{
ContainerID: "a",
@@ -7394,9 +7397,9 @@ func TestProjectTagSummaryDoesNotMutateSharedReferenceResults(t *testing.T) {
)
require.True(t, merged["example:3.1.0"].HasUpdate, "updater=false keeps the shared result")
require.True(t, merged["other:1.0"].HasUpdate, "references without runtime services are untouched")
- merged = projectdetails.MergeProjectContainerUpdateInfo(base, []projecttypes.RuntimeService{{ContainerID: "a", Image: "example:3.1.0", ContainerLabels: unmonitored}}, nil)
+ merged = projectdetails.MergeProjectContainerUpdateInfo(known, []projecttypes.RuntimeService{{ContainerID: "a", Image: "example:3.1.0", ContainerLabels: unmonitored}}, nil)
require.NotContains(t, merged, "example:3.1.0")
- require.True(t, base["example:3.1.0"].HasUpdate)
+ require.True(t, known["example:3.1.0"].HasUpdate)
}
func TestCountProjectsWithPendingTagUpdatesUsesRuntimeContainers(t *testing.T) {
@@ -8542,7 +8545,7 @@ func TestRunPreDeploy_KillSwitchAndLastRunState(t *testing.T) {
scriptPath := "pre-deploy.sh"
runnerImage := "alpine:latest"
- sync := &GitOpsSync{
+ gitOpsSync := &GitOpsSync{
Name: "demo-sync",
EnvironmentID: "0",
RepositoryID: "repo-1",
@@ -8554,8 +8557,8 @@ func TestRunPreDeploy_KillSwitchAndLastRunState(t *testing.T) {
PreDeployScriptPath: &scriptPath,
PreDeployRunnerImage: &runnerImage,
}
- sync.ID = syncID
- require.NoError(t, db.Create(sync).Error)
+ gitOpsSync.ID = syncID
+ require.NoError(t, db.Create(gitOpsSync).Error)
loadSync := func() GitOpsSync {
var stored GitOpsSync
@@ -8602,7 +8605,7 @@ func TestRunPreDeploy_NoScriptConfigured(t *testing.T) {
project.ID = "proj-1"
require.NoError(t, db.Create(project).Error)
- sync := &GitOpsSync{
+ gitOpsSync := &GitOpsSync{
Name: "demo-sync",
EnvironmentID: "0",
RepositoryID: "repo-1",
@@ -8612,8 +8615,8 @@ func TestRunPreDeploy_NoScriptConfigured(t *testing.T) {
ProjectName: "demo",
ProjectID: &project.ID,
}
- sync.ID = syncID
- require.NoError(t, db.Create(sync).Error)
+ gitOpsSync.ID = syncID
+ require.NoError(t, db.Create(gitOpsSync).Error)
require.NoError(t, svc.runPreDeploy(t.Context(), project, usertypes.Actor{}))
}
@@ -8635,7 +8638,7 @@ func TestRunPreDeploy_MissingRunnerImage(t *testing.T) {
require.NoError(t, db.Create(project).Error)
scriptPath := "pre-deploy.sh"
- sync := &GitOpsSync{
+ gitOpsSync := &GitOpsSync{
Name: "demo-sync",
EnvironmentID: "0",
RepositoryID: "repo-1",
@@ -8646,8 +8649,8 @@ func TestRunPreDeploy_MissingRunnerImage(t *testing.T) {
ProjectID: &project.ID,
PreDeployScriptPath: &scriptPath,
}
- sync.ID = syncID
- require.NoError(t, db.Create(sync).Error)
+ gitOpsSync.ID = syncID
+ require.NoError(t, db.Create(gitOpsSync).Error)
err := svc.runPreDeploy(t.Context(), project, usertypes.Actor{})
require.Error(t, err)
@@ -8670,7 +8673,7 @@ func TestRunPreDeploy_PathTraversalRejected(t *testing.T) {
scriptPath := "../etc/passwd"
runnerImage := "alpine:latest"
- sync := &GitOpsSync{
+ gitOpsSync := &GitOpsSync{
Name: "demo-sync",
EnvironmentID: "0",
RepositoryID: "repo-1",
@@ -8682,8 +8685,8 @@ func TestRunPreDeploy_PathTraversalRejected(t *testing.T) {
PreDeployScriptPath: &scriptPath,
PreDeployRunnerImage: &runnerImage,
}
- sync.ID = syncID
- require.NoError(t, db.Create(sync).Error)
+ gitOpsSync.ID = syncID
+ require.NoError(t, db.Create(gitOpsSync).Error)
err := svc.runPreDeploy(t.Context(), project, usertypes.Actor{})
require.Error(t, err)
@@ -8709,3 +8712,120 @@ func TestListProjectReferencesIncludesArchivedProjects(t *testing.T) {
{ID: "project-archived", Name: "archived"},
}, references)
}
+
+func projectScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ _ = json.NewEncoder(w).Encode(body)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregateProjectsMergesEnvironments(t *testing.T) {
+ bodies := map[string]any{
+ "a": base.Paginated[projecttypes.Details]{Success: true, Data: []projecttypes.Details{
+ {ID: "a1", Name: "web", ServiceCount: 2},
+ {ID: "a2", Name: "db", ServiceCount: 1},
+ }, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 3}},
+ "b": base.Paginated[projecttypes.Details]{Success: true, Data: []projecttypes.Details{
+ {ID: "b1", Name: "cache", ServiceCount: 3},
+ }, Pagination: base.PaginationResponse{TotalItems: 1}},
+ }
+ rowLabels := func(items []projecttypes.Details) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.Name)
+ }
+ return out
+ }
+ svc := &ProjectService{}
+
+ t.Run("sorts by name across environments and pages", func(t *testing.T) {
+ ctx, queries := projectScopeContext(t, "/api/environments/a,b,c/projects?start=0&limit=2&sort=name&order=asc", bodies)
+ body, err := svc.AggregateProjects(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "name", Order: pagination.SortAsc}, Params: pagination.Params{Limit: 2}})
+ require.NoError(t, err)
+
+ assert.True(t, body.Success)
+ assert.Equal(t, []string{"b/cache", "a/db"}, rowLabels(body.Data))
+ assert.Equal(t, "Env b", body.Data[0].EnvironmentName)
+ assert.Equal(t, int64(3), body.Pagination.TotalItems)
+ assert.Equal(t, int64(4), body.Pagination.GrandTotalItems)
+ assert.Equal(t, int64(2), body.Pagination.TotalPages)
+ require.Len(t, body.FailedEnvironments, 1)
+ assert.Equal(t, "c", body.FailedEnvironments[0].Environment.ID)
+ assert.Equal(t, "environment offline", body.FailedEnvironments[0].Message)
+ for _, id := range []string{"a", "b"} {
+ query, ok := queries.Load(id)
+ require.True(t, ok)
+ assert.Equal(t, "0", query.(url.Values).Get("start"))
+ assert.Equal(t, "-1", query.(url.Values).Get("limit"))
+ }
+ })
+
+ t.Run("sorts by service count descending", func(t *testing.T) {
+ ctx, _ := projectScopeContext(t, "/api/environments/a,b,c/projects", bodies)
+ body, err := svc.AggregateProjects(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "serviceCount", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ assert.Equal(t, []string{"b/cache", "a/web", "a/db"}, rowLabels(body.Data))
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, queries := projectScopeContext(t, "/api/environments/a,b,c/projects?sort=environment&order=desc", bodies)
+ body, err := svc.AggregateProjects(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ assert.Equal(t, []string{"b/cache", "a/web", "a/db"}, rowLabels(body.Data))
+ query, _ := queries.Load("a")
+ assert.Empty(t, query.(url.Values).Get("sort"))
+ })
+
+ t.Run("all environments failing returns empty data", func(t *testing.T) {
+ ctx, _ := projectScopeContext(t, "/api/environments/a,b,c/projects", map[string]any{})
+ body, err := svc.AggregateProjects(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}})
+ require.NoError(t, err)
+ assert.Empty(t, body.Data)
+ assert.Len(t, body.FailedEnvironments, 3)
+ })
+}
+
+func TestAggregateProjectCountsSumsEnvironments(t *testing.T) {
+ ctx, _ := projectScopeContext(t, "/api/environments/a,b,c/projects/counts", map[string]any{
+ "a": base.ApiResponse[projecttypes.StatusCounts]{Success: true, Data: projecttypes.StatusCounts{RunningProjects: 2, StoppedProjects: 1, TotalProjects: 4, ArchivedProjects: 1}},
+ "b": base.ApiResponse[projecttypes.StatusCounts]{Success: true, Data: projecttypes.StatusCounts{RunningProjects: 1, StoppedProjects: 3, TotalProjects: 4}},
+ })
+
+ body, err := (&ProjectService{}).AggregateProjectCounts(ctx)
+ require.NoError(t, err)
+
+ assert.True(t, body.Success)
+ assert.Equal(t, projecttypes.StatusCounts{RunningProjects: 3, StoppedProjects: 4, TotalProjects: 8, ArchivedProjects: 1}, body.Data)
+ require.Len(t, body.FailedEnvironments, 1)
+ assert.Equal(t, "c", body.FailedEnvironments[0].Environment.ID)
+}
+
+func TestAggregateProjectTagsUnionsByName(t *testing.T) {
+ ctx, _ := projectScopeContext(t, "/api/environments/a,b,c/projects/tags", map[string]any{
+ "a": base.ApiResponse[[]projecttypes.TagOption]{Success: true, Data: []projecttypes.TagOption{{Name: "prod", Color: "red"}, {Name: "web", Color: "blue"}}},
+ "b": base.ApiResponse[[]projecttypes.TagOption]{Success: true, Data: []projecttypes.TagOption{{Name: "api", Color: "green"}, {Name: "prod", Color: "gray"}}},
+ })
+
+ body, err := (&ProjectService{}).AggregateProjectTags(ctx)
+ require.NoError(t, err)
+
+ assert.Equal(t, []projecttypes.TagOption{{Name: "api", Color: "green"}, {Name: "prod", Color: "red"}, {Name: "web", Color: "blue"}}, body.Data)
+ require.Len(t, body.FailedEnvironments, 1)
+}
diff --git a/backend/internal/settings/model.go b/backend/internal/settings/model.go
index 66a945b40..84f0529e7 100644
--- a/backend/internal/settings/model.go
+++ b/backend/internal/settings/model.go
@@ -75,6 +75,7 @@ type Settings struct {
DefaultShell SettingVariable `key:"defaultShell" meta:"label=Default Shell;type=text;keywords=shell,default,shellpath,path,login;category=general;description=Default shell to use for commands"`
ExperimentalFeaturesEnabled SettingVariable `key:"experimentalFeaturesEnabled,authrequired" meta:"label=Experimental Features;type=boolean;keywords=experimental,features,beta,preview,flags;category=general;description=Enable experimental features that may change or be removed in future releases"`
DevelopmentBrandingEnabled SettingVariable `key:"developmentBrandingEnabled,public" meta:"label=Development Branding;type=boolean;keywords=development,branding,logo,icon,favicon,next,dev,preview,prerelease;category=general;description=Show the development icon instead of the standard Arcane logo on non-stable builds"`
+ AllEnvironmentsLimit SettingVariable `key:"allEnvironmentsLimit,public" meta:"label=All Environments Limit;type=number;keywords=all,environments,limit,maximum,preview,merged,scope,request;category=internal;description=Maximum environments included in one all-environments request (default: 64)"`
EnvironmentHealthInterval SettingVariable `key:"environmentHealthInterval" meta:"label=Environment Health Check Interval;type=cron;keywords=environment,health,check,interval,frequency,heartbeat,status,monitoring,uptime,jobs,schedule;description=How often to check environment connectivity (cron expression)" catmeta:"id=jobschedule;title=Automations;icon=jobs;url=/settings/jobs;description=Configure how often Arcane background jobs run"`
// Docker category
diff --git a/backend/internal/settings/service.go b/backend/internal/settings/service.go
index 0e2e0fd2c..c8a0fb9d8 100644
--- a/backend/internal/settings/service.go
+++ b/backend/internal/settings/service.go
@@ -301,6 +301,7 @@ func DefaultSettingsConfig() *Settings {
ExperimentalFeaturesEnabled: SettingVariable{Value: "false"},
DevelopmentBrandingEnabled: SettingVariable{Value: "true"},
AvatarMaxUploadSizeMb: SettingVariable{Value: "2"},
+ AllEnvironmentsLimit: SettingVariable{Value: "64"},
DefaultShell: SettingVariable{Value: "/bin/sh"},
DockerHost: SettingVariable{Value: "unix:///var/run/docker.sock"},
BuildsDirectory: SettingVariable{Value: "/builds"},
@@ -421,6 +422,12 @@ func validateSettingValue(key, value string) error {
return common.Classify(common.ErrValidation, errors.New("upgradeLogRetentionDays must be a whole number between 0 and 3650"))
}
}
+ if key == "allEnvironmentsLimit" {
+ limit, err := strconv.Atoi(value)
+ if err != nil || limit < 2 || limit > 256 {
+ return common.Classify(common.ErrValidation, errors.New("allEnvironmentsLimit must be a whole number between 2 and 256"))
+ }
+ }
for _, definition := range features.All() {
if definition.SettingKey == key && value != "true" && value != "false" {
return common.Classify(common.ErrValidation, fmt.Errorf("%s must be true or false", key))
diff --git a/backend/internal/volume/handler.go b/backend/internal/volume/handler.go
index 39eb24532..f75c04811 100644
--- a/backend/internal/volume/handler.go
+++ b/backend/internal/volume/handler.go
@@ -25,6 +25,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
// VolumeHandler provides Huma-based volume management endpoints.
@@ -115,6 +116,8 @@ type GetVolumeSizesInput struct {
// VolumeSizeInfo represents size information for a single volume.
type VolumeSizeInfo struct {
+ base.EnvironmentTag
+
Name string `json:"name"`
Size int64 `json:"size"`
RefCount int64 `json:"refCount"`
@@ -129,6 +132,14 @@ func (h *VolumeHandler) ListVolumes(ctx context.Context, input *ListVolumesInput
params.Limit = cmp.Or(params.Limit, 20)
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.volumeService.AggregateVolumes(ctx, params)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to list volumes: " + err.Error())
+ }
+ return &ListVolumesOutput{Body: body}, nil
+ }
+
volumes, paginationResp, counts, err := h.volumeService.ListVolumesPaginated(ctx, params, input.IncludeInternal)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to list volumes: " + err.Error())
@@ -383,6 +394,14 @@ func (h *VolumeHandler) GetVolumeUsageCounts(ctx context.Context, input *GetVolu
// GetVolumeSizes returns disk usage sizes for all volumes.
// This is a slow operation as it requires calculating disk usage.
func (h *VolumeHandler) GetVolumeSizes(ctx context.Context, input *GetVolumeSizesInput) (*handlerutil.Out[[]VolumeSizeInfo], error) {
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.volumeService.AggregateVolumeSizes(ctx)
+ if err != nil {
+ return nil, huma.Error500InternalServerError(err.Error())
+ }
+ return &handlerutil.Out[[]VolumeSizeInfo]{Body: body}, nil
+ }
+
sizes, err := h.volumeService.GetVolumeSizes(ctx)
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
diff --git a/backend/internal/volume/module.go b/backend/internal/volume/module.go
index 18ef1d815..f07ba76ba 100644
--- a/backend/internal/volume/module.go
+++ b/backend/internal/volume/module.go
@@ -98,6 +98,7 @@ func RegisterVolumes(
Description: "Get a paginated list of Docker volumes",
Tags: []string{"Volumes"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermVolumesList, h.ListVolumes)
middleware.RegisterWithPermission(api, huma.Operation{
@@ -168,5 +169,6 @@ func RegisterVolumes(
Description: "Get disk usage sizes for all volumes (slow operation)",
Tags: []string{"Volumes"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermVolumesList, h.GetVolumeSizes)
}
diff --git a/backend/internal/volume/service.go b/backend/internal/volume/service.go
index 7e10a59be..8b9b8cd3e 100644
--- a/backend/internal/volume/service.go
+++ b/backend/internal/volume/service.go
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log/slog"
+ "net/url"
"regexp"
"strings"
"sync"
@@ -13,6 +14,7 @@ import (
"github.com/containerd/errdefs"
backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/scheduler"
usertypes "github.com/getarcaneapp/arcane/types/v2/user"
volumetypes "github.com/getarcaneapp/arcane/types/v2/volume"
@@ -45,6 +47,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/flow"
"github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/runs"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
workspacepkg "github.com/getarcaneapp/arcane/backend/v2/pkg/workspace"
)
@@ -658,7 +662,7 @@ func (s *VolumeService) buildVolumeContainerMapInternal(ctx context.Context) (ma
return volumeContainerMap, nil
}
-func (s *VolumeService) buildVolumePaginationConfigInternal() pagination.Config[volumetypes.Volume] {
+func (s *VolumeService) buildVolumePaginationConfig() pagination.Config[volumetypes.Volume] {
return pagination.Config[volumetypes.Volume]{
SearchAccessors: []pagination.SearchAccessor[volumetypes.Volume]{
func(v volumetypes.Volume) (string, error) { return v.Name, nil },
@@ -977,7 +981,7 @@ func (s *VolumeService) ListVolumesPaginated(ctx context.Context, params paginat
items = append(items, volDto)
}
- paginationConfig := s.buildVolumePaginationConfigInternal()
+ paginationConfig := s.buildVolumePaginationConfig()
result := paginationConfig.SearchOrderAndPaginate(items, effectiveParams)
counts := calculateVolumeUsageCountsInternal(items)
paginationResp := pagination.BuildResponse(result.TotalCount, result.TotalAvailable, effectiveParams)
@@ -1004,6 +1008,53 @@ func (s *VolumeService) ListVolumesPaginated(ctx context.Context, params paginat
return result.Items, paginationResp, counts, nil
}
+func (s *VolumeService) AggregateVolumes(ctx context.Context, params pagination.QueryParams) (base.PaginatedWithCounts[volumetypes.Volume, VolumeUsageCountsData], error) {
+ collection, err := httpx.CollectEnvironments[base.PaginatedWithCounts[volumetypes.Volume, VolumeUsageCountsData]](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return base.PaginatedWithCounts[volumetypes.Volume, VolumeUsageCountsData]{}, err
+ }
+
+ pages := make([]pagination.EnvironmentPage[volumetypes.Volume], 0, len(collection.Results))
+ counts := VolumeUsageCountsData{}
+ for _, result := range collection.Results {
+ page := result.Value
+ pages = append(pages, pagination.EnvironmentPage[volumetypes.Volume]{
+ Environment: result.Environment,
+ Items: page.Data,
+ TotalAvailable: cmp.Or(page.Pagination.GrandTotalItems, page.Pagination.TotalItems),
+ })
+ counts.Inuse += page.Counts.Inuse
+ counts.Unused += page.Counts.Unused
+ counts.Total += page.Counts.Total
+ }
+ data, resp := pagination.MergeEnvironmentPages(s.buildVolumePaginationConfig(), pages, params)
+
+ return base.PaginatedWithCounts[volumetypes.Volume, VolumeUsageCountsData]{
+ Success: true,
+ Data: data,
+ Counts: counts,
+ Pagination: handlerutil.PaginationResponse(resp),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
+func (s *VolumeService) AggregateVolumeSizes(ctx context.Context) (base.ApiResponse[[]VolumeSizeInfo], error) {
+ collection, err := httpx.CollectEnvironments[base.ApiResponse[[]VolumeSizeInfo]](ctx, nil)
+ if err != nil {
+ return base.ApiResponse[[]VolumeSizeInfo]{}, err
+ }
+
+ sizes := make([]VolumeSizeInfo, 0)
+ for _, result := range collection.Results {
+ for _, size := range result.Value.Data {
+ size.SetEnvironment(result.Environment)
+ sizes = append(sizes, size)
+ }
+ }
+
+ return base.ApiResponse[[]VolumeSizeInfo]{Success: true, Data: sizes, FailedEnvironments: collection.Failures}, nil
+}
+
// RenameVolume copies an unused volume to a new name and removes the source.
func (s *VolumeService) RenameVolume(ctx context.Context, oldName, newName string, user usertypes.Actor) (*volumetypes.Volume, error) {
oldName = strings.TrimSpace(oldName)
diff --git a/backend/internal/volume/service_test.go b/backend/internal/volume/service_test.go
index 63d88ab34..f55e3543a 100644
--- a/backend/internal/volume/service_test.go
+++ b/backend/internal/volume/service_test.go
@@ -10,13 +10,16 @@ import (
"net"
"net/http"
"net/http/httptest"
+ "net/url"
"os"
"slices"
"strings"
+ "sync"
"testing"
"time"
"github.com/getarcaneapp/arcane/types/v2/backup"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/user"
volumetypes "github.com/getarcaneapp/arcane/types/v2/volume"
"github.com/libtnb/sqlite"
@@ -33,6 +36,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/docker"
"github.com/getarcaneapp/arcane/backend/v2/internal/s3"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func TestEnrichVolumesWithUsageDataInternal(t *testing.T) {
@@ -480,3 +484,110 @@ func TestVolumeBackupPolicy_GetReturnsLastRunForEachPolicyFromStore(t *testing.T
require.NoError(t, err)
require.Equal(t, "failed", second.Status)
}
+
+func volumeScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ _ = json.NewEncoder(w).Encode(body)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregateVolumesMergesEnvironments(t *testing.T) {
+ bodies := map[string]any{
+ "a": base.PaginatedWithCounts[volumetypes.Volume, VolumeUsageCountsData]{Success: true, Data: []volumetypes.Volume{
+ {Name: "data", Size: 30, InUse: true},
+ {Name: "logs", Size: 5},
+ }, Counts: VolumeUsageCountsData{Inuse: 1, Unused: 1, Total: 2}, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 3}},
+ "b": base.PaginatedWithCounts[volumetypes.Volume, VolumeUsageCountsData]{Success: true, Data: []volumetypes.Volume{
+ {Name: "cache", Size: 10},
+ }, Counts: VolumeUsageCountsData{Unused: 1, Total: 1}, Pagination: base.PaginationResponse{TotalItems: 1}},
+ }
+ rowLabels := func(items []volumetypes.Volume) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.Name)
+ }
+ return out
+ }
+ svc := &VolumeService{}
+
+ t.Run("sorts by name across environments and pages", func(t *testing.T) {
+ ctx, queries := volumeScopeContext(t, "/api/environments/a,b,c/volumes?start=0&limit=2&sort=name&order=asc", bodies)
+ body, err := svc.AggregateVolumes(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "name", Order: pagination.SortAsc}, Params: pagination.Params{Limit: 2}})
+ require.NoError(t, err)
+
+ require.True(t, body.Success)
+ require.Equal(t, []string{"b/cache", "a/data"}, rowLabels(body.Data))
+ require.Equal(t, "Env b", body.Data[0].EnvironmentName)
+ require.Equal(t, int64(3), body.Pagination.TotalItems)
+ require.Equal(t, int64(4), body.Pagination.GrandTotalItems)
+ require.Equal(t, int64(2), body.Pagination.TotalPages)
+ require.Equal(t, VolumeUsageCountsData{Inuse: 1, Unused: 2, Total: 3}, body.Counts)
+ require.Equal(t, []base.EnvironmentFailure{{Environment: base.EnvironmentRef{ID: "c", Name: "c"}, Message: "environment offline"}}, body.FailedEnvironments)
+ for _, id := range []string{"a", "b"} {
+ stored, ok := queries.Load(id)
+ require.True(t, ok)
+ require.Equal(t, "0", stored.(url.Values).Get("start"))
+ require.Equal(t, "-1", stored.(url.Values).Get("limit"))
+ }
+ })
+
+ t.Run("sorts by size reported by each environment", func(t *testing.T) {
+ ctx, queries := volumeScopeContext(t, "/api/environments/a,b,c/volumes?sort=size&order=desc", bodies)
+ body, err := svc.AggregateVolumes(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "size", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ require.Equal(t, []string{"a/data", "b/cache", "a/logs"}, rowLabels(body.Data))
+ stored, _ := queries.Load("b")
+ require.Equal(t, "size", stored.(url.Values).Get("sort"))
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, _ := volumeScopeContext(t, "/api/environments/a,b,c/volumes?sort=environment&order=desc", bodies)
+ body, err := svc.AggregateVolumes(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ require.Equal(t, []string{"b/cache", "a/data", "a/logs"}, rowLabels(body.Data))
+ })
+
+ t.Run("all environments failing returns empty data", func(t *testing.T) {
+ ctx, _ := volumeScopeContext(t, "/api/environments/a,b,c/volumes", map[string]any{})
+ body, err := svc.AggregateVolumes(ctx, pagination.QueryParams{Params: pagination.Params{Limit: 20}})
+ require.NoError(t, err)
+ require.Empty(t, body.Data)
+ require.Len(t, body.FailedEnvironments, 3)
+ })
+}
+
+func TestAggregateVolumeSizesKeepsEnvironmentRows(t *testing.T) {
+ ctx, _ := volumeScopeContext(t, "/api/environments/a,b,c/volumes/sizes", map[string]any{
+ "a": base.ApiResponse[[]VolumeSizeInfo]{Success: true, Data: []VolumeSizeInfo{{Name: "data", Size: 30, RefCount: 1}, {Name: "logs", Size: 5}}},
+ "b": base.ApiResponse[[]VolumeSizeInfo]{Success: true, Data: []VolumeSizeInfo{{Name: "data", Size: 7}}},
+ })
+
+ body, err := (&VolumeService{}).AggregateVolumeSizes(ctx)
+ require.NoError(t, err)
+
+ require.True(t, body.Success)
+ require.Equal(t, []VolumeSizeInfo{
+ {EnvironmentTag: base.EnvironmentTag{EnvironmentID: "a", EnvironmentName: "Env a"}, Name: "data", Size: 30, RefCount: 1},
+ {EnvironmentTag: base.EnvironmentTag{EnvironmentID: "a", EnvironmentName: "Env a"}, Name: "logs", Size: 5},
+ {EnvironmentTag: base.EnvironmentTag{EnvironmentID: "b", EnvironmentName: "Env b"}, Name: "data", Size: 7},
+ }, body.Data)
+ require.Len(t, body.FailedEnvironments, 1)
+ require.Equal(t, "c", body.FailedEnvironments[0].Environment.ID)
+}
diff --git a/backend/internal/vulnerability/children/query/handler.go b/backend/internal/vulnerability/children/query/handler.go
index a9b4857d9..92b8b4a59 100644
--- a/backend/internal/vulnerability/children/query/handler.go
+++ b/backend/internal/vulnerability/children/query/handler.go
@@ -16,6 +16,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
// Handler serves the vulnerability results and risk endpoints.
@@ -247,6 +248,13 @@ func (input *ListAllVulnerabilitiesInput) queryParamsInternal() pagination.Query
}
func (h *Handler) ListAllVulnerabilities(ctx context.Context, input *ListAllVulnerabilitiesInput) (*handlerutil.Page[vulnerability.VulnerabilityWithImage], error) {
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.service.AggregateVulnerabilities(ctx, input.queryParamsInternal())
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to retrieve vulnerability scan: " + err.Error())
+ }
+ return &handlerutil.Page[vulnerability.VulnerabilityWithImage]{Body: body}, nil
+ }
items, paginationResp, err := h.service.ListAllVulnerabilities(ctx, input.EnvironmentID, input.queryParamsInternal())
if err != nil {
return nil, huma.Error500InternalServerError("Failed to retrieve vulnerability scan: " + err.Error())
@@ -296,6 +304,13 @@ func (h *Handler) ExportVulnerabilities(ctx context.Context, input *ExportVulner
// ListAllVulnerabilityImageOptions returns available image options for vulnerability filtering.
func (h *Handler) ListAllVulnerabilityImageOptions(ctx context.Context, input *ListAllVulnerabilityImageOptionsInput) (*handlerutil.Out[[]string], error) {
+ if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
+ body, err := h.service.AggregateImageOptions(ctx)
+ if err != nil {
+ return nil, huma.Error500InternalServerError("Failed to retrieve vulnerability scan: " + err.Error())
+ }
+ return &handlerutil.Out[[]string]{Body: body}, nil
+ }
items, err := h.service.ListAllVulnerabilityImageOptions(ctx, input.Severity)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to retrieve vulnerability scan: " + err.Error())
diff --git a/backend/internal/vulnerability/children/query/module.go b/backend/internal/vulnerability/children/query/module.go
index f78283c39..1580392a4 100644
--- a/backend/internal/vulnerability/children/query/module.go
+++ b/backend/internal/vulnerability/children/query/module.go
@@ -87,6 +87,7 @@ func RegisterRoutes(api huma.API, h *Handler, featureMiddleware huma.Middlewares
Description: "Retrieves paginated vulnerabilities across all scanned images in the environment",
Tags: []string{"Vulnerabilities"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermVulnsRead, h.ListAllVulnerabilities)
middleware.RegisterWithPermission(api, huma.Operation{
@@ -109,5 +110,6 @@ func RegisterRoutes(api huma.API, h *Handler, featureMiddleware huma.Middlewares
Description: "Retrieves available image filter options for environment vulnerabilities",
Tags: []string{"Vulnerabilities"},
Security: handlerutil.DefaultOperationSecurity(),
+ Metadata: map[string]any{authz.MetaEnvironmentAggregate: true},
}, authz.PermVulnsRead, h.ListAllVulnerabilityImageOptions)
}
diff --git a/backend/internal/vulnerability/children/query/service.go b/backend/internal/vulnerability/children/query/service.go
index 1dd27faa1..733933c96 100644
--- a/backend/internal/vulnerability/children/query/service.go
+++ b/backend/internal/vulnerability/children/query/service.go
@@ -11,6 +11,7 @@ import (
"iter"
"log/slog"
"math"
+ "net/url"
"slices"
"sort"
"strconv"
@@ -20,6 +21,7 @@ import (
"time"
"github.com/getarcaneapp/arcane/types/v2"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/features"
"github.com/getarcaneapp/arcane/types/v2/vulnerability"
"github.com/moby/moby/api/types/container"
@@ -37,6 +39,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/vuln"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/dbutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
const (
@@ -459,11 +463,11 @@ func (s *Service) listAllVulnerabilitiesQueryInternal(ctx context.Context, db *g
onlyIgnored := params.Filters["ignored"] == "true"
delete(params.Filters, "ignored")
- base := scanItemsQueryInternal(ctx, db, envID, onlyIgnored, mo.TupleToOption(s.currentImageIDsInternal(ctx))).
+ countQuery := scanItemsQueryInternal(ctx, db, envID, onlyIgnored, mo.TupleToOption(s.currentImageIDsInternal(ctx))).
Select("vulnerability_scan_items.*", "vulnerability_scans.image_name AS image_name", "COALESCE(vulnerability_ignores.id, '') AS ignore_id").
Session(&gorm.Session{})
- q := pagination.ApplyFilter(base, "vulnerability_scan_items.severity", strings.ToUpper(params.Filters["severity"]))
+ q := pagination.ApplyFilter(countQuery, "vulnerability_scan_items.severity", strings.ToUpper(params.Filters["severity"]))
if params.Filters["fixAvailable"] == "true" {
q = q.Where("vulnerability_scan_items.fixed_version <> ''")
}
@@ -488,7 +492,7 @@ func (s *Service) listAllVulnerabilitiesQueryInternal(ctx context.Context, db *g
}
params.Sort = ""
- return base, q
+ return countQuery, q
}
// ListAllVulnerabilities returns a paginated list of vulnerabilities across all scanned images.
@@ -499,10 +503,10 @@ func (s *Service) ListAllVulnerabilities(ctx context.Context, envID string, para
return []vulnerability.VulnerabilityWithImage{}, pagination.BuildResponse(0, 0, params), nil
}
- base, q := s.listAllVulnerabilitiesQueryInternal(ctx, s.db.DB, envID, &params)
+ countQuery, q := s.listAllVulnerabilitiesQueryInternal(ctx, s.db.DB, envID, &params)
var totalAvailable int64
- if err := base.Count(&totalAvailable).Error; err != nil {
+ if err := countQuery.Count(&totalAvailable).Error; err != nil {
return nil, pagination.Response{}, fmt.Errorf("failed to count vulnerabilities: %w", err)
}
@@ -546,6 +550,54 @@ func (s *Service) ListAllVulnerabilities(ctx context.Context, envID string, para
return items, response, nil
}
+func (s *Service) AggregateVulnerabilities(ctx context.Context, params pagination.QueryParams) (base.Paginated[vulnerability.VulnerabilityWithImage], error) {
+ collection, err := httpx.CollectEnvironments[base.Paginated[vulnerability.VulnerabilityWithImage]](ctx, url.Values{"start": {"0"}, "limit": {"-1"}})
+ if err != nil {
+ return base.Paginated[vulnerability.VulnerabilityWithImage]{}, fmt.Errorf("collect environment vulnerabilities: %w", err)
+ }
+ pages := make([]pagination.EnvironmentPage[vulnerability.VulnerabilityWithImage], 0, len(collection.Results))
+ for _, result := range collection.Results {
+ pages = append(pages, pagination.EnvironmentPage[vulnerability.VulnerabilityWithImage]{
+ Environment: result.Environment,
+ Items: result.Value.Data,
+ TotalAvailable: cmp.Or(result.Value.Pagination.GrandTotalItems, result.Value.Pagination.TotalItems),
+ })
+ }
+ items, resp := pagination.MergeEnvironmentPages(pagination.Config[vulnerability.VulnerabilityWithImage]{
+ SortBindings: []pagination.SortBinding[vulnerability.VulnerabilityWithImage]{
+ {Key: "imageId", Fn: func(a, b vulnerability.VulnerabilityWithImage) int {
+ return cmp.Or(
+ strings.Compare(a.ImageName, b.ImageName),
+ strings.Compare(a.ImageID, b.ImageID),
+ strings.Compare(a.PkgName, b.PkgName),
+ strings.Compare(a.InstalledVersion, b.InstalledVersion),
+ strings.Compare(a.VulnerabilityID, b.VulnerabilityID),
+ )
+ }},
+ {Key: "vulnerabilityId", Fn: func(a, b vulnerability.VulnerabilityWithImage) int {
+ return strings.Compare(a.VulnerabilityID, b.VulnerabilityID)
+ }},
+ {Key: "pkgName", Fn: func(a, b vulnerability.VulnerabilityWithImage) int { return strings.Compare(a.PkgName, b.PkgName) }},
+ {Key: "severity", Fn: func(a, b vulnerability.VulnerabilityWithImage) int {
+ return cmp.Compare(vuln.SeverityRank(a.Severity), vuln.SeverityRank(b.Severity))
+ }},
+ {Key: "installedVersion", Fn: func(a, b vulnerability.VulnerabilityWithImage) int {
+ return strings.Compare(a.InstalledVersion, b.InstalledVersion)
+ }},
+ {Key: "fixedVersion", Fn: func(a, b vulnerability.VulnerabilityWithImage) int {
+ return strings.Compare(a.FixedVersion, b.FixedVersion)
+ }},
+ {Key: "imageName", Fn: func(a, b vulnerability.VulnerabilityWithImage) int { return strings.Compare(a.ImageName, b.ImageName) }},
+ },
+ }, pages, params)
+ return base.Paginated[vulnerability.VulnerabilityWithImage]{
+ Success: true,
+ Data: items,
+ Pagination: handlerutil.PaginationResponse(resp),
+ FailedEnvironments: collection.Failures,
+ }, nil
+}
+
// ExportVulnerabilities opens a cursor over every vulnerability matching params and yields rows one at a time in the requested order.
func (s *Service) ExportVulnerabilities(ctx context.Context, envID string, params pagination.QueryParams) (iter.Seq2[vulnerability.VulnerabilityWithImage, error], error) {
if !s.settingsService.IsFeatureEnabled(ctx, features.VulnerabilityManagement) || s.db == nil {
@@ -647,6 +699,19 @@ func (s *Service) ListAllVulnerabilityImageOptions(ctx context.Context, severity
return options, nil
}
+func (s *Service) AggregateImageOptions(ctx context.Context) (base.ApiResponse[[]string], error) {
+ collection, err := httpx.CollectEnvironments[base.ApiResponse[[]string]](ctx, nil)
+ if err != nil {
+ return base.ApiResponse[[]string]{}, fmt.Errorf("collect environment vulnerability image options: %w", err)
+ }
+ options := make([]string, 0)
+ for _, result := range collection.Results {
+ options = append(options, result.Value.Data...)
+ }
+ slices.Sort(options)
+ return base.ApiResponse[[]string]{Success: true, Data: slices.Compact(options), FailedEnvironments: collection.Failures}, nil
+}
+
// GetScanSummariesByImageIDs retrieves scan summaries for multiple images
func (s *Service) GetScanSummariesByImageIDs(ctx context.Context, imageIDs []string) (map[string]*vulnerability.ScanSummary, error) {
if !s.settingsService.IsFeatureEnabled(ctx, features.VulnerabilityManagement) || s.db == nil || len(imageIDs) == 0 {
@@ -773,7 +838,7 @@ type riskAccumulatorInternal struct {
func (a *riskAccumulatorInternal) addInternal(row *riskFindingRowInternal, exposure vulnerability.ImageExposure, runningContainers int) {
severity := vulnerability.Severity(row.Severity)
- base := vuln.BaseScore(*cmp.Or(row.CVSSScore, new(0.0)), severity)
+ baseScore := vuln.BaseScore(*cmp.Or(row.CVSSScore, new(0.0)), severity)
fixable := strings.TrimSpace(row.FixedVersion) != ""
summary := &a.overview.Summary
@@ -810,7 +875,7 @@ func (a *riskAccumulatorInternal) addInternal(row *riskFindingRowInternal, expos
a.images[row.ImageID] = img
}
img.image.Findings++
- priority := img.score.AddFinding(row.VulnerabilityID, fixable, base, vuln.ThreatIntel{KnownExploited: row.KnownExploited, EPSS: row.EPSSScore}, exposure)
+ priority := img.score.AddFinding(row.VulnerabilityID, fixable, baseScore, vuln.ThreatIntel{KnownExploited: row.KnownExploited, EPSS: row.EPSSScore}, exposure)
risk := priority / 10
if severity == vulnerability.SeverityCritical {
img.image.Critical++
@@ -825,7 +890,7 @@ func (a *riskAccumulatorInternal) addInternal(row *riskFindingRowInternal, expos
img.image.Fixable++
a.overview.Drivers.Fixable++
}
- if fixable && base > 0 {
+ if fixable && baseScore > 0 {
a.considerDriverInternal(row, exposure, priority)
}
@@ -851,7 +916,7 @@ func (a *riskAccumulatorInternal) addInternal(row *riskFindingRowInternal, expos
}
// The highest-priority instance of a CVE describes it; any fixed version beats none.
if risk > agg.finding.Risk {
- agg.finding.Risk, agg.finding.CVSS, agg.finding.Severity = risk, base, severity
+ agg.finding.Risk, agg.finding.CVSS, agg.finding.Severity = risk, baseScore, severity
agg.finding.PkgName, agg.finding.Title = row.PkgName, row.Title
if fixable {
agg.finding.FixedVersion = row.FixedVersion
diff --git a/backend/internal/vulnerability/children/query/service_test.go b/backend/internal/vulnerability/children/query/service_test.go
index 68c421999..727075387 100644
--- a/backend/internal/vulnerability/children/query/service_test.go
+++ b/backend/internal/vulnerability/children/query/service_test.go
@@ -2,13 +2,24 @@ package query
import (
"bytes"
+ "context"
+ "encoding/json/v2"
"iter"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "sync"
"testing"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/vulnerability"
"github.com/libtnb/sqlite"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
+
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
func TestSeverityCountColumnsForFilterInternal(t *testing.T) {
@@ -55,15 +66,15 @@ func TestSeverityCountColumnsForFilterInternal(t *testing.T) {
func TestApplyListAllVulnerabilityRecordPrefiltersInternal_NoPossibleMatches(t *testing.T) {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
- base := db.WithContext(t.Context()).Table(scansTableInternal)
+ scans := db.WithContext(t.Context()).Table(scansTableInternal)
- _, ok := applyListAllVulnerabilityRecordPrefiltersInternal(base, map[string]string{"severity": "not-a-severity"}).Get()
+ _, ok := applyListAllVulnerabilityRecordPrefiltersInternal(scans, map[string]string{"severity": "not-a-severity"}).Get()
require.False(t, ok)
- _, ok = applyListAllVulnerabilityRecordPrefiltersInternal(base, map[string]string{"imageName": " , "}).Get()
+ _, ok = applyListAllVulnerabilityRecordPrefiltersInternal(scans, map[string]string{"imageName": " , "}).Get()
require.False(t, ok)
- _, ok = applyListAllVulnerabilityRecordPrefiltersInternal(base, map[string]string{
+ _, ok = applyListAllVulnerabilityRecordPrefiltersInternal(scans, map[string]string{
"severity": "high,bad",
"imageName": "nginx",
}).Get()
@@ -129,3 +140,117 @@ func seqInternal(items []vulnerability.VulnerabilityWithImage) iter.Seq2[vulnera
}
}
}
+
+func vulnerabilityScopeContext(t *testing.T, target string, bodies map[string]any) (context.Context, *sync.Map) {
+ t.Helper()
+ var queries sync.Map
+ return httpx.WithEnvironmentScope(t.Context(), &httpx.EnvironmentScope{
+ IDs: []string{"a", "b", "c"},
+ Request: httptest.NewRequest(http.MethodGet, target, http.NoBody),
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/environments/"), "/")[0]
+ queries.Store(id, r.URL.Query())
+ body, ok := bodies[id]
+ if !ok {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"detail":"environment offline"}`))
+ return
+ }
+ payload, err := json.Marshal(body)
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ _, _ = w.Write(payload)
+ }),
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ }), &queries
+}
+
+func TestAggregateVulnerabilitiesMergesEnvironments(t *testing.T) {
+ finding := func(id string, severity vulnerability.Severity, imageName string) vulnerability.VulnerabilityWithImage {
+ return vulnerability.VulnerabilityWithImage{
+ Vulnerability: vulnerability.Vulnerability{VulnerabilityID: id, PkgName: "pkg-" + id, InstalledVersion: "1.0", Severity: severity},
+ ImageID: "sha256:" + imageName,
+ ImageName: imageName,
+ }
+ }
+ bodies := map[string]any{
+ "a": base.Paginated[vulnerability.VulnerabilityWithImage]{Success: true, Data: []vulnerability.VulnerabilityWithImage{
+ finding("CVE-1", vulnerability.SeverityLow, "nginx"),
+ finding("CVE-2", vulnerability.SeverityCritical, "nginx"),
+ }, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 5}},
+ "b": base.Paginated[vulnerability.VulnerabilityWithImage]{Success: true, Data: []vulnerability.VulnerabilityWithImage{
+ finding("CVE-3", vulnerability.SeverityHigh, "redis"),
+ finding("CVE-4", vulnerability.SeverityUnknown, "redis"),
+ }, Pagination: base.PaginationResponse{TotalItems: 2}},
+ }
+ rowLabels := func(items []vulnerability.VulnerabilityWithImage) []string {
+ out := make([]string, 0, len(items))
+ for _, item := range items {
+ out = append(out, item.EnvironmentID+"/"+item.VulnerabilityID)
+ }
+ return out
+ }
+ svc := &Service{}
+
+ t.Run("orders severity by rank", func(t *testing.T) {
+ ctx, queries := vulnerabilityScopeContext(t, "/api/environments/a,b,c/vulnerabilities/all?sort=severity&order=asc", bodies)
+ ascending, err := svc.AggregateVulnerabilities(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "severity", Order: pagination.SortAsc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ require.Equal(t, []string{"b/CVE-4", "a/CVE-1", "b/CVE-3", "a/CVE-2"}, rowLabels(ascending.Data))
+
+ descending, err := svc.AggregateVulnerabilities(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "severity", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ require.Equal(t, []string{"a/CVE-2", "b/CVE-3", "a/CVE-1", "b/CVE-4"}, rowLabels(descending.Data))
+
+ require.Len(t, ascending.FailedEnvironments, 1)
+ require.Equal(t, "c", ascending.FailedEnvironments[0].Environment.ID)
+ for _, id := range []string{"a", "b"} {
+ query, ok := queries.Load(id)
+ require.True(t, ok)
+ require.Equal(t, "0", query.(url.Values).Get("start"))
+ require.Equal(t, "-1", query.(url.Values).Get("limit"))
+ }
+ })
+
+ t.Run("sorts by image name descending and pages", func(t *testing.T) {
+ ctx, _ := vulnerabilityScopeContext(t, "/api/environments/a,b,c/vulnerabilities/all", bodies)
+ body, err := svc.AggregateVulnerabilities(ctx, pagination.QueryParams{
+ SortParams: pagination.SortParams{Sort: "imageName", Order: pagination.SortDesc},
+ Params: pagination.Params{Start: 1, Limit: 2},
+ })
+ require.NoError(t, err)
+ require.True(t, body.Success)
+ require.Equal(t, []string{"b/CVE-4", "a/CVE-1"}, rowLabels(body.Data))
+ require.Equal(t, "Env b", body.Data[0].EnvironmentName)
+ require.Equal(t, int64(4), body.Pagination.TotalItems)
+ require.Equal(t, int64(7), body.Pagination.GrandTotalItems)
+ })
+
+ t.Run("sorts by environment", func(t *testing.T) {
+ ctx, _ := vulnerabilityScopeContext(t, "/api/environments/a,b,c/vulnerabilities/all?sort=environment", bodies)
+ body, err := svc.AggregateVulnerabilities(ctx, pagination.QueryParams{SortParams: pagination.SortParams{Sort: "environment", Order: pagination.SortDesc}, Params: pagination.Params{Limit: -1}})
+ require.NoError(t, err)
+ require.Equal(t, []string{"b/CVE-3", "b/CVE-4", "a/CVE-1", "a/CVE-2"}, rowLabels(body.Data))
+ })
+}
+
+func TestAggregateImageOptionsUnionsEnvironments(t *testing.T) {
+ ctx, queries := vulnerabilityScopeContext(t, "/api/environments/a,b,c/vulnerabilities/image-options?severity=high", map[string]any{
+ "a": base.ApiResponse[[]string]{Success: true, Data: []string{"nginx", "redis"}},
+ "b": base.ApiResponse[[]string]{Success: true, Data: []string{"alpine", "redis"}},
+ })
+
+ body, err := (&Service{}).AggregateImageOptions(ctx)
+ require.NoError(t, err)
+
+ require.True(t, body.Success)
+ require.Equal(t, []string{"alpine", "nginx", "redis"}, body.Data)
+ require.Len(t, body.FailedEnvironments, 1)
+ query, ok := queries.Load("a")
+ require.True(t, ok)
+ require.Equal(t, "high", query.(url.Values).Get("severity"))
+}
diff --git a/backend/internal/vulnerability/module.go b/backend/internal/vulnerability/module.go
index efe19184b..713d160c1 100644
--- a/backend/internal/vulnerability/module.go
+++ b/backend/internal/vulnerability/module.go
@@ -17,6 +17,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/vulnerability/children/query"
"github.com/getarcaneapp/arcane/backend/v2/internal/vulnerability/children/scan"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/tracing"
)
@@ -81,7 +82,7 @@ func (m *Module) RegisterRoutes(api huma.API, appCtx handlerutil.ActivityAppCont
service = &VulnerabilityService{}
}
featureMiddleware := huma.Middlewares{func(ctx huma.Context, next func(huma.Context)) {
- if service.settingsService != nil {
+ if _, scoped := httpx.EnvironmentScopeFromContext(ctx.Context()); !scoped && service.settingsService != nil {
if err := service.settingsService.RequireFeature(ctx.Context(), features.VulnerabilityManagement); err != nil {
ctx.SetHeader("Content-Type", "application/problem+json")
ctx.SetStatus(http.StatusForbidden)
diff --git a/backend/pkg/authz/permission_matcher.go b/backend/pkg/authz/permission_matcher.go
index 5f13c8c74..558edbcdd 100644
--- a/backend/pkg/authz/permission_matcher.go
+++ b/backend/pkg/authz/permission_matcher.go
@@ -23,7 +23,8 @@ import (
// The matcher is populated once during startup and is read-only afterwards, so
// concurrent Lookup calls during request serving need no synchronization.
type PermissionMatcher struct {
- routes []permRoute
+ routes []permRoute
+ aggregates map[string]struct{}
}
type permRoute struct {
@@ -104,6 +105,11 @@ func (m *PermissionMatcher) Lookup(method, suffixPath string) mo.Option[string]
return mo.None[string]()
}
+func (m *PermissionMatcher) SupportsEnvironmentAggregation(method, suffix string) bool {
+ _, ok := m.aggregates[strings.ToUpper(method)+" "+suffix]
+ return ok
+}
+
// pathSegmentsInternal splits a path into its non-empty segments, ignoring
// leading and trailing slashes.
func pathSegmentsInternal(p string) []string {
diff --git a/backend/pkg/authz/permission_matcher_huma.go b/backend/pkg/authz/permission_matcher_huma.go
index 245edb5f6..91f2f7936 100644
--- a/backend/pkg/authz/permission_matcher_huma.go
+++ b/backend/pkg/authz/permission_matcher_huma.go
@@ -16,6 +16,8 @@ const (
// to an agent.
MetaRequiredPermission = "arcane:requiredPermission"
+ MetaEnvironmentAggregate = "arcane:environmentAggregate"
+
envPathPrefixInternal = "/environments/{id}"
)
@@ -44,6 +46,12 @@ func (m *PermissionMatcher) CollectFromHumaAPI(humaAPI huma.API) {
if op == nil {
continue
}
+ if aggregate, _ := op.Metadata[MetaEnvironmentAggregate].(bool); aggregate && method == http.MethodGet {
+ if m.aggregates == nil {
+ m.aggregates = map[string]struct{}{}
+ }
+ m.aggregates[method+" "+suffix] = struct{}{}
+ }
if perm, localOk := requiredPermissionInternal(op).Get(); localOk {
m.Add(method, suffix, perm)
continue
diff --git a/backend/pkg/authz/permission_matcher_test.go b/backend/pkg/authz/permission_matcher_test.go
index a67c9c93f..0ad2af887 100644
--- a/backend/pkg/authz/permission_matcher_test.go
+++ b/backend/pkg/authz/permission_matcher_test.go
@@ -1,8 +1,12 @@
package authz
import (
+ "context"
+ "net/http"
"testing"
+ "github.com/danielgtaylor/huma/v2"
+ "github.com/danielgtaylor/huma/v2/humatest"
"github.com/stretchr/testify/require"
)
@@ -101,3 +105,33 @@ func TestPermissionMatcherUnmappedReturnsNotFound(t *testing.T) {
"expected unmapped path to return not found")
}
}
+
+func TestPermissionMatcherCollectsEnvironmentAggregation(t *testing.T) {
+ _, api := humatest.New(t)
+ handler := func(context.Context, *struct {
+ ID string `path:"id"`
+ },
+ ) (*struct{}, error) {
+ return &struct{}{}, nil
+ }
+ aggregate := map[string]any{MetaRequiredPermission: "containers:list", MetaEnvironmentAggregate: true}
+ huma.Register(api, huma.Operation{OperationID: "list", Method: http.MethodGet, Path: "/environments/{id}/containers", Metadata: aggregate}, handler)
+ huma.Register(api, huma.Operation{OperationID: "create", Method: http.MethodPost, Path: "/environments/{id}/projects", Metadata: aggregate}, handler)
+ huma.Register(api, huma.Operation{
+ OperationID: "images",
+ Method: http.MethodGet,
+ Path: "/environments/{id}/images",
+ Metadata: map[string]any{MetaRequiredPermission: "images:list"},
+ }, handler)
+
+ m := NewPermissionMatcher()
+ m.CollectFromHumaAPI(api)
+
+ require.True(t, m.SupportsEnvironmentAggregation(http.MethodGet, "/containers"))
+ require.False(t, m.SupportsEnvironmentAggregation(http.MethodPost, "/projects"), "only GET operations aggregate")
+ require.False(t, m.SupportsEnvironmentAggregation(http.MethodGet, "/images"), "operations must opt in")
+ require.False(t, m.SupportsEnvironmentAggregation(http.MethodGet, "/containers/abc"), "suffix must match exactly")
+ perm, ok := m.Lookup(http.MethodGet, "/containers").Get()
+ require.True(t, ok)
+ require.Equal(t, "containers:list", perm)
+}
diff --git a/backend/pkg/pagination/environment.go b/backend/pkg/pagination/environment.go
new file mode 100644
index 000000000..edd03f55a
--- /dev/null
+++ b/backend/pkg/pagination/environment.go
@@ -0,0 +1,60 @@
+package pagination
+
+import (
+ "cmp"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ kit "go.getarcane.app/kit/pkg"
+)
+
+type Environmental interface {
+ Environment() base.EnvironmentRef
+}
+
+type EnvironmentPage[T any] struct {
+ Environment base.EnvironmentRef
+ Items []T
+ TotalAvailable int64
+}
+
+func WithEnvironment[T Environmental](c Config[T]) Config[T] {
+ byEnvironment := func(a, b T) int {
+ left, right := a.Environment(), b.Environment()
+ return cmp.Or(cmp.Compare(left.Name, right.Name), cmp.Compare(left.ID, right.ID))
+ }
+ if len(c.SortBindings) == 0 {
+ c.SortBindings = []SortBinding[T]{{Key: "environment", Fn: byEnvironment, DescFn: reverSortFn(byEnvironment)}}
+ return c
+ }
+
+ first := c.SortBindings[0]
+ c.SortBindings = append(append([]SortBinding[T]{{
+ Key: first.Key,
+ Fn: chainSortFn(first.Fn, byEnvironment),
+ DescFn: kit.Ternary[SortOption[T]](first.DescFn != nil, chainSortFn(first.DescFn, byEnvironment), nil),
+ }}, c.SortBindings[1:]...), SortBinding[T]{
+ Key: "environment",
+ Fn: chainSortFn(byEnvironment, first.Fn),
+ DescFn: chainSortFn(reverSortFn(byEnvironment), first.Fn),
+ })
+ return c
+}
+
+func MergeEnvironmentPages[T Environmental, PT interface {
+ *T
+ SetEnvironment(base.EnvironmentRef)
+}](config Config[T], pages []EnvironmentPage[T], params QueryParams) ([]T, Response) {
+ all := make([]T, 0)
+ var totalAvailable int64
+ for _, page := range pages {
+ for _, item := range page.Items {
+ PT(&item).SetEnvironment(page.Environment)
+ all = append(all, item)
+ }
+ totalAvailable += page.TotalAvailable
+ }
+ if params.Limit > 0 && params.Start >= len(all) {
+ params.Start = max(len(all)-1, 0) / params.Limit * params.Limit
+ }
+ return WithEnvironment(config).OrderAndPaginate(all, params), BuildResponse(int64(len(all)), totalAvailable, params)
+}
diff --git a/backend/pkg/pagination/environment_test.go b/backend/pkg/pagination/environment_test.go
new file mode 100644
index 000000000..0f3ffe08a
--- /dev/null
+++ b/backend/pkg/pagination/environment_test.go
@@ -0,0 +1,74 @@
+package pagination
+
+import (
+ "cmp"
+ "testing"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ "github.com/stretchr/testify/require"
+)
+
+type environmentRow struct {
+ base.EnvironmentTag
+
+ Name string
+}
+
+func environmentRowConfig() Config[environmentRow] {
+ return Config[environmentRow]{SortBindings: []SortBinding[environmentRow]{
+ {Key: "name", Fn: func(a, b environmentRow) int { return cmp.Compare(a.Name, b.Name) }},
+ }}
+}
+
+func environmentRowPages() []EnvironmentPage[environmentRow] {
+ return []EnvironmentPage[environmentRow]{
+ {Environment: base.EnvironmentRef{ID: "b", Name: "Beta"}, Items: []environmentRow{{Name: "x"}, {Name: "a"}}, TotalAvailable: 5},
+ {Environment: base.EnvironmentRef{ID: "a", Name: "Alpha"}, Items: []environmentRow{{Name: "x"}, {Name: "c"}}, TotalAvailable: 3},
+ }
+}
+
+func environmentRowLabels(rows []environmentRow) []string {
+ labels := make([]string, 0, len(rows))
+ for _, row := range rows {
+ labels = append(labels, row.EnvironmentID+"/"+row.Name)
+ }
+ return labels
+}
+
+func TestMergeEnvironmentPagesBreaksTiesByEnvironment(t *testing.T) {
+ rows, response := MergeEnvironmentPages(environmentRowConfig(), environmentRowPages(), QueryParams{SortParams: SortParams{Sort: "name", Order: SortAsc}})
+
+ require.Equal(t, []string{"b/a", "a/c", "a/x", "b/x"}, environmentRowLabels(rows))
+ require.Equal(t, "Alpha", rows[1].EnvironmentName)
+ require.Equal(t, int64(4), response.TotalItems)
+ require.Equal(t, int64(8), response.GrandTotalItems)
+}
+
+func TestMergeEnvironmentPagesSortsByEnvironment(t *testing.T) {
+ ascending, _ := MergeEnvironmentPages(environmentRowConfig(), environmentRowPages(), QueryParams{SortParams: SortParams{Sort: "environment", Order: SortAsc}})
+ descending, _ := MergeEnvironmentPages(environmentRowConfig(), environmentRowPages(), QueryParams{SortParams: SortParams{Sort: "environment", Order: SortDesc}})
+
+ require.Equal(t, []string{"a/c", "a/x", "b/a", "b/x"}, environmentRowLabels(ascending))
+ require.Equal(t, []string{"b/a", "b/x", "a/c", "a/x"}, environmentRowLabels(descending))
+}
+
+func TestMergeEnvironmentPagesSlicesPage(t *testing.T) {
+ rows, response := MergeEnvironmentPages(environmentRowConfig(), environmentRowPages(), QueryParams{
+ SortParams: SortParams{Sort: "environment", Order: SortAsc},
+ Params: Params{Start: 2, Limit: 2},
+ })
+
+ require.Equal(t, []string{"b/a", "b/x"}, environmentRowLabels(rows))
+ require.Equal(t, int64(2), response.TotalPages)
+ require.Equal(t, 2, response.CurrentPage)
+}
+
+func TestWithEnvironmentLeavesInputConfigUnchanged(t *testing.T) {
+ config := environmentRowConfig()
+
+ merged := WithEnvironment(config)
+
+ require.Len(t, config.SortBindings, 1)
+ require.Len(t, merged.SortBindings, 2)
+ require.Equal(t, "environment", merged.SortBindings[1].Key)
+}
diff --git a/backend/pkg/utils/httpx/aggregate.go b/backend/pkg/utils/httpx/aggregate.go
new file mode 100644
index 000000000..fa9ed5166
--- /dev/null
+++ b/backend/pkg/utils/httpx/aggregate.go
@@ -0,0 +1,200 @@
+package httpx
+
+import (
+ "bytes"
+ "cmp"
+ "context"
+ "encoding/json/v2"
+ "errors"
+ "fmt"
+ "net/http"
+ "net/url"
+ "slices"
+ "strings"
+ "time"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ kit "go.getarcane.app/kit/pkg"
+ "golang.org/x/sync/errgroup"
+
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
+)
+
+const (
+ environmentsPathPrefix = "/api/environments/"
+ environmentRequestTimeout = 20 * time.Second
+ environmentResponseLimit = 64 << 20
+ environmentConcurrency = 8
+)
+
+var errEnvironmentResponseTooLarge = errors.New("response too large")
+
+type EnvironmentResolver func(ctx context.Context, id string) (base.EnvironmentRef, error)
+
+type EnvironmentScope struct {
+ IDs []string
+ Request *http.Request // clone of the outer request captured before auth; carries no scope
+ Handler http.Handler // the Echo instance, for in-process re-dispatch
+ Resolve EnvironmentResolver
+}
+
+type EnvironmentResult[R any] struct {
+ Environment base.EnvironmentRef
+ Value R
+}
+
+type EnvironmentCollection[R any] struct {
+ Results []EnvironmentResult[R]
+ Failures []base.EnvironmentFailure
+}
+
+type environmentScopeKey struct{}
+
+type environmentOutcome[R any] struct {
+ value R
+ err error
+}
+
+func WithEnvironmentScope(ctx context.Context, scope *EnvironmentScope) context.Context {
+ return context.WithValue(ctx, environmentScopeKey{}, scope)
+}
+
+func EnvironmentScopeFromContext(ctx context.Context) (*EnvironmentScope, bool) {
+ scope, _ := ctx.Value(environmentScopeKey{}).(*EnvironmentScope)
+ return scope, scope != nil
+}
+
+func CollectEnvironments[R any](ctx context.Context, overrides url.Values) (EnvironmentCollection[R], error) {
+ scope, ok := EnvironmentScopeFromContext(ctx)
+ if !ok {
+ return EnvironmentCollection[R]{}, errors.New("request has no environment scope")
+ }
+
+ collectCtx, cancel := context.WithTimeout(ctx, environmentRequestTimeout)
+ defer cancel()
+ outcomes := make([]environmentOutcome[R], len(scope.IDs))
+ var group errgroup.Group
+ group.SetLimit(environmentConcurrency)
+ for i, id := range scope.IDs {
+ group.Go(func() (workerErr error) {
+ defer func() { outcomes[i].err = workerErr }()
+ defer utils.RecoverToError(&workerErr, "environment aggregate worker", "environmentId", id)
+ outcomes[i].value, workerErr = dispatchEnvironment[R](collectCtx, scope, id, overrides)
+ return workerErr
+ })
+ }
+ _ = group.Wait()
+
+ collection := EnvironmentCollection[R]{Results: make([]EnvironmentResult[R], 0, len(scope.IDs))}
+ for i, id := range scope.IDs {
+ outcome := outcomes[i]
+ if outcome.err != nil {
+ collection.Failures = append(collection.Failures, base.EnvironmentFailure{Environment: base.EnvironmentRef{ID: id, Name: id}, Message: outcome.err.Error()})
+ continue
+ }
+ ref := base.EnvironmentRef{ID: id, Name: id}
+ if scope.Resolve != nil {
+ if resolved, err := scope.Resolve(ctx, id); err == nil {
+ ref = resolved
+ }
+ }
+ collection.Results = append(collection.Results, EnvironmentResult[R]{Environment: ref, Value: outcome.value})
+ }
+ return collection, nil
+}
+
+func dispatchEnvironment[R any](ctx context.Context, scope *EnvironmentScope, id string, overrides url.Values) (R, error) {
+ var value R
+ rest, ok := strings.CutPrefix(scope.Request.URL.EscapedPath(), environmentsPathPrefix)
+ if !ok {
+ return value, errors.New("request is not environment-scoped")
+ }
+ _, suffix, hasSuffix := strings.Cut(rest, "/")
+ target, err := url.Parse(environmentsPathPrefix + url.PathEscape(id) + kit.Ternary(hasSuffix, "/"+suffix, ""))
+ if err != nil {
+ return value, fmt.Errorf("invalid environment path: %w", err)
+ }
+
+ limit := time.Now().Add(environmentRequestTimeout)
+ deadline, hasDeadline := ctx.Deadline()
+ requestCtx, cancel := context.WithDeadline(scope.Request.Context(), kit.Ternary(hasDeadline && deadline.Before(limit), deadline, limit))
+ defer cancel()
+ stop := context.AfterFunc(ctx, cancel)
+ defer stop()
+
+ request := scope.Request.Clone(WithEnvironmentScope(requestCtx, nil))
+ request.Body, request.GetBody, request.ContentLength = http.NoBody, nil, 0
+ request.URL.Path, request.URL.RawPath = target.Path, target.RawPath
+ query := request.URL.Query()
+ for key, values := range overrides {
+ if len(values) == 0 {
+ query.Del(key)
+ continue
+ }
+ query[key] = slices.Clone(values)
+ }
+ if query.Get("sort") == "environment" {
+ query.Del("sort")
+ query.Del("order")
+ }
+ request.URL.RawQuery = query.Encode()
+ request.RequestURI = request.URL.RequestURI()
+ request.Header.Set("Accept", "application/json")
+ request.Header.Del("Accept-Encoding")
+ request.Header.Del("If-None-Match")
+ request.Header.Del("If-Modified-Since")
+
+ writer := &boundedResponseWriter{header: http.Header{}}
+ scope.Handler.ServeHTTP(writer, request)
+ status := cmp.Or(writer.status, http.StatusOK)
+ switch {
+ case writer.overflow:
+ return value, errEnvironmentResponseTooLarge
+ case requestCtx.Err() != nil && (writer.status == 0 || status >= http.StatusMultipleChoices):
+ return value, errors.New(kit.Ternary(errors.Is(requestCtx.Err(), context.DeadlineExceeded), "request timed out", "request cancelled"))
+ case writer.header.Get("Content-Encoding") != "":
+ return value, errors.New("unexpected encoded response")
+ case status < http.StatusOK || status >= http.StatusMultipleChoices:
+ var body struct {
+ Detail string `json:"detail"`
+ Title string `json:"title"`
+ Message string `json:"message"`
+ Error string `json:"error"`
+ Data struct {
+ Error string `json:"error"`
+ } `json:"data"`
+ }
+ _ = json.Unmarshal(writer.body.Bytes(), &body)
+ return value, errors.New(cmp.Or(body.Detail, body.Title, body.Message, body.Data.Error, body.Error, http.StatusText(status), fmt.Sprintf("status %d", status)))
+ }
+ if decodeErr := json.Unmarshal(writer.body.Bytes(), &value); decodeErr != nil {
+ return value, fmt.Errorf("invalid response: %w", decodeErr)
+ }
+ return value, nil
+}
+
+type boundedResponseWriter struct {
+ header http.Header
+ status int
+ body bytes.Buffer
+ overflow bool
+}
+
+func (w *boundedResponseWriter) Header() http.Header {
+ return w.header
+}
+
+func (w *boundedResponseWriter) WriteHeader(status int) {
+ w.status = cmp.Or(w.status, status)
+}
+
+func (w *boundedResponseWriter) Write(p []byte) (int, error) {
+ w.WriteHeader(http.StatusOK)
+ if w.overflow || w.body.Len()+len(p) > environmentResponseLimit {
+ w.overflow = true
+ return 0, errEnvironmentResponseTooLarge
+ }
+ return w.body.Write(p)
+}
+
+func (w *boundedResponseWriter) Flush() {}
diff --git a/backend/pkg/utils/httpx/aggregate_test.go b/backend/pkg/utils/httpx/aggregate_test.go
new file mode 100644
index 000000000..73904b5c9
--- /dev/null
+++ b/backend/pkg/utils/httpx/aggregate_test.go
@@ -0,0 +1,172 @@
+package httpx
+
+import (
+ "bytes"
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ "github.com/stretchr/testify/require"
+)
+
+type aggregateRow struct {
+ ID string `json:"id"`
+}
+
+func aggregateContext(t *testing.T, handler http.Handler, ids ...string) context.Context {
+ t.Helper()
+ request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/environments/"+strings.Join(ids, ",")+"/containers?search=web&sort=environment&order=desc&drop=1", nil)
+ request.Header.Set("Authorization", "Bearer token")
+ request.Header.Set("Accept-Encoding", "gzip")
+ request.Header.Set("If-None-Match", `"etag"`)
+ return WithEnvironmentScope(t.Context(), &EnvironmentScope{
+ IDs: ids,
+ Request: request,
+ Handler: handler,
+ Resolve: func(_ context.Context, id string) (base.EnvironmentRef, error) {
+ return base.EnvironmentRef{ID: id, Name: "Env " + id}, nil
+ },
+ })
+}
+
+func aggregateEnvironmentID(r *http.Request) string {
+ id, _, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, environmentsPathPrefix), "/")
+ return id
+}
+
+func TestCollectEnvironmentsRequiresScope(t *testing.T) {
+ _, err := CollectEnvironments[aggregateRow](t.Context(), nil)
+ require.Error(t, err)
+}
+
+func TestCollectEnvironmentsDispatchesEachEnvironmentInScopeOrder(t *testing.T) {
+ var mu sync.Mutex
+ requests := map[string]*http.Request{}
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := aggregateEnvironmentID(r)
+ if id == "b" {
+ time.Sleep(20 * time.Millisecond)
+ }
+ mu.Lock()
+ requests[id] = r
+ mu.Unlock()
+ _, _ = w.Write([]byte(`{"id":"` + id + `"}`))
+ })
+
+ collection, err := CollectEnvironments[aggregateRow](aggregateContext(t, handler, "b", "a", "c"), url.Values{"limit": {"-1"}, "drop": nil})
+
+ require.NoError(t, err)
+ require.Empty(t, collection.Failures)
+ require.Len(t, collection.Results, 3)
+ for i, id := range []string{"b", "a", "c"} {
+ require.Equal(t, base.EnvironmentRef{ID: id, Name: "Env " + id}, collection.Results[i].Environment)
+ require.Equal(t, id, collection.Results[i].Value.ID)
+ }
+
+ request := requests["a"]
+ require.Equal(t, "/api/environments/a/containers", request.URL.Path)
+ require.Equal(t, url.Values{"search": {"web"}, "limit": {"-1"}}, request.URL.Query())
+ require.Equal(t, request.URL.RequestURI(), request.RequestURI)
+ require.Equal(t, "Bearer token", request.Header.Get("Authorization"))
+ require.Equal(t, "application/json", request.Header.Get("Accept"))
+ require.Empty(t, request.Header.Get("Accept-Encoding"))
+ require.Empty(t, request.Header.Get("If-None-Match"))
+ _, scoped := EnvironmentScopeFromContext(request.Context())
+ require.False(t, scoped, "sub-requests must not fan out again")
+ _, hasDeadline := request.Context().Deadline()
+ require.True(t, hasDeadline)
+}
+
+func TestCollectEnvironmentsMapsFailures(t *testing.T) {
+ bodies := map[string]struct {
+ status int
+ body string
+ }{
+ "huma": {status: http.StatusUnprocessableEntity, body: `{"title":"Unprocessable Entity","detail":"invalid sort"}`},
+ "api": {status: http.StatusInternalServerError, body: `{"message":"docker unavailable"}`},
+ "proxy": {status: http.StatusBadGateway, body: `{"success":false,"data":{"error":"Edge agent is not connected"}}`},
+ "forbidden": {status: http.StatusForbidden, body: `{"success":false,"data":{"error":"denied"}}`},
+ "empty": {status: http.StatusServiceUnavailable},
+ "ok": {status: http.StatusOK, body: `{"id":"ok"}`},
+ }
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ response := bodies[aggregateEnvironmentID(r)]
+ w.WriteHeader(response.status)
+ _, _ = w.Write([]byte(response.body))
+ })
+
+ collection, err := CollectEnvironments[aggregateRow](aggregateContext(t, handler, "huma", "api", "proxy", "forbidden", "empty", "ok"), nil)
+
+ require.NoError(t, err)
+ require.Len(t, collection.Results, 1)
+ require.Equal(t, []base.EnvironmentFailure{
+ {Environment: base.EnvironmentRef{ID: "huma", Name: "huma"}, Message: "invalid sort"},
+ {Environment: base.EnvironmentRef{ID: "api", Name: "api"}, Message: "docker unavailable"},
+ {Environment: base.EnvironmentRef{ID: "proxy", Name: "proxy"}, Message: "Edge agent is not connected"},
+ {Environment: base.EnvironmentRef{ID: "forbidden", Name: "forbidden"}, Message: "denied"},
+ {Environment: base.EnvironmentRef{ID: "empty", Name: "empty"}, Message: "Service Unavailable"},
+ }, collection.Failures)
+}
+
+func TestCollectEnvironmentsReturnsFailuresWhenEveryEnvironmentFails(t *testing.T) {
+ handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ w.WriteHeader(http.StatusNotFound)
+ })
+
+ collection, err := CollectEnvironments[aggregateRow](aggregateContext(t, handler, "a", "b"), nil)
+
+ require.NoError(t, err)
+ require.NotNil(t, collection.Results)
+ require.Empty(t, collection.Results)
+ require.Len(t, collection.Failures, 2)
+ require.Equal(t, base.EnvironmentRef{ID: "a", Name: "a"}, collection.Failures[0].Environment)
+}
+
+func TestCollectEnvironmentsRejectsOversizedAndEncodedResponses(t *testing.T) {
+ chunk := bytes.Repeat([]byte(" "), 1<<20)
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch aggregateEnvironmentID(r) {
+ case "large":
+ for range environmentResponseLimit/len(chunk) + 1 {
+ if _, err := w.Write(chunk); err != nil {
+ return
+ }
+ }
+ case "encoded":
+ w.Header().Set("Content-Encoding", "gzip")
+ _, _ = w.Write([]byte{0x1f, 0x8b})
+ }
+ })
+
+ collection, err := CollectEnvironments[aggregateRow](aggregateContext(t, handler, "large", "encoded"), nil)
+
+ require.NoError(t, err)
+ require.Empty(t, collection.Results)
+ require.Len(t, collection.Failures, 2)
+ require.Equal(t, "response too large", collection.Failures[0].Message)
+ require.Equal(t, "unexpected encoded response", collection.Failures[1].Message)
+}
+
+func TestCollectEnvironmentsRecoversAbortedHandler(t *testing.T) {
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if aggregateEnvironmentID(r) == "aborted" {
+ panic(http.ErrAbortHandler)
+ }
+ _, _ = w.Write([]byte(`{"id":"ok"}`))
+ })
+
+ collection, err := CollectEnvironments[aggregateRow](aggregateContext(t, handler, "aborted", "ok"), nil)
+
+ require.NoError(t, err)
+ require.Len(t, collection.Results, 1)
+ require.Equal(t, "ok", collection.Results[0].Value.ID)
+ require.Len(t, collection.Failures, 1)
+ require.Equal(t, "aborted", collection.Failures[0].Environment.ID)
+ require.Contains(t, collection.Failures[0].Message, http.ErrAbortHandler.Error())
+}
diff --git a/backend/pkg/utils/httpx/request.go b/backend/pkg/utils/httpx/request.go
index e0bceab76..2e64fd434 100644
--- a/backend/pkg/utils/httpx/request.go
+++ b/backend/pkg/utils/httpx/request.go
@@ -3,6 +3,7 @@ package httpx
import (
"cmp"
"fmt"
+ "net"
"net/http"
"net/url"
"strconv"
@@ -10,6 +11,7 @@ import (
"time"
httpxtypes "github.com/getarcaneapp/arcane/types/v2/httpx"
+ kit "go.getarcane.app/kit/pkg"
)
type HeaderSetter interface {
@@ -146,6 +148,7 @@ func GetClientBaseURL(origin, forwardedHost, forwardedProto, host, appURL string
func NewHTTPClient(options httpxtypes.ClientOptions) *http.Client {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
+ DialContext: kit.Ternary(options.DialTimeout > 0, (&net.Dialer{Timeout: options.DialTimeout, KeepAlive: 30 * time.Second}).DialContext, nil),
MaxIdleConns: 100,
MaxIdleConnsPerHost: 32,
IdleConnTimeout: 90 * time.Second,
diff --git a/cli/internal/cmdutil/run.go b/cli/internal/cmdutil/run.go
index 0219a0d9e..98c51e4d6 100644
--- a/cli/internal/cmdutil/run.go
+++ b/cli/internal/cmdutil/run.go
@@ -1,12 +1,14 @@
package cmdutil
import (
+ "cmp"
"encoding/json/jsontext"
"encoding/json/v2"
"errors"
"fmt"
"net/http"
"net/url"
+ "slices"
"time"
"github.com/getarcaneapp/arcane/types/v2/base"
@@ -56,7 +58,15 @@ func RunList[T any](cmd *cobra.Command, c *client.Client, spec ListSpec[T]) erro
return fmt.Errorf("failed to list %s: %w", spec.Resource, err)
}
+ merged, _ := cmd.Context().Value(mergedScopeKey{}).(*mergedScope)
if JSONOutputEnabled(cmd) {
+ if merged != nil {
+ var envelope base.Paginated[jsontext.Value]
+ if unmarshalErr := json.Unmarshal(body, &envelope); unmarshalErr != nil {
+ return fmt.Errorf("failed to parse response: %w", unmarshalErr)
+ }
+ merged.failures = append(merged.failures, envelope.FailedEnvironments...)
+ }
return PrintRawJSON(body)
}
@@ -67,12 +77,24 @@ func RunList[T any](cmd *cobra.Command, c *client.Client, spec ListSpec[T]) erro
loading.Finish("", nil)
loading.Stop()
- rows := make([][]string, len(result.Data))
+ headers, rows := spec.Headers, make([][]string, len(result.Data))
+ if merged != nil {
+ merged.failures = append(merged.failures, result.FailedEnvironments...)
+ headers = slices.Concat([]string{"ENVIRONMENT"}, spec.Headers)
+ }
for i, item := range result.Data {
rows[i] = spec.Row(item)
+ if merged == nil {
+ continue
+ }
+ var ref base.EnvironmentRef
+ if tagged, ok := any(item).(interface{ Environment() base.EnvironmentRef }); ok {
+ ref = tagged.Environment()
+ }
+ rows[i] = slices.Concat([]string{cmp.Or(ref.Name, merged.names[cmp.Or(ref.ID, c.EnvID())], ref.ID)}, rows[i])
}
- return PrintTable(cmd, spec.Headers, rows, TableSummary{Resource: spec.Resource, Total: result.Pagination.TotalItems, Paginated: true})
+ return PrintTable(cmd, headers, rows, TableSummary{Resource: spec.Resource, Total: result.Pagination.TotalItems, Paginated: true})
}
// Spec describes a request with raw JSON data output or typed text rendering.
@@ -102,6 +124,13 @@ func Run[T any](cmd *cobra.Command, c *client.Client, spec Spec[T]) error {
}
loading.Finish("", nil)
loading.Stop()
+ merged, isMerged := cmd.Context().Value(mergedScopeKey{}).(*mergedScope)
+ if isMerged {
+ merged.failures = append(merged.failures, result.FailedEnvironments...)
+ }
+ if jsonMode && isMerged {
+ return PrintJSON(result)
+ }
if jsonMode {
if len(result.Data) == 0 {
return PrintJSON(nil)
diff --git a/cli/internal/cmdutil/scope.go b/cli/internal/cmdutil/scope.go
index d21337ce8..9bc17009c 100644
--- a/cli/internal/cmdutil/scope.go
+++ b/cli/internal/cmdutil/scope.go
@@ -7,24 +7,37 @@ import (
"encoding/json/jsontext"
"errors"
"fmt"
+ "net/http"
"slices"
+ "strconv"
"strings"
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ "github.com/getarcaneapp/arcane/types/v2/settings"
"github.com/spf13/cobra"
"github.com/getarcaneapp/arcane/cli/v2/internal/output"
"github.com/getarcaneapp/arcane/cli/v2/internal/prompt"
"github.com/getarcaneapp/arcane/cli/v2/internal/runtime"
+ "github.com/getarcaneapp/arcane/cli/v2/internal/types"
)
const (
ScopeRead = "read"
+ ScopeMerged = "merged"
ScopeFleet = "fleet"
ScopeDestination = "destination"
ScopeResource = "resource"
ScopeGlobal = "global"
)
+type mergedScopeKey struct{}
+
+type mergedScope struct {
+ names map[string]string
+ failures []base.EnvironmentFailure
+}
+
type environmentResult struct {
EnvironmentID string `json:"environmentId"`
EnvironmentName string `json:"environmentName"`
@@ -98,6 +111,9 @@ func RunScoped(cmd *cobra.Command, args []string, run func(*cobra.Command, []str
if scope == ScopeResource {
return run(cmd, args)
}
+ if scope == ScopeMerged {
+ return RunMerged(cmd, args, run)
+ }
if scope != ScopeDestination {
return RunEnvironments(cmd, args, run, scope == ScopeFleet)
}
@@ -124,6 +140,63 @@ func RunScoped(cmd *cobra.Command, args []string, run func(*cobra.Command, []str
return run(cmd, args)
}
+func RunMerged(cmd *cobra.Command, args []string, run func(*cobra.Command, []string) error) error {
+ app, _ := runtime.From(cmd.Context())
+ environments, discoveryErr := app.Environments(cmd.Context())
+ if discoveryErr != nil {
+ return discoveryErr
+ }
+ if len(environments) == 0 {
+ return RunEnvironments(cmd, args, run, false)
+ }
+ limit := 64
+ // The limit is at least 2, so only more environments need it.
+ if len(environments) > 2 {
+ c, clientErr := ClientFromCommand(cmd)
+ if clientErr != nil {
+ return clientErr
+ }
+ publicSettings, settingsErr := c.DoJSON[[]settings.PublicSetting](cmd.Context(), http.MethodGet, types.SettingsPublic("0"), nil)
+ if settingsErr != nil {
+ return fmt.Errorf("failed to read the environment limit: %w", settingsErr)
+ }
+ for _, setting := range publicSettings {
+ if parsed, parseErr := strconv.Atoi(setting.Value); setting.Key == "allEnvironmentsLimit" && parseErr == nil && parsed > 0 {
+ limit = parsed
+ }
+ }
+ }
+ merged := &mergedScope{names: make(map[string]string, len(environments))}
+ ids := make([]string, 0, min(limit, len(environments)))
+ for i, env := range environments {
+ merged.names[env.ID] = cmp.Or(env.Name, env.ID)
+ if i < limit {
+ ids = append(ids, env.ID)
+ continue
+ }
+ merged.failures = append(merged.failures, base.EnvironmentFailure{
+ Environment: base.EnvironmentRef{ID: env.ID, Name: merged.names[env.ID]},
+ Message: fmt.Sprintf("not included: more than %d environments are shown at once", limit),
+ })
+ }
+ commandContext := cmd.Context()
+ cmd.SetContext(context.WithValue(commandContext, mergedScopeKey{}, merged))
+ defer cmd.SetContext(commandContext)
+ app.SelectEnvironment(strings.Join(ids, ","))
+ defer app.SelectEnvironment("")
+ if runErr := run(cmd, args); runErr != nil {
+ return runErr
+ }
+ failures := make([]error, len(merged.failures))
+ for i, failure := range merged.failures {
+ failures[i] = fmt.Errorf("%s (%s): %s", cmp.Or(merged.names[failure.Environment.ID], failure.Environment.Name), failure.Environment.ID, failure.Message)
+ if !JSONOutputEnabled(cmd) {
+ _, _ = fmt.Fprintln(output.Stderr(), failures[i])
+ }
+ }
+ return errors.Join(failures...)
+}
+
// RunEnvironments executes a command sequentially and reports each environment's result.
func RunEnvironments(cmd *cobra.Command, args []string, run func(*cobra.Command, []string) error, mutation bool) error {
app, _ := runtime.From(cmd.Context())
diff --git a/cli/internal/cmdutil/scope_test.go b/cli/internal/cmdutil/scope_test.go
new file mode 100644
index 000000000..fcf77c496
--- /dev/null
+++ b/cli/internal/cmdutil/scope_test.go
@@ -0,0 +1,116 @@
+package cmdutil
+
+import (
+ "bytes"
+ "cmp"
+ "context"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "path/filepath"
+ "testing"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+ "github.com/spf13/cobra"
+ "github.com/stretchr/testify/require"
+
+ "github.com/getarcaneapp/arcane/cli/v2/internal/config"
+ "github.com/getarcaneapp/arcane/cli/v2/internal/output"
+ "github.com/getarcaneapp/arcane/cli/v2/internal/runtime"
+ "github.com/getarcaneapp/arcane/cli/v2/internal/types"
+)
+
+func TestRunScopedMerged(t *testing.T) {
+ type row struct {
+ base.EnvironmentTag
+
+ Name string `json:"name"`
+ }
+ const tagged = `{"success":true,"data":[{"environmentId":"0","environmentName":"Alpha","name":"web"},{"environmentId":"b","environmentName":"Beta","name":"db"}],"pagination":{"totalItems":7}}`
+ for _, tt := range []struct {
+ name, environments, body, scope, wantErr, limit string
+ json bool
+ wantOut []string
+ }{
+ {
+ name: "table names rows from their tags", environments: `[{"id":"b","name":"Beta","enabled":true},{"id":"0","name":"Alpha","enabled":true}]`,
+ body: tagged, scope: "/api/environments/0,b/rows",
+ wantOut: []string{"ENVIRONMENT", "Alpha", "web", "Beta", "db", "Showing: 2/7 rows"},
+ },
+ {
+ name: "single environment names untagged rows from the scope", environments: `[{"id":"0","name":"Alpha","enabled":true}]`,
+ body: `{"success":true,"data":[{"name":"web"}],"pagination":{"totalItems":1}}`, scope: "/api/environments/0/rows",
+ wantOut: []string{"ENVIRONMENT", "Alpha", "web"},
+ },
+ {
+ name: "failed environments are named and returned", environments: `[{"id":"0","name":"Alpha","enabled":true},{"id":"b","name":"Beta","enabled":true}]`,
+ body: `{"success":true,"data":[{"environmentId":"0","environmentName":"Alpha","name":"web"}],"pagination":{"totalItems":1},` +
+ `"failedEnvironments":[{"environment":{"id":"b","name":"b"},"message":"offline"}]}`,
+ scope: "/api/environments/0,b/rows", wantErr: "Beta (b): offline", wantOut: []string{"Alpha", "web"},
+ },
+ {
+ name: "environments beyond the limit are reported and not sent",
+ environments: `[{"id":"0","name":"Alpha","enabled":true},{"id":"b","name":"Beta","enabled":true},{"id":"c","name":"Gamma","enabled":true}]`,
+ body: tagged, scope: "/api/environments/0,b/rows", limit: "2",
+ wantErr: "Gamma (c): not included: more than 2 environments are shown at once", wantOut: []string{"Alpha", "web", "Beta", "db"},
+ },
+ {
+ name: "JSON passes the merged response through", environments: `[{"id":"0","name":"Alpha","enabled":true},{"id":"b","name":"Beta","enabled":true}]`,
+ body: tagged, scope: "/api/environments/0,b/rows", json: true,
+ wantOut: []string{`"environmentName": "Beta"`, `"totalItems": 7`},
+ },
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ var requests []string
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == types.Environments() {
+ _, _ = fmt.Fprintf(w, `{"success":true,"data":%s}`, tt.environments)
+ return
+ }
+ if r.URL.Path == types.SettingsPublic("0") {
+ _, _ = fmt.Fprintf(w, `[{"key":"allEnvironmentsLimit","type":"number","value":%q}]`, cmp.Or(tt.limit, "64"))
+ return
+ }
+ requests = append(requests, r.URL.Path)
+ _, _ = fmt.Fprint(w, tt.body)
+ }))
+ defer server.Close()
+ t.Setenv("HOME", t.TempDir())
+ require.NoError(t, config.SetConfigPath(filepath.Join(t.TempDir(), "arcanecli.yml")))
+ t.Cleanup(func() { require.NoError(t, config.SetConfigPath("")) })
+ cfg := config.DefaultConfig()
+ cfg.ServerURL, cfg.APIKey, cfg.Previews.AllEnvironments = server.URL, "test-key", true
+ require.NoError(t, config.Save(t.Context(), cfg))
+ app, appErr := runtime.New(runtime.Options{})
+ require.NoError(t, appErr)
+
+ var stdout, stderr bytes.Buffer
+ oldOut, oldErr := output.Stdout(), output.Stderr()
+ output.Configure(&stdout, &stderr, tt.json, true, "List rows")
+ defer output.Configure(oldOut, oldErr, false, true, "Loading")
+ cmd := &cobra.Command{Use: "list", Short: "List rows"}
+ SetScope(ScopeMerged, cmd)
+ cmd.Flags().Bool("json", tt.json, "")
+ cmd.SetContext(context.WithValue(t.Context(), runtime.AppContextKey, app))
+ runErr := RunScoped(cmd, nil, func(command *cobra.Command, _ []string) error {
+ c, err := ClientFromCommand(command)
+ require.NoError(t, err)
+ return RunList(command, c, ListSpec[row]{
+ Resource: "rows", Endpoint: types.Environment(c.EnvID()) + "/rows", Headers: []string{"NAME"},
+ Row: func(item row) []string { return []string{item.Name} },
+ })
+ })
+ if tt.wantErr == "" {
+ require.NoError(t, runErr)
+ } else {
+ require.ErrorContains(t, runErr, tt.wantErr)
+ require.Contains(t, stderr.String(), tt.wantErr)
+ }
+ require.Equal(t, []string{tt.scope}, requests)
+ for _, want := range tt.wantOut {
+ require.Contains(t, stdout.String(), want)
+ }
+ require.Equal(t, cfg.DefaultEnvironment, app.EnvID(), "selection is restored")
+ })
+ }
+}
diff --git a/cli/pkg/containers/cmd.go b/cli/pkg/containers/cmd.go
index 7f7509beb..df7f6cea6 100644
--- a/cli/pkg/containers/cmd.go
+++ b/cli/pkg/containers/cmd.go
@@ -71,7 +71,7 @@ func newContainerListCmd(updatesOnly bool) *cobra.Command {
includeHidden bool
}
command := &cobra.Command{
- Annotations: map[string]string{"arcane:scope": cmdutil.ScopeRead},
+ Annotations: map[string]string{"arcane:scope": cmdutil.ScopeMerged},
Use: kit.Ternary(updatesOnly, "updates", "list"),
Aliases: kit.Ternary(updatesOnly, []string(nil), []string{"ls"}),
Short: kit.Ternary(updatesOnly, "List containers with available updates", "List containers"),
diff --git a/cli/pkg/containers/cmd_test.go b/cli/pkg/containers/cmd_test.go
index 935244c17..35951cc27 100644
--- a/cli/pkg/containers/cmd_test.go
+++ b/cli/pkg/containers/cmd_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ kit "go.getarcane.app/kit/pkg"
"github.com/getarcaneapp/arcane/cli/v2/internal/testutil"
)
@@ -162,13 +163,7 @@ func TestContainersListSendsLimitAndStart(t *testing.T) {
if tc.all {
wantQuery = "limit=-1"
}
- require.Equal(t, wantQuery, queries["/api/environments/0/containers"])
- if tc.preview {
- require.Len(t, queries, 2)
- require.Equal(t, wantQuery, queries["/api/environments/remote/containers"])
- } else {
- require.Len(t, queries, 1)
- }
+ require.Equal(t, map[string]string{kit.Ternary(tc.preview, "/api/environments/0,remote/containers", "/api/environments/0/containers"): wantQuery}, queries)
require.True(t, json.Valid([]byte(out)))
require.Contains(t, out, "pagination")
})
@@ -217,6 +212,7 @@ func TestContainersListExplicitStartZeroSendsStartZero(t *testing.T) {
}
func TestContainersListTextShowsShowingSummary(t *testing.T) {
+ const localRow = `{"environmentId":"0","environmentName":"Local","id":"same-id","names":["/nginx"],"image":"nginx:latest","state":"running","status":"Up 1 hour"}`
for _, tc := range []struct {
name string
command []string
@@ -237,11 +233,14 @@ func TestContainersListTextShowsShowingSummary(t *testing.T) {
switch {
case r.URL.Path == "/api/environments":
_, _ = w.Write([]byte(`{"success":true,"data":[{"id":"remote","name":"Remote","enabled":true},{"id":"0","name":"Local","enabled":true}]}`))
- case tc.unavailable && strings.Contains(r.URL.Path, "/remote/"):
- w.WriteHeader(http.StatusForbidden)
- _, _ = w.Write([]byte(`{"success":false,"error":"resource denied"}`))
case tc.empty:
_, _ = w.Write([]byte(`{"success":true,"data":[],"pagination":{"totalItems":0}}`))
+ case r.URL.Path == "/api/environments/0,remote/containers" && tc.unavailable:
+ _, _ = w.Write([]byte(`{"success":true,"data":[` + localRow + `],"pagination":{"totalItems":26},` +
+ `"failedEnvironments":[{"environment":{"id":"remote","name":"remote"},"message":"resource denied"}]}`))
+ case r.URL.Path == "/api/environments/0,remote/containers":
+ remoteRow := strings.Replace(localRow, `"0","environmentName":"Local"`, `"remote","environmentName":"Remote"`, 1)
+ _, _ = w.Write([]byte(`{"success":true,"data":[` + localRow + `,` + remoteRow + `],"pagination":{"totalItems":52}}`))
case strings.HasSuffix(r.URL.Path, "/jobs"):
_, _ = w.Write([]byte(`{"jobs":[{"id":"same-id","name":"nginx"}],"isAgent":false,"durableRuns":true}`))
case strings.HasSuffix(r.URL.Path, "/settings"):
@@ -284,6 +283,7 @@ func TestContainersListTextShowsShowingSummary(t *testing.T) {
require.NotContains(t, out, "(remote)")
if tc.unavailable {
require.NotContains(t, out, "Remote")
+ require.Contains(t, diagnostics, "Remote (remote): resource denied")
return
}
require.Contains(t, out, "Remote")
@@ -414,6 +414,9 @@ func TestContainersListJSONContract(t *testing.T) {
w.WriteHeader(http.StatusForbidden)
_, _ = w.Write([]byte(`{"success":false,"error":"resource denied"}`))
return
+ case r.URL.Path == "/api/environments/0,remote/containers" && tc.unavailable:
+ _, _ = w.Write([]byte(`{"success":true,"data":[{"environmentId":"0","environmentName":"Local","id":"same-id"}],"pagination":{"totalItems":1},` +
+ `"failedEnvironments":[{"environment":{"id":"remote","name":"remote"},"message":"resource denied"}]}`))
case strings.HasSuffix(r.URL.Path, "/jobs"):
_, _ = w.Write([]byte(`{"jobs":[],"isAgent":false,"durableRuns":true}`))
case strings.HasSuffix(r.URL.Path, "/settings"):
@@ -443,6 +446,21 @@ func TestContainersListJSONContract(t *testing.T) {
} else {
require.NoError(t, runErr, diagnostics)
}
+ mu.Lock()
+ paths := append([]string(nil), requested...)
+ mu.Unlock()
+ if tc.args == nil && !tc.explicit && !tc.empty {
+ require.Equal(t, []string{"/api/environments", "/api/environments/0,remote/containers"}, paths)
+ require.True(t, json.Valid([]byte(out)), out)
+ if tc.unavailable {
+ require.ErrorContains(t, runErr, "Remote (remote): resource denied")
+ require.Contains(t, out, "failedEnvironments")
+ return
+ }
+ require.Contains(t, out, "counts")
+ require.Contains(t, out, "groups")
+ return
+ }
var result struct {
Success bool `json:"success"`
Environments []struct {
@@ -452,9 +470,6 @@ func TestContainersListJSONContract(t *testing.T) {
} `json:"environments"`
}
require.NoError(t, json.Unmarshal([]byte(out), &result), out)
- mu.Lock()
- paths := append([]string(nil), requested...)
- mu.Unlock()
if tc.explicit {
require.NotContains(t, paths, "/api/environments")
require.Empty(t, result.Environments)
@@ -471,14 +486,6 @@ func TestContainersListJSONContract(t *testing.T) {
require.Len(t, result.Environments, 2)
require.Equal(t, "0", result.Environments[0].ID)
require.Equal(t, "remote", result.Environments[1].ID)
- if tc.unavailable {
- require.NotEmpty(t, result.Environments[1].Error)
- require.Empty(t, result.Environments[1].Result)
- }
- if tc.args == nil {
- require.Contains(t, string(result.Environments[0].Result), "counts")
- require.Contains(t, string(result.Environments[0].Result), "groups")
- }
require.NotContains(t, out, "Selected")
})
}
diff --git a/cli/pkg/images/cmd.go b/cli/pkg/images/cmd.go
index ebfe3ec8f..9cd50ddd3 100644
--- a/cli/pkg/images/cmd.go
+++ b/cli/pkg/images/cmd.go
@@ -63,7 +63,7 @@ func newListCmd() *cobra.Command {
updatesFilter string
}
command := &cobra.Command{
- Annotations: map[string]string{"arcane:scope": cmdutil.ScopeRead},
+ Annotations: map[string]string{"arcane:scope": cmdutil.ScopeMerged},
Use: "list",
Aliases: []string{"ls"},
Short: "List images",
@@ -341,7 +341,7 @@ func newPruneCmd() *cobra.Command {
func newCountsCmd() *cobra.Command {
return &cobra.Command{
- Annotations: map[string]string{"arcane:scope": cmdutil.ScopeRead},
+ Annotations: map[string]string{"arcane:scope": cmdutil.ScopeMerged},
Use: "counts",
Short: "Get image usage counts",
SilenceUsage: true,
diff --git a/cli/pkg/images/extra.go b/cli/pkg/images/extra.go
index 1abb075ea..c44dad4b3 100644
--- a/cli/pkg/images/extra.go
+++ b/cli/pkg/images/extra.go
@@ -411,7 +411,7 @@ func newBuildsCmd() *cobra.Command {
provider string
}
command := &cobra.Command{
- Annotations: map[string]string{"arcane:scope": cmdutil.ScopeRead},
+ Annotations: map[string]string{"arcane:scope": cmdutil.ScopeMerged},
Use: "builds",
Short: "List image build history",
SilenceUsage: true,
diff --git a/cli/pkg/networks/cmd.go b/cli/pkg/networks/cmd.go
index c8f3ac31a..cba47014a 100644
--- a/cli/pkg/networks/cmd.go
+++ b/cli/pkg/networks/cmd.go
@@ -100,7 +100,7 @@ func newListCmd() *cobra.Command {
cmd.Flags().BoolVarP(&opts.allFlag, "all", "a", false, cmdutil.AllFlagUsage)
cmd.Flags().BoolVar(&opts.inUseOnlyFlag, "inuse", false, "Only show networks currently in use")
cmd.Flags().BoolVar(&opts.unusedOnlyFlag, "unused", false, "Only show networks not in use")
- cmdutil.SetScope(cmdutil.ScopeRead, cmd)
+ cmdutil.SetScope(cmdutil.ScopeMerged, cmd)
cmd.PreRunE = func(cmd *cobra.Command, _ []string) error { return cmdutil.ExclusiveFlags(cmd, "inuse", "unused") }
return cmd
}
diff --git a/cli/pkg/projects/actions.go b/cli/pkg/projects/actions.go
index c759c0ce5..f1afb0e19 100644
--- a/cli/pkg/projects/actions.go
+++ b/cli/pkg/projects/actions.go
@@ -49,7 +49,7 @@ func newTagsCmd() *cobra.Command {
})
},
}
- cmdutil.SetScope(cmdutil.ScopeRead, cmd)
+ cmdutil.SetScope(cmdutil.ScopeMerged, cmd)
return cmd
}
diff --git a/cli/pkg/projects/cmd.go b/cli/pkg/projects/cmd.go
index da7bcfce6..7905c9fa4 100644
--- a/cli/pkg/projects/cmd.go
+++ b/cli/pkg/projects/cmd.go
@@ -76,7 +76,7 @@ func newListCmd() *cobra.Command {
cmd.Flags().StringVar(&opts.projectsStatusFilter, "status", "", "Filter by status (comma-separated: running, stopped, partially running)")
cmdutil.EnumVarP(cmd, cmd.Flags(), &opts.projectsArchivedFilter, "archived", "", "", "Archived filter", "true", "false", "all")
cmd.Flags().StringVar(&opts.projectsLabelFilter, "label", "", "Filter by container label (key or key=value)")
- cmdutil.SetScope(cmdutil.ScopeRead, cmd)
+ cmdutil.SetScope(cmdutil.ScopeMerged, cmd)
return cmd
}
@@ -92,7 +92,7 @@ func newUpdatesCmd() *cobra.Command {
cmd.Flags().IntVarP(&opts.limitFlag, "limit", "n", 20, "Number of projects to show")
cmd.Flags().IntVar(&opts.startFlag, "start", 0, cmdutil.StartFlagUsage)
cmd.Flags().BoolVarP(&opts.allFlag, "all", "a", false, cmdutil.AllFlagUsage)
- cmdutil.SetScope(cmdutil.ScopeRead, cmd)
+ cmdutil.SetScope(cmdutil.ScopeMerged, cmd)
return cmd
}
@@ -626,7 +626,7 @@ func newCountsCmd() *cobra.Command {
})
},
}
- cmdutil.SetScope(cmdutil.ScopeRead, cmd)
+ cmdutil.SetScope(cmdutil.ScopeMerged, cmd)
return cmd
}
diff --git a/cli/pkg/volumes/cmd.go b/cli/pkg/volumes/cmd.go
index 7e15e1999..16ebaae97 100644
--- a/cli/pkg/volumes/cmd.go
+++ b/cli/pkg/volumes/cmd.go
@@ -103,7 +103,7 @@ func newListCmd() *cobra.Command {
cmd.Flags().BoolVar(&opts.inUseOnlyFlag, "inuse", false, "Only show volumes currently in use")
cmd.Flags().BoolVar(&opts.unusedOnlyFlag, "unused", false, "Only show volumes not in use")
cmd.Flags().BoolVar(&opts.includeFlag, "include-internal", false, "Include Arcane-internal volumes")
- cmdutil.SetScope(cmdutil.ScopeRead, cmd)
+ cmdutil.SetScope(cmdutil.ScopeMerged, cmd)
cmd.PreRunE = func(cmd *cobra.Command, _ []string) error { return cmdutil.ExclusiveFlags(cmd, "inuse", "unused") }
return cmd
}
@@ -261,7 +261,7 @@ func newSizesCmd() *cobra.Command {
})
},
}
- cmdutil.SetScope(cmdutil.ScopeRead, cmd)
+ cmdutil.SetScope(cmdutil.ScopeMerged, cmd)
return cmd
}
diff --git a/cli/pkg/vulnerabilities/cmd.go b/cli/pkg/vulnerabilities/cmd.go
index 5db20ba70..83b91f385 100644
--- a/cli/pkg/vulnerabilities/cmd.go
+++ b/cli/pkg/vulnerabilities/cmd.go
@@ -101,7 +101,7 @@ func newListCmd() *cobra.Command {
image string
}
command := &cobra.Command{
- Annotations: map[string]string{"arcane:scope": cmdutil.ScopeRead},
+ Annotations: map[string]string{"arcane:scope": cmdutil.ScopeMerged},
Use: "list",
Aliases: []string{"ls"},
Short: "List vulnerabilities across all scanned images",
diff --git a/frontend/messages/en.json b/frontend/messages/en.json
index b9b332360..bf55435b2 100644
--- a/frontend/messages/en.json
+++ b/frontend/messages/en.json
@@ -3964,6 +3964,10 @@
"all_environments_confirm_note": "This runs on all {count} environments.",
"all_environments_count": "{count} environments",
"all_environments_unavailable": "Not shown, these environments could not be loaded: {environments}",
+ "all_environments_not_included": "not included: more than {limit} environments are shown at once",
+ "all_environments_limit": "Environment limit",
+ "all_environments_limit_description": "Maximum environments a list includes, for every user. Environments beyond the limit are reported as not included.",
+ "all_environments_limit_help": "Enter a whole number from 2 to 256.",
"environment_colors": "Environment colors",
"environment_colors_description": "Colors for environment names in resource lists while the all environments preview is on.",
"environment_color_in_use": "That color is already used by another environment",
diff --git a/frontend/src/lib/components/dialogs/image-registry-search-dialog.svelte b/frontend/src/lib/components/dialogs/image-registry-search-dialog.svelte
index a87ed7339..e8df8a7c5 100644
--- a/frontend/src/lib/components/dialogs/image-registry-search-dialog.svelte
+++ b/frontend/src/lib/components/dialogs/image-registry-search-dialog.svelte
@@ -119,6 +119,7 @@
<EnvironmentDestinationSelect
bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
permission="images:pull"
+ variant="field"
/>
<div class="grid gap-3 sm:grid-cols-content-36-action">
<div class="space-y-2">
diff --git a/frontend/src/lib/components/form/environment-destination-select.svelte b/frontend/src/lib/components/form/environment-destination-select.svelte
index be40ff04c..74724135a 100644
--- a/frontend/src/lib/components/form/environment-destination-select.svelte
+++ b/frontend/src/lib/components/form/environment-destination-select.svelte
@@ -1,10 +1,16 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/state';
+ import { Select as SelectPrimitive } from 'bits-ui';
+ import { ArcaneButton } from '#lib/components/arcane-button/index.js';
import SelectWithLabel from '#lib/components/form/select-with-label.svelte';
+ import { Label } from '#lib/components/ui/label/index.js';
+ import * as Select from '#lib/components/ui/select/index.js';
+ import { ArrowDownIcon, EdgeConnectionIcon, EnvironmentsIcon, RemoteEnvironmentIcon } from '#lib/icons/index.js';
import { m } from '#lib/paraglide/messages.js';
- import { environmentStore, type EnvironmentRef } from '#lib/stores/environment.store.svelte.js';
+ import { environmentColor } from '#lib/stores/environment-colors.store.svelte.js';
+ import { environmentStore, LOCAL_DOCKER_ENVIRONMENT_ID, type EnvironmentRef } from '#lib/stores/environment.store.svelte.js';
import { hasPermission } from '#lib/utils/auth.js';
// Picks one environment while several are shown. A bound `value` is a create's destination and never changes
@@ -13,15 +19,22 @@
let {
value = $bindable(),
permission,
- environments
- }: { value?: string; permission?: string; environments?: EnvironmentRef[] } = $props();
+ environments,
+ variant = 'toolbar',
+ size = 'default'
+ }: {
+ value?: string;
+ permission?: string;
+ environments?: EnvironmentRef[];
+ variant?: 'toolbar' | 'field';
+ size?: 'sm' | 'default';
+ } = $props();
const selectsEnvironment = $derived(value === undefined);
- const options = $derived(
- (environments ?? environmentStore.shown)
- .filter((env) => !permission || hasPermission(permission, env.id))
- .map((env) => ({ value: env.id, label: env.name }))
+ const permitted = $derived(
+ (environments ?? environmentStore.shown).filter((env) => !permission || hasPermission(permission, env.id))
);
+ const options = $derived(permitted.map((env) => ({ value: env.id, label: env.name })));
// Routes under /environments/[id] show that environment, which can differ from the selected one.
const routeEnvironmentId = $derived(page.route.id?.includes('/environments/[id]') ? page.params.id : undefined);
const pageEnvironmentId = $derived(
@@ -29,6 +42,16 @@
options[0]?.value ??
''
);
+ const currentId = $derived(selectsEnvironment ? pageEnvironmentId : (value ?? ''));
+ const current = $derived(permitted.find((env) => env.id === currentId));
+
+ function pick(environmentId: string) {
+ if (selectsEnvironment) {
+ selectEnvironment(environmentId);
+ return;
+ }
+ value = environmentId;
+ }
function selectEnvironment(environmentId: string) {
const environment = environmentStore.available.find((env) => env.id === environmentId);
@@ -44,26 +67,60 @@
}
void environmentStore.setEnvironment(environment);
}
+
+ function environmentIcon(environmentId: string) {
+ if (environmentId === LOCAL_DOCKER_ENVIRONMENT_ID) return EnvironmentsIcon;
+ if (environmentStore.available.find((env) => env.id === environmentId)?.isEdge) return EdgeConnectionIcon;
+ return RemoteEnvironmentIcon;
+ }
</script>
+{#snippet environmentGlyph(environmentId: string)}
+ {@const Icon = environmentIcon(environmentId)}
+ <span
+ class="environment-glyph inline-flex shrink-0"
+ style:--environment-color={environmentColor(environmentId) ?? 'var(--muted-foreground)'}
+ >
+ <Icon class="size-4" />
+ </span>
+{/snippet}
+
{#if environmentStore.shown.length > 1}
- {#if selectsEnvironment}
+ {#if variant === 'toolbar'}
+ <Label for="page-environment" class="sr-only">{m.resource_environment_cap()}</Label>
+ <Select.Root type="single" bind:value={() => currentId, pick}>
+ <SelectPrimitive.Trigger id="page-environment">
+ {#snippet child({ props })}
+ <ArcaneButton {...props} action="base" {size} title={current?.name} class="shrink-0">
+ {#if current}
+ {@render environmentGlyph(current.id)}
+ <span class="hidden max-w-48 truncate sm:inline">{current.name}</span>
+ {/if}
+ <ArrowDownIcon class="size-3.5 opacity-60" />
+ </ArcaneButton>
+ {/snippet}
+ </SelectPrimitive.Trigger>
+ <Select.Content class="w-auto max-w-72 min-w-48">
+ {#each permitted as env (env.id)}
+ <Select.Item value={env.id} label={env.name}>
+ {@render environmentGlyph(env.id)}
+ <span class="truncate">{env.name}</span>
+ </Select.Item>
+ {/each}
+ </Select.Content>
+ </Select.Root>
+ {:else}
<SelectWithLabel
- id="page-environment"
+ id="destination-environment"
label={m.resource_environment_cap()}
- bind:value={() => pageEnvironmentId, selectEnvironment}
+ bind:value={() => currentId, pick}
{options}
- hideLabel
- compact
/>
- {:else}
- <div class="max-w-sm">
- <SelectWithLabel
- id="destination-environment"
- label={m.resource_environment_cap()}
- bind:value={() => value ?? '', (next) => (value = next)}
- {options}
- />
- </div>
{/if}
{/if}
+
+<style>
+ .environment-glyph {
+ color: color-mix(in srgb, var(--environment-color) 75%, var(--foreground));
+ }
+</style>
diff --git a/frontend/src/lib/components/sheets/create-network-sheet.svelte b/frontend/src/lib/components/sheets/create-network-sheet.svelte
index 21ef2ccd8..e5ac1f63e 100644
--- a/frontend/src/lib/components/sheets/create-network-sheet.svelte
+++ b/frontend/src/lib/components/sheets/create-network-sheet.svelte
@@ -163,6 +163,7 @@
<EnvironmentDestinationSelect
bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
permission="networks:create"
+ variant="field"
/>
<div class="space-y-2">
<Label for="network-name">{m.network_name_label()}</Label>
diff --git a/frontend/src/lib/components/sheets/create-volume-sheet.svelte b/frontend/src/lib/components/sheets/create-volume-sheet.svelte
index c2e420109..30acbe9ee 100644
--- a/frontend/src/lib/components/sheets/create-volume-sheet.svelte
+++ b/frontend/src/lib/components/sheets/create-volume-sheet.svelte
@@ -92,6 +92,7 @@
<EnvironmentDestinationSelect
bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
permission="volumes:create"
+ variant="field"
/>
<FormInput
label={m.volume_name_label()}
diff --git a/frontend/src/lib/components/sheets/image-pull-sheet.svelte b/frontend/src/lib/components/sheets/image-pull-sheet.svelte
index 86bd910fd..3eb76727e 100644
--- a/frontend/src/lib/components/sheets/image-pull-sheet.svelte
+++ b/frontend/src/lib/components/sheets/image-pull-sheet.svelte
@@ -98,6 +98,7 @@
<EnvironmentDestinationSelect
bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
permission="images:pull"
+ variant="field"
/>
<FormInput
label={m.images_image_name_label()}
diff --git a/frontend/src/lib/layouts/resource-page-layout.svelte b/frontend/src/lib/layouts/resource-page-layout.svelte
index c9341c164..567d0f240 100644
--- a/frontend/src/lib/layouts/resource-page-layout.svelte
+++ b/frontend/src/lib/layouts/resource-page-layout.svelte
@@ -89,10 +89,12 @@
</div>
</div>
- {#if environmentPicker}
- <EnvironmentDestinationSelect />
- {/if}
- <ActionButtonGroup buttons={actionButtons} />
+ <div class="flex min-w-0 items-center justify-end gap-2">
+ {#if environmentPicker}
+ <EnvironmentDestinationSelect />
+ {/if}
+ <ActionButtonGroup buttons={actionButtons} />
+ </div>
</header>
<div class="md:-mx-5 md:-mb-5 md:px-2 md:pb-2">
diff --git a/frontend/src/lib/layouts/settings-page-layout.svelte b/frontend/src/lib/layouts/settings-page-layout.svelte
index 9532bc16a..da7deface 100644
--- a/frontend/src/lib/layouts/settings-page-layout.svelte
+++ b/frontend/src/lib/layouts/settings-page-layout.svelte
@@ -87,7 +87,7 @@
<div class="flex flex-1 items-center justify-end gap-2">
{#if environmentPicker}
- <EnvironmentDestinationSelect />
+ <EnvironmentDestinationSelect size={pageType === 'form' ? 'sm' : 'default'} />
{/if}
{#if showReadOnlyTag}
diff --git a/frontend/src/lib/services/api-service.ts b/frontend/src/lib/services/api-service.ts
index 4d0d6cea6..82357ee6b 100644
--- a/frontend/src/lib/services/api-service.ts
+++ b/frontend/src/lib/services/api-service.ts
@@ -15,6 +15,7 @@ export interface APIRequestConfig {
signal?: AbortSignal;
suppressAccessDeniedToast?: boolean;
timeout?: number | false;
+ onFailedEnvironments?: (failures: { environment: { id: string; name: string }; message: string }[]) => void;
}
interface InternalRequestConfig extends APIRequestConfig {
@@ -311,6 +312,7 @@ class APIClient {
const response = await this.client(requestUrl, options);
const parsed = upperMethod === 'HEAD' ? undefined : await parseResponseBody(response.clone(), config.responseType);
if (session?.aborted) throw new SessionCancelledError();
+ if (Array.isArray(parsed?.failedEnvironments)) config.onFailedEnvironments?.(parsed.failedEnvironments);
return {
data: parsed as T,
headers: response.headers,
diff --git a/frontend/src/lib/types/settings.ts b/frontend/src/lib/types/settings.ts
index 8b003b285..80c998e08 100644
--- a/frontend/src/lib/types/settings.ts
+++ b/frontend/src/lib/types/settings.ts
@@ -74,6 +74,7 @@ export type Settings = {
developmentBrandingEnabled: boolean;
apnsEnabled?: boolean;
avatarMaxUploadSizeMb: number;
+ allEnvironmentsLimit: number;
uiConfigDisabled: boolean;
defaultShell: string;
dockerHost: string;
diff --git a/frontend/src/lib/utils/environments.ts b/frontend/src/lib/utils/environments.ts
index a32b3b04d..5f4010ad6 100644
--- a/frontend/src/lib/utils/environments.ts
+++ b/frontend/src/lib/utils/environments.ts
@@ -1,9 +1,9 @@
import { error } from '@sveltejs/kit';
import { toast } from 'svelte-sonner';
-import { TABLE_PAGE_SIZE_ALL } from '#lib/constants/table-pagination.js';
import { m } from '#lib/paraglide/messages.js';
import type { APIRequestConfig } from '#lib/services/api-service.js';
+import settingsStore from '#lib/stores/config-store.svelte.js';
import {
allEnvironmentsPreview,
environmentStore,
@@ -15,7 +15,7 @@ import { extractApiErrorMessage } from '#lib/utils/api.js';
import { hasPermission } from '#lib/utils/auth.js';
import { tryCatch } from '#lib/utils/try-catch.js';
-/** A row tagged by fetchAcrossEnvironments; `environmentTagged` tells it apart from DTOs that carry an environment. */
+/** A row tagged by environment; `environmentTagged` tells it apart from DTOs that carry an environment. */
export type EnvironmentRow<T> = T & { environmentId: string; environmentName: string; environmentTagged: true };
export type EnvironmentFailure = { environment: EnvironmentRef; message: string };
@@ -36,7 +36,7 @@ export function rowKey(row: { id: string; environmentId?: string; environmentTag
return row.environmentTagged && row.environmentId !== undefined ? `${row.environmentId}${KEY_SEPARATOR}${row.id}` : row.id;
}
-export function tagEnvironment<T extends object>(row: T, environment: EnvironmentRef): EnvironmentRow<T> {
+function tagEnvironment<T extends object>(row: T, environment: EnvironmentRef): EnvironmentRow<T> {
return { ...row, environmentId: environment.id, environmentName: environment.name, environmentTagged: true };
}
@@ -161,23 +161,52 @@ export async function settleAcrossEnvironments<T>(
return values;
}
-function sumNumbers<C extends object>(values: C[]): C {
- const total: Record<string, unknown> = { ...values[0] };
- for (const value of values.slice(1)) {
- for (const [key, entry] of Object.entries(value)) {
- if (typeof entry === 'number') total[key] = Number(total[key] ?? 0) + entry;
- }
+export async function readAcrossEnvironments<T>(
+ environments: EnvironmentRef[],
+ run: (scope: string, config: APIRequestConfig) => Promise<T>,
+ reportInline = environments.length > 1
+): Promise<{ value: T; failures: EnvironmentFailure[] }> {
+ if (environments.length === 0) throw new Error('readAcrossEnvironments needs at least one environment');
+ const limit = Number(settingsStore.current?.allEnvironmentsLimit) || 64;
+ const included = environments.slice(0, limit);
+ const excluded = environments
+ .slice(limit)
+ .map((environment) => ({ environment, message: m.all_environments_not_included({ limit }) }));
+ let reported: EnvironmentFailure[] = [];
+ const result = await tryCatch(
+ run(included.map((env) => env.id).join(','), {
+ suppressAccessDeniedToast: reportInline,
+ onFailedEnvironments: (failures) => {
+ reported = failures;
+ }
+ })
+ );
+ if (result.error !== null) {
+ if (!reportInline || included.length > 1) throw result.error;
+ throw new Error(`${included[0]!.name}: ${extractApiErrorMessage(result.error)}`);
}
- return total as C;
+ const failures = reported.map(({ environment, message }) => ({
+ environment: included.find((env) => env.id === environment.id) ?? environment,
+ message
+ }));
+ if (failures.length > 0 && failures.length === included.length) {
+ throw new Error(failures.map(({ environment, message }) => `${environment.name}: ${message}`).join('\n'));
+ }
+ return { value: result.data, failures: [...failures, ...excluded] };
}
-/** Sums numeric count fields across environments; failing environments are reported, not counted. */
+export function environmentRows<T extends object>(rows: T[], environments: EnvironmentRef[]): EnvironmentRow<T>[] {
+ if (environments.length === 1) return rows.map((row) => tagEnvironment(row, environments[0]!));
+ return rows.map((row) => ({ ...(row as EnvironmentRow<T>), environmentTagged: true }));
+}
+
+/** Count fields across environments, summed by the server; failing environments are reported, not counted. */
export async function sumAcrossEnvironments<C extends object>(
environments: EnvironmentRef[],
- fetch: (environmentId: string, config: APIRequestConfig) => Promise<C>
+ fetch: (scope: string, config: APIRequestConfig) => Promise<C>
): Promise<C & { failedEnvironments: EnvironmentFailure[] }> {
- const { values, failures } = await collectAcrossEnvironments(environments, fetch);
- return { ...sumNumbers(values.map((result) => result.value)), failedEnvironments: failures };
+ const { value, failures } = await readAcrossEnvironments(environments, fetch);
+ return { ...value, failedEnvironments: failures };
}
/** Adds failures from related reads, such as counts or tags, to a list's inline alert, once per environment. */
@@ -195,43 +224,11 @@ export function withFailedEnvironments<P extends { failedEnvironments?: Environm
};
}
-/** Orders two sort values, comparing arrays element by element; missing values always sort last, whatever the direction. */
-function compareSortValues(left: unknown, right: unknown, sign: number): number {
- const leftMissing = left === undefined || left === null;
- const rightMissing = right === undefined || right === null;
- if (leftMissing || rightMissing) return Number(leftMissing) - Number(rightMissing);
- if (Array.isArray(left) && Array.isArray(right)) {
- return left.reduce<number>((result, value, index) => result || compareSortValues(value, right[index], sign), 0);
- }
- if (typeof left === 'number' && typeof right === 'number') return sign * (left - right);
- // The backend sorts true first when ascending.
- if (typeof left === 'boolean' && typeof right === 'boolean') return sign * (Number(right) - Number(left));
- return sign * String(left).localeCompare(String(right), undefined, { numeric: true });
-}
-
-/** Splits units into pages the way the backend pages groups: a unit is never split, so the one crossing the limit finishes its page. */
-function pageUnits<T>(units: T[][], limit: number): T[][][] {
- if (limit <= 0) return [units];
- const pages: T[][][] = [[]];
- let count = 0;
- for (const unit of units) {
- pages.at(-1)!.push(unit);
- count += unit.length;
- if (count >= limit) {
- pages.push([]);
- count = 0;
- }
- }
- if (pages.length > 1 && pages.at(-1)!.length === 0) pages.pop();
- return pages;
-}
-
/**
- * Runs the same list request per environment and tags each row with its environment. With one
- * environment the request passes through; with several, every row is fetched, merged, sorted
- * and sliced to the requested page. `sortValue` reads computed sort columns; it defaults to the
- * row field named by the column. Same-named groups merge across environments and are paged whole.
- * An `environment` filter narrows the environments queried.
+ * Runs a list request for several environments as one request, which the server fans out, merges, sorts
+ * and pages, so rows of different environments interleave by the requested sort and same-named groups
+ * merge. With one environment the request passes through. An `environment` filter narrows the
+ * environments queried; sorting by environment is only sent when several are.
*/
export async function fetchAcrossEnvironments<
P extends Page<T>,
@@ -240,94 +237,28 @@ export async function fetchAcrossEnvironments<
>(
environments: EnvironmentRef[],
options: O,
- fetch: (environmentId: string, options: O, config: APIRequestConfig) => Promise<P>,
- sortValue: (row: T, column: string) => unknown = (row, column) => row[column as keyof T]
+ fetch: (scope: string, options: O, config: APIRequestConfig) => Promise<P>
): Promise<EnvironmentPage<P>> {
const { [ENVIRONMENT_FILTER]: environmentFilter, ...filters } = options.filters ?? {};
// Picks are checked against every shown environment, so picking only ones this page skips shows nothing.
const picked = environmentFilterIds(environmentFilter);
const targets = picked.length > 0 ? environments.filter((env) => picked.includes(env.id)) : environments;
- // The environment filter and sort are applied here, never by an environment's API.
- const sortsByEnvironment = options.sort?.column === ENVIRONMENT_FILTER;
- const scoped: O = { ...options, filters: options.filters && filters, sort: sortsByEnvironment ? undefined : options.sort };
if (targets.length === 0) {
const pagination = { totalPages: 0, totalItems: 0, currentPage: 1, itemsPerPage: options.pagination?.limit ?? 0 };
return { data: [], pagination, failedEnvironments: [] } as unknown as EnvironmentPage<P>;
}
- const merging = targets.length > 1;
- const request = merging ? { ...scoped, pagination: { page: 1, limit: TABLE_PAGE_SIZE_ALL } } : scoped;
+ const sortsByEnvironment = options.sort?.column === ENVIRONMENT_FILTER && targets.length === 1;
+ const request: O = { ...options, filters: options.filters && filters, sort: sortsByEnvironment ? undefined : options.sort };
// Filtering to one of several environments still reports its failure the merged way.
- const { values: pages, failures } = await collectAcrossEnvironments(
+ const { value: page, failures } = await readAcrossEnvironments(
targets,
- (id, config) => fetch(id, request, config),
+ (scope, config) => fetch(scope, request, config),
environments.length > 1
);
- const tag = <R extends object>(environment: EnvironmentRef, rows: R[]) => rows.map((row) => tagEnvironment(row, environment));
- const { environment: firstEnvironment, value: first } = pages[0]!;
- // Environments without rows omit groups, so any response having them decides. If one with rows has none
- // (an older agent), the merged list stays flat rather than dropping its rows.
- const grouped = pages.some(({ value }) => value.groups) && pages.every(({ value }) => value.groups || value.data.length === 0);
- if (!merging) {
- return {
- ...first,
- data: tag(firstEnvironment, first.data),
- groups: first.groups?.map((group) => ({ ...group, items: tag(firstEnvironment, group.items) })),
- failedEnvironments: []
- };
- }
-
- const { column, direction } = options.sort ?? {};
- const sign = direction === 'desc' ? -1 : 1;
- const rowSortValue = (row: EnvironmentRow<T>, sortColumn: string) =>
- sortsByEnvironment ? row.environmentName : sortValue(row, sortColumn);
- const rows = pages
- .flatMap(({ environment, value }) => tag(environment, value.data))
- .sort(
- (a, b) =>
- (column ? compareSortValues(rowSortValue(a, column), rowSortValue(b, column), sign) : 0) ||
- a.environmentName.localeCompare(b.environmentName) ||
- a.environmentId.localeCompare(b.environmentId)
- );
-
- // Group items are separate objects from the data rows, so membership is matched by environment and ID.
- const memberKey = (environmentId: string, row: object) =>
- `${environmentId}${KEY_SEPARATOR}${'id' in row ? String(row.id) : ''}`;
- const groupOf = new Map(
- pages.flatMap(({ environment, value }) =>
- (value.groups ?? []).flatMap((group) =>
- group.items.map((item) => [memberKey(environment.id, item), group.groupName] as const)
- )
- )
- );
- // Same-named groups merge across environments and follow the merged row order, like the backend groups its sorted rows.
- const groups = new Map<string, { groupName: string; items: typeof rows }>();
- for (const row of grouped ? rows : []) {
- const groupName = groupOf.get(memberKey(row.environmentId, row));
- if (groupName === undefined) continue;
- const entry = groups.get(groupName) ?? { groupName, items: [] };
- entry.items.push(row);
- groups.set(groupName, entry);
- }
-
- const limit = options.pagination?.limit ?? TABLE_PAGE_SIZE_ALL;
- const groupList = [...groups.values()];
- const pageList = pageUnits(grouped ? groupList.map((group) => group.items) : rows.map((row) => [row]), limit);
- const page = Math.min(limit > 0 ? (options.pagination?.page ?? 1) : 1, pageList.length);
- const selected = pageList[page - 1]!;
- const totals = sumNumbers(pages.map(({ value }) => value.pagination));
return {
- ...first,
- data: selected.flat(),
- pagination: {
- ...totals,
- currentPage: page,
- itemsPerPage: limit > 0 ? limit : totals.totalItems,
- totalPages: pageList.length
- },
- counts: pages.some(({ value }) => value.counts)
- ? sumNumbers(pages.flatMap(({ value }) => (value.counts ? [value.counts] : [])))
- : undefined,
- groups: grouped ? groupList.filter((group) => selected.includes(group.items)) : undefined,
+ ...page,
+ data: environmentRows(page.data, targets),
+ groups: page.groups?.map((group) => ({ ...group, items: environmentRows(group.items, targets) })),
failedEnvironments: failures
};
}
diff --git a/frontend/src/lib/utils/settings.svelte.ts b/frontend/src/lib/utils/settings.svelte.ts
index cf93c966d..8d2e90efe 100644
--- a/frontend/src/lib/utils/settings.svelte.ts
+++ b/frontend/src/lib/utils/settings.svelte.ts
@@ -4,6 +4,7 @@ import type { FormInputs } from '#lib/types/form.js';
const LOCAL_SETTING_KEYS = new Set([
'avatarMaxUploadSizeMb',
+ 'allEnvironmentsLimit',
'authLocalEnabled',
'authSessionTimeout',
'authPasswordPolicy',
diff --git a/frontend/src/lib/utils/vulnerability.ts b/frontend/src/lib/utils/vulnerability.ts
index dac90e66f..be3e31d3c 100644
--- a/frontend/src/lib/utils/vulnerability.ts
+++ b/frontend/src/lib/utils/vulnerability.ts
@@ -188,13 +188,6 @@ export function vulnerabilityOverviewEnvironment(): EnvironmentRef | undefined {
return environments.find((env) => env.id === environmentStore.selected?.id) ?? environments[0];
}
-/** Sort value for a vulnerability column; severity sorts by rank from unknown to critical, as the backend does. */
-export function vulnerabilitySortValue(vulnerability: VulnerabilityWithImage, column: string): unknown {
- if (column !== 'severity') return vulnerability[column as keyof VulnerabilityWithImage];
- const index = severityOrder.indexOf(vulnerability.severity as (typeof severityOrder)[number]);
- return index < 0 ? 0 : severityOrder.length - 1 - index;
-}
-
/**
* Shown environments with vulnerability management enabled. With several shown, ones whose features
* could not load stay in, so their failure shows inline with a retry instead of silently missing.
diff --git a/frontend/src/routes/(app)/account/components/all-environments-preferences.svelte b/frontend/src/routes/(app)/account/components/all-environments-preferences.svelte
index 7c38ae303..642292f15 100644
--- a/frontend/src/routes/(app)/account/components/all-environments-preferences.svelte
+++ b/frontend/src/routes/(app)/account/components/all-environments-preferences.svelte
@@ -7,9 +7,12 @@
import SettingsRow from '#lib/components/settings/settings-row.svelte';
import SettingsSection from '#lib/components/settings/settings-section.svelte';
import * as Alert from '#lib/components/ui/alert/index.js';
+ import { Input } from '#lib/components/ui/input/index.js';
import { Switch } from '#lib/components/ui/switch/index.js';
import { AlertTriangleIcon, ResetIcon } from '#lib/icons/index.js';
import { m } from '#lib/paraglide/messages.js';
+ import { settingsService } from '#lib/services/settings-service.js';
+ import settingsStore from '#lib/stores/config-store.svelte.js';
import {
environmentColor,
environmentColorPalette,
@@ -19,6 +22,9 @@
import { featureStore } from '#lib/stores/features.store.svelte.js';
import userStore from '#lib/stores/user-store.svelte.js';
import type { EnvironmentLabelStyle } from '#lib/types/auth.js';
+ import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
+ import { isGlobalAdmin } from '#lib/utils/auth.js';
+ import { tryCatch } from '#lib/utils/try-catch.js';
import { saveUserPreferences } from '#lib/utils/user-preferences.js';
const environments = $derived(environmentStore.available.filter((env) => env.enabled));
@@ -36,6 +42,28 @@
else toast.info(m.environment_color_in_use());
}
+ const environmentLimit = $derived(Number(settingsStore.current?.allEnvironmentsLimit) || 64);
+ let environmentLimitInput = $derived(String(environmentLimit));
+ let environmentLimitError = $state<string | null>(null);
+
+ async function commitEnvironmentLimit() {
+ const parsed = Number(environmentLimitInput);
+ if (!Number.isInteger(parsed) || parsed < 2 || parsed > 256) {
+ environmentLimitError = m.all_environments_limit_help();
+ return;
+ }
+ environmentLimitError = null;
+ if (parsed === environmentLimit) return;
+ await handleApiResultWithCallbacks({
+ result: await tryCatch(settingsService.updateSettings({ allEnvironmentsLimit: parsed })),
+ message: m.common_update_failed({ resource: m.settings() }),
+ onSuccess: (updated) => {
+ settingsStore.set(updated);
+ toast.success(m.common_update_success({ resource: m.settings() }));
+ }
+ });
+ }
+
async function setPreview(enabled: boolean) {
await saveUserPreferences({ allEnvironmentsPreview: enabled });
// Navigation gates on features in any shown environment.
@@ -61,6 +89,26 @@
onCheckedChange={(checked) => void setPreview(checked)}
/>
</SettingsRow>
+ {#if isGlobalAdmin()}
+ <SettingsRow
+ for="account-all-environments-limit"
+ label={m.all_environments_limit()}
+ description={m.all_environments_limit_description()}
+ helpText={m.all_environments_limit_help()}
+ error={environmentLimitError}
+ >
+ <Input
+ id="account-all-environments-limit"
+ type="number"
+ min="2"
+ max="256"
+ bind:value={environmentLimitInput}
+ placeholder="64"
+ aria-invalid={Boolean(environmentLimitError)}
+ onblur={() => void commitEnvironmentLimit()}
+ />
+ </SettingsRow>
+ {/if}
{#if allEnvironmentsPreview.current}
<SettingsRow label={m.environment_label_style()} description={m.environment_label_style_description()} layout="switch">
<div class="w-52">
diff --git a/frontend/src/routes/(app)/containers/+page.svelte b/frontend/src/routes/(app)/containers/+page.svelte
index b42358e43..5962bfa82 100644
--- a/frontend/src/routes/(app)/containers/+page.svelte
+++ b/frontend/src/routes/(app)/containers/+page.svelte
@@ -15,7 +15,6 @@
import ContainerEnvironmentSync from './components/container-environment-sync.svelte';
import ContainerTable from './components/container-table.svelte';
- import { containerSortValue } from './container-table.helpers';
let { data } = $props();
@@ -50,11 +49,8 @@
isRefreshing = true;
}
try {
- const next = await fetchAcrossEnvironments(
- environmentStore.shown,
- options,
- (id, request, config) => containerService.getContainersForEnvironment(id, request, config),
- containerSortValue
+ const next = await fetchAcrossEnvironments(environmentStore.shown, options, (id, request, config) =>
+ containerService.getContainersForEnvironment(id, request, config)
);
if (requestedScope !== scope || generation !== refreshGeneration) {
return containers;
diff --git a/frontend/src/routes/(app)/containers/+page.ts b/frontend/src/routes/(app)/containers/+page.ts
index b22efb609..e39090cea 100644
--- a/frontend/src/routes/(app)/containers/+page.ts
+++ b/frontend/src/routes/(app)/containers/+page.ts
@@ -8,7 +8,6 @@ import { resolveInitialTableRequest } from '#lib/utils/tables.js';
import { tryCatch } from '#lib/utils/try-catch.js';
import type { PageLoad } from './$types';
-import { containerSortValue } from './container-table.helpers';
export const load: PageLoad = async ({ parent }) => {
const { queryClient } = await parent();
@@ -26,11 +25,8 @@ export const load: PageLoad = async ({ parent }) => {
queryClient.query({
queryKey: queryKeys.containers.list(scope, containerRequestOptions),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- containerRequestOptions,
- (id, request, config) => containerService.getContainersForEnvironment(id, request, config),
- containerSortValue
+ fetchAcrossEnvironments(environments, containerRequestOptions, (id, request, config) =>
+ containerService.getContainersForEnvironment(id, request, config)
)
}))()
);
diff --git a/frontend/src/routes/(app)/containers/components/container-table.svelte b/frontend/src/routes/(app)/containers/components/container-table.svelte
index 5aaacc6d4..dd57d0112 100644
--- a/frontend/src/routes/(app)/containers/components/container-table.svelte
+++ b/frontend/src/routes/(app)/containers/components/container-table.svelte
@@ -163,11 +163,8 @@
async function fetchContainers(request: ContainerListRequestOptions): Promise<ContainerPage> {
return onRefreshData
? onRefreshData(request)
- : fetchAcrossEnvironments(
- environmentStore.shown,
- request,
- (id, request, config) => containerService.getContainersForEnvironment(id, request, config),
- containerSortValue
+ : fetchAcrossEnvironments(environmentStore.shown, request, (id, request, config) =>
+ containerService.getContainersForEnvironment(id, request, config)
);
}
diff --git a/frontend/src/routes/(app)/containers/new/+page.svelte b/frontend/src/routes/(app)/containers/new/+page.svelte
index 524e93599..840b3bc40 100644
--- a/frontend/src/routes/(app)/containers/new/+page.svelte
+++ b/frontend/src/routes/(app)/containers/new/+page.svelte
@@ -60,15 +60,15 @@
customLabel={m.common_back()}
/>
<div class="hidden h-4 w-px bg-border sm:block"></div>
- <h1 class="text-base font-semibold">{m.create_container_title()}</h1>
+ <h1 class="flex-1 text-base font-semibold">{m.create_container_title()}</h1>
+ <EnvironmentDestinationSelect
+ bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
+ permission="containers:create"
+ />
</div>
</div>
<div class="mx-auto w-full max-w-full space-y-6 px-6 py-6">
- <EnvironmentDestinationSelect
- bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
- permission="containers:create"
- />
<ContainerForm
mode="create"
{form}
diff --git a/frontend/src/routes/(app)/images/+page.svelte b/frontend/src/routes/(app)/images/+page.svelte
index 7c0c00061..e7434b29b 100644
--- a/frontend/src/routes/(app)/images/+page.svelte
+++ b/frontend/src/routes/(app)/images/+page.svelte
@@ -38,7 +38,7 @@
import { bytes } from '#lib/utils/formatting.js';
import { tryCatch } from '#lib/utils/try-catch.js';
- import ImageTable, { imageSortValue } from './components/image-table.svelte';
+ import ImageTable from './components/image-table.svelte';
let { data } = $props();
const queryClient = useQueryClient();
@@ -75,11 +75,8 @@
return {
queryKey: queryKeys.images.list(queryScope, options),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- options,
- (id, request, config) => imageService.getImagesForEnvironment(id, request, config),
- imageSortValue
+ fetchAcrossEnvironments(environments, options, (id, request, config) =>
+ imageService.getImagesForEnvironment(id, request, config)
),
placeholderData: (previous, query) => {
if (query?.queryKey[1] === queryScope) return previous;
@@ -265,11 +262,8 @@
queryClient.query({
queryKey: queryKeys.images.list(requestedScope, options),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- options,
- (id, request, config) => imageService.getImagesForEnvironment(id, request, config),
- imageSortValue
+ fetchAcrossEnvironments(environments, options, (id, request, config) =>
+ imageService.getImagesForEnvironment(id, request, config)
),
staleTime: 0
}),
@@ -428,6 +422,7 @@
<EnvironmentDestinationSelect
bind:value={() => uploadEnvironmentId, (value) => (pickedUploadEnvironmentId = value)}
permission="images:upload"
+ variant="field"
/>
<FileDropZone
{onUpload}
diff --git a/frontend/src/routes/(app)/images/+page.ts b/frontend/src/routes/(app)/images/+page.ts
index 97b3bb43d..4641dfcd2 100644
--- a/frontend/src/routes/(app)/images/+page.ts
+++ b/frontend/src/routes/(app)/images/+page.ts
@@ -7,7 +7,6 @@ import { resolveListPageLoadContext } from '#lib/utils/tables.js';
import { tryCatch } from '#lib/utils/try-catch.js';
import type { PageLoad } from './$types';
-import { imageSortValue } from './components/image-table.svelte';
export const load: PageLoad = async ({ parent }) => {
const {
@@ -28,11 +27,8 @@ export const load: PageLoad = async ({ parent }) => {
queryClient.query({
queryKey: queryKeys.images.list(scope, imageRequestOptions),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- imageRequestOptions,
- (id, request, config) => imageService.getImagesForEnvironment(id, request, config),
- imageSortValue
+ fetchAcrossEnvironments(environments, imageRequestOptions, (id, request, config) =>
+ imageService.getImagesForEnvironment(id, request, config)
)
}),
queryClient.query({
diff --git a/frontend/src/routes/(app)/images/builds/components/build-config-panel.svelte b/frontend/src/routes/(app)/images/builds/components/build-config-panel.svelte
index 213ef108d..37658a31e 100644
--- a/frontend/src/routes/(app)/images/builds/components/build-config-panel.svelte
+++ b/frontend/src/routes/(app)/images/builds/components/build-config-panel.svelte
@@ -39,8 +39,8 @@
<div class="space-y-7 p-8">
<form onsubmit={preventDefault(() => onSubmit?.())} class="space-y-7">
- <EnvironmentDestinationSelect bind:value={environmentId} permission="images:build" />
<div class="space-y-4">
+ <EnvironmentDestinationSelect bind:value={environmentId} permission="images:build" variant="field" />
{#if isPushMode}
<SelectWithLabel
id="build-push-registry"
diff --git a/frontend/src/routes/(app)/images/components/image-table.svelte b/frontend/src/routes/(app)/images/components/image-table.svelte
index f5f75d696..9be9e9e89 100644
--- a/frontend/src/routes/(app)/images/components/image-table.svelte
+++ b/frontend/src/routes/(app)/images/components/image-table.svelte
@@ -1,15 +1,3 @@
-<script module lang="ts">
- import type { ImageSummaryDto } from '#lib/types/docker.js';
-
- /** Sort value for an image column; vulnerabilities sort by total, critical, then high, unscanned first, as the backend does. */
- export function imageSortValue(image: ImageSummaryDto, column: string): unknown {
- if (column !== 'vulnerabilities') return image[column as keyof ImageSummaryDto];
- const scan = image.vulnerabilityScan;
- if (scan?.status !== 'completed' || !scan.summary) return [-1];
- return [scan.summary.total, scan.summary.critical, scan.summary.high];
- }
-</script>
-
<script lang="ts">
import { goto } from '$app/navigation';
import { useQueryClient } from '@tanstack/svelte-query';
@@ -51,7 +39,7 @@
import { environmentStore } from '#lib/stores/environment.store.svelte.js';
import { featureStore } from '#lib/stores/features.store.svelte.js';
import userStore from '#lib/stores/user-store.svelte.js';
- import type { ImageUpdateInfoDto } from '#lib/types/docker.js';
+ import type { ImageSummaryDto, ImageUpdateInfoDto } from '#lib/types/docker.js';
import type { VulnerabilityScanSummary } from '#lib/types/environment.js';
import type { SearchPaginationSortRequest } from '#lib/types/shared.js';
import { activityToastOptions, extractActivityId } from '#lib/utils/activity-toast.js';
@@ -144,11 +132,8 @@
await onRefreshData(options);
return;
}
- images = await fetchAcrossEnvironments(
- environmentStore.shown,
- options,
- (id, request, config) => imageService.getImagesForEnvironment(id, request, config),
- imageSortValue
+ images = await fetchAcrossEnvironments(environmentStore.shown, options, (id, request, config) =>
+ imageService.getImagesForEnvironment(id, request, config)
);
}
diff --git a/frontend/src/routes/(app)/networks/+page.svelte b/frontend/src/routes/(app)/networks/+page.svelte
index 446039464..307fa06c0 100644
--- a/frontend/src/routes/(app)/networks/+page.svelte
+++ b/frontend/src/routes/(app)/networks/+page.svelte
@@ -18,7 +18,7 @@
import { ResourceListPageState } from '#lib/utils/resource-list-page.svelte.js';
import { tryCatch } from '#lib/utils/try-catch.js';
- import NetworkTable, { networkSortValue } from './components/network-table.svelte';
+ import NetworkTable from './components/network-table.svelte';
let { data } = $props();
const queryClient = useQueryClient();
@@ -34,11 +34,8 @@
return {
queryKey: queryKeys.networks.list(queryScope, options),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- options,
- (id, request, config) => networkService.getNetworksForEnvironment(id, request, config),
- networkSortValue
+ fetchAcrossEnvironments(environments, options, (id, request, config) =>
+ networkService.getNetworksForEnvironment(id, request, config)
),
placeholderData: (previous, query) => {
if (query?.queryKey[1] === queryScope) return previous;
@@ -86,11 +83,8 @@
await queryClient.query({
queryKey: queryKeys.networks.list(requestedScope, options),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- options,
- (id, request, config) => networkService.getNetworksForEnvironment(id, request, config),
- networkSortValue
+ fetchAcrossEnvironments(environments, options, (id, request, config) =>
+ networkService.getNetworksForEnvironment(id, request, config)
),
staleTime: 0
});
diff --git a/frontend/src/routes/(app)/networks/+page.ts b/frontend/src/routes/(app)/networks/+page.ts
index 5c98d9366..51af2481f 100644
--- a/frontend/src/routes/(app)/networks/+page.ts
+++ b/frontend/src/routes/(app)/networks/+page.ts
@@ -6,7 +6,6 @@ import { resolveListPageLoadContext } from '#lib/utils/tables.js';
import { tryCatch } from '#lib/utils/try-catch.js';
import type { PageLoad } from './$types';
-import { networkSortValue } from './components/network-table.svelte';
export const load: PageLoad = async ({ parent }) => {
const {
@@ -26,11 +25,8 @@ export const load: PageLoad = async ({ parent }) => {
queryClient.query({
queryKey: queryKeys.networks.list(scope, networkRequestOptions),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- networkRequestOptions,
- (id, request, config) => networkService.getNetworksForEnvironment(id, request, config),
- networkSortValue
+ fetchAcrossEnvironments(environments, networkRequestOptions, (id, request, config) =>
+ networkService.getNetworksForEnvironment(id, request, config)
)
}))()
);
diff --git a/frontend/src/routes/(app)/networks/components/network-table.svelte b/frontend/src/routes/(app)/networks/components/network-table.svelte
index c2f75d928..19b64b87f 100644
--- a/frontend/src/routes/(app)/networks/components/network-table.svelte
+++ b/frontend/src/routes/(app)/networks/components/network-table.svelte
@@ -97,11 +97,8 @@
}
return;
}
- networks = await fetchAcrossEnvironments(
- environmentStore.shown,
- options,
- (id, request, config) => networkService.getNetworksForEnvironment(id, request, config),
- networkSortValue
+ networks = await fetchAcrossEnvironments(environmentStore.shown, options, (id, request, config) =>
+ networkService.getNetworksForEnvironment(id, request, config)
);
onNetworksChange?.(networks);
}
diff --git a/frontend/src/routes/(app)/projects/new/+page.svelte b/frontend/src/routes/(app)/projects/new/+page.svelte
index a3ba88d2f..5dbd066a9 100644
--- a/frontend/src/routes/(app)/projects/new/+page.svelte
+++ b/frontend/src/routes/(app)/projects/new/+page.svelte
@@ -363,6 +363,10 @@
</div>
<div class="flex items-center gap-2">
+ <EnvironmentDestinationSelect
+ bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
+ permission="projects:create"
+ />
<ComposeCreateMenu
tooltipOpen={!effectiveName && !createMenuBusy ? undefined : false}
tooltipVisible={effectiveName === ''}
@@ -409,11 +413,6 @@
<ProjectTagEditor bind:tags={newProjectTags} availableTags={availableProjectTags} canEdit={!ui.saving} class="mt-2" />
</div>
- <EnvironmentDestinationSelect
- bind:value={() => environmentId, (value) => (pickedEnvironmentId = value)}
- permission="projects:create"
- />
-
{#if templateLoadNotices.length > 0}
<Alert.Root variant="warning" size="sm">
<AlertIcon class="size-4" />
diff --git a/frontend/src/routes/(app)/projects/projects-table.helpers.ts b/frontend/src/routes/(app)/projects/projects-table.helpers.ts
index 0b74894fa..f8c68d5f0 100644
--- a/frontend/src/routes/(app)/projects/projects-table.helpers.ts
+++ b/frontend/src/routes/(app)/projects/projects-table.helpers.ts
@@ -1,7 +1,7 @@
import { projectService } from '#lib/services/project-service.js';
import type { EnvironmentRef } from '#lib/stores/environment.store.svelte.js';
import type { ProjectTagOption } from '#lib/types/swarm.js';
-import { collectAcrossEnvironments, type EnvironmentFailure } from '#lib/utils/environments.js';
+import { readAcrossEnvironments, type EnvironmentFailure } from '#lib/utils/environments.js';
export type ActionStatus =
| 'starting'
@@ -18,12 +18,8 @@ export type ProjectTagOptions = { tags: ProjectTagOption[]; failedEnvironments:
/** Tag options from every shown environment, one per name, with the environments that failed. */
export async function fetchProjectTags(environments: EnvironmentRef[]): Promise<ProjectTagOptions> {
- const { values, failures } = await collectAcrossEnvironments(environments, (id, config) =>
- projectService.getProjectTagsForEnvironment(id, config)
+ const { value, failures } = await readAcrossEnvironments(environments, (scope, config) =>
+ projectService.getProjectTagsForEnvironment(scope, config)
);
- const tags = values.flatMap((result) => result.value);
- return {
- tags: tags.filter((tag, index) => tags.findIndex((other) => other.name === tag.name) === index),
- failedEnvironments: failures
- };
+ return { tags: value, failedEnvironments: failures };
}
diff --git a/frontend/src/routes/(app)/security/+page.svelte b/frontend/src/routes/(app)/security/+page.svelte
index 686b4c87d..ded723801 100644
--- a/frontend/src/routes/(app)/security/+page.svelte
+++ b/frontend/src/routes/(app)/security/+page.svelte
@@ -31,7 +31,6 @@
vulnerabilityEnvironments,
vulnerabilityOverviewEnvironment,
vulnerabilityOverviewEnvironments,
- vulnerabilitySortValue,
withVulnerabilityToggles,
type VulnerabilityRow
} from '#lib/utils/vulnerability.js';
@@ -157,11 +156,8 @@
},
vulnerabilities: {
fetch: () =>
- fetchAcrossEnvironments(
- vulnerabilityEnvironments(),
- requestForApi,
- (id, request, config) => vulnerabilityService.getAllVulnerabilitiesForEnvironment(id, request, config),
- vulnerabilitySortValue
+ fetchAcrossEnvironments(vulnerabilityEnvironments(), requestForApi, (id, request, config) =>
+ vulnerabilityService.getAllVulnerabilitiesForEnvironment(id, request, config)
),
onSuccess: (data) => {
if (stillCurrent()) vulnerabilities = mapVulnerabilityPage(data, requestOptions);
@@ -456,14 +452,6 @@
{/if}
{/snippet}
-{#snippet overviewEnvironmentPicker()}
- {#if environmentStore.shown.length > 1}
- <div class="mb-3">
- <EnvironmentDestinationSelect environments={vulnerabilityOverviewEnvironments()} />
- </div>
- {/if}
-{/snippet}
-
<ResourcePageLayout
title={m.security()}
subtitle={m.security_subtitle()}
@@ -474,16 +462,19 @@
<FeatureDisabled />
{:else}
<Tabs.Root value={activeTab}>
- <TabBar items={securityTabItems} value={activeTab} onValueChange={handleTabChange} />
+ <div class="flex flex-wrap items-center justify-between gap-2">
+ <TabBar items={securityTabItems} value={activeTab} onValueChange={handleTabChange} />
+ {#if activeTab === 'overview' || (activeTab === 'vulnerabilities' && overview)}
+ <EnvironmentDestinationSelect environments={vulnerabilityOverviewEnvironments()} />
+ {/if}
+ </div>
<Tabs.Content value="overview" class="mt-4">
- {@render overviewEnvironmentPicker()}
{#if overview}
<SecurityOverview {overview} environmentId={overviewEnvironmentId} onSelectVulnerability={showVulnerability} />
{/if}
</Tabs.Content>
<Tabs.Content value="vulnerabilities" class="mt-4">
{#if overview}
- {@render overviewEnvironmentPicker()}
<div class="mb-3 flex flex-wrap items-center gap-x-5 gap-y-1.5 text-xs text-muted-foreground">
<span>
{m.security_images_scanned()}:
diff --git a/frontend/src/routes/(app)/security/+page.ts b/frontend/src/routes/(app)/security/+page.ts
index 9eb3c70c5..5cc7748e0 100644
--- a/frontend/src/routes/(app)/security/+page.ts
+++ b/frontend/src/routes/(app)/security/+page.ts
@@ -11,8 +11,7 @@ import {
mapVulnerabilityPage,
mapVulnerabilityRequest,
vulnerabilityEnvironments,
- vulnerabilityOverviewEnvironment,
- vulnerabilitySortValue
+ vulnerabilityOverviewEnvironment
} from '#lib/utils/vulnerability.js';
import type { PageLoad } from './$types';
@@ -75,11 +74,8 @@ export const load: PageLoad = async ({ parent }) => {
queryClient.query({
queryKey: queryKeys.vulnerabilities.allByEnvironment(environmentStore.shownKey, requestForApi),
queryFn: () =>
- fetchAcrossEnvironments(
- vulnerabilityEnvironments(),
- requestForApi,
- (id, request, config) => vulnerabilityService.getAllVulnerabilitiesForEnvironment(id, request, config),
- vulnerabilitySortValue
+ fetchAcrossEnvironments(vulnerabilityEnvironments(), requestForApi, (id, request, config) =>
+ vulnerabilityService.getAllVulnerabilitiesForEnvironment(id, request, config)
)
})
]))()
diff --git a/frontend/src/routes/(app)/security/components/security-patch-table.svelte b/frontend/src/routes/(app)/security/components/security-patch-table.svelte
index 881fc3037..1d1287619 100644
--- a/frontend/src/routes/(app)/security/components/security-patch-table.svelte
+++ b/frontend/src/routes/(app)/security/components/security-patch-table.svelte
@@ -15,12 +15,8 @@
/** Patch targets from every shown environment with vulnerability management, keyed by environment and image. */
export async function fetchPatchTargets(options: SearchPaginationSortRequest): Promise<PatchTargetPage> {
- const page = await fetchAcrossEnvironments(
- vulnerabilityEnvironments(),
- options,
- (id, request, config) => imageService.listPatchTargets(request, id, config),
- // The image column sorts as imageName but the row holds imageRef.
- (target, column) => (column === 'imageName' ? target.imageRef : target[column as keyof ImagePatchTargetDto])
+ const page = await fetchAcrossEnvironments(vulnerabilityEnvironments(), options, (id, request, config) =>
+ imageService.listPatchTargets(request, id, config)
);
// The table keys rows by environment and id, so the id is the image's own.
return { ...page, data: page.data.map((target) => ({ ...target, id: target.imageId })) };
diff --git a/frontend/src/routes/(app)/security/components/security-vulnerability-table.svelte b/frontend/src/routes/(app)/security/components/security-vulnerability-table.svelte
index 8d5d3e744..41fd3e5cf 100644
--- a/frontend/src/routes/(app)/security/components/security-vulnerability-table.svelte
+++ b/frontend/src/routes/(app)/security/components/security-vulnerability-table.svelte
@@ -37,12 +37,12 @@
import { downloadBlob } from '#lib/utils/browser-download.js';
import { bulkConfirmAndRun, confirmAndRun } from '#lib/utils/bulk-actions.js';
import {
- collectAcrossEnvironments,
ENVIRONMENT_FILTER,
environmentFilterIds,
environmentHref,
environmentName,
fetchAcrossEnvironments,
+ readAcrossEnvironments,
rowKey,
settleAcrossEnvironments
} from '#lib/utils/environments.js';
@@ -56,7 +56,6 @@
resolveSeverityFilter,
toBulkVulnerabilityFilters,
vulnerabilityEnvironments,
- vulnerabilitySortValue,
withVulnerabilityToggles,
type VulnerabilityRow
} from '#lib/utils/vulnerability.js';
@@ -274,10 +273,15 @@
const operationResult = await tryCatch(
(async () => {
- const { values } = await collectAcrossEnvironments(vulnerabilityEnvironments(), (id, config) =>
- vulnerabilityService.getAllVulnerabilityImageOptionsForEnvironment(id, request, config)
- );
- const optionsResponse = [...new Set(values.flatMap((result) => result.value))].sort();
+ const environments = vulnerabilityEnvironments();
+ const optionsResponse =
+ environments.length === 0
+ ? []
+ : (
+ await readAcrossEnvironments(environments, (scope, config) =>
+ vulnerabilityService.getAllVulnerabilityImageOptionsForEnvironment(scope, request, config)
+ )
+ ).value;
if (vulnerabilityManagementEnabled && requestedScope === scope) imageNameFilterOptions = optionsResponse;
return optionsResponse;
})()
@@ -298,11 +302,8 @@
const request = mapVulnerabilityRequest(withVulnerabilityToggles(options, { showIgnored, fixAvailable }));
const [response, optionsResponse] = await Promise.all([
- fetchAcrossEnvironments(
- vulnerabilityEnvironments(),
- request,
- (id, request, config) => vulnerabilityService.getAllVulnerabilitiesForEnvironment(id, request, config),
- vulnerabilitySortValue
+ fetchAcrossEnvironments(vulnerabilityEnvironments(), request, (id, request, config) =>
+ vulnerabilityService.getAllVulnerabilitiesForEnvironment(id, request, config)
),
refreshImageNameFilterOptions(request)
]);
diff --git a/frontend/src/routes/(app)/swarm/stacks/new/+page.svelte b/frontend/src/routes/(app)/swarm/stacks/new/+page.svelte
index 9d9af0853..a8d90cf9d 100644
--- a/frontend/src/routes/(app)/swarm/stacks/new/+page.svelte
+++ b/frontend/src/routes/(app)/swarm/stacks/new/+page.svelte
@@ -288,6 +288,9 @@
</div>
<div class="flex items-center gap-2">
+ {#if !isEditMode}
+ <EnvironmentDestinationSelect />
+ {/if}
<ComposeCreateMenu
tooltipOpen={!inputs.name.value && !ui.saving && !ui.converting && !ui.isLoadingTemplateContent ? undefined : false}
tooltipVisible={inputs.name.value === ''}
@@ -334,7 +337,6 @@
<form class="flex h-full min-h-0 flex-1 flex-col gap-3 px-2 pb-4 sm:px-6" onsubmit={preventDefault(handleSubmit)}>
{#if !isEditMode}
<div class="flex shrink-0 flex-wrap items-center gap-x-6 gap-y-3 rounded-lg border border-border bg-card px-4 py-3">
- <EnvironmentDestinationSelect />
<div class="flex items-center gap-2">
<Checkbox
id="swarm-stack-prune"
diff --git a/frontend/src/routes/(app)/updates/+page.svelte b/frontend/src/routes/(app)/updates/+page.svelte
index 8b0e5e765..b6df21906 100644
--- a/frontend/src/routes/(app)/updates/+page.svelte
+++ b/frontend/src/routes/(app)/updates/+page.svelte
@@ -21,7 +21,6 @@
import { actionEnvironments, fetchAcrossEnvironments, listScopes, settleAcrossEnvironments } from '#lib/utils/environments.js';
import { confirmAndApplyAllUpdates } from '#lib/utils/update-actions.js';
- import { containerSortValue } from '../containers/container-table.helpers';
import ContainerUpdatesTable from './components/container-updates-table.svelte';
import ProjectUpdatesTable from './components/project-updates-table.svelte';
@@ -42,11 +41,8 @@
return {
queryKey: queryKeys.containers.list(queryScope, options),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- options,
- (id, request, config) => containerService.getContainersForEnvironment(id, request, config),
- containerSortValue
+ fetchAcrossEnvironments(environments, options, (id, request, config) =>
+ containerService.getContainersForEnvironment(id, request, config)
),
placeholderData: (previous, query) => {
if (query?.queryKey[1] === queryScope) return previous;
@@ -90,11 +86,8 @@
await queryClient.fetchQuery({
queryKey: queryKeys.containers.list(requestedScope, requestedOptions),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- requestedOptions,
- (id, request, config) => containerService.getContainersForEnvironment(id, request, config),
- containerSortValue
+ fetchAcrossEnvironments(environments, requestedOptions, (id, request, config) =>
+ containerService.getContainersForEnvironment(id, request, config)
)
});
}
diff --git a/frontend/src/routes/(app)/updates/+page.ts b/frontend/src/routes/(app)/updates/+page.ts
index 1e8019ed3..82f49e61a 100644
--- a/frontend/src/routes/(app)/updates/+page.ts
+++ b/frontend/src/routes/(app)/updates/+page.ts
@@ -9,7 +9,6 @@ import { fetchAcrossEnvironments } from '#lib/utils/environments.js';
import { resolveInitialTableRequest } from '#lib/utils/tables.js';
import { tryCatch } from '#lib/utils/try-catch.js';
-import { containerSortValue } from '../containers/container-table.helpers';
import type { PageLoad } from './$types';
export const load: PageLoad = async ({ parent }) => {
@@ -37,11 +36,8 @@ export const load: PageLoad = async ({ parent }) => {
queryClient.query({
queryKey: queryKeys.containers.list(scope, containerRequestOptions),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- containerRequestOptions,
- (id, request, config) => containerService.getContainersForEnvironment(id, request, config),
- containerSortValue
+ fetchAcrossEnvironments(environments, containerRequestOptions, (id, request, config) =>
+ containerService.getContainersForEnvironment(id, request, config)
)
}),
queryClient.query({
diff --git a/frontend/src/routes/(app)/volumes/+page.svelte b/frontend/src/routes/(app)/volumes/+page.svelte
index 872c75629..2e5fbda0a 100644
--- a/frontend/src/routes/(app)/volumes/+page.svelte
+++ b/frontend/src/routes/(app)/volumes/+page.svelte
@@ -17,7 +17,7 @@
import { fetchAcrossEnvironments, hasShownPermission } from '#lib/utils/environments.js';
import { ResourceListPageState } from '#lib/utils/resource-list-page.svelte.js';
- import VolumeTable, { volumeSortValue } from './components/volume-table.svelte';
+ import VolumeTable from './components/volume-table.svelte';
let { data } = $props();
const queryClient = useQueryClient();
@@ -33,11 +33,8 @@
return {
queryKey: queryKeys.volumes.table(queryScope, options),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- options,
- (id, request, config) => volumeService.getVolumesForEnvironment(id, request, config),
- volumeSortValue
+ fetchAcrossEnvironments(environments, options, (id, request, config) =>
+ volumeService.getVolumesForEnvironment(id, request, config)
),
placeholderData: (previous, query) => {
if (query?.queryKey[1] === queryScope) return previous;
@@ -86,11 +83,8 @@
await queryClient.query({
queryKey: queryKeys.volumes.table(requestedScope, options),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- options,
- (id, request, config) => volumeService.getVolumesForEnvironment(id, request, config),
- volumeSortValue
+ fetchAcrossEnvironments(environments, options, (id, request, config) =>
+ volumeService.getVolumesForEnvironment(id, request, config)
)
});
}
diff --git a/frontend/src/routes/(app)/volumes/+page.ts b/frontend/src/routes/(app)/volumes/+page.ts
index 90013d43e..60a08dddd 100644
--- a/frontend/src/routes/(app)/volumes/+page.ts
+++ b/frontend/src/routes/(app)/volumes/+page.ts
@@ -6,7 +6,6 @@ import { fetchAcrossEnvironments } from '#lib/utils/environments.js';
import { resolveInitialTableRequest } from '#lib/utils/tables.js';
import type { PageLoad } from './$types';
-import { volumeSortValue } from './components/volume-table.svelte';
export const load: PageLoad = async ({ parent }) => {
const { queryClient } = await parent();
@@ -32,11 +31,8 @@ export const load: PageLoad = async ({ parent }) => {
const volumes = await queryClient.query({
queryKey: queryKeys.volumes.table(scope, volumeRequestOptions),
queryFn: () =>
- fetchAcrossEnvironments(
- environments,
- volumeRequestOptions,
- (id, request, config) => volumeService.getVolumesForEnvironment(id, request, config),
- volumeSortValue
+ fetchAcrossEnvironments(environments, volumeRequestOptions, (id, request, config) =>
+ volumeService.getVolumesForEnvironment(id, request, config)
)
});
diff --git a/frontend/src/routes/(app)/volumes/components/volume-table.svelte b/frontend/src/routes/(app)/volumes/components/volume-table.svelte
index 85c2dc57a..071a3ca96 100644
--- a/frontend/src/routes/(app)/volumes/components/volume-table.svelte
+++ b/frontend/src/routes/(app)/volumes/components/volume-table.svelte
@@ -1,13 +1,3 @@
-<script module lang="ts">
- import type { VolumeSummaryDto } from '#lib/types/docker.js';
-
- /** Sort value for a volume column; size falls back to usage data, as the backend sorts it. */
- export function volumeSortValue(volume: VolumeSummaryDto, column: string): unknown {
- if (column === 'size') return volume.size || (volume.usageData?.size ?? 0);
- return volume[column as keyof VolumeSummaryDto];
- }
-</script>
-
<script lang="ts">
import { goto } from '$app/navigation';
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
@@ -32,7 +22,7 @@
import settingsStore from '#lib/stores/config-store.svelte.js';
import { environmentStore } from '#lib/stores/environment.store.svelte.js';
import userStore from '#lib/stores/user-store.svelte.js';
- import type { VolumeSizeInfo } from '#lib/types/docker.js';
+ import type { VolumeSizeInfo, VolumeSummaryDto } from '#lib/types/docker.js';
import type { SearchPaginationSortRequest } from '#lib/types/shared.js';
import { activityToastOptions, extractActivityId } from '#lib/utils/activity-toast.js';
import { handleApiResultWithCallbacks, extractApiErrorMessage } from '#lib/utils/api.js';
@@ -41,11 +31,12 @@
import {
collectAcrossEnvironments,
environmentHref,
+ environmentRows,
fetchAcrossEnvironments,
hasShownPermission,
parseRowKey,
+ readAcrossEnvironments,
rowKey,
- tagEnvironment,
withFailedEnvironments,
type EnvironmentPage,
type EnvironmentRow
@@ -101,11 +92,8 @@
if (onRefreshData) {
await onRefreshData(options);
} else {
- volumes = await fetchAcrossEnvironments(
- environmentStore.shown,
- options,
- (id, request, config) => volumeService.getVolumesForEnvironment(id, request, config),
- volumeSortValue
+ volumes = await fetchAcrossEnvironments(environmentStore.shown, options, (id, request, config) =>
+ volumeService.getVolumesForEnvironment(id, request, config)
);
}
if (refreshSizes && sizesEnabled) {
@@ -188,11 +176,10 @@
return {
queryKey: queryKeys.volumes.sizes(queryScope),
queryFn: async () => {
- const { values } = await collectAcrossEnvironments(
- environments.filter((env) => hasPermission('volumes:read', env.id)),
- (id, config) => volumeService.getVolumeSizes(id, config)
- );
- return values.flatMap(({ environment, value }) => value.map((size) => tagEnvironment(size, environment)));
+ const readable = environments.filter((env) => hasPermission('volumes:read', env.id));
+ if (readable.length === 0) return [];
+ const { value } = await readAcrossEnvironments(readable, (scope, config) => volumeService.getVolumeSizes(scope, config));
+ return environmentRows(value, readable);
},
enabled: sizesEnabled,
refetchOnWindowFocus: false,
diff --git a/frontend/src/routes/+layout.ts b/frontend/src/routes/+layout.ts
index 1ff1049a2..8453f5825 100644
--- a/frontend/src/routes/+layout.ts
+++ b/frontend/src/routes/+layout.ts
@@ -28,16 +28,19 @@ import type { LayoutLoad } from './$types';
export const ssr = false;
+const spansEnvironments = ({ queryKey }: { queryKey: readonly unknown[] }) =>
+ environmentStore.shown.length > 1 && queryKey.includes(environmentStore.shownKey);
+
const queryClient = new QueryClient({
defaultOptions: {
queries: {
enabled: browser,
// Covers the gap between a page loader's fetch and the component's
// mount so the same query isn't fetched twice in one navigation.
- staleTime: 2_000,
+ staleTime: (query) => (spansEnvironments(query) ? 15_000 : 2_000),
gcTime: 60 * 1000,
refetchOnMount: true,
- refetchOnWindowFocus: 'always',
+ refetchOnWindowFocus: (query) => (spansEnvironments(query) ? true : 'always'),
refetchOnReconnect: 'always'
}
}
diff --git a/tests/spec/all-environments.spec.ts b/tests/spec/all-environments.spec.ts
index a5a27fec5..2d6b05177 100644
--- a/tests/spec/all-environments.spec.ts
+++ b/tests/spec/all-environments.spec.ts
@@ -56,13 +56,56 @@ async function enablePreview(page: Page) {
}
// Lists the real local environment plus a mocked remote one, and keeps the live stream from replacing that list.
+// The merged request gets the real local rows plus the remote row, or a remote failure while `remote.failure` is set.
async function mockRemoteEnvironment(page: Page) {
+ const remote: { failure?: string } = {};
const local = await page.request.get('/api/environments/0');
const localEnvironment = (await local.json()).data;
await page.route(/\/api\/stream(?:\?.*)?$/, (route) => route.abort());
await page.route(/\/api\/environments(?:\?.*)?$/, (route) =>
route.fulfill({ json: paginated([localEnvironment, remoteEnvironment]) })
);
+ await page.route(
+ new RegExp(`/api/environments/0(?:,|%2C)${remoteEnvironment.id}/containers(?:\\?.*)?$`),
+ async (route) => {
+ const url = new URL(route.request().url());
+ const query = new URLSearchParams(url.search);
+ const descending = query.get('sort') === 'environment' && query.get('order') === 'desc';
+ query.delete('sort');
+ query.delete('order');
+ const localPage = await (
+ await page.request.get(`/api/environments/0/containers?${query}`)
+ ).json();
+ const localRows = localPage.data.map((row: object) => ({
+ ...row,
+ environmentId: localEnvironment.id,
+ environmentName: localEnvironment.name
+ }));
+ const remoteRows = remote.failure
+ ? []
+ : [
+ {
+ ...remoteContainer,
+ environmentId: remoteEnvironment.id,
+ environmentName: remoteEnvironment.name
+ }
+ ];
+ const rows = descending ? [...remoteRows, ...localRows] : [...localRows, ...remoteRows];
+ await route.fulfill({
+ json: {
+ ...paginated(rows, localPage.counts),
+ failedEnvironments: remote.failure
+ ? [
+ {
+ environment: { id: remoteEnvironment.id, name: remoteEnvironment.id },
+ message: remote.failure
+ }
+ ]
+ : undefined
+ }
+ });
+ }
+ );
await page.route(
new RegExp(`/api/environments/${remoteEnvironment.id}/containers(?:\\?.*)?$`),
(route) =>
@@ -74,6 +117,7 @@ async function mockRemoteEnvironment(page: Page) {
})
})
);
+ return remote;
}
test.describe('All environments preview', () => {
@@ -147,22 +191,15 @@ test.describe('All environments preview', () => {
test('a failed environment is named inline and can be retried', async ({ page }) => {
await enablePreview(page);
- await mockRemoteEnvironment(page);
- let remoteAvailable = false;
- await page.route(
- new RegExp(`/api/environments/${remoteEnvironment.id}/containers(?:\\?.*)?$`),
- (route) =>
- remoteAvailable
- ? route.fallback()
- : route.fulfill({ status: 500, json: { success: false, error: 'remote unavailable' } })
- );
+ const remote = await mockRemoteEnvironment(page);
+ remote.failure = 'remote unavailable';
await page.goto('/containers');
const alert = page.getByRole('alert').filter({ hasText: remoteEnvironment.name });
await expect(alert).toBeVisible();
await expect(page.getByRole('row').filter({ hasText: 'remote-preview-web' })).toHaveCount(0);
- remoteAvailable = true;
+ remote.failure = undefined;
await alert.getByRole('button', { name: 'Retry', exact: true }).click();
await expect(page.getByRole('row').filter({ hasText: 'remote-preview-web' })).toBeVisible();
@@ -186,9 +223,7 @@ test.describe('All environments preview', () => {
await dialog.getByRole('button', { name: 'Cancel', exact: true }).click();
});
- test('sorting by environment orders rows without sending the sort to environments', async ({
- page
- }) => {
+ test('sorting by environment asks the server for the merged order', async ({ page }) => {
await enablePreview(page);
await mockRemoteEnvironment(page);
const listRequests: string[] = [];
@@ -206,7 +241,9 @@ test.describe('All environments preview', () => {
await page.getByRole('menuitem', { name: 'Desc', exact: true }).click();
await expect(page.getByRole('row').nth(1)).toContainText(remoteEnvironment.name);
- expect(listRequests.some((url) => /[?&]sort=environment/.test(url))).toBe(false);
+ expect(
+ listRequests.some((url) => /[?&]sort=environment/.test(url) && /[?&]order=desc/.test(url))
+ ).toBe(true);
});
test('the dot label style shows the environment name on hover', async ({ page }) => {
diff --git a/types/base/environment.go b/types/base/environment.go
new file mode 100644
index 000000000..14f3a8243
--- /dev/null
+++ b/types/base/environment.go
@@ -0,0 +1,25 @@
+package base
+
+type EnvironmentRef struct {
+ ID string `json:"id" doc:"Environment ID"`
+ Name string `json:"name" doc:"Environment display name"`
+}
+
+type EnvironmentFailure struct {
+ Environment EnvironmentRef `json:"environment" doc:"Environment that failed"`
+ Message string `json:"message" doc:"Why the environment's results are missing"`
+}
+
+//nolint:recvcheck // Rows read the tag by value and set it through a pointer.
+type EnvironmentTag struct {
+ EnvironmentID string `json:"environmentId,omitempty" doc:"Environment the row came from in a multi-environment list"`
+ EnvironmentName string `json:"environmentName,omitempty" doc:"Display name of the environment the row came from"`
+}
+
+func (t EnvironmentTag) Environment() EnvironmentRef {
+ return EnvironmentRef{ID: t.EnvironmentID, Name: t.EnvironmentName}
+}
+
+func (t *EnvironmentTag) SetEnvironment(ref EnvironmentRef) {
+ t.EnvironmentID, t.EnvironmentName = ref.ID, ref.Name
+}
diff --git a/types/base/response.go b/types/base/response.go
index 780e88135..06e8b435f 100644
--- a/types/base/response.go
+++ b/types/base/response.go
@@ -22,15 +22,17 @@ type PaginationResponse struct {
// ApiResponse is a generic wrapper for API responses.
type ApiResponse[T any] struct {
- Success bool `json:"success" doc:"Whether the request was successful"`
- Data T `json:"data" doc:"Response data"`
+ Success bool `json:"success" doc:"Whether the request was successful"`
+ Data T `json:"data" doc:"Response data"`
+ FailedEnvironments []EnvironmentFailure `json:"failedEnvironments,omitempty" doc:"Environments whose results are missing from a multi-environment response"`
}
// Paginated is a generic wrapper for paginated responses.
type Paginated[T any] struct {
- Success bool `json:"success" doc:"Whether the request was successful"`
- Data []T `json:"data" doc:"Array of items for the current page"`
- Pagination PaginationResponse `json:"pagination" doc:"Pagination metadata"`
+ Success bool `json:"success" doc:"Whether the request was successful"`
+ Data []T `json:"data" doc:"Array of items for the current page"`
+ Pagination PaginationResponse `json:"pagination" doc:"Pagination metadata"`
+ FailedEnvironments []EnvironmentFailure `json:"failedEnvironments,omitempty" doc:"Environments whose results are missing from a multi-environment response"`
}
// PaginatedWithWarnings is a generic wrapper for paginated responses with warnings.
@@ -43,8 +45,9 @@ type PaginatedWithWarnings[T any] struct {
// PaginatedWithCounts is a generic wrapper for paginated responses with aggregate counts.
type PaginatedWithCounts[T, C any] struct {
- Success bool `json:"success" doc:"Whether the request was successful"`
- Data []T `json:"data" doc:"Array of items for the current page"`
- Counts C `json:"counts" doc:"Aggregate counts for the filtered collection"`
- Pagination PaginationResponse `json:"pagination" doc:"Pagination metadata"`
+ Success bool `json:"success" doc:"Whether the request was successful"`
+ Data []T `json:"data" doc:"Array of items for the current page"`
+ Counts C `json:"counts" doc:"Aggregate counts for the filtered collection"`
+ Pagination PaginationResponse `json:"pagination" doc:"Pagination metadata"`
+ FailedEnvironments []EnvironmentFailure `json:"failedEnvironments,omitempty" doc:"Environments whose results are missing from a multi-environment response"`
}
diff --git a/types/container/container.go b/types/container/container.go
index 83b035f2a..87994e868 100644
--- a/types/container/container.go
+++ b/types/container/container.go
@@ -9,6 +9,7 @@ import (
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
imagetypes "github.com/getarcaneapp/arcane/types/v2/image"
)
@@ -837,6 +838,8 @@ type ResourceSample struct {
// Summary represents a container summary.
type Summary struct {
+ base.EnvironmentTag
+
// ID is the unique identifier of the container.
//
// Required: true
diff --git a/types/httpx/client.go b/types/httpx/client.go
index fdb0428bf..090f632c0 100644
--- a/types/httpx/client.go
+++ b/types/httpx/client.go
@@ -7,4 +7,5 @@ import "time"
type ClientOptions struct {
Timeout time.Duration
TLSHandshakeTimeout time.Duration
+ DialTimeout time.Duration
}
diff --git a/types/image/build_history.go b/types/image/build_history.go
index f5e592bdf..baa2d19e1 100644
--- a/types/image/build_history.go
+++ b/types/image/build_history.go
@@ -1,8 +1,14 @@
package image
-import "time"
+import (
+ "time"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+)
// BuildRecord represents a historical image build entry.
+//
+//nolint:recvcheck // Rows read the environment by value and set it through a pointer.
type BuildRecord struct {
CreatedAt time.Time `json:"createdAt" sortable:"true"`
BuildArgs map[string]string `json:"buildArgs,omitempty"`
@@ -17,6 +23,7 @@ type BuildRecord struct {
Ulimits map[string]string `json:"ulimits,omitempty"`
ID string `json:"id" sortable:"true"`
EnvironmentID string `json:"environmentId"`
+ EnvironmentName string `json:"environmentName,omitempty"`
Status string `json:"status" sortable:"true"`
Provider string `json:"provider,omitempty"`
ContextDir string `json:"contextDir"`
@@ -38,3 +45,11 @@ type BuildRecord struct {
Load bool `json:"load"`
OutputTruncated bool `json:"outputTruncated"`
}
+
+func (r BuildRecord) Environment() base.EnvironmentRef {
+ return base.EnvironmentRef{ID: r.EnvironmentID, Name: r.EnvironmentName}
+}
+
+func (r *BuildRecord) SetEnvironment(ref base.EnvironmentRef) {
+ r.EnvironmentID, r.EnvironmentName = ref.ID, ref.Name
+}
diff --git a/types/image/image.go b/types/image/image.go
index d8b294ad9..b0d511540 100644
--- a/types/image/image.go
+++ b/types/image/image.go
@@ -24,6 +24,8 @@ type ListResponse struct {
//
// Required: true
MaxImageUploadSize int `json:"maxImageUploadSize" doc:"Maximum image archive upload size in MB"`
+
+ FailedEnvironments []base.EnvironmentFailure `json:"failedEnvironments,omitempty" doc:"Environments whose results are missing from a multi-environment response"`
}
type UpdateInfo struct {
@@ -111,6 +113,8 @@ type UsedBy struct {
}
type Summary struct {
+ base.EnvironmentTag
+
// ID is the unique identifier of the image.
//
// Required: true
diff --git a/types/imagepatch/imagepatch.go b/types/imagepatch/imagepatch.go
index a69cdf86c..925c7de2c 100644
--- a/types/imagepatch/imagepatch.go
+++ b/types/imagepatch/imagepatch.go
@@ -1,7 +1,11 @@
// Package imagepatch contains shared types for Copacetic-based image patching.
package imagepatch
-import "time"
+import (
+ "time"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+)
// PatchMode describes how package updates for a patch run were determined.
type PatchMode string
@@ -47,6 +51,8 @@ type PatchScanSummary struct {
// PatchTarget describes a scanned image from the patching point of view: how
// many of its vulnerabilities are fixable and what the latest patch run did.
type PatchTarget struct {
+ base.EnvironmentTag
+
ImageID string `json:"imageId"`
ImageRef string `json:"imageRef"`
FixableCount int `json:"fixableCount"`
diff --git a/types/network/network.go b/types/network/network.go
index 1d605c996..6a7357efa 100644
--- a/types/network/network.go
+++ b/types/network/network.go
@@ -4,9 +4,13 @@ import (
"time"
"github.com/moby/moby/api/types/network"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
)
type Summary struct {
+ base.EnvironmentTag
+
// ID is the unique identifier of the network.
//
// Required: true
diff --git a/types/port/port.go b/types/port/port.go
index 7c401e716..c4293b6cb 100644
--- a/types/port/port.go
+++ b/types/port/port.go
@@ -1,6 +1,10 @@
package port
+import "github.com/getarcaneapp/arcane/types/v2/base"
+
type PortMapping struct {
+ base.EnvironmentTag
+
// ID is the stable unique identifier for the mapping row.
//
// Required: true
diff --git a/types/project/project.go b/types/project/project.go
index 29831b178..fe165e5c1 100644
--- a/types/project/project.go
+++ b/types/project/project.go
@@ -5,6 +5,7 @@ import (
composetypes "github.com/compose-spec/compose-go/v2/types"
+ "github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
imagetypes "github.com/getarcaneapp/arcane/types/v2/image"
)
@@ -453,6 +454,8 @@ type ConfigurationError struct {
// Details contains detailed information about a project.
type Details struct {
+ base.EnvironmentTag
+
// Tags are the effective UI and Compose tags for the project.
Tags []Tag `json:"tags"`
diff --git a/types/settings/settings.go b/types/settings/settings.go
index 43ae19c98..3f8d4ab4a 100644
--- a/types/settings/settings.go
+++ b/types/settings/settings.go
@@ -269,6 +269,11 @@ type Update struct {
// Required: false
AvatarMaxUploadSizeMb *string `json:"avatarMaxUploadSizeMb,omitzero"`
+ // AllEnvironmentsLimit is the maximum number of environments in one multi-environment request.
+ //
+ // Required: false
+ AllEnvironmentsLimit *string `json:"allEnvironmentsLimit,omitzero"`
+
ExperimentalFeaturesEnabled *string `json:"experimentalFeaturesEnabled,omitzero"`
// DevelopmentBrandingEnabled shows the development icon on non-stable builds.
diff --git a/types/volume/volume.go b/types/volume/volume.go
index cb5eaa9bb..23f7d36c8 100644
--- a/types/volume/volume.go
+++ b/types/volume/volume.go
@@ -1,9 +1,15 @@
package volume
-import "github.com/moby/moby/api/types/volume"
+import (
+ "github.com/moby/moby/api/types/volume"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
+)
// Volume represents a Docker volume.
type Volume struct {
+ base.EnvironmentTag
+
// ID is the unique identifier of the volume.
//
// Required: true
diff --git a/types/vulnerability/vulnerability.go b/types/vulnerability/vulnerability.go
index 3a3b3640c..277f8f536 100644
--- a/types/vulnerability/vulnerability.go
+++ b/types/vulnerability/vulnerability.go
@@ -2,6 +2,8 @@ package vulnerability
import (
"time"
+
+ "github.com/getarcaneapp/arcane/types/v2/base"
)
// ScannerStatus reports vulnerability scanner availability and version.
@@ -114,6 +116,7 @@ type Vulnerability struct {
// VulnerabilityWithImage represents a vulnerability with its source image context
type VulnerabilityWithImage struct {
Vulnerability
+ base.EnvironmentTag
// Ignored marks a vulnerability the user chose to ignore. Only set when
// the list was requested with ignored entries included.