Skip to content

Commit

Permalink
#345: Improve Jasypt ToolCommandlet (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvomiero authored Jun 18, 2024
1 parent b870e36 commit b0dbea3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 16 deletions.
26 changes: 18 additions & 8 deletions cli/src/main/java/com/devonfw/tools/ide/tool/jasypt/Jasypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
import com.devonfw.tools.ide.tool.java.Java;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
* {@link ToolCommandlet} for <a href="http://www.jasypt.org/">Jasypt</a>, The java library which allows to add basic
* encryption capabilities with minimum effort.
* {@link ToolCommandlet} for <a href="http://www.jasypt.org/">Jasypt</a>, The java library which allows to add basic encryption capabilities with minimum
* effort.
*/
public class Jasypt extends LocalToolCommandlet {

/** {@link EnumProperty} for the command (encrypt or decrypt) */
public final EnumProperty<JasyptCommand> command;

/** {@link PasswordProperty} for the master password */
public final PasswordProperty masterPassword;

/** {@link PasswordProperty} for the secret to be encrypted or decrypted */
public final PasswordProperty secret;

private static final String CLASS_NAME_ENCRYPTION = "org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI";
Expand Down Expand Up @@ -84,14 +90,18 @@ public void run() {

private void runJasypt(String className) {

Java java = getCommandlet(Java.class);
List<String> arguments = new ArrayList<>(
Arrays.asList("-cp", resolveJasyptJarPath().toString(), className, "password=" + this.masterPassword.getValue(), "input=" + this.secret.getValue()));

String jasyptOpts = this.context.getVariables().get("JASYPT_OPTS");
if (jasyptOpts != null && !jasyptOpts.trim().isEmpty()) {
String[] jasyptOptions = jasyptOpts.split("\\s+");

String[] jasyptOptions = this.context.getVariables().get("JASYPT_OPTS").split(" ");
String algorithm = jasyptOptions[0];
String generatorClassName = jasyptOptions[1];
arguments.addAll(Arrays.asList(jasyptOptions));
}

java.runTool(null, "-cp", resolveJasyptJarPath().toString(), className, algorithm, generatorClassName,
"password=" + this.masterPassword.getValue(), "input=" + this.secret.getValue());
Java java = getCommandlet(Java.class);
java.runTool(null, arguments.toArray(new String[0]));
}

private Path resolveJasyptJarPath() {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/resources/nls/Help.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ cmd.helm=Tool commandlet for Helm (Kubernetes Package Manager).
cmd.help=Prints this help.
cmd.install=Install the selected tool.
cmd.intellij=Tool commandlet for IntelliJ (IDE)
cmd.jasypt=Tool commandlet for Jasypt (encryption/decryption).
cmd.jasypt.val.command=Modues (encrypt | decrypt)
cmd.jasypt=Tool commandlet for Jasypt, allows to install Jasypt and encrypt or decrypt secrets using strong encryption with a master password. It is possible to input custom arguments by setting the JASYPT_OPTS variable, which defaults to JASYPT_OPTS="algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator".
cmd.jasypt.val.command=Action (encrypt | decrypt)
cmd.jasypt.val.masterPassword=master password.
cmd.jasypt.val.secret=The secret to be encrypted or decrypted.
cmd.java=Tool commandlet for Java (OpenJDK).
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/resources/nls/Help_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cmd.helm=Werkzeug Kommando für Helm (Kubernetes Package Manager).
cmd.help=Zeigt diese Hilfe an.
cmd.install=Installiert das selektierte Werkzeug.
cmd.intellij=Werkzeug Kommando für Intellij (IDE)
cmd.jasypt=Werkzeug Kommando für Jasypt.
cmd.jasypt=Werkzeug Kommando für Jasypt, ermöglicht die Installation von Jasypt und die Verschlüsselung oder Entschlüsselung von Geheimnissen mit starker Verschlüsselung durch einen Master-Passwort. Es ist möglich, benutzerdefinierte Argumente durch Setzen der JASYPT_OPTS-Variable einzugeben, die standardmäßig auf JASYPT_OPTS="algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator" gesetzt ist.
cmd.jasypt.val.command=Mode (encrypt | decrypt)
cmd.jasypt.val.masterPassword=master Passwort.
cmd.jasypt.val.secret=Das zu verschlüsselnde oder zu entschlüsselnde Geheimnis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

/**
* Integration test of {@link Jasypt}.
*/
@ExtendWith(SystemStubsExtension.class)
public class JasyptTest extends AbstractIdeContextTest {

private static final String PROJECT_JASYPT = "jasypt";
private static final String JASYPT_OPTS = "custom_argument";

private static final String PROJECT_JASYPT = "jasypt";

/**
* Tests if {@link Jasypt} is properly installed by the {@link InstallCommandlet}
*/
@Test
public void testJasyptInstallCommandlet() {

Expand All @@ -27,6 +37,9 @@ public void testJasyptInstallCommandlet() {
checkInstallation(context);
}

/**
* Tests if {@link Jasypt} Commandlet installation is properly working
*/
@Test
public void testJasyptInstall() {

Expand All @@ -42,6 +55,9 @@ public void testJasyptInstall() {
checkInstallation(context);
}

/**
* Tests if {@link Jasypt} Commandlet is properly running
*/
@Test
public void testJasyptRun() {

Expand All @@ -57,8 +73,35 @@ public void testJasyptRun() {
commandlet.run();

// assert
assertLogMessage(context, IdeLogLevel.INFO, "executing java:");
assertLogMessage(context, IdeLogLevel.INFO, "This is a jar file.");
assertLogMessage(context, IdeLogLevel.INFO, context.getVariables().get("JASYPT_OPTS"));
checkInstallation(context);
}

@SystemStub
private final EnvironmentVariables environment = new EnvironmentVariables();

/**
* Tests if {@link Jasypt} Commandlet is properly running with a user-defined JASYPT_OPTS env variable
*/
@Test
public void testJasyptRunWithCustomVariable() {

// arrange
environment.set("JASYPT_OPTS", JASYPT_OPTS);

IdeTestContext context = newContext(PROJECT_JASYPT);
Jasypt commandlet = new Jasypt(context);

commandlet.command.setValue(JasyptCommand.ENCRYPT);
commandlet.masterPassword.setValue("password");
commandlet.secret.setValue("input");

// act
commandlet.run();

// assert
assertLogMessage(context, IdeLogLevel.INFO, context.getVariables().get("JASYPT_OPTS"));

checkInstallation(context);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash
echo "executing java:"
cat $2 # .jar file
echo "${@:6}" # optional arguments

0 comments on commit b0dbea3

Please sign in to comment.