Skip to content

Commit 3c3ecbf

Browse files
committed
add config option to disable organization creation in single organization mode
Signed-off-by: rafi <refaei.shikho@hotmail.com>
1 parent 97f7763 commit 3c3ecbf

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

controllers/org_controller.go

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

41-
func NewOrganizationController(repository shared.OrganizationRepository, orgService shared.OrgService, rbacProvider shared.RBACProvider, projectService shared.ProjectService, invitationRepository shared.InvitationRepository) *OrgController {
42+
func NewOrganizationController(repository shared.OrganizationRepository, orgService shared.OrgService, rbacProvider shared.RBACProvider, projectService shared.ProjectService, invitationRepository shared.InvitationRepository, configService shared.ConfigService) *OrgController {
4243
return &OrgController{
4344
organizationRepository: repository,
4445
orgService: orgService,
4546
rbacProvider: rbacProvider,
4647
projectService: projectService,
4748
invitationRepository: invitationRepository,
49+
configService: configService,
4850
}
4951
}
5052

@@ -56,6 +58,15 @@ func NewOrganizationController(repository shared.OrganizationRepository, orgServ
5658
// @Success 200 {object} models.Org
5759
// @Router /organizations [post]
5860
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(400, "creating organizations is not allowed in single organization mode")
69+
}
5970

6071
var req dtos.OrgCreateRequest
6172
if err := ctx.Bind(&req); err != nil {
@@ -71,7 +82,7 @@ func (controller *OrgController) Create(ctx shared.Context) error {
7182
return echo.NewHTTPError(400, "slug is required")
7283
}
7384

74-
err := controller.orgService.CreateOrganization(ctx, &organization)
85+
err = controller.orgService.CreateOrganization(ctx, &organization)
7586
if err != nil {
7687
return err
7788
}

daemons/daemon.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package daemons
33
import (
44
"context"
55
"log/slog"
6+
"os"
67
"time"
78

89
"github.com/l3montree-dev/devguard/shared"
@@ -68,8 +69,23 @@ func (runner *DaemonRunner) CleanupOrphanedRecords(ctx context.Context) error {
6869
return nil
6970
}
7071

72+
func (runner *DaemonRunner) SetInstanceSettings(ctx context.Context) error {
73+
singleOrganizationMode := os.Getenv("SINGLE_ORGANIZATION_MODE")
74+
if singleOrganizationMode == "true" {
75+
return runner.configService.SetJSONConfig(ctx, "instance_settings", shared.InstanceSettings{
76+
SingleOrganizationMode: true,
77+
})
78+
}
79+
return nil
80+
}
81+
7182
func (runner *DaemonRunner) runDaemons() {
7283
ctx := context.Background()
84+
85+
if err := runner.SetInstanceSettings(ctx); err != nil {
86+
slog.Error("could not set instance settings", "err", err)
87+
}
88+
7389
if err := runner.maybeRunAndMark("maintain.cleanup", func() error {
7490
return runner.CleanupOrphanedRecords(ctx)
7591
}); err != nil {

router/apiv1_router.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func NewAPIV1Router(srv api.Server,
4141
pool *pgxpool.Pool,
4242
thirdPartyIntegration shared.IntegrationAggregate,
4343
oryAdmin shared.AdminClient,
44+
configService shared.ConfigService,
4445
assetController *controllers.AssetController,
4546
intotoController *controllers.InToToController,
4647
csafController *controllers.CSAFController,
@@ -202,6 +203,17 @@ func NewAPIV1Router(srv api.Server,
202203
apiV1Router.POST("/scan-unauthenticated/", scanController.ScanDependencyVulnUnauthenticated)
203204
apiV1Router.GET("/renovate/recommendation/", dependencyVulnController.GetRecommendation)
204205

206+
apiV1Router.GET("/instance-settings/", func(ctx echo.Context) error {
207+
var settings shared.InstanceSettings
208+
err := configService.GetJSONConfig(ctx.Request().Context(), "instance_settings", &settings)
209+
if err != nil {
210+
return ctx.JSON(404, map[string]string{
211+
"error": "instance settings not found",
212+
})
213+
}
214+
return ctx.JSON(200, settings)
215+
})
216+
205217
// csaf routes
206218
apiV1Router.GET("/.well-known/csaf-aggregator/aggregator.json/", csafController.GetAggregatorJSON)
207219
apiV1Router.GET("/organizations/:organization/csaf/provider-metadata.json/", csafController.GetProviderMetadataForOrganization, middlewares.CsafMiddleware(true, orgRepository, projectRepository, assetRepository, assetVersionRepository, artifactRepository))

shared/common_interfaces.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,7 @@ const (
749749
ContainerScan ScannerType = "container-scan"
750750
TestScanner ScannerType = "test-scanner"
751751
)
752+
753+
type InstanceSettings struct {
754+
SingleOrganizationMode bool `json:"singleOrganizationMode"`
755+
}

0 commit comments

Comments
 (0)