Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
ISICO-15108: javascript validation done on updating w/d (#3805)
Browse files Browse the repository at this point in the history
* ISICO-15108: javascript validation done on updating w/d

* ISICO-14902: NPE while checking EvaluatorType is fixed

* ISICO-14902: unused variables removed

* ISICO-15108: javascript validation added in the constraint violation part

* ISICO-15108: getExpression() is used for switch javascript code
  • Loading branch information
sarmuru2 authored Nov 25, 2023
1 parent 921981f commit 43316a5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.lang.annotation.Target;
import java.text.ParseException;
import java.time.format.DateTimeParseException;
import java.util.Map;
import java.util.Optional;

import javax.script.ScriptException;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
Expand All @@ -30,6 +32,7 @@
import com.netflix.conductor.common.metadata.tasks.TaskDef;
import com.netflix.conductor.common.metadata.tasks.TaskType;
import com.netflix.conductor.common.metadata.workflow.WorkflowTask;
import com.netflix.conductor.core.events.ScriptEvaluator;
import com.netflix.conductor.core.utils.DateTimeUtils;

import static com.netflix.conductor.core.execution.tasks.Terminate.getTerminationStatusParameter;
Expand Down Expand Up @@ -166,9 +169,34 @@ private boolean isDecisionTaskValid(
context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
valid = false;
}

if (workflowTask.getCaseExpression() != null) {
try {
validateScriptExpression(
workflowTask.getCaseExpression(), workflowTask.getInputParameters());
} catch (Exception ee) {
String message =
String.format(
ee.getMessage() + ", taskType: DECISION taskName %s",
workflowTask.getName());
context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
valid = false;
}
}

return valid;
}

private void validateScriptExpression(
String expression, Map<String, Object> inputParameters) {
try {
Object returnValue = ScriptEvaluator.eval(expression, inputParameters);
} catch (ScriptException e) {
throw new IllegalArgumentException(
String.format("Expression is not well formatted: %s", e.getMessage()));
}
}

private boolean isSwitchTaskValid(
WorkflowTask workflowTask, ConstraintValidatorContext context) {
boolean valid = true;
Expand Down Expand Up @@ -209,6 +237,21 @@ private boolean isSwitchTaskValid(
context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
valid = false;
}

if ("javascript".equals(workflowTask.getEvaluatorType())
&& workflowTask.getExpression() != null) {
try {
validateScriptExpression(
workflowTask.getExpression(), workflowTask.getInputParameters());
} catch (Exception ee) {
String message =
String.format(
ee.getMessage() + ", taskType: SWITCH taskName %s",
workflowTask.getName());
context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
valid = false;
}
}
return valid;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
*/
package com.netflix.conductor.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import javax.validation.ConstraintViolationException;

Expand All @@ -35,6 +29,7 @@
import com.netflix.conductor.common.metadata.workflow.WorkflowDef;
import com.netflix.conductor.common.metadata.workflow.WorkflowDefSummary;
import com.netflix.conductor.common.metadata.workflow.WorkflowTask;
import com.netflix.conductor.common.model.BulkResponse;
import com.netflix.conductor.core.config.ConductorProperties;
import com.netflix.conductor.core.exception.NotFoundException;
import com.netflix.conductor.dao.EventHandlerDAO;
Expand Down Expand Up @@ -282,6 +277,66 @@ public void testUpdateWorkflowDef() {
verify(metadataDAO, times(1)).updateWorkflowDef(workflowDef);
}

@Test(expected = ConstraintViolationException.class)
public void testUpdateWorkflowDefWithCaseExpression() {
WorkflowDef workflowDef = new WorkflowDef();
workflowDef.setName("somename");
workflowDef.setOwnerEmail("[email protected]");
List<WorkflowTask> tasks = new ArrayList<>();
WorkflowTask workflowTask = new WorkflowTask();
workflowTask.setTaskReferenceName("hello");
workflowTask.setName("hello");
workflowTask.setType("DECISION");

WorkflowTask caseTask = new WorkflowTask();
caseTask.setTaskReferenceName("casetrue");
caseTask.setName("casetrue");

List<WorkflowTask> caseTaskList = new ArrayList<>();
caseTaskList.add(caseTask);

Map<String, List<WorkflowTask>> decisionCases = new HashMap();
decisionCases.put("true", caseTaskList);

workflowTask.setDecisionCases(decisionCases);
workflowTask.setCaseExpression("1 >0abcd");
tasks.add(workflowTask);
workflowDef.setTasks(tasks);
when(metadataDAO.getTaskDef(any())).thenReturn(new TaskDef());
BulkResponse bulkResponse =
metadataService.updateWorkflowDef(Collections.singletonList(workflowDef));
}

@Test(expected = ConstraintViolationException.class)
public void testUpdateWorkflowDefWithJavscriptEvaluator() {
WorkflowDef workflowDef = new WorkflowDef();
workflowDef.setName("somename");
workflowDef.setOwnerEmail("[email protected]");
List<WorkflowTask> tasks = new ArrayList<>();
WorkflowTask workflowTask = new WorkflowTask();
workflowTask.setTaskReferenceName("hello");
workflowTask.setName("hello");
workflowTask.setType("SWITCH");
workflowTask.setEvaluatorType("javascript");
workflowTask.setExpression("1>abcd");
WorkflowTask caseTask = new WorkflowTask();
caseTask.setTaskReferenceName("casetrue");
caseTask.setName("casetrue");

List<WorkflowTask> caseTaskList = new ArrayList<>();
caseTaskList.add(caseTask);

Map<String, List<WorkflowTask>> decisionCases = new HashMap();
decisionCases.put("true", caseTaskList);

workflowTask.setDecisionCases(decisionCases);
tasks.add(workflowTask);
workflowDef.setTasks(tasks);
when(metadataDAO.getTaskDef(any())).thenReturn(new TaskDef());
BulkResponse bulkResponse =
metadataService.updateWorkflowDef(Collections.singletonList(workflowDef));
}

@Test(expected = ConstraintViolationException.class)
public void testRegisterWorkflowDefNoName() {
try {
Expand Down

0 comments on commit 43316a5

Please sign in to comment.