Skip to content
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

#345: Improve Jasypt ToolCommandlet #358

Merged
merged 9 commits into from
Jun 18, 2024
29 changes: 18 additions & 11 deletions cli/src/main/java/com/devonfw/tools/ide/tool/jasypt/Jasypt.java
Original file line number Diff line number Diff line change
@@ -9,19 +9,22 @@
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 {

public final EnumProperty<JasyptCommand> command;
final EnumProperty<JasyptCommand> command;
hohwille marked this conversation as resolved.
Show resolved Hide resolved

public final PasswordProperty masterPassword;
final PasswordProperty masterPassword;

public final PasswordProperty secret;
final PasswordProperty secret;

private static final String CLASS_NAME_ENCRYPTION = "org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI";

@@ -84,14 +87,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() {
4 changes: 2 additions & 2 deletions cli/src/main/resources/nls/Help.properties
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 1 addition & 1 deletion cli/src/main/resources/nls/Help_de.properties
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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 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() {

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

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

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

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

@@ -62,6 +78,35 @@ public void testJasyptRun() {
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, "executing java:");
assertLogMessage(context, IdeLogLevel.INFO, "This is a jar file.");
assertLogMessage(context, IdeLogLevel.INFO, JASYPT_OPTS);
checkInstallation(context);
}

private void checkInstallation(IdeTestContext context) {

// install - java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/bin/bash
echo "executing java:"
cat $2 # .jar file
cat $2 # .jar file

if [ ! -z "$6" ]; then
echo "$6"
hohwille marked this conversation as resolved.
Show resolved Hide resolved
fi