Skip to content

Commit

Permalink
Validate workspace scope on DSL submission.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Nov 26, 2023
1 parent 66bedbe commit 504d851
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -125,6 +128,8 @@ private Workspace fromDsl(String dsl) throws StructurizrDslParserException {
workspace.getViews().createDefaultViews();
}

WorkspaceValidationUtils.validateWorkspaceScope(workspace);

return workspace;
}

Expand Down

0 comments on commit 504d851

Please sign in to comment.