From c6b20e92eaf0d9e058fa0f45042c929b76666bcf Mon Sep 17 00:00:00 2001 From: bbedward Date: Thu, 12 Nov 2026 14:37:44 +0000 Subject: [PATCH] api: let every signed in user read system information GET /system/get required viewer on the system, which only instance owners hold. Team and project members got Forbidden in the domain card, where the DNS record needs the external IP, and in volume expand, which needs the storage limits. Reading it now takes a session and no role. It returns the external IP, version, storage capacity, networking provider, wildcard domain and BuildKit settings. Updating settings still requires editor on the system. --- apps/api/internal/api/handlers/system/meta.go | 5 +- .../internal/api/handlers/system/system.go | 2 +- .../internal/services/system/read_settings.go | 17 +---- .../services/system/read_settings_test.go | 62 +++++++++++++++++++ 4 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 apps/api/internal/services/system/read_settings_test.go diff --git a/apps/api/internal/api/handlers/system/meta.go b/apps/api/internal/api/handlers/system/meta.go index ecdd3b0b..91122d0e 100644 --- a/apps/api/internal/api/handlers/system/meta.go +++ b/apps/api/internal/api/handlers/system/meta.go @@ -35,8 +35,7 @@ type SystemMetaResponse struct { } func (self *HandlerGroup) GetSystemInformation(ctx context.Context, input *server.BaseAuthInput) (*SystemMetaResponse, error) { - user, _, err := self.srv.AuthenticatedUser(ctx) - if err != nil { + if _, _, err := self.srv.AuthenticatedUser(ctx); err != nil { return nil, err } @@ -61,7 +60,7 @@ func (self *HandlerGroup) GetSystemInformation(ctx context.Context, input *serve } // Get buildkit settings - settings, err := self.srv.SystemService.GetSettings(ctx, user.ID) + settings, err := self.srv.SystemService.GetSettings(ctx) if err != nil { log.Error("Error getting buildkit settings", "err", err) return nil, oapi.MapError(err) diff --git a/apps/api/internal/api/handlers/system/system.go b/apps/api/internal/api/handlers/system/system.go index cfadcce1..2c1d01be 100644 --- a/apps/api/internal/api/handlers/system/system.go +++ b/apps/api/internal/api/handlers/system/system.go @@ -20,7 +20,7 @@ func RegisterHandlers(server *server.Server, grp *huma.Group) { oapi.Register(grp, oapi.Read, huma.Operation{ OperationID: "get-system-information", Summary: "Get System Information", - Description: "Get system information such as the external load balancer IP, used for DNS configuration.", + Description: "Get system information such as the external load balancer IP, used for DNS configuration. Any signed in user can read it.", Path: "/get", Method: http.MethodGet, }, handlers.GetSystemInformation) diff --git a/apps/api/internal/services/system/read_settings.go b/apps/api/internal/services/system/read_settings.go index ddc49085..6613c817 100644 --- a/apps/api/internal/services/system/read_settings.go +++ b/apps/api/internal/services/system/read_settings.go @@ -22,22 +22,7 @@ type SystemSettingsUpdateInput struct { BuildkitSettings *schema.BuildkitSettings `json:"buildkit_settings" required:"false" doc:"Buildkit settings"` } -func (self *SystemService) GetSettings(ctx context.Context, requesterUserID uuid.UUID) (*SystemSettingsResponse, error) { - permissionChecks := []permissions_repo.PermissionCheck{ - { - Action: schema.ActionViewer, - ResourceType: schema.ResourceTypeSystem, - }, - } - - if err := self.repo.Permissions().Check( - ctx, - requesterUserID, - permissionChecks, - ); err != nil { - return nil, err - } - +func (self *SystemService) GetSettings(ctx context.Context) (*SystemSettingsResponse, error) { settings, err := self.repo.System().GetSystemSettings(ctx, nil) if err != nil { log.Errorf("Failed to get system settings from DB: %v", err) diff --git a/apps/api/internal/services/system/read_settings_test.go b/apps/api/internal/services/system/read_settings_test.go new file mode 100644 index 00000000..bef9b517 --- /dev/null +++ b/apps/api/internal/services/system/read_settings_test.go @@ -0,0 +1,62 @@ +package system_service + +import ( + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/suite" + "github.com/unbindapp/unbind-api/config" + "github.com/unbindapp/unbind-api/ent" + "github.com/unbindapp/unbind-api/ent/schema" + "github.com/unbindapp/unbind-api/internal/common/errdefs" + "github.com/unbindapp/unbind-api/internal/infrastructure/buildkitd" + permissions_repo "github.com/unbindapp/unbind-api/internal/repositories/permissions" + "github.com/unbindapp/unbind-api/internal/services" + "k8s.io/client-go/kubernetes/fake" +) + +type SettingsSuite struct { + services.ServiceTestSuite + service *SystemService +} + +func (suite *SettingsSuite) SetupTest() { + suite.ServiceTestSuite.SetupTest() + cfg := &config.Config{SystemNamespace: "unbind-system"} + suite.service = &SystemService{ + cfg: cfg, + repo: suite.MockRepo, + buildkitManager: buildkitd.NewBuildkitSettingsManager(cfg, suite.MockRepo, suite.MockK8s), + k8s: suite.MockK8s, + } +} + +func (suite *SettingsSuite) TearDownTest() { + suite.ServiceTestSuite.TearDownTest() +} + +func (suite *SettingsSuite) TestGetSettingsNeedsNoSystemRole() { + wildcard := "apps.example.com" + suite.MockSystemRepo.EXPECT().GetSystemSettings(suite.Ctx, nil).Return(&ent.SystemSetting{WildcardBaseURL: &wildcard}, nil).Once() + suite.MockK8s.EXPECT().GetInternalClient().Return(fake.NewClientset()).Once() + + settings, err := suite.service.GetSettings(suite.Ctx) + suite.Require().NoError(err) + suite.Equal(wildcard, *settings.WildcardDomain) + suite.False(settings.CanUpdateBuildkit) +} + +func (suite *SettingsSuite) TestUpdateSettingsNeedsSystemEditor() { + memberID := uuid.New() + suite.MockPermissionsRepo.EXPECT(). + Check(suite.Ctx, memberID, []permissions_repo.PermissionCheck{{Action: schema.ActionEditor, ResourceType: schema.ResourceTypeSystem}}). + Return(errdefs.ErrUnauthorized).Once() + + wildcard := "apps.example.com" + _, err := suite.service.UpdateSettings(suite.Ctx, memberID, &SystemSettingsUpdateInput{WildcardDomain: &wildcard}) + suite.ErrorIs(err, errdefs.ErrUnauthorized) +} + +func TestSettingsSuite(t *testing.T) { + suite.Run(t, new(SettingsSuite)) +}