diff --git a/server/src/main/java/io/littlehorse/common/model/corecommand/subcommand/RunWfRequestModel.java b/server/src/main/java/io/littlehorse/common/model/corecommand/subcommand/RunWfRequestModel.java index 302037331..44614db25 100644 --- a/server/src/main/java/io/littlehorse/common/model/corecommand/subcommand/RunWfRequestModel.java +++ b/server/src/main/java/io/littlehorse/common/model/corecommand/subcommand/RunWfRequestModel.java @@ -1,5 +1,6 @@ package io.littlehorse.common.model.corecommand.subcommand; +import com.google.common.base.Strings; import com.google.protobuf.Message; import io.grpc.Status; import io.littlehorse.common.LHSerializable; @@ -91,8 +92,20 @@ public boolean hasResponse() { return true; } + private boolean isIdValid() { + return (!id.equals("") && LHUtil.isValidLHName(id)); + } + @Override public WfRun process(ProcessorExecutionContext processorContext, LHServerConfig config) { + if (Strings.isNullOrEmpty(wfSpecName)) { + throw new LHApiException(Status.INVALID_ARGUMENT, "Missing required argument 'wf_spec_name'"); + } + + if (id != null && !this.isIdValid()) { + throw new LHApiException(Status.INVALID_ARGUMENT, "Optional argument 'id' must be a valid hostname"); + } + GetableManager getableManager = processorContext.getableManager(); WfSpecModel spec = processorContext.service().getWfSpec(wfSpecName, majorVersion, revision); if (spec == null) { diff --git a/server/src/main/java/io/littlehorse/server/LHServerListener.java b/server/src/main/java/io/littlehorse/server/LHServerListener.java index e792f8fbe..121c33c06 100644 --- a/server/src/main/java/io/littlehorse/server/LHServerListener.java +++ b/server/src/main/java/io/littlehorse/server/LHServerListener.java @@ -1,5 +1,6 @@ package io.littlehorse.server; +import com.google.common.base.Strings; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; import com.google.protobuf.Message; @@ -591,6 +592,16 @@ public void searchScheduledWfRun(SearchScheduledWfRunRequest req, StreamObserver @Override @Authorize(resources = ACLResource.ACL_WORKFLOW, actions = ACLAction.RUN) public void runWf(RunWfRequest req, StreamObserver ctx) { + if (Strings.isNullOrEmpty(req.getWfSpecName())) { + throw new LHApiException(Status.INVALID_ARGUMENT, "Missing required argument 'wf_spec_name'"); + } + + if (req.hasId()) { + if (req.getId().equals("") || !LHUtil.isValidLHName(req.getId())) { + throw new LHApiException(Status.INVALID_ARGUMENT, "Optional argument 'id' must be a valid hostname"); + } + } + RunWfRequestModel reqModel = LHSerializable.fromProto(req, RunWfRequestModel.class, requestContext()); processCommand(new CommandModel(reqModel), ctx, WfRun.class, true); } diff --git a/server/src/test/java/e2e/BasicTest.java b/server/src/test/java/e2e/BasicTest.java index c3832ee6c..d716f0495 100644 --- a/server/src/test/java/e2e/BasicTest.java +++ b/server/src/test/java/e2e/BasicTest.java @@ -1,7 +1,11 @@ package e2e; -import io.littlehorse.sdk.common.config.LHConfig; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import io.grpc.Status.Code; +import io.grpc.StatusRuntimeException; import io.littlehorse.sdk.common.proto.LHStatus; +import io.littlehorse.sdk.common.proto.WfRunId; import io.littlehorse.sdk.wfsdk.Workflow; import io.littlehorse.sdk.wfsdk.internal.WorkflowImpl; import io.littlehorse.sdk.worker.LHTaskMethod; @@ -9,6 +13,7 @@ import io.littlehorse.test.LHWorkflow; import io.littlehorse.test.WithWorkers; import io.littlehorse.test.WorkflowVerifier; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @LHTest @@ -19,8 +24,6 @@ public class BasicTest { @LHWorkflow("test-basic") private Workflow basicWf; - private LHConfig config; - private WorkflowVerifier verifier; @Test @@ -40,6 +43,17 @@ public Workflow getBasic() { }); } + @Test + public void runWfShouldFailWithInvalidId() { + StatusRuntimeException caught = assertThrows(StatusRuntimeException.class, () -> { + verifier.prepareRun(basicWf) + .waitForStatus(LHStatus.COMPLETED) + .start(WfRunId.newBuilder().setId("my_workflow").build()); + }); + + Assertions.assertThat(caught.getStatus().getCode()).isEqualTo(Code.INVALID_ARGUMENT); + } + public Object basicWorker() { return new BasicWorker(); }