Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: streamline remote environment logic (#2524)
Kyle Mendella29b241290ea
diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.goindex dc04e4380..f5c353384 100644--- a/backend/internal/bootstrap/jobs_bootstrap.go+++ b/backend/internal/bootstrap/jobs_bootstrap.go@@ -133,13 +133,13 @@ func syncTimeoutSettingsToAgentsInternal(ctx context.Context, appServices *Servislog.InfoContext(ctx, "Syncing environment settings to remote environments", "count", len(envs), "keys", keys)for _, env := range envs {- _, statusCode, err := appServices.Environment.ProxyRequest(ctx, env.ID, http.MethodPut, "/api/environments/0/settings", body)+ resp, err := appServices.Environment.ExecuteRemoteRequest(ctx, env.ID, http.MethodPut, "/api/environments/0/settings", body)if err != nil {slog.WarnContext(ctx, "Failed to sync timeout settings to environment", "environmentID", env.ID, "environmentName", env.Name, "error", err)continue}- if statusCode != http.StatusOK {- slog.WarnContext(ctx, "Environment returned non-OK status for timeout sync", "environmentID", env.ID, "environmentName", env.Name, "statusCode", statusCode)+ if err := resp.RequireSuccess(); err != nil {+ slog.WarnContext(ctx, "Environment returned non-OK status for timeout sync", "environmentID", env.ID, "environmentName", env.Name, "statusCode", resp.StatusCode, "response", string(resp.Body))continue}slog.DebugContext(ctx, "Successfully synced timeout settings to environment", "environmentID", env.ID, "environmentName", env.Name)diff --git a/backend/internal/huma/handlers/helpers.go b/backend/internal/huma/handlers/helpers.goindex a57beb135..6ddc41525 100644--- a/backend/internal/huma/handlers/helpers.go+++ b/backend/internal/huma/handlers/helpers.go@@ -2,10 +2,14 @@ package handlersimport ("context"+ "encoding/json"+ "errors""github.com/danielgtaylor/huma/v2"humamw "github.com/getarcaneapp/arcane/backend/internal/huma/middleware"+ "github.com/getarcaneapp/arcane/backend/internal/services""github.com/getarcaneapp/arcane/backend/pkg/pagination"+ "github.com/getarcaneapp/arcane/backend/pkg/remenv")// checkAdmin checks if the current user is an admin and returns a 403 error if not.@@ -48,3 +52,54 @@ func buildPaginationParams(page, start, limit int, sortCol, sortDir, search stri}return params}++func proxyRemoteJSONInternal[T any](+ ctx context.Context,+ environmentService *services.EnvironmentService,+ environmentID string,+ method string,+ path string,+ requestBody any,+) (*T, error) {+ body, err := marshalRemoteRequestBodyInternal(requestBody)+ if err != nil {+ return nil, huma.Error500InternalServerError("failed to marshal request body: " + err.Error())+ }++ var output T+ if err := environmentService.ProxyJSONRequest(ctx, environmentID, method, path, body, &output); err != nil {+ return nil, translateRemoteProxyErrorInternal(err)+ }++ return &output, nil+}++func marshalRemoteRequestBodyInternal(requestBody any) ([]byte, error) {+ switch value := requestBody.(type) {+ case nil:+ return nil, nil+ case []byte:+ return value, nil+ default:+ return json.Marshal(value)+ }+}++func translateRemoteProxyErrorInternal(err error) error {+ var transportErr *remenv.TransportError+ if errors.As(err, &transportErr) {+ return huma.Error502BadGateway("failed to proxy request to environment: " + transportErr.Error())+ }++ var statusErr *remenv.StatusError+ if errors.As(err, &statusErr) {+ return huma.NewError(statusErr.StatusCode, "environment returned error: "+string(statusErr.Body), nil)+ }++ var decodeErr *remenv.DecodeError+ if errors.As(err, &decodeErr) {+ return huma.Error500InternalServerError("failed to decode environment response: " + decodeErr.Error())+ }++ return huma.Error500InternalServerError("failed to proxy request to environment: " + err.Error())+}diff --git a/backend/internal/huma/handlers/job_schedules.go b/backend/internal/huma/handlers/job_schedules.goindex 1fb0f7cd6..f99a4540a 100644--- a/backend/internal/huma/handlers/job_schedules.go+++ b/backend/internal/huma/handlers/job_schedules.go@@ -2,7 +2,6 @@ package handlersimport ("context"- "encoding/json""net/http""github.com/danielgtaylor/huma/v2"@@ -118,18 +117,11 @@ func (h *JobSchedulesHandler) ListJobs(ctx context.Context, input *ListJobsInputif h.environmentService == nil {return nil, huma.Error500InternalServerError("environment service not available")}- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.ID, http.MethodGet, "/api/environments/0/jobs", nil)+ jobs, err := proxyRemoteJSONInternal[jobschedule.JobListResponse](ctx, h.environmentService, input.ID, http.MethodGet, "/api/environments/0/jobs", nil)if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())+ return nil, err}- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "environment returned error: "+string(respBody), nil)- }- var jobs jobschedule.JobListResponse- if err := json.Unmarshal(respBody, &jobs); err != nil {- return nil, huma.Error500InternalServerError("failed to decode environment response: " + err.Error())- }- return &GetJobsOutput{Body: jobs}, nil+ return &GetJobsOutput{Body: *jobs}, nil}jobs, err := h.jobService.ListJobs(ctx)@@ -149,18 +141,11 @@ func (h *JobSchedulesHandler) RunJob(ctx context.Context, input *RunJobInput) (*if h.environmentService == nil {return nil, huma.Error500InternalServerError("environment service not available")}- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.ID, http.MethodPost, "/api/environments/0/jobs/"+input.JobID+"/run", nil)+ runResp, err := proxyRemoteJSONInternal[jobschedule.JobRunResponse](ctx, h.environmentService, input.ID, http.MethodPost, "/api/environments/0/jobs/"+input.JobID+"/run", nil)if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())- }- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "environment returned error: "+string(respBody), nil)+ return nil, err}- var runResp jobschedule.JobRunResponse- if err := json.Unmarshal(respBody, &runResp); err != nil {- return nil, huma.Error500InternalServerError("failed to decode environment response: " + err.Error())- }- return &RunJobOutput{Body: runResp}, nil+ return &RunJobOutput{Body: *runResp}, nil}err := h.jobService.RunJobNowInline(ctx, input.JobID)@@ -185,18 +170,11 @@ func (h *JobSchedulesHandler) Get(ctx context.Context, input *GetJobSchedulesInpif h.environmentService == nil {return nil, huma.Error500InternalServerError("environment service not available")}- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.ID, http.MethodGet, "/api/environments/0/job-schedules", nil)+ cfg, err := proxyRemoteJSONInternal[jobschedule.Config](ctx, h.environmentService, input.ID, http.MethodGet, "/api/environments/0/job-schedules", nil)if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())- }- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "environment returned error: "+string(respBody), nil)- }- var cfg jobschedule.Config- if err := json.Unmarshal(respBody, &cfg); err != nil {- return nil, huma.Error500InternalServerError("failed to decode environment response: " + err.Error())+ return nil, err}- return &GetJobSchedulesOutput{Body: cfg}, nil+ return &GetJobSchedulesOutput{Body: *cfg}, nil}cfg := h.jobService.GetJobSchedules(ctx)@@ -213,25 +191,12 @@ func (h *JobSchedulesHandler) Update(ctx context.Context, input *UpdateJobSchedureturn nil, huma.Error500InternalServerError("environment service not available")}- body, err := json.Marshal(input.Body)+ apiResp, err := proxyRemoteJSONInternal[base.ApiResponse[jobschedule.Config]](ctx, h.environmentService, input.ID, http.MethodPut, "/api/environments/0/job-schedules", input.Body)if err != nil {- return nil, huma.Error500InternalServerError("failed to marshal request body: " + err.Error())- }-- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.ID, http.MethodPut, "/api/environments/0/job-schedules", body)- if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())- }- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "environment returned error: "+string(respBody), nil)- }-- var apiResp base.ApiResponse[jobschedule.Config]- if err := json.Unmarshal(respBody, &apiResp); err != nil {- return nil, huma.Error500InternalServerError("failed to decode environment response: " + err.Error())+ return nil, err}- return &UpdateJobSchedulesOutput{Body: apiResp}, nil+ return &UpdateJobSchedulesOutput{Body: *apiResp}, nil}cfg, err := h.jobService.UpdateJobSchedules(ctx, input.Body)diff --git a/backend/internal/huma/handlers/remenv_handlers_test.go b/backend/internal/huma/handlers/remenv_handlers_test.gonew file mode 100644index 000000000..5204f8ff7--- /dev/null+++ b/backend/internal/huma/handlers/remenv_handlers_test.go@@ -0,0 +1,141 @@+package handlers++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "testing"+ "time"++ "github.com/danielgtaylor/huma/v2"+ "github.com/getarcaneapp/arcane/backend/internal/database"+ "github.com/getarcaneapp/arcane/backend/internal/models"+ "github.com/getarcaneapp/arcane/backend/internal/services"+ "github.com/getarcaneapp/arcane/types/base"+ "github.com/getarcaneapp/arcane/types/env"+ "github.com/getarcaneapp/arcane/types/jobschedule"+ settingstypes "github.com/getarcaneapp/arcane/types/settings"+ glsqlite "github.com/glebarez/sqlite"+ "github.com/stretchr/testify/require"+ "gorm.io/gorm"+)++func setupRemoteHandlerEnvironmentServiceInternal(t *testing.T, server *httptest.Server) *services.EnvironmentService {+ t.Helper()++ db, err := gorm.Open(glsqlite.Open(":memory:"), &gorm.Config{})+ require.NoError(t, err)+ require.NoError(t, db.AutoMigrate(&models.Environment{}))++ now := time.Now()+ env := &models.Environment{+ BaseModel: models.BaseModel{+ ID: "env-remote",+ CreatedAt: now,+ UpdatedAt: &now,+ },+ Name: "Remote Env",+ ApiUrl: server.URL,+ Status: string(models.EnvironmentStatusOnline),+ Enabled: true,+ IsEdge: false,+ }+ require.NoError(t, db.WithContext(context.Background()).Create(env).Error)++ return services.NewEnvironmentService(&database.DB{DB: db}, server.Client(), nil, nil, nil, nil)+}++func TestProxyRemoteJSONInternal_MapsStatusError(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "bad request from remote", http.StatusBadRequest)+ }))+ defer server.Close()++ envSvc := setupRemoteHandlerEnvironmentServiceInternal(t, server)++ _, err := proxyRemoteJSONInternal[jobschedule.JobListResponse](context.Background(), envSvc, "env-remote", http.MethodGet, "/api/environments/0/jobs", nil)+ require.Error(t, err)++ var statusErr huma.StatusError+ require.ErrorAs(t, err, &statusErr)+ require.Equal(t, http.StatusBadRequest, statusErr.GetStatus())+ require.Contains(t, statusErr.Error(), "bad request from remote")+}++func TestProxyRemoteJSONInternal_MapsDecodeError(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusOK)+ _, _ = w.Write([]byte(`{"broken"`))+ }))+ defer server.Close()++ envSvc := setupRemoteHandlerEnvironmentServiceInternal(t, server)++ _, err := proxyRemoteJSONInternal[jobschedule.JobListResponse](context.Background(), envSvc, "env-remote", http.MethodGet, "/api/environments/0/jobs", nil)+ require.Error(t, err)++ var statusErr huma.StatusError+ require.ErrorAs(t, err, &statusErr)+ require.Equal(t, http.StatusInternalServerError, statusErr.GetStatus())+ require.Contains(t, statusErr.Error(), "failed to decode environment response")+}++func TestJobSchedulesHandler_ListJobs_RemoteSuccess(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.Equal(t, "/api/environments/0/jobs", r.URL.Path)+ require.NoError(t, json.NewEncoder(w).Encode(jobschedule.JobListResponse{+ Jobs: []jobschedule.JobStatus{{ID: "job-1", Name: "Test Job"}},+ }))+ }))+ defer server.Close()++ handler := &JobSchedulesHandler{+ jobService: &services.JobService{},+ environmentService: setupRemoteHandlerEnvironmentServiceInternal(t, server),+ }++ output, err := handler.ListJobs(context.Background(), &ListJobsInput{ID: "env-remote"})+ require.NoError(t, err)+ require.Len(t, output.Body.Jobs, 1)+ require.Equal(t, "job-1", output.Body.Jobs[0].ID)+}++func TestSettingsHandler_GetPublicSettings_RemoteSuccess(t *testing.T) {+ expected := []settingstypes.PublicSetting{{Key: "theme", Type: "string", Value: "dark"}}+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.Equal(t, "/api/environments/0/settings/public", r.URL.Path)+ require.NoError(t, json.NewEncoder(w).Encode(expected))+ }))+ defer server.Close()++ handler := &SettingsHandler{+ settingsService: &services.SettingsService{},+ environmentService: setupRemoteHandlerEnvironmentServiceInternal(t, server),+ }++ output, err := handler.GetPublicSettings(context.Background(), &GetPublicSettingsInput{EnvironmentID: "env-remote"})+ require.NoError(t, err)+ require.Equal(t, expected, output.Body)+}++func TestTemplateHandler_GetGlobalVariables_RemoteSuccess(t *testing.T) {+ expected := base.ApiResponse[[]env.Variable]{+ Success: true,+ Data: []env.Variable{{Key: "FOO", Value: "bar"}},+ }+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.Equal(t, "/api/environments/0/templates/variables", r.URL.Path)+ require.NoError(t, json.NewEncoder(w).Encode(expected))+ }))+ defer server.Close()++ handler := &TemplateHandler{+ templateService: &services.TemplateService{},+ environmentService: setupRemoteHandlerEnvironmentServiceInternal(t, server),+ }++ output, err := handler.GetGlobalVariables(context.Background(), &GetGlobalVariablesInput{EnvironmentID: "env-remote"})+ require.NoError(t, err)+ require.Equal(t, expected, output.Body)+}diff --git a/backend/internal/huma/handlers/settings.go b/backend/internal/huma/handlers/settings.goindex 38119943a..d5c8d2595 100644--- a/backend/internal/huma/handlers/settings.go+++ b/backend/internal/huma/handlers/settings.go@@ -2,7 +2,6 @@ package handlersimport ("context"- "encoding/json""errors""net/http""strconv"@@ -237,18 +236,11 @@ func (h *SettingsHandler) GetPublicSettings(ctx context.Context, input *GetPubliif h.environmentService == nil {return nil, huma.Error500InternalServerError("environment service not available")}- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.EnvironmentID, http.MethodGet, "/api/environments/0/settings/public", nil)+ settingsDto, err := proxyRemoteJSONInternal[[]settings.PublicSetting](ctx, h.environmentService, input.EnvironmentID, http.MethodGet, "/api/environments/0/settings/public", nil)if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())+ return nil, err}- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "environment returned error: "+string(respBody), nil)- }- var settingsDto []settings.PublicSetting- if err := json.Unmarshal(respBody, &settingsDto); err != nil {- return nil, huma.Error500InternalServerError("failed to decode environment response: " + err.Error())- }- return &GetPublicSettingsOutput{Body: settingsDto}, nil+ return &GetPublicSettingsOutput{Body: *settingsDto}, nil}settingsList := h.settingsService.ListSettings(models.SettingVisibilityPublic)@@ -273,18 +265,11 @@ func (h *SettingsHandler) GetSettings(ctx context.Context, input *GetSettingsInpif h.environmentService == nil {return nil, huma.Error500InternalServerError("environment service not available")}- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.EnvironmentID, http.MethodGet, "/api/environments/0/settings", nil)+ settingsDto, err := proxyRemoteJSONInternal[[]settings.PublicSetting](ctx, h.environmentService, input.EnvironmentID, http.MethodGet, "/api/environments/0/settings", nil)if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())- }- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "environment returned error: "+string(respBody), nil)+ return nil, err}- var settingsDto []settings.PublicSetting- if err := json.Unmarshal(respBody, &settingsDto); err != nil {- return nil, huma.Error500InternalServerError("failed to decode environment response: " + err.Error())- }- return &GetSettingsOutput{Body: settingsDto}, nil+ return &GetSettingsOutput{Body: *settingsDto}, nil}visibility := models.SettingVisibilityNonAdmin@@ -358,25 +343,12 @@ func (h *SettingsHandler) updateSettingsForRemoteEnvironment(ctx context.Contextreturn nil, huma.Error403Forbidden((&common.AuthSettingsUpdateError{}).Error())}- body, err := json.Marshal(input.Body)+ apiResp, err := proxyRemoteJSONInternal[base.ApiResponse[[]settings.SettingDto]](ctx, h.environmentService, input.EnvironmentID, http.MethodPut, "/api/environments/0/settings", input.Body)if err != nil {- return nil, huma.Error500InternalServerError("failed to marshal request body: " + err.Error())- }-- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.EnvironmentID, http.MethodPut, "/api/environments/0/settings", body)- if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())- }- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "environment returned error: "+string(respBody), nil)- }-- var apiResp base.ApiResponse[[]settings.SettingDto]- if err := json.Unmarshal(respBody, &apiResp); err != nil {- return nil, huma.Error500InternalServerError("failed to decode environment response: " + err.Error())+ return nil, err}- return &UpdateSettingsOutput{Body: apiResp}, nil+ return &UpdateSettingsOutput{Body: *apiResp}, nil}func (h *SettingsHandler) updateSettingsForLocalEnvironment(ctx context.Context, input settings.Update) (*UpdateSettingsOutput, error) {diff --git a/backend/internal/huma/handlers/templates.go b/backend/internal/huma/handlers/templates.goindex 31476246c..ae943e292 100644--- a/backend/internal/huma/handlers/templates.go+++ b/backend/internal/huma/handlers/templates.go@@ -868,21 +868,12 @@ func (h *TemplateHandler) getGlobalVariablesForRemoteEnvironmentInternal(ctx conreturn nil, huma.Error500InternalServerError("environment service not available")}- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.EnvironmentID, http.MethodGet, "/api/environments/0/templates/variables", nil)+ response, err := proxyRemoteJSONInternal[base.ApiResponse[[]env.Variable]](ctx, h.environmentService, input.EnvironmentID, http.MethodGet, "/api/environments/0/templates/variables", nil)if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())+ return nil, err}- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "remote environment returned error: "+string(respBody), nil)- }-- var response base.ApiResponse[[]env.Variable]- if err := json.Unmarshal(respBody, &response); err != nil {- return nil, huma.Error500InternalServerError("failed to parse remote response: " + err.Error())- }-- return &GetGlobalVariablesOutput{Body: response}, nil+ return &GetGlobalVariablesOutput{Body: *response}, nil}// UpdateGlobalVariables updates global template variables.@@ -914,24 +905,10 @@ func (h *TemplateHandler) updateGlobalVariablesForRemoteEnvironmentInternal(ctxreturn nil, huma.Error500InternalServerError("environment service not available")}- body, err := json.Marshal(input.Body)+ response, err := proxyRemoteJSONInternal[base.ApiResponse[base.MessageResponse]](ctx, h.environmentService, input.EnvironmentID, http.MethodPut, "/api/environments/0/templates/variables", input.Body)if err != nil {- return nil, huma.Error500InternalServerError("failed to marshal request body: " + err.Error())- }-- respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.EnvironmentID, http.MethodPut, "/api/environments/0/templates/variables", body)- if err != nil {- return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())- }-- if statusCode != http.StatusOK {- return nil, huma.NewError(statusCode, "remote environment returned error: "+string(respBody), nil)- }-- var response base.ApiResponse[base.MessageResponse]- if err := json.Unmarshal(respBody, &response); err != nil {- return nil, huma.Error500InternalServerError("failed to parse remote response: " + err.Error())+ return nil, err}- return &UpdateGlobalVariablesOutput{Body: response}, nil+ return &UpdateGlobalVariablesOutput{Body: *response}, nil}diff --git a/backend/internal/services/dashboard_service.go b/backend/internal/services/dashboard_service.goindex c5e514a2a..32abc7133 100644--- a/backend/internal/services/dashboard_service.go+++ b/backend/internal/services/dashboard_service.go@@ -2,9 +2,9 @@ package servicesimport ("context"- "encoding/json""fmt""log/slog"+ "net/http""sort""time"@@ -505,24 +505,17 @@ func (s *DashboardService) getSnapshotForEnvironmentInternal(return s.GetSnapshot(reqCtx, options)}- respBody, statusCode, err := s.environmentService.ProxyRequest(+ var response base.ApiResponse[dashboardtypes.Snapshot]+ if err := s.environmentService.ProxyJSONRequest(reqCtx,env.ID,- "GET",+ http.MethodGet,buildEnvironmentDashboardProxyPathInternal(options),nil,- )- if err != nil {+ &response,+ ); err != nil {return nil, fmt.Errorf("failed to proxy dashboard snapshot: %w", err)}- if statusCode < 200 || statusCode >= 300 {- return nil, fmt.Errorf("unexpected dashboard status code: %d", statusCode)- }-- var response base.ApiResponse[dashboardtypes.Snapshot]- if err := json.Unmarshal(respBody, &response); err != nil {- return nil, fmt.Errorf("failed to decode dashboard snapshot: %w", err)- }if !response.Success {return nil, fmt.Errorf("dashboard snapshot request was not successful")}@@ -557,27 +550,18 @@ func (s *DashboardService) getVersionInfoForEnvironmentInternal(return nil}- respBody, statusCode, err := s.environmentService.ProxyRequest(+ var versionInfo versiontypes.Info+ if err := s.environmentService.ProxyJSONRequest(reqCtx,env.ID,- "GET",+ http.MethodGet,"/api/app-version",nil,- )- if err != nil {+ &versionInfo,+ ); err != nil {slog.DebugContext(reqCtx, "Failed to fetch environment version info for dashboard", "environment_id", env.ID, "error", err)return nil}- if statusCode < 200 || statusCode >= 300 {- slog.DebugContext(reqCtx, "Unexpected environment version status code for dashboard", "environment_id", env.ID, "status_code", statusCode)- return nil- }-- var versionInfo versiontypes.Info- if err := json.Unmarshal(respBody, &versionInfo); err != nil {- slog.DebugContext(reqCtx, "Failed to decode environment version info for dashboard", "environment_id", env.ID, "error", err)- return nil- }return &versionInfo}diff --git a/backend/internal/services/environment_service.go b/backend/internal/services/environment_service.goindex 7bfbca3d7..13219cd84 100644--- a/backend/internal/services/environment_service.go+++ b/backend/internal/services/environment_service.go@@ -18,6 +18,7 @@ import ("github.com/getarcaneapp/arcane/backend/pkg/libarcane/edge""github.com/getarcaneapp/arcane/backend/pkg/libarcane/timeouts""github.com/getarcaneapp/arcane/backend/pkg/pagination"+ "github.com/getarcaneapp/arcane/backend/pkg/remenv""github.com/getarcaneapp/arcane/backend/pkg/utils/mapper""github.com/getarcaneapp/arcane/types/containerregistry""github.com/getarcaneapp/arcane/types/environment"@@ -34,6 +35,7 @@ type EnvironmentService struct {eventService *EventServicesettingsService *SettingsServiceapiKeyService *ApiKeyService+ remoteClient *remenv.ClienttokenCacheMu sync.RWMutextokenCache map[string]edgeTokenCacheEntrytokenByEnvID map[string]string@@ -62,8 +64,12 @@ func NewEnvironmentService(db *database.DB, httpClient *http.Client, dockerServieventService: eventService,settingsService: settingsService,apiKeyService: apiKeyService,- tokenCache: make(map[string]edgeTokenCacheEntry),- tokenByEnvID: make(map[string]string),+ remoteClient: remenv.NewClient(httpClient, remenv.TunnelTransportFuncs{+ EnsureAvailableFunc: ensureRemoteEnvironmentTunnelAvailableInternal,+ DoFunc: doRemoteEnvironmentTunnelRequestInternal,+ }),+ tokenCache: make(map[string]edgeTokenCacheEntry),+ tokenByEnvID: make(map[string]string),}}@@ -1124,20 +1130,184 @@ func buildMTLSDeploymentSnippetInternal(managerURL string, apiKey string, genera}}-// SyncRegistriesToEnvironment syncs all registries from this manager to a remote environment-func (s *EnvironmentService) SyncRegistriesToEnvironment(ctx context.Context, environmentID string) error {- // Get the environment- environment, err := s.GetEnvironmentByID(ctx, environmentID)+type remoteEnvironmentTargetInternal struct {+ ID string+ Name string+ IsEdge bool+ AccessToken *string+ TargetURL string+}++func (s *EnvironmentService) resolveRemoteEnvironmentTargetInternal(ctx context.Context, envID string) (*remoteEnvironmentTargetInternal, error) {+ environment, err := s.GetEnvironmentByID(ctx, envID)+ if err != nil {+ return nil, fmt.Errorf("failed to get environment: %w", err)+ }++ if envID == "0" {+ return nil, fmt.Errorf("cannot proxy request to local environment")+ }++ return &remoteEnvironmentTargetInternal{+ ID: environment.ID,+ Name: environment.Name,+ IsEdge: environment.IsEdge,+ AccessToken: environment.AccessToken,+ TargetURL: strings.TrimRight(environment.ApiUrl, "/"),+ }, nil+}++func (s *EnvironmentService) getProxyRequestContextInternal(ctx context.Context) (context.Context, context.CancelFunc) {+ if s != nil && s.settingsService != nil {+ settings := s.settingsService.GetSettingsConfig()+ return timeouts.WithTimeout(ctx, settings.ProxyRequestTimeout.AsInt(), timeouts.DefaultProxyRequest)+ }++ return context.WithTimeout(ctx, timeouts.DefaultProxyRequest) //nolint:gosec // helper intentionally returns the cancel func to callers.+}++func (s *EnvironmentService) buildRemoteRequestInternal(+ target *remoteEnvironmentTargetInternal,+ method string,+ path string,+ body []byte,+ headers map[string]string,+) (remenv.Request, error) {+ if target == nil {+ return remenv.Request{}, fmt.Errorf("remote environment target is required")+ }++ requestHeaders := make(map[string]string, len(headers)+2)+ for key, value := range headers {+ requestHeaders[key] = value+ }+ if len(body) > 0 && method != http.MethodGet && requestHeaders["Content-Type"] == "" {+ requestHeaders["Content-Type"] = "application/json"+ }+ remenv.ApplyAgentTokenHeaderMap(requestHeaders, target.AccessToken)++ return remenv.Request{+ EnvironmentID: target.ID,+ IsEdge: target.IsEdge,+ Method: method,+ URL: target.TargetURL + path,+ Path: path,+ Headers: requestHeaders,+ Body: body,+ }, nil+}++func (s *EnvironmentService) ExecuteRemoteRequest(ctx context.Context, envID string, method string, path string, body []byte) (*remenv.Response, error) {+ target, err := s.resolveRemoteEnvironmentTargetInternal(ctx, envID)+ if err != nil {+ return nil, err+ }++ return s.executeRemoteRequestForTargetInternal(ctx, target, method, path, body)+}++func (s *EnvironmentService) executeRemoteRequestForTargetInternal(+ ctx context.Context,+ target *remoteEnvironmentTargetInternal,+ method string,+ path string,+ body []byte,+) (*remenv.Response, error) {+ request, err := s.buildRemoteRequestInternal(target, method, path, body, nil)+ if err != nil {+ return nil, err+ }++ resp, err := s.remoteClient.Do(ctx, request)+ if err != nil {+ return nil, fmt.Errorf("failed to send request to environment %s: %w", target.Name, err)+ }++ return resp, nil+}++func (s *EnvironmentService) ProxyJSONRequest(ctx context.Context, envID string, method string, path string, body []byte, out any) error {+ proxyCtx, cancel := s.getProxyRequestContextInternal(ctx)+ defer cancel()++ target, err := s.resolveRemoteEnvironmentTargetInternal(proxyCtx, envID)+ if err != nil {+ return err+ }++ return s.proxyJSONRequestForTargetInternal(proxyCtx, target, method, path, body, out)+}++func (s *EnvironmentService) proxyJSONRequestForTargetInternal(+ ctx context.Context,+ target *remoteEnvironmentTargetInternal,+ method string,+ path string,+ body []byte,+ out any,+) error {+ resp, err := s.executeRemoteRequestForTargetInternal(ctx, target, method, path, body)+ if err != nil {+ return err+ }+ if err := resp.RequireSuccess(); err != nil {+ return err+ }+ if err := resp.DecodeJSON(out); err != nil {+ return err+ }++ return nil+}++func ensureRemoteEnvironmentTunnelAvailableInternal(ctx context.Context, envID string) error {+ if edge.HasActiveTunnel(envID) {+ return nil+ }++ if _, ok := edge.RequestTunnelAndWait(ctx, envID, edge.DefaultTunnelDemandTTL, edge.DefaultTunnelAcquireTimeout()); ok {+ return nil+ }++ return fmt.Errorf("edge agent is not connected")+}++func doRemoteEnvironmentTunnelRequestInternal(+ ctx context.Context,+ envID string,+ method string,+ path string,+ headers map[string]string,+ body []byte,+) (*remenv.Response, error) {+ tunnel, ok := edge.GetRegistry().Get(envID)+ if !ok {+ return nil, fmt.Errorf("no active tunnel for environment %s", envID)+ }+ if tunnel.Conn.IsClosed() {+ return nil, fmt.Errorf("tunnel for environment %s is closed", envID)+ }++ statusCode, respHeaders, respBody, err := edge.ProxyRequest(ctx, tunnel, method, path, "", headers, body)if err != nil {- return fmt.Errorf("failed to get environment: %w", err)+ return nil, fmt.Errorf("tunnel request failed: %w", err)}- // Don't sync to local environment (ID "0")- if environmentID == "0" {- return fmt.Errorf("cannot sync registries to local environment")+ return &remenv.Response{+ StatusCode: statusCode,+ Body: respBody,+ Headers: respHeaders,+ }, nil+}++// SyncRegistriesToEnvironment syncs all registries from this manager to a remote environment+func (s *EnvironmentService) SyncRegistriesToEnvironment(ctx context.Context, environmentID string) error {+ target, err := s.resolveRemoteEnvironmentTargetInternal(ctx, environmentID)+ if err != nil {+ return err}- slog.InfoContext(ctx, "Starting registry sync to environment", "environmentID", environmentID, "environmentName", environment.Name, "apiUrl", environment.ApiUrl)+ slog.InfoContext(ctx, "Starting registry sync to environment", "environmentID", environmentID, "environmentName", target.Name, "apiUrl", target.TargetURL)// Get all registries from this managervar registries []models.ContainerRegistry@@ -1201,37 +1371,10 @@ func (s *EnvironmentService) SyncRegistriesToEnvironment(ctx context.Context, enreturn fmt.Errorf("failed to marshal sync request: %w", err)}- // Send the sync request to the remote environmentreqCtx, cancel := context.WithTimeout(ctx, 30*time.Second)defer cancel()- // Build headers- headers := map[string]string{- "Content-Type": "application/json",- }- if environment.AccessToken != nil && *environment.AccessToken != "" {- headers["X-Arcane-Agent-Token"] = *environment.AccessToken- headers["X-API-Key"] = *environment.AccessToken- slog.DebugContext(ctx, "Set auth headers for sync request")- } else {- slog.WarnContext(ctx, "No access token available for environment sync", "environmentID", environmentID)- }-- targetURL := strings.TrimRight(environment.ApiUrl, "/") + "/api/container-registries/sync"- apiPath := "/api/container-registries/sync"-- slog.InfoContext(ctx, "Sending sync request to agent", "url", targetURL, "registryCount", len(syncItems), "isEdge", environment.IsEdge)-- // Use edge-aware client that routes through tunnel for edge environments- resp, err := edge.DoEdgeAwareRequest(reqCtx, environmentID, environment.IsEdge, http.MethodPost, targetURL, apiPath, headers, reqBody)- if err != nil {- return fmt.Errorf("failed to send sync request: %w", err)- }-- if resp.StatusCode != http.StatusOK {- slog.ErrorContext(ctx, "Sync request failed", "statusCode", resp.StatusCode, "response", string(resp.Body))- return fmt.Errorf("sync request failed with status %d: %s", resp.StatusCode, string(resp.Body))- }+ slog.InfoContext(ctx, "Sending sync request to agent", "url", target.TargetURL+"/api/container-registries/sync", "registryCount", len(syncItems), "isEdge", target.IsEdge)var result struct {Success bool `json:"success"`@@ -1239,33 +1382,27 @@ func (s *EnvironmentService) SyncRegistriesToEnvironment(ctx context.Context, enMessage string `json:"message"`} `json:"data"`}- if err := json.Unmarshal(resp.Body, &result); err != nil {- return fmt.Errorf("failed to decode sync response: %w", err)+ if err := s.proxyJSONRequestForTargetInternal(reqCtx, target, http.MethodPost, "/api/container-registries/sync", reqBody, &result); err != nil {+ return fmt.Errorf("failed to send sync request: %w", err)}if !result.Success {return fmt.Errorf("sync failed: %s", result.Data.Message)}- slog.InfoContext(ctx, "Successfully synced registries to environment", "environmentID", environmentID, "environmentName", environment.Name)+ slog.InfoContext(ctx, "Successfully synced registries to environment", "environmentID", environmentID, "environmentName", target.Name)return nil}// SyncRepositoriesToEnvironment syncs all git repositories from this manager to a remote environmentfunc (s *EnvironmentService) SyncRepositoriesToEnvironment(ctx context.Context, environmentID string) error {- // Get the environment- environment, err := s.GetEnvironmentByID(ctx, environmentID)+ target, err := s.resolveRemoteEnvironmentTargetInternal(ctx, environmentID)if err != nil {- return fmt.Errorf("failed to get environment: %w", err)- }-- // Don't sync to local environment (ID "0")- if environmentID == "0" {- return fmt.Errorf("cannot sync repositories to local environment")+ return err}- slog.InfoContext(ctx, "Starting git repository sync to environment", "environmentID", environmentID, "environmentName", environment.Name, "apiUrl", environment.ApiUrl)+ slog.InfoContext(ctx, "Starting git repository sync to environment", "environmentID", environmentID, "environmentName", target.Name, "apiUrl", target.TargetURL)// Get all git repositories from this managervar repositories []models.GitRepository@@ -1326,37 +1463,10 @@ func (s *EnvironmentService) SyncRepositoriesToEnvironment(ctx context.Context,return fmt.Errorf("failed to marshal sync request: %w", err)}- // Send the sync request to the remote environmentreqCtx, cancel := context.WithTimeout(ctx, 30*time.Second)defer cancel()- // Build headers- headers := map[string]string{- "Content-Type": "application/json",- }- if environment.AccessToken != nil && *environment.AccessToken != "" {- headers["X-Arcane-Agent-Token"] = *environment.AccessToken- headers["X-API-Key"] = *environment.AccessToken- slog.DebugContext(ctx, "Set auth headers for git repository sync request")- } else {- slog.WarnContext(ctx, "No access token available for environment git repository sync", "environmentID", environmentID)- }-- targetURL := strings.TrimRight(environment.ApiUrl, "/") + "/api/git-repositories/sync"- apiPath := "/api/git-repositories/sync"-- slog.InfoContext(ctx, "Sending git repository sync request to agent", "url", targetURL, "repositoryCount", len(syncItems), "isEdge", environment.IsEdge)-- // Use edge-aware client that routes through tunnel for edge environments- resp, err := edge.DoEdgeAwareRequest(reqCtx, environmentID, environment.IsEdge, http.MethodPost, targetURL, apiPath, headers, reqBody)- if err != nil {- return fmt.Errorf("failed to send sync request: %w", err)- }-- if resp.StatusCode != http.StatusOK {- slog.ErrorContext(ctx, "Git repository sync request failed", "statusCode", resp.StatusCode, "response", string(resp.Body))- return fmt.Errorf("sync request failed with status %d: %s", resp.StatusCode, string(resp.Body))- }+ slog.InfoContext(ctx, "Sending git repository sync request to agent", "url", target.TargetURL+"/api/git-repositories/sync", "repositoryCount", len(syncItems), "isEdge", target.IsEdge)var result struct {Success bool `json:"success"`@@ -1364,51 +1474,27 @@ func (s *EnvironmentService) SyncRepositoriesToEnvironment(ctx context.Context,Message string `json:"message"`} `json:"data"`}- if err := json.Unmarshal(resp.Body, &result); err != nil {- return fmt.Errorf("failed to decode sync response: %w", err)+ if err := s.proxyJSONRequestForTargetInternal(reqCtx, target, http.MethodPost, "/api/git-repositories/sync", reqBody, &result); err != nil {+ return fmt.Errorf("failed to send sync request: %w", err)}if !result.Success {return fmt.Errorf("sync failed: %s", result.Data.Message)}- slog.InfoContext(ctx, "Successfully synced git repositories to environment", "environmentID", environmentID, "environmentName", environment.Name)+ slog.InfoContext(ctx, "Successfully synced git repositories to environment", "environmentID", environmentID, "environmentName", target.Name)return nil}// ProxyRequest sends a request to a remote environment's API.func (s *EnvironmentService) ProxyRequest(ctx context.Context, envID string, method string, path string, body []byte) ([]byte, int, error) {- environment, err := s.GetEnvironmentByID(ctx, envID)- if err != nil {- return nil, 0, fmt.Errorf("failed to get environment: %w", err)- }-- if envID == "0" {- return nil, 0, fmt.Errorf("cannot proxy request to local environment")- }-- targetURL := strings.TrimRight(environment.ApiUrl, "/") + path-- settings := s.settingsService.GetSettingsConfig()- proxyCtx, cancel := timeouts.WithTimeout(ctx, settings.ProxyRequestTimeout.AsInt(), timeouts.DefaultProxyRequest)+ proxyCtx, cancel := s.getProxyRequestContextInternal(ctx)defer cancel()- // Build headers- headers := make(map[string]string)- if method != http.MethodGet && len(body) > 0 {- headers["Content-Type"] = "application/json"- }-- // Use appropriate auth header- if environment.AccessToken != nil && *environment.AccessToken != "" {- headers["X-Arcane-Agent-Token"] = *environment.AccessToken- headers["X-API-Key"] = *environment.AccessToken- }- // Use edge-aware client that routes through tunnel for edge environments- resp, err := edge.DoEdgeAwareRequest(proxyCtx, envID, environment.IsEdge, method, targetURL, path, headers, body)+ resp, err := s.ExecuteRemoteRequest(proxyCtx, envID, method, path, body)if err != nil {- return nil, 0, fmt.Errorf("failed to send request: %w", err)+ return nil, 0, err}return resp.Body, resp.StatusCode, nildiff --git a/backend/internal/services/environment_service_test.go b/backend/internal/services/environment_service_test.goindex a2e39ec08..46bd69329 100644--- a/backend/internal/services/environment_service_test.go+++ b/backend/internal/services/environment_service_test.go@@ -24,6 +24,7 @@ import ("github.com/getarcaneapp/arcane/backend/pkg/libarcane/edge""github.com/getarcaneapp/arcane/backend/pkg/pagination""github.com/getarcaneapp/arcane/types/containerregistry"+ "github.com/getarcaneapp/arcane/types/gitops")func setupEnvironmentServiceTestDB(t *testing.T) *database.DB {@@ -258,6 +259,48 @@ func TestEnvironmentService_SyncRegistriesToEnvironment_IncludesECRFields(t *tesrequire.NoError(t, err)}+func TestEnvironmentService_SyncRepositoriesToEnvironment_UsesAgentHeaders(t *testing.T) {+ ctx := context.Background()+ db := setupEnvironmentServiceTestDB(t)+ require.NoError(t, db.AutoMigrate(&models.GitRepository{}))+ svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)++ description := "test repo"+ createTestGitRepository(t, db, models.GitRepository{+ BaseModel: models.BaseModel{ID: "repo-1", CreatedAt: time.Now()},+ Name: "repo-1",+ URL: "https://github.com/getarcaneapp/arcane.git",+ AuthType: "http",+ Username: "arcane",+ Token: encryptSecretForTest(t, "repo-token"),+ Enabled: true,+ Description: &description,+ })++ accessToken := "token-1"+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.Equal(t, http.MethodPost, r.Method)+ require.Equal(t, "/api/git-repositories/sync", r.URL.Path)+ require.Equal(t, accessToken, r.Header.Get("X-API-Key"))+ require.Equal(t, accessToken, r.Header.Get("X-Arcane-Agent-Token"))++ var syncReq gitops.RepositorySyncRequest+ require.NoError(t, json.NewDecoder(r.Body).Decode(&syncReq))+ require.Len(t, syncReq.Repositories, 1)+ require.Equal(t, "repo-token", syncReq.Repositories[0].Token)+ require.Equal(t, "arcane", syncReq.Repositories[0].Username)++ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"success":true,"data":{"message":"ok"}}`))+ }))+ defer server.Close()++ createTestEnvironment(t, db, "env-1", server.URL, &accessToken)++ err := svc.SyncRepositoriesToEnvironment(ctx, "env-1")+ require.NoError(t, err)+}+func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_SkipsRemoteWithoutAccessToken(t *testing.T) {ctx := context.Background()db := setupEnvironmentServiceTestDB(t)diff --git a/backend/internal/services/swarm_service.go b/backend/internal/services/swarm_service.goindex 60273a915..0810348c1 100644--- a/backend/internal/services/swarm_service.go+++ b/backend/internal/services/swarm_service.go@@ -643,20 +643,12 @@ func (s *SwarmService) fetchSwarmNodeIdentityViaEdgeInternal(ctx context.ContextreqCtx, cancel := context.WithTimeout(ctx, 5*time.Second)defer cancel()- body, statusCode, err := s.environmentService.ProxyRequest(reqCtx, environmentID, http.MethodGet, "/api/swarm/node-identity", nil)- if err != nil {- return nil, err- }- if statusCode != http.StatusOK {- return nil, fmt.Errorf("unexpected status code: %d", statusCode)- }-var parsed struct {Success bool `json:"success"`Data SwarmNodeIdentity `json:"data"`}- if err := json.Unmarshal(body, &parsed); err != nil {- return nil, fmt.Errorf("failed to decode swarm node identity response: %w", err)+ if err := s.environmentService.ProxyJSONRequest(reqCtx, environmentID, http.MethodGet, "/api/swarm/node-identity", nil, &parsed); err != nil {+ return nil, err}if !parsed.Success {return nil, fmt.Errorf("swarm node identity probe failed")diff --git a/backend/pkg/libarcane/edge/http_client.go b/backend/pkg/libarcane/edge/http_client.godeleted file mode 100644index 96804e420..000000000--- a/backend/pkg/libarcane/edge/http_client.go+++ /dev/null@@ -1,167 +0,0 @@-package edge--import (- "bytes"- "context"- "fmt"- "io"- "net/http"- "time"-)--// EdgeAwareClient is an HTTP client that automatically routes requests-// to edge environments through the active edge tunnel instead of direct HTTP.-// Works with both gRPC and WebSocket tunnel transports.-type EdgeAwareClient struct {- httpClient *http.Client-}--// NewEdgeAwareClient creates a new edge-aware HTTP client-func NewEdgeAwareClient(timeout time.Duration) *EdgeAwareClient {- return &EdgeAwareClient{- httpClient: &http.Client{Timeout: timeout},- }-}--// EdgeResponse wraps the response from either direct HTTP or tunnel request-type EdgeResponse struct {- StatusCode int- Body []byte- Headers map[string]string-}--// DoForEnvironment makes an HTTP request, automatically routing through the edge-// tunnel if the environment is an edge environment with an active tunnel.-// Parameters:-// - ctx: request context-// - envID: environment ID (used to find tunnel for edge envs)-// - isEdge: whether this is an edge environment-// - method: HTTP method (GET, POST, etc.)-// - url: full URL for direct requests (ignored for edge, only path is used)-// - path: API path (e.g., "/api/health") - used for both edge and non-edge-// - headers: HTTP headers to include-// - body: request body (can be nil)-//-// Returns EdgeResponse with status code, body bytes, and headers-func (c *EdgeAwareClient) DoForEnvironment(- ctx context.Context,- envID string,- isEdge bool,- method string,- url string,- path string,- headers map[string]string,- body []byte,-) (*EdgeResponse, error) {- // For edge environments with active tunnels, route through tunnel- if isEdge && HasActiveTunnel(envID) {- return c.doViaTunnel(ctx, envID, method, path, headers, body)- }-- if isEdge {- if _, ok := RequestTunnelAndWait(ctx, envID, DefaultTunnelDemandTTL, DefaultTunnelAcquireTimeout()); ok {- return c.doViaTunnel(ctx, envID, method, path, headers, body)- }- return nil, fmt.Errorf("edge agent is not connected (no active tunnel)")- }-- // For non-edge environments, do direct HTTP request- return c.doDirectHTTP(ctx, method, url, headers, body)-}--// doViaTunnel routes the request through the edge tunnel-func (c *EdgeAwareClient) doViaTunnel(- ctx context.Context,- envID string,- method string,- path string,- headers map[string]string,- body []byte,-) (*EdgeResponse, error) {- tunnel, ok := GetRegistry().Get(envID)- if !ok {- return nil, fmt.Errorf("no active tunnel for environment %s", envID)- }- if tunnel.Conn.IsClosed() {- return nil, fmt.Errorf("tunnel for environment %s is closed", envID)- }-- // Use the existing ProxyRequest function- statusCode, respHeaders, respBody, err := ProxyRequest(ctx, tunnel, method, path, "", headers, body)- if err != nil {- return nil, fmt.Errorf("tunnel request failed: %w", err)- }-- return &EdgeResponse{- StatusCode: statusCode,- Body: respBody,- Headers: respHeaders,- }, nil-}--// doDirectHTTP makes a direct HTTP request-func (c *EdgeAwareClient) doDirectHTTP(- ctx context.Context,- method string,- url string,- headers map[string]string,- body []byte,-) (*EdgeResponse, error) {- var bodyReader io.Reader- if body != nil {- bodyReader = bytes.NewReader(body)- }-- req, err := http.NewRequestWithContext(ctx, method, url, bodyReader)- if err != nil {- return nil, fmt.Errorf("failed to create request: %w", err)- }-- // Set headers- for k, v := range headers {- req.Header.Set(k, v)- }-- resp, err := c.httpClient.Do(req) //nolint:gosec // intentional request to configured environment URL in non-edge mode- if err != nil {- return nil, err- }- defer func() { _ = resp.Body.Close() }()-- respBody, err := io.ReadAll(resp.Body)- if err != nil {- return nil, fmt.Errorf("failed to read response body: %w", err)- }-- // Convert response headers- respHeaders := make(map[string]string)- for k, v := range resp.Header {- if len(v) > 0 {- respHeaders[k] = v[0]- }- }-- return &EdgeResponse{- StatusCode: resp.StatusCode,- Body: respBody,- Headers: respHeaders,- }, nil-}--// DefaultEdgeAwareClient is a singleton client with reasonable defaults-var DefaultEdgeAwareClient = NewEdgeAwareClient(30 * time.Second)--// DoRequest is a convenience function for making edge-aware requests-// using the default client-func DoEdgeAwareRequest(- ctx context.Context,- envID string,- isEdge bool,- method string,- url string,- path string,- headers map[string]string,- body []byte,-) (*EdgeResponse, error) {- return DefaultEdgeAwareClient.DoForEnvironment(ctx, envID, isEdge, method, url, path, headers, body)-}diff --git a/backend/pkg/libarcane/edge/remenv.go b/backend/pkg/libarcane/edge/remenv.goindex b52021c2e..ead5f1fb8 100644--- a/backend/pkg/libarcane/edge/remenv.go+++ b/backend/pkg/libarcane/edge/remenv.go@@ -5,6 +5,7 @@ import ("net/http""strings"+ "github.com/getarcaneapp/arcane/backend/pkg/remenv""github.com/gin-gonic/gin")@@ -46,10 +47,7 @@ func SetAuthHeader(req *http.Request, c *gin.Context) {}func SetAgentToken(req *http.Request, accessToken *string) {- if accessToken != nil && *accessToken != "" {- req.Header.Set(HeaderAgentToken, *accessToken)- req.Header.Set(HeaderAPIKey, *accessToken)- }+ remenv.ApplyAgentTokenHeaders(req.Header, accessToken)}// BuildWebSocketHeaders constructs a header set for proxying WebSocket requests@@ -77,10 +75,7 @@ func BuildWebSocketHeaders(c *gin.Context, accessToken *string) http.Header {}// Set agent token for remote environment authentication- if accessToken != nil && *accessToken != "" {- headers.Set(HeaderAgentToken, *accessToken)- headers.Set(HeaderAPIKey, *accessToken)- }+ remenv.ApplyAgentTokenHeaders(headers, accessToken)return headers}diff --git a/backend/pkg/libarcane/edge/http_client_test.go b/backend/pkg/libarcane/edge/remenv_client_integration_test.gosimilarity index 60%rename from backend/pkg/libarcane/edge/http_client_test.gorename to backend/pkg/libarcane/edge/remenv_client_integration_test.goindex fdd33eb29..283690058 100644--- a/backend/pkg/libarcane/edge/http_client_test.go+++ b/backend/pkg/libarcane/edge/remenv_client_integration_test.go@@ -3,20 +3,57 @@ package edgeimport ("context""errors"+ "fmt""net""net/http"- "net/http/httptest""testing""time"tunnelpb "github.com/getarcaneapp/arcane/backend/pkg/libarcane/edge/proto/tunnel/v1"+ "github.com/getarcaneapp/arcane/backend/pkg/remenv""github.com/stretchr/testify/assert""github.com/stretchr/testify/require""google.golang.org/grpc""google.golang.org/grpc/credentials/insecure")-func TestEdgeAwareClient_DoForEnvironment_EdgeWithTunnel(t *testing.T) {+func newTestRemenvClientInternal(timeout time.Duration) *remenv.Client {+ return remenv.NewClient(&http.Client{Timeout: timeout}, remenv.TunnelTransportFuncs{+ EnsureAvailableFunc: func(ctx context.Context, envID string) error {+ if HasActiveTunnel(envID) {+ return nil+ }++ if _, ok := RequestTunnelAndWait(ctx, envID, DefaultTunnelDemandTTL, DefaultTunnelAcquireTimeout()); ok {+ return nil+ }++ return fmt.Errorf("edge agent is not connected (no active tunnel)")+ },+ DoFunc: func(ctx context.Context, envID, method, path string, headers map[string]string, body []byte) (*remenv.Response, error) {+ tunnel, ok := GetRegistry().Get(envID)+ if !ok {+ return nil, fmt.Errorf("no active tunnel for environment %s", envID)+ }+ if tunnel.Conn.IsClosed() {+ return nil, fmt.Errorf("tunnel for environment %s is closed", envID)+ }++ statusCode, respHeaders, respBody, err := ProxyRequest(ctx, tunnel, method, path, "", headers, body)+ if err != nil {+ return nil, fmt.Errorf("tunnel request failed: %w", err)+ }++ return &remenv.Response{+ StatusCode: statusCode,+ Body: respBody,+ Headers: respHeaders,+ }, nil+ },+ })+}++func TestRemenvClient_EdgeWithTunnel(t *testing.T) {server, tunnel := setupMockAgentServer(t, func(msg *TunnelMessage) *TunnelMessage {return &TunnelMessage{ID: msg.ID,@@ -33,94 +70,39 @@ func TestEdgeAwareClient_DoForEnvironment_EdgeWithTunnel(t *testing.T) {GetRegistry().Register(envID, tunnel)defer GetRegistry().Unregister(envID)- client := NewEdgeAwareClient(1 * time.Second)-- resp, err := client.DoForEnvironment(- context.Background(),- envID,- true, // isEdge- "GET",- "http://ignored/api/health",- "/api/health",- map[string]string{"X-H": "v"},- nil,- )+ client := newTestRemenvClientInternal(1 * time.Second)++ resp, err := client.Do(context.Background(), remenv.Request{+ EnvironmentID: envID,+ IsEdge: true,+ Method: http.MethodGet,+ URL: "http://ignored/api/health",+ Path: "/api/health",+ Headers: map[string]string{"X-H": "v"},+ })require.NoError(t, err)assert.Equal(t, http.StatusOK, resp.StatusCode)assert.Equal(t, []byte(`{"edge":true}`), resp.Body)}-func TestEdgeAwareClient_DoForEnvironment_EdgeNoTunnel(t *testing.T) {- client := NewEdgeAwareClient(1 * time.Second)-- _, err := client.DoForEnvironment(- context.Background(),- "env-edge-missing",- true, // isEdge- "GET",- "http://ignored/api/health",- "/api/health",- nil,- nil,- )-- assert.Contains(t, err.Error(), "not connected")-}--func TestEdgeAwareClient_DoForEnvironment_NonEdge(t *testing.T) {- // Setup direct server- directServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- assert.Equal(t, "/api/direct", r.URL.Path)- assert.Equal(t, "val", r.Header.Get("X-Direct"))- w.WriteHeader(http.StatusAccepted)- _, _ = w.Write([]byte("direct response"))- }))- defer directServer.Close()-- client := NewEdgeAwareClient(1 * time.Second)-- resp, err := client.DoForEnvironment(- context.Background(),- "env-direct",- false, // isEdge (false)- "GET",- directServer.URL+"/api/direct",- "/api/direct",- map[string]string{"X-Direct": "val"},- nil,- )-- require.NoError(t, err)- assert.Equal(t, http.StatusAccepted, resp.StatusCode)- assert.Equal(t, []byte("direct response"), resp.Body)-}+func TestRemenvClient_EdgeNoTunnel(t *testing.T) {+ client := newTestRemenvClientInternal(1 * time.Second)-func TestDoEdgeAwareRequest_Helper(t *testing.T) {- // Just test it calls the default client correctly (using non-edge for simplicity)- directServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- w.WriteHeader(http.StatusOK)- _, _ = w.Write([]byte("helper"))- }))- defer directServer.Close()-- resp, err := DoEdgeAwareRequest(- context.Background(),- "env-helper",- false,- "GET",- directServer.URL,- "/",- nil,- nil,- )+ _, err := client.Do(context.Background(), remenv.Request{+ EnvironmentID: "env-edge-missing",+ IsEdge: true,+ Method: http.MethodGet,+ URL: "http://ignored/api/health",+ Path: "/api/health",+ })- require.NoError(t, err)- assert.Equal(t, http.StatusOK, resp.StatusCode)- assert.Equal(t, []byte("helper"), resp.Body)+ var transportErr *remenv.TransportError+ require.ErrorAs(t, err, &transportErr)+ assert.Contains(t, err.Error(), "not connected")}-func TestEdgeAwareClient_DoForEnvironment_EdgeWithGRPCTunnel(t *testing.T) {+func TestRemenvClient_EdgeWithGRPCTunnel(t *testing.T) {ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)envID := "env-edge-grpc-1"@@ -214,17 +196,16 @@ func TestEdgeAwareClient_DoForEnvironment_EdgeWithGRPCTunnel(t *testing.T) {return ok && tunnel != nil && !tunnel.Conn.IsClosed()}, time.Second, 10*time.Millisecond)- client := NewEdgeAwareClient(1 * time.Second)- resp, err := client.DoForEnvironment(- ctx,- envID,- true,- http.MethodPost,- "http://ignored/api/environments/0/projects",- "/api/environments/0/projects",- map[string]string{"X-H": "v", "Content-Type": "application/json"},- []byte(`{"edge":true}`),- )+ client := newTestRemenvClientInternal(1 * time.Second)+ resp, err := client.Do(ctx, remenv.Request{+ EnvironmentID: envID,+ IsEdge: true,+ Method: http.MethodPost,+ URL: "http://ignored/api/environments/0/projects",+ Path: "/api/environments/0/projects",+ Headers: map[string]string{"X-H": "v", "Content-Type": "application/json"},+ Body: []byte(`{"edge":true}`),+ })require.NoError(t, err)assert.Equal(t, http.StatusCreated, resp.StatusCode)diff --git a/backend/pkg/remenv/client.go b/backend/pkg/remenv/client.gonew file mode 100644index 000000000..ec2bb1bcc--- /dev/null+++ b/backend/pkg/remenv/client.go@@ -0,0 +1,267 @@+package remenv++import (+ "bytes"+ "context"+ "encoding/json"+ "fmt"+ "io"+ "net/http"+ "strings"+ "time"+)++const (+ HeaderAPIKey = "X-API-Key" // #nosec G101: header name, not a credential+ HeaderAgentToken = "X-Arcane-Agent-Token" // #nosec G101: header name, not a credential+)++type Request struct {+ EnvironmentID string+ IsEdge bool+ Method string+ URL string+ Path string+ Headers map[string]string+ Body []byte+}++type Response struct {+ StatusCode int+ Body []byte+ Headers map[string]string+}++type TunnelTransport interface {+ EnsureAvailable(ctx context.Context, envID string) error+ Do(ctx context.Context, envID, method, path string, headers map[string]string, body []byte) (*Response, error)+}++type TunnelTransportFuncs struct {+ EnsureAvailableFunc func(ctx context.Context, envID string) error+ DoFunc func(ctx context.Context, envID, method, path string, headers map[string]string, body []byte) (*Response, error)+}++func (t TunnelTransportFuncs) EnsureAvailable(ctx context.Context, envID string) error {+ if t.EnsureAvailableFunc == nil {+ return fmt.Errorf("edge transport unavailable")+ }+ return t.EnsureAvailableFunc(ctx, envID)+}++func (t TunnelTransportFuncs) Do(ctx context.Context, envID, method, path string, headers map[string]string, body []byte) (*Response, error) {+ if t.DoFunc == nil {+ return nil, fmt.Errorf("edge transport unavailable")+ }+ return t.DoFunc(ctx, envID, method, path, headers, body)+}++type Client struct {+ httpClient *http.Client+ tunnel TunnelTransport+}++func NewClient(httpClient *http.Client, tunnel TunnelTransport) *Client {+ if httpClient == nil {+ httpClient = &http.Client{Timeout: 30 * time.Second}+ }++ return &Client{+ httpClient: httpClient,+ tunnel: tunnel,+ }+}++func (c *Client) Do(ctx context.Context, req Request) (*Response, error) {+ if req.IsEdge {+ return c.doViaTunnelInternal(ctx, req)+ }++ return c.doDirectHTTPInternal(ctx, req)+}++func (c *Client) DoJSON(ctx context.Context, req Request, out any) error {+ resp, err := c.Do(ctx, req)+ if err != nil {+ return err+ }++ if err := resp.RequireSuccess(); err != nil {+ return err+ }++ return resp.DecodeJSON(out)+}++func (c *Client) doViaTunnelInternal(ctx context.Context, req Request) (*Response, error) {+ if c.tunnel == nil {+ return nil, &TransportError{Err: fmt.Errorf("edge transport unavailable")}+ }++ if err := c.tunnel.EnsureAvailable(ctx, req.EnvironmentID); err != nil {+ return nil, &TransportError{Err: err}+ }++ resp, err := c.tunnel.Do(ctx, req.EnvironmentID, req.Method, req.Path, cloneHeaders(req.Headers), req.Body)+ if err != nil {+ return nil, &TransportError{Err: err}+ }++ return resp, nil+}++func (c *Client) doDirectHTTPInternal(ctx context.Context, req Request) (*Response, error) {+ var bodyReader io.Reader+ if len(req.Body) > 0 {+ bodyReader = bytes.NewReader(req.Body)+ }++ httpReq, err := http.NewRequestWithContext(ctx, req.Method, req.URL, bodyReader)+ if err != nil {+ return nil, &TransportError{Err: fmt.Errorf("failed to create request: %w", err)}+ }++ for key, value := range req.Headers {+ httpReq.Header.Set(key, value)+ }++ resp, err := c.httpClient.Do(httpReq) //nolint:gosec // intentional request to configured remote environment URL+ if err != nil {+ return nil, &TransportError{Err: err}+ }+ defer func() { _ = resp.Body.Close() }()++ respBody, err := io.ReadAll(resp.Body)+ if err != nil {+ return nil, &TransportError{Err: fmt.Errorf("failed to read response body: %w", err)}+ }++ return &Response{+ StatusCode: resp.StatusCode,+ Body: respBody,+ Headers: flattenHeaders(resp.Header),+ }, nil+}++func (r *Response) RequireSuccess() error {+ if r != nil && r.StatusCode >= http.StatusOK && r.StatusCode < http.StatusMultipleChoices {+ return nil+ }++ if r == nil {+ return &StatusError{}+ }++ return &StatusError{+ StatusCode: r.StatusCode,+ Body: r.Body,+ }+}++func (r *Response) DecodeJSON(out any) error {+ if out == nil {+ return nil+ }+ if r == nil {+ return &DecodeError{Err: fmt.Errorf("response is nil")}+ }+ if err := json.Unmarshal(r.Body, out); err != nil {+ return &DecodeError{Err: err}+ }+ return nil+}++type TransportError struct {+ Err error+}++func (e *TransportError) Error() string {+ if e == nil || e.Err == nil {+ return "transport error"+ }+ return e.Err.Error()+}++func (e *TransportError) Unwrap() error {+ if e == nil {+ return nil+ }+ return e.Err+}++type StatusError struct {+ StatusCode int+ Body []byte+}++func (e *StatusError) Error() string {+ if e == nil {+ return "unexpected status code"+ }+ if body := strings.TrimSpace(string(e.Body)); body != "" {+ return fmt.Sprintf("unexpected status code %d: %s", e.StatusCode, body)+ }+ return fmt.Sprintf("unexpected status code %d", e.StatusCode)+}++type DecodeError struct {+ Err error+}++func (e *DecodeError) Error() string {+ if e == nil || e.Err == nil {+ return "failed to decode response body"+ }+ return fmt.Sprintf("failed to decode response body: %v", e.Err)+}++func (e *DecodeError) Unwrap() error {+ if e == nil {+ return nil+ }+ return e.Err+}++func ApplyAgentTokenHeaders(headers http.Header, accessToken *string) {+ if headers == nil || accessToken == nil || strings.TrimSpace(*accessToken) == "" {+ return+ }++ headers.Set(HeaderAgentToken, *accessToken)+ headers.Set(HeaderAPIKey, *accessToken)+}++func ApplyAgentTokenHeaderMap(headers map[string]string, accessToken *string) {+ if headers == nil || accessToken == nil || strings.TrimSpace(*accessToken) == "" {+ return+ }++ headers[HeaderAgentToken] = *accessToken+ headers[HeaderAPIKey] = *accessToken+}++func flattenHeaders(headers http.Header) map[string]string {+ if len(headers) == 0 {+ return map[string]string{}+ }++ out := make(map[string]string, len(headers))+ for key, values := range headers {+ if len(values) > 0 {+ out[key] = values[0]+ }+ }+ return out+}++func cloneHeaders(headers map[string]string) map[string]string {+ if len(headers) == 0 {+ return map[string]string{}+ }++ out := make(map[string]string, len(headers))+ for key, value := range headers {+ out[key] = value+ }+ return out+}diff --git a/backend/pkg/remenv/client_test.go b/backend/pkg/remenv/client_test.gonew file mode 100644index 000000000..da1bc2ae3--- /dev/null+++ b/backend/pkg/remenv/client_test.go@@ -0,0 +1,139 @@+package remenv++import (+ "context"+ "errors"+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/stretchr/testify/require"+)++func TestApplyAgentTokenHeaders(t *testing.T) {+ headers := http.Header{}+ ApplyAgentTokenHeaders(headers, nil)+ require.Empty(t, headers.Get(HeaderAPIKey))+ require.Empty(t, headers.Get(HeaderAgentToken))++ token := "token-123"+ ApplyAgentTokenHeaders(headers, &token)+ require.Equal(t, token, headers.Get(HeaderAPIKey))+ require.Equal(t, token, headers.Get(HeaderAgentToken))++ headerMap := map[string]string{}+ ApplyAgentTokenHeaderMap(headerMap, &token)+ require.Equal(t, token, headerMap[HeaderAPIKey])+ require.Equal(t, token, headerMap[HeaderAgentToken])+}++func TestClientDo_DirectHTTPSuccess(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.Equal(t, http.MethodPost, r.Method)+ require.Equal(t, "/api/registries/sync", r.URL.Path)+ require.Equal(t, "application/json", r.Header.Get("Content-Type"))+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(http.StatusAccepted)+ _, _ = w.Write([]byte(`{"ok":true}`))+ }))+ defer server.Close()++ client := NewClient(server.Client(), nil)+ resp, err := client.Do(context.Background(), Request{+ Method: http.MethodPost,+ URL: server.URL + "/api/registries/sync",+ Path: "/api/registries/sync",+ Headers: map[string]string{"Content-Type": "application/json"},+ Body: []byte(`{"sync":true}`),+ })+ require.NoError(t, err)+ require.Equal(t, http.StatusAccepted, resp.StatusCode)+ require.Equal(t, `{"ok":true}`, string(resp.Body))+ require.Equal(t, "application/json", resp.Headers["Content-Type"])+}++func TestClientDo_EdgeUsesTunnelTransport(t *testing.T) {+ var ensured bool+ client := NewClient(nil, TunnelTransportFuncs{+ EnsureAvailableFunc: func(ctx context.Context, envID string) error {+ ensured = true+ require.Equal(t, "env-edge-1", envID)+ return nil+ },+ DoFunc: func(ctx context.Context, envID, method, path string, headers map[string]string, body []byte) (*Response, error) {+ require.Equal(t, "env-edge-1", envID)+ require.Equal(t, http.MethodGet, method)+ require.Equal(t, "/api/health", path)+ return &Response{+ StatusCode: http.StatusOK,+ Body: []byte(`{"edge":true}`),+ Headers: map[string]string{"Content-Type": "application/json"},+ }, nil+ },+ })++ resp, err := client.Do(context.Background(), Request{+ EnvironmentID: "env-edge-1",+ IsEdge: true,+ Method: http.MethodGet,+ Path: "/api/health",+ })+ require.NoError(t, err)+ require.True(t, ensured)+ require.Equal(t, http.StatusOK, resp.StatusCode)+ require.Equal(t, `{"edge":true}`, string(resp.Body))+}++func TestClientDoJSON_ClassifiesStatusAndDecodeErrors(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch r.URL.Path {+ case "/status":+ http.Error(w, "bad gateway", http.StatusBadGateway)+ case "/decode":+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"broken"`))+ default:+ http.NotFound(w, r)+ }+ }))+ defer server.Close()++ client := NewClient(server.Client(), nil)++ var statusOut map[string]any+ err := client.DoJSON(context.Background(), Request{+ Method: http.MethodGet,+ URL: server.URL + "/status",+ Path: "/status",+ }, &statusOut)+ var statusErr *StatusError+ require.ErrorAs(t, err, &statusErr)+ require.Equal(t, http.StatusBadGateway, statusErr.StatusCode)++ var decodeOut map[string]any+ err = client.DoJSON(context.Background(), Request{+ Method: http.MethodGet,+ URL: server.URL + "/decode",+ Path: "/decode",+ }, &decodeOut)+ var decodeErr *DecodeError+ require.ErrorAs(t, err, &decodeErr)+}++func TestClientDo_WrapsTransportErrors(t *testing.T) {+ client := NewClient(nil, TunnelTransportFuncs{+ EnsureAvailableFunc: func(ctx context.Context, envID string) error {+ return errors.New("not connected")+ },+ })++ _, err := client.Do(context.Background(), Request{+ EnvironmentID: "env-edge-2",+ IsEdge: true,+ Method: http.MethodGet,+ Path: "/api/health",+ })+ var transportErr *TransportError+ require.ErrorAs(t, err, &transportErr)+ require.Contains(t, transportErr.Error(), "not connected")+}