diff --git a/CHANGELOG.md b/CHANGELOG.md index e3353dbdaa..c69b94999d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ to docs, or any other relevant information. API key and mTLS material from AWS Secrets Manager secret IDs. - `temporal-aws-lambda` now loads Temporal client credentials from AWS Secrets Manager when secret IDs are configured with `TEMPORAL_EXT_AWS_SECRETS_MANAGER_*` environment variables or in the TOML config. +- `workflowcheck check` can load a rules file from `TEMPORAL_WORKFLOWCHECK_CONFIG`. ### Changed - Updated Cloud API bindings to v0.27.0, including opt-in user group memberships, projects, diff --git a/temporal-workflowcheck/README.md b/temporal-workflowcheck/README.md index 7d433e0cd1..6eb8c21dcf 100644 --- a/temporal-workflowcheck/README.md +++ b/temporal-workflowcheck/README.md @@ -48,6 +48,12 @@ The `check` command accepts the following arguments: `:` for Nix), or prefixed with an `@` symbol saying it's a file with entries one per line, or just as separate arguments. They are all combined to one large classpath when running. +Set `TEMPORAL_WORKFLOWCHECK_CONFIG` to the path of a `.properties` file to load it without +adding a `--config` argument. The file is loaded after the default rules and before any +`--config` files. An unset or empty variable is ignored. + + TEMPORAL_WORKFLOWCHECK_CONFIG=workflowcheck.properties java -jar path/to/temporal-workflowcheck--all.jar check path/to/my.jar + ### Running in a Gradle project See the [Gradle sample](samples/gradle). diff --git a/temporal-workflowcheck/src/main/java/io/temporal/workflowcheck/Main.java b/temporal-workflowcheck/src/main/java/io/temporal/workflowcheck/Main.java index 78f4af8156..e87fda91a9 100644 --- a/temporal-workflowcheck/src/main/java/io/temporal/workflowcheck/Main.java +++ b/temporal-workflowcheck/src/main/java/io/temporal/workflowcheck/Main.java @@ -37,7 +37,9 @@ private static int check(String[] args) throws IOException { "Analyze Temporal workflows for common mistakes.\n" + "\n" + "Usage:\n" - + " workflowcheck check [--config ] [--no-default-config] [--show-valid]"); + + " workflowcheck check [--config ] [--no-default-config] [--show-valid]\n" + + "\n" + + "Set TEMPORAL_WORKFLOWCHECK_CONFIG to load a configuration file before any --config files."); return 0; } // Args list that removes options as encountered @@ -48,6 +50,10 @@ private static int check(String[] args) throws IOException { if (!argsList.remove("--no-default-config")) { configProps.add(Config.defaultProperties()); } + String environmentConfig = System.getenv("TEMPORAL_WORKFLOWCHECK_CONFIG"); + if (environmentConfig != null && !environmentConfig.isEmpty()) { + configProps.add(loadProperties(environmentConfig)); + } while (true) { int configIndex = argsList.indexOf("--config"); if (configIndex == -1) { @@ -57,11 +63,7 @@ private static int check(String[] args) throws IOException { return 1; } argsList.remove(configIndex); - Properties props = new Properties(); - try (FileInputStream is = new FileInputStream(argsList.remove(configIndex))) { - props.load(is); - } - configProps.add(props); + configProps.add(loadProperties(argsList.remove(configIndex))); } // Whether we should also show valid @@ -114,6 +116,14 @@ private static int check(String[] args) throws IOException { return anyInvalidImpls ? 1 : 0; } + private static Properties loadProperties(String path) throws IOException { + Properties props = new Properties(); + try (FileInputStream is = new FileInputStream(path)) { + props.load(is); + } + return props; + } + private static int prebuildConfig(String[] args) { System.err.println("TODO"); return 1; diff --git a/temporal-workflowcheck/src/test/java/io/temporal/workflowcheck/MainTest.java b/temporal-workflowcheck/src/test/java/io/temporal/workflowcheck/MainTest.java new file mode 100644 index 0000000000..cdb4f8a142 --- /dev/null +++ b/temporal-workflowcheck/src/test/java/io/temporal/workflowcheck/MainTest.java @@ -0,0 +1,106 @@ +package io.temporal.workflowcheck; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import io.temporal.workflowcheck.testdata.Configured; +import java.io.File; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class MainTest { + @Rule public final TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void loadsConfigFromEnvironment() throws Exception { + assertCheckExitCode(1, configFile("environment rules.properties", true).toString()); + } + + @Test + public void explicitConfigOverridesEnvironment() throws Exception { + Path environment = configFile("environment.properties", true); + Path explicit = configFile("explicit.properties", false); + assertCheckExitCode(0, environment.toString(), "--config", explicit.toString()); + } + + @Test + public void laterExplicitConfigStillWins() throws Exception { + Path first = configFile("first.properties", true); + Path second = configFile("second.properties", false); + assertCheckExitCode(0, null, "--config", first.toString(), "--config", second.toString()); + } + + @Test + public void emptyEnvironmentUsesDefaultConfig() throws Exception { + assertCheckExitCode(0, ""); + } + + @Test + public void worksWithoutDefaultConfig() throws Exception { + assertCheckExitCode( + 1, configFile("environment.properties", true).toString(), "--no-default-config"); + } + + private Path configFile(String name, boolean invalid) throws Exception { + Path file = temp.newFile(name).toPath(); + Files.write( + file, + ("temporal.workflowcheck.invalid.configuredInvalidJustName=" + invalid + "\n") + .getBytes(StandardCharsets.UTF_8)); + return file; + } + + private void assertCheckExitCode(int expected, String environment, String... args) + throws Exception { + Path classpath = temp.newFolder().toPath(); + for (Class type : Arrays.asList(Configured.class, Configured.ConfiguredImpl.class)) { + String name = type.getName().replace('.', '/') + ".class"; + Path target = classpath.resolve(name); + Files.createDirectories(target.getParent()); + try (InputStream input = type.getResourceAsStream("/" + name)) { + Files.copy(input, target); + } + } + + String executable = File.separatorChar == '\\' ? "java.exe" : "java"; + List command = + new ArrayList<>( + Arrays.asList( + Paths.get(System.getProperty("java.home"), "bin", executable).toString(), + "-cp", + System.getProperty("java.class.path"), + Main.class.getName(), + "check", + classpath.toString())); + command.addAll(Arrays.asList(args)); + File output = temp.newFile(); + ProcessBuilder builder = + new ProcessBuilder(command).redirectErrorStream(true).redirectOutput(output); + if (environment == null) { + builder.environment().remove("TEMPORAL_WORKFLOWCHECK_CONFIG"); + } else { + builder.environment().put("TEMPORAL_WORKFLOWCHECK_CONFIG", environment); + } + Process process = builder.start(); + try { + assertTrue("workflowcheck did not exit", process.waitFor(30, TimeUnit.SECONDS)); + } finally { + if (process.isAlive()) { + process.destroyForcibly(); + } + } + String result = new String(Files.readAllBytes(output.toPath()), StandardCharsets.UTF_8); + assertTrue(result, result.contains("Found 1 class(es) with workflow methods")); + assertEquals(result, expected, process.exitValue()); + } +}