Skip to content

Commit 36467a7

Browse files
committed
add instance settings middleware
Signed-off-by: rafi <refaei.shikho@hotmail.com>
1 parent ff0f64e commit 36467a7

6 files changed

Lines changed: 27 additions & 23 deletions

File tree

controllers/org_controller.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@ type OrgController struct {
3636
rbacProvider shared.RBACProvider
3737
projectService shared.ProjectService
3838
invitationRepository shared.InvitationRepository
39-
configService shared.ConfigService
4039
}
4140

42-
func NewOrganizationController(repository shared.OrganizationRepository, orgService shared.OrgService, rbacProvider shared.RBACProvider, projectService shared.ProjectService, invitationRepository shared.InvitationRepository, configService shared.ConfigService) *OrgController {
41+
func NewOrganizationController(repository shared.OrganizationRepository, orgService shared.OrgService, rbacProvider shared.RBACProvider, projectService shared.ProjectService, invitationRepository shared.InvitationRepository) *OrgController {
4342
return &OrgController{
4443
organizationRepository: repository,
4544
orgService: orgService,
4645
rbacProvider: rbacProvider,
4746
projectService: projectService,
4847
invitationRepository: invitationRepository,
49-
configService: configService,
5048
}
5149
}
5250

@@ -58,16 +56,6 @@ func NewOrganizationController(repository shared.OrganizationRepository, orgServ
5856
// @Success 200 {object} models.Org
5957
// @Router /organizations [post]
6058
func (controller *OrgController) Create(ctx shared.Context) error {
61-
var settings shared.InstanceSettings
62-
err := controller.configService.GetJSONConfig(ctx.Request().Context(), "instance_settings", &settings)
63-
if err != nil {
64-
// if there is an error getting the instance settings, we assume that the instance settings do not exist and we allow the creation of the organization
65-
settings = shared.InstanceSettings{}
66-
}
67-
if settings.SingleOrganizationMode {
68-
return echo.NewHTTPError(403, "creating organizations is not allowed in single organization mode")
69-
}
70-
7159
var req dtos.OrgCreateRequest
7260
if err := ctx.Bind(&req); err != nil {
7361
return err
@@ -82,8 +70,7 @@ func (controller *OrgController) Create(ctx shared.Context) error {
8270
return echo.NewHTTPError(400, "slug is required")
8371
}
8472

85-
err = controller.orgService.CreateOrganization(ctx, &organization)
86-
if err != nil {
73+
if err := controller.orgService.CreateOrganization(ctx, &organization); err != nil {
8774
return err
8875
}
8976

daemons/daemon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ func (runner *DaemonRunner) CleanupOrphanedRecords(ctx context.Context) error {
7272
func (runner *DaemonRunner) SetInstanceSettings(ctx context.Context) error {
7373
singleOrganizationMode := os.Getenv("SINGLE_ORGANIZATION_MODE")
7474
if singleOrganizationMode == "true" {
75-
return runner.configService.SetJSONConfig(ctx, "instance_settings", shared.InstanceSettings{
75+
return runner.configService.SetJSONConfig(ctx, "instanceSettings", shared.InstanceSettings{
7676
SingleOrganizationMode: true,
7777
})
7878
} else {
79-
return runner.configService.SetJSONConfig(ctx, "instance_settings", shared.InstanceSettings{
79+
return runner.configService.SetJSONConfig(ctx, "instanceSettings", shared.InstanceSettings{
8080
SingleOrganizationMode: false,
8181
})
8282
}

daemons/providers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ func NewDaemonRunner(
141141

142142
// Start initiates all background daemons
143143
func (runner *DaemonRunner) Start(ctx context.Context) {
144+
if err := runner.SetInstanceSettings(ctx); err != nil {
145+
slog.Error("could not set instance settings", "err", err)
146+
}
147+
144148
go func() {
145149
runner.tick()
146150
ticker := time.NewTicker(5 * time.Minute)
@@ -152,10 +156,6 @@ func (runner *DaemonRunner) Start(ctx context.Context) {
152156
}
153157

154158
func (runner *DaemonRunner) tick() {
155-
if err := runner.SetInstanceSettings(context.Background()); err != nil {
156-
slog.Error("could not set instance settings", "err", err)
157-
}
158-
159159
if runner.leaderElector.IsLeader() {
160160
slog.Info("this instance is the leader - running background jobs")
161161
runner.runDaemons()

middlewares/access_control_middlewares.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ import (
2929
"github.com/labstack/echo/v4"
3030
)
3131

32+
func InstanceSettings(configService shared.ConfigService, disabled func(shared.InstanceSettings) bool) echo.MiddlewareFunc {
33+
return func(next echo.HandlerFunc) echo.HandlerFunc {
34+
return func(ctx echo.Context) error {
35+
var settings shared.InstanceSettings
36+
if err := configService.GetJSONConfig(ctx.Request().Context(), "instanceSettings", &settings); err != nil {
37+
// settings not found — allow the request
38+
return next(ctx)
39+
}
40+
if disabled(settings) {
41+
return echo.NewHTTPError(403, "this endpoint is disabled by the instance configuration")
42+
}
43+
return next(ctx)
44+
}
45+
}
46+
}
47+
3248
func OrganizationAccessControlMiddleware(obj shared.Object, act shared.Action) echo.MiddlewareFunc {
3349
return func(next echo.HandlerFunc) echo.HandlerFunc {
3450
return func(ctx echo.Context) error {

router/apiv1_router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func NewAPIV1Router(srv api.Server,
272272

273273
apiV1Router.GET("/instance-settings/", func(ctx echo.Context) error {
274274
var settings shared.InstanceSettings
275-
err := configService.GetJSONConfig(ctx.Request().Context(), "instance_settings", &settings)
275+
err := configService.GetJSONConfig(ctx.Request().Context(), "instanceSettings", &settings)
276276
if err != nil {
277277
// If the setting is not found, return empty settings with 200 status
278278
return ctx.JSON(200, shared.InstanceSettings{})

router/org_router.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type OrgRouter struct {
3030

3131
func NewOrgRouter(
3232
sessionGroup SessionRouter,
33+
configService shared.ConfigService,
3334
orgController *controllers.OrgController,
3435
projectController *controllers.ProjectController,
3536
dependencyProxyController *dependencyfirewall.DependencyProxyController,
@@ -49,7 +50,7 @@ func NewOrgRouter(
4950
*/
5051
orgRouter := sessionGroup.Group.Group("/organizations")
5152
orgRouter.GET("/", orgController.List)
52-
orgRouter.POST("/", orgController.Create, middlewares.NeededScope([]string{"manage"}))
53+
orgRouter.POST("/", orgController.Create, middlewares.InstanceSettings(configService, func(s shared.InstanceSettings) bool { return s.SingleOrganizationMode }), middlewares.NeededScope([]string{"manage"}))
5354

5455
/**
5556
Organization scoped router

0 commit comments

Comments
 (0)