From 504d8510badaa7873bb0c626386c681484a5ee7c Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Sun, 26 Nov 2023 10:42:06 +0000 Subject: [PATCH] Validate workspace scope on DSL submission. --- .../workspace/WorkspaceComponentImpl.java | 19 ++--------------- .../util/WorkspaceValidationUtils.java | 21 +++++++++++++++++++ .../web/workspace/DslEditorController.java | 9 ++++++-- 3 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 structurizr-onpremises/src/main/java/com/structurizr/onpremises/util/WorkspaceValidationUtils.java diff --git a/structurizr-onpremises/src/main/java/com/structurizr/onpremises/component/workspace/WorkspaceComponentImpl.java b/structurizr-onpremises/src/main/java/com/structurizr/onpremises/component/workspace/WorkspaceComponentImpl.java index e76cfc1..7fc24d1 100644 --- a/structurizr-onpremises/src/main/java/com/structurizr/onpremises/component/workspace/WorkspaceComponentImpl.java +++ b/structurizr-onpremises/src/main/java/com/structurizr/onpremises/component/workspace/WorkspaceComponentImpl.java @@ -15,10 +15,7 @@ import com.structurizr.onpremises.domain.Image; import com.structurizr.onpremises.domain.InputStreamAndContentLength; import com.structurizr.onpremises.domain.User; -import com.structurizr.onpremises.util.Configuration; -import com.structurizr.onpremises.util.DateUtils; -import com.structurizr.onpremises.util.Features; -import com.structurizr.onpremises.util.StructurizrProperties; +import com.structurizr.onpremises.util.*; import com.structurizr.util.StringUtils; import com.structurizr.util.WorkspaceUtils; import com.structurizr.validation.WorkspaceScopeValidationException; @@ -312,7 +309,7 @@ public void putWorkspace(long workspaceId, String json) throws WorkspaceComponen } else { Workspace workspace = WorkspaceUtils.fromJson(json); - validateWorkspaceScope(workspace); + WorkspaceValidationUtils.validateWorkspaceScope(workspace); workspace.setId(workspaceId); workspace.setLastModifiedDate(DateUtils.removeMilliseconds(DateUtils.getNow())); @@ -407,18 +404,6 @@ public void putWorkspace(long workspaceId, String json) throws WorkspaceComponen } } - private void validateWorkspaceScope(Workspace workspace) throws WorkspaceScopeValidationException { - // if workspace scope validation is enabled, reject workspaces without a defined scope - if (Configuration.getInstance().isFeatureEnabled(Features.WORKSPACE_SCOPE_VALIDATION)) { - if (workspace.getConfiguration().getScope() == null) { - throw new WorkspaceScopeValidationException("Strict workspace scope validation has been enabled on this on-premises installation, but this workspace has no defined scope - see https://docs.structurizr.com/workspaces for more information."); - } - } - - // validate workspace scope - WorkspaceScopeValidatorFactory.getValidator(workspace).validate(workspace); - } - private WorkspaceEvent createWorkspaceEvent(WorkspaceMetaData workspaceMetaData, String workspaceAsJson) { return new WorkspaceEvent() { diff --git a/structurizr-onpremises/src/main/java/com/structurizr/onpremises/util/WorkspaceValidationUtils.java b/structurizr-onpremises/src/main/java/com/structurizr/onpremises/util/WorkspaceValidationUtils.java new file mode 100644 index 0000000..e39c7c6 --- /dev/null +++ b/structurizr-onpremises/src/main/java/com/structurizr/onpremises/util/WorkspaceValidationUtils.java @@ -0,0 +1,21 @@ +package com.structurizr.onpremises.util; + +import com.structurizr.Workspace; +import com.structurizr.validation.WorkspaceScopeValidationException; +import com.structurizr.validation.WorkspaceScopeValidatorFactory; + +public class WorkspaceValidationUtils { + + public static void validateWorkspaceScope(Workspace workspace) throws WorkspaceScopeValidationException { + // if workspace scope validation is enabled, reject workspaces without a defined scope + if (Configuration.getInstance().isFeatureEnabled(Features.WORKSPACE_SCOPE_VALIDATION)) { + if (workspace.getConfiguration().getScope() == null) { + throw new WorkspaceScopeValidationException("Strict workspace scope validation has been enabled on this on-premises installation, but this workspace has no defined scope - see https://docs.structurizr.com/workspaces for more information."); + } + } + + // validate workspace scope + WorkspaceScopeValidatorFactory.getValidator(workspace).validate(workspace); + } + +} diff --git a/structurizr-onpremises/src/main/java/com/structurizr/onpremises/web/workspace/DslEditorController.java b/structurizr-onpremises/src/main/java/com/structurizr/onpremises/web/workspace/DslEditorController.java index 0667cd3..bfd3965 100644 --- a/structurizr-onpremises/src/main/java/com/structurizr/onpremises/web/workspace/DslEditorController.java +++ b/structurizr-onpremises/src/main/java/com/structurizr/onpremises/web/workspace/DslEditorController.java @@ -8,8 +8,11 @@ import com.structurizr.onpremises.domain.User; import com.structurizr.onpremises.util.Configuration; import com.structurizr.onpremises.util.HtmlUtils; +import com.structurizr.onpremises.util.WorkspaceValidationUtils; import com.structurizr.util.StringUtils; import com.structurizr.util.WorkspaceUtils; +import com.structurizr.validation.WorkspaceScopeValidationException; +import com.structurizr.validation.WorkspaceScopeValidatorFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.security.access.prepost.PreAuthorize; @@ -98,7 +101,7 @@ public DslEditorResponse postToDslEditor( newWorkspace.getViews().getConfiguration().copyConfigurationFrom(oldWorkspace.getViews().getConfiguration()); return new DslEditorResponse(WorkspaceUtils.toJson(newWorkspace, false)); - } catch (StructurizrDslParserException e) { + } catch (StructurizrDslParserException | WorkspaceScopeValidationException e) { String errorMessage = e.getMessage(); return new DslEditorResponse(false, errorMessage); @@ -112,7 +115,7 @@ public DslEditorResponse postToDslEditor( } } - private Workspace fromDsl(String dsl) throws StructurizrDslParserException { + private Workspace fromDsl(String dsl) throws StructurizrDslParserException, WorkspaceScopeValidationException { StructurizrDslParser parser = new StructurizrDslParser(); parser.setRestricted(true); parser.parse(dsl); @@ -125,6 +128,8 @@ private Workspace fromDsl(String dsl) throws StructurizrDslParserException { workspace.getViews().createDefaultViews(); } + WorkspaceValidationUtils.validateWorkspaceScope(workspace); + return workspace; }