From 8b762e2f11187aca6ba7245f02e13629e3fc7632 Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:05:29 +0200 Subject: [PATCH] fix: Pass non-default session name to fcli: action statements (fixes #555) --- .../cli/cmd/AbstractActionRunCommand.java | 23 ++++++++++++++++++- .../fortify/cli/common/util/EnvHelper.java | 6 ++++- .../action/cli/cmd/FoDActionRunCommand.java | 5 ++++ .../action/cli/cmd/SSCActionRunCommand.java | 5 ++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/cli/cmd/AbstractActionRunCommand.java b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/cli/cmd/AbstractActionRunCommand.java index c7fcc206a0..652530708e 100644 --- a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/cli/cmd/AbstractActionRunCommand.java +++ b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/cli/cmd/AbstractActionRunCommand.java @@ -29,6 +29,7 @@ import com.fortify.cli.common.progress.helper.ProgressWriterType; import com.fortify.cli.common.util.DisableTest; import com.fortify.cli.common.util.DisableTest.TestType; +import com.fortify.cli.common.util.EnvHelper; import lombok.SneakyThrows; import picocli.CommandLine.Mixin; @@ -68,7 +69,26 @@ public final Integer call() { private Callable run(ActionRunner actionRunner, IProgressWriterI18n progressWriter) { actionRunner.getSpelEvaluator().configure(context->configure(actionRunner, context)); progressWriter.writeProgress("Executing action %s", actionRunner.getAction().getMetadata().getName()); - return actionRunner.run(actionArgs); + // We need to set the FCLI_DEFAULT__SESSION environment variable to allow fcli: statements to + // pick up the current session name, and (although probably not needed currently), reset the default + // session name to the previous value once the action completes. + var sessionEnvName = String.format("%s_%s_SESSION", System.getProperty("fcli.env.default.prefix", "FCLI_DEFAULT"), getType().toUpperCase()); + var sessionPropertyName = EnvHelper.envSystemPropertyName(sessionEnvName); + var sessionEnvOrgValue = EnvHelper.env(sessionEnvName); + try { + setOrClearSystemProperty(sessionPropertyName, getSessionName()); + return actionRunner.run(actionArgs); + } finally { + setOrClearSystemProperty(sessionPropertyName, sessionEnvOrgValue); + } + } + + private void setOrClearSystemProperty(String name, String value) { + if ( value==null ) { + System.clearProperty(name); + } else { + System.setProperty(name, value); + } } private ParameterException onValidationErrors(OptionsParseResult optionsParseResult) { @@ -79,5 +99,6 @@ private ParameterException onValidationErrors(OptionsParseResult optionsParseRes } protected abstract String getType(); + protected abstract String getSessionName(); protected abstract void configure(ActionRunner actionRunner, SimpleEvaluationContext context); } diff --git a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java index 9e461853d8..e826e69fe4 100644 --- a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java +++ b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java @@ -59,7 +59,11 @@ public static final String envName(String productEnvId, String suffix) { * other purposes. */ public static final String env(String name) { - return System.getProperty("fcli.env."+name, System.getenv(name)); + return System.getProperty(envSystemPropertyName(name), System.getenv(name)); + } + + public static String envSystemPropertyName(String envName) { + return "fcli.env."+envName; } public static final Boolean asBoolean(String s) { diff --git a/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/action/cli/cmd/FoDActionRunCommand.java b/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/action/cli/cmd/FoDActionRunCommand.java index ae15d3d104..a8e04c4837 100644 --- a/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/action/cli/cmd/FoDActionRunCommand.java +++ b/fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/action/cli/cmd/FoDActionRunCommand.java @@ -42,6 +42,11 @@ protected final String getType() { return "FoD"; } + @Override + protected String getSessionName() { + return unirestInstanceSupplier.getSessionName(); + } + @Override protected void configure(ActionRunner templateRunner, SimpleEvaluationContext context) { templateRunner diff --git a/fcli-core/fcli-ssc/src/main/java/com/fortify/cli/ssc/action/cli/cmd/SSCActionRunCommand.java b/fcli-core/fcli-ssc/src/main/java/com/fortify/cli/ssc/action/cli/cmd/SSCActionRunCommand.java index 0554be6e8e..d521f47ab2 100644 --- a/fcli-core/fcli-ssc/src/main/java/com/fortify/cli/ssc/action/cli/cmd/SSCActionRunCommand.java +++ b/fcli-core/fcli-ssc/src/main/java/com/fortify/cli/ssc/action/cli/cmd/SSCActionRunCommand.java @@ -68,6 +68,11 @@ protected final String getType() { return "SSC"; } + @Override + protected String getSessionName() { + return unirestInstanceSupplier.getSessionName(); + } + @Override protected void configure(ActionRunner templateRunner, SimpleEvaluationContext context) { templateRunner