-
Notifications
You must be signed in to change notification settings - Fork 178
Prohibit use of WorkflowClient from workflow code #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
temporal-sdk/src/test/java/io/temporal/workflow/ProhibitedCallsFromWorkflowTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package io.temporal.workflow; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import io.temporal.client.WorkflowClient; | ||
import io.temporal.workflow.shared.SDKTestWorkflowRule; | ||
import io.temporal.workflow.shared.TestWorkflows.ITestNamedChild; | ||
import io.temporal.workflow.shared.TestWorkflows.NoArgsWorkflow; | ||
import io.temporal.workflow.shared.TestWorkflows.TestNamedChild; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class ProhibitedCallsFromWorkflowTest { | ||
|
||
@Rule | ||
public SDKTestWorkflowRule testWorkflowRule = | ||
SDKTestWorkflowRule.newBuilder() | ||
.setWorkflowTypes(TestWorkflow.class, TestNamedChild.class) | ||
.build(); | ||
|
||
private static WorkflowClient workflowClient; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
workflowClient = testWorkflowRule.getWorkflowClient(); | ||
} | ||
|
||
@Test | ||
public void testWorkflowClientCallFromWorkflow() { | ||
NoArgsWorkflow client = testWorkflowRule.newWorkflowStubTimeoutOptions(NoArgsWorkflow.class); | ||
client.execute(); | ||
} | ||
|
||
public static class TestWorkflow implements NoArgsWorkflow { | ||
@Override | ||
public void execute() { | ||
ITestNamedChild child = Workflow.newChildWorkflowStub(ITestNamedChild.class); | ||
try { | ||
WorkflowClient.execute(child::execute, "hello"); | ||
fail("should be unreachable, we expect an exception"); | ||
} catch (IllegalStateException e) { | ||
assertTrue(e.getMessage().startsWith("Cannot be called from workflow thread.")); | ||
} | ||
try { | ||
WorkflowClient.start(child::execute, "world"); | ||
fail("should be unreachable, we expect an exception"); | ||
} catch (IllegalStateException e) { | ||
assertTrue(e.getMessage().startsWith("Cannot be called from workflow thread.")); | ||
} | ||
try { | ||
// let's imagine that the workflow code somehow got a WorkflowClient instance (from DI for | ||
// example). | ||
// Let's make sure it still can't trigger it's methods | ||
workflowClient.getOptions(); | ||
fail("should be unreachable, we expect an exception"); | ||
} catch (IllegalStateException e) { | ||
assertTrue(e.getMessage().startsWith("Cannot be called from workflow thread.")); | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
temporal-serviceclient/src/main/java/io/temporal/conf/EnvironmentVariableNames.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.temporal.conf; | ||
|
||
public final class EnvironmentVariableNames { | ||
/** | ||
* Specify this env variable to disable checks and enforcement for classes that are not intended | ||
* to be accessed from workflow code. | ||
* | ||
* <p>Not specifying it or setting it to "false" (case insensitive) leaves the checks enforced. | ||
* | ||
* <p>This option is exposed for backwards compatibility only and should never be enabled for any | ||
* new code or application. | ||
*/ | ||
public static final String DISABLE_NON_WORKFLOW_CODE_ENFORCEMENTS = | ||
"TEMPORAL_DISABLE_NON_WORKFLOW_CODE_ENFORCEMENTS"; | ||
|
||
private EnvironmentVariableNames() {} | ||
} |
86 changes: 86 additions & 0 deletions
86
temporal-serviceclient/src/main/java/io/temporal/internal/WorkflowThreadMarker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package io.temporal.internal; | ||
|
||
import io.temporal.conf.EnvironmentVariableNames; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Proxy; | ||
|
||
/** | ||
* Provides an access to information about a thread type the current code executes in to perform | ||
* different type of access checks inside Temporal library code. | ||
* | ||
* <p>Note: This class is a singleton and is not intended for an extension. | ||
* | ||
* <p>Note: This class shouldn't be accessed in any way by the application code. | ||
*/ | ||
public abstract class WorkflowThreadMarker { | ||
protected static final ThreadLocal<Boolean> isWorkflowThreadThreadLocal = | ||
ThreadLocal.withInitial(() -> false); | ||
|
||
private static final boolean enableEnforcements; | ||
|
||
static { | ||
String envValue = | ||
System.getenv(EnvironmentVariableNames.DISABLE_NON_WORKFLOW_CODE_ENFORCEMENTS); | ||
enableEnforcements = envValue == null || "false".equalsIgnoreCase(envValue); | ||
} | ||
|
||
/** @return true if the current thread is workflow thread */ | ||
public static boolean isWorkflowThread() { | ||
return isWorkflowThreadThreadLocal.get(); | ||
} | ||
|
||
/** | ||
* Throws {@link IllegalStateException} if it's called from workflow thread. | ||
* | ||
* @see io.temporal.conf.EnvironmentVariableNames#DISABLE_NON_WORKFLOW_CODE_ENFORCEMENTS | ||
*/ | ||
public static void enforceNonWorkflowThread() { | ||
if (enableEnforcements && isWorkflowThread()) { | ||
throw new IllegalStateException("Cannot be called from workflow thread."); | ||
} | ||
} | ||
|
||
/** | ||
* Create a proxy that checks all methods executions if they are done from a workflow thread and | ||
* makes them throw an IllegalStateException if they are indeed triggered from workflow code | ||
* | ||
* @param instance an instance to wrap | ||
* @param iface an interface the {@code instance} implements and that proxy should implement and | ||
* intercept | ||
* @return a proxy that makes sure that it's methods can't be called from workflow thread | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> T protectFromWorkflowThread(T instance, Class<T> iface) { | ||
return (T) | ||
Proxy.newProxyInstance( | ||
iface.getClassLoader(), | ||
new Class<?>[] {iface}, | ||
(proxy, method, args) -> { | ||
enforceNonWorkflowThread(); | ||
try { | ||
return method.invoke(instance, args); | ||
} catch (InvocationTargetException e) { | ||
throw e.getCause(); | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should consider adding an option that would disable this enforcement. If users are already running "bad" workflows in production, then there should be a way to disable this feature until workflows are refactored/updated, otherwise we are forcing users to stay on older SDK versions.