Skip to content

Commit

Permalink
#26: Implement ToolCommandlet for Jasypt (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvomiero authored Mar 21, 2024
1 parent c29606a commit be4ab47
Show file tree
Hide file tree
Showing 19 changed files with 237 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.devonfw.tools.ide.tool.sonar.Sonar;
import com.devonfw.tools.ide.tool.terraform.Terraform;
import com.devonfw.tools.ide.tool.vscode.Vscode;
import com.devonfw.tools.ide.tool.jasypt.Jasypt;

/**
* Implementation of {@link CommandletManager}.
Expand Down Expand Up @@ -88,6 +89,7 @@ public CommandletManagerImpl(IdeContext context) {
add(new Aws(context));
add(new Cobigen(context));
add(new Jmc(context));
add(new Jasypt(context));
add(new Docker(context));
add(new Sonar(context));
}
Expand Down
7 changes: 7 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/common/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public final class Tag {
/** {@link #Tag} for github. */
public static final Tag GITHUB = create("github", GIT);


/** {@link #Tag} for diff (tools that compare files and determine the difference). */
public static final Tag DIFF = create("diff", CONFIG_MANAGEMENT, false, "patch");

Expand All @@ -311,6 +312,12 @@ public final class Tag {
/** {@link #Tag} for Linux. */
public static final Tag LINUX = create("linux", OS, false);

/** {@link #getParent() Parent} for cryptography. */
public static final Tag CRYPTO = create("cryptography", ROOT, false, "crypto");

/** {@link #Tag} for encryption. */
public static final Tag ENCRYPTION = create("encryption", CRYPTO);

private final String id;

private final Tag parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ protected String getValue(String name) {
if (!name.equals(key)) {
value = this.parent.get(key);
}
if (value != null) {
if (value == null) {
value = var.getDefaultValueAsString(this.context);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.devonfw.tools.ide.property;

/**
* {@link Property} with {@link #getValueType() value type} {@link String} representing a password.
*/
public class PasswordProperty extends StringProperty {

/**
* The constructor.
*
* @param name the {@link #getName() property name}.
* @param required the {@link #isRequired() required flag}.
* @param alias the {@link #getAlias() property alias}.
*/
public PasswordProperty(String name, boolean required, String alias) {

super(name, required, alias);
}

}
103 changes: 103 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/tool/jasypt/Jasypt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.devonfw.tools.ide.tool.jasypt;

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.EnumProperty;
import com.devonfw.tools.ide.property.PasswordProperty;
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.tool.java.Java;

import java.nio.file.Path;
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.
*/
public class Jasypt extends LocalToolCommandlet {

public final EnumProperty<JasyptCommand> command;

public final PasswordProperty masterPassword;

public final PasswordProperty secret;

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

private static final String CLASS_NAME_DECRYPTION = "org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI";

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public Jasypt(IdeContext context) {

super(context, "jasypt", Set.of(Tag.JAVA, Tag.ENCRYPTION));

this.command = add(new EnumProperty<>("", true, "command", JasyptCommand.class));
this.masterPassword = add(new PasswordProperty("", true, "masterPassword"));
this.secret = add(new PasswordProperty("", true, "secret"));
}

@Override
protected void initProperties() {

// Empty on purpose
}

@Override
public boolean doInstall(boolean silent) {

getCommandlet(Java.class).install();

return super.doInstall(silent);
}

@Override
protected boolean isExtract() {

return false;
}

@Override
public void run() {

Path toolPath = getToolPath();
if (!toolPath.toFile().exists()) {
super.install(true);
}

JasyptCommand command = this.command.getValue();
switch (command) {
case ENCRYPT:
runJasypt(CLASS_NAME_ENCRYPTION);
break;
case DECRYPT:
runJasypt(CLASS_NAME_DECRYPTION);
break;

default:
}
}

private void runJasypt(String className) {

Java java = getCommandlet(Java.class);

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

java.runTool(null, "-cp", resolveJasyptJarPath().toString(), className, algorithm, generatorClassName,
"password=" + this.masterPassword.getValue(), "input=" + this.secret.getValue());
}

private Path resolveJasyptJarPath() {

Path toolPath = this.getToolPath();
String installedVersion = getInstalledVersion().toString();
return toolPath.resolve("jasypt-" + installedVersion + ".jar");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.devonfw.tools.ide.tool.jasypt;

/**
* Represents commands for controlling a jasypt operation in The{@link Jasypt} Tool.
*/
public enum JasyptCommand {
ENCRYPT, DECRYPT
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ public interface IdeVariables {
/** {@link VariableDefinition} for {@link com.devonfw.tools.ide.context.IdeContext#getWorkspaceName() WORKSPACE}. */
VariableDefinitionString GRAALVM_EDITION = new VariableDefinitionString("GRAALVM_EDITION", null, c -> "community");

/** {@link VariableDefinition} for options of jasypt */
VariableDefinitionString JASYPT_OPTS = new VariableDefinitionString("JASYPT_OPTS", null,
c -> "algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator");

/** A {@link Collection} with all pre-defined {@link VariableDefinition}s. */
Collection<VariableDefinition<?>> VARIABLES = List.of(PATH, HOME, WORKSPACE_PATH, IDE_HOME, IDE_ROOT, WORKSPACE,
IDE_TOOLS, CREATE_START_SCRIPTS, IDE_MIN_VERSION, MVN_VERSION, DOCKER_EDITION, GRAALVM_EDITION);
IDE_TOOLS, CREATE_START_SCRIPTS, IDE_MIN_VERSION, MVN_VERSION, DOCKER_EDITION, GRAALVM_EDITION, JASYPT_OPTS);

/**
* @param name the name of the requested {@link VariableDefinition}.
Expand Down
4 changes: 4 additions & 0 deletions cli/src/main/resources/nls/Ide.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ cmd-shell=Commandlet to start built-in shell with advanced auto-completion.
cmd-terraform=Tool commandlet for Terraform
cmd-vscode=Tool commandlet for Visual Studio Code (IDE)
cmd-cobigen=Tool commandlet for Cobigen
cmd-jasypt=Tool commandlet for Jasypt
cmd-sonar=Tool commandlet for SonarQube
val-args=The commandline arguments to pass to the tool.
val-edition=The tool edition.
val-sonar-command=START|STOP|ANALYZE
val-jasypt-command=encrypt | decrypt
val-jasypt-masterPassword=master password
val-jasypt-secret=The secret to be encrypted or decrypted
val-tool=The tool commandlet to select.
val-version=The tool version
val-set-version-version=The tool version to set.
Expand Down
1 change: 1 addition & 0 deletions cli/src/main/resources/nls/Ide_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cmd-set-version=Setzt die Version des selektierten Werkzeugs.
cmd-terraform=Werkzeug Kommando für Terraform.
cmd-vscode=Werkzeug Kommando für Visual Studio Code (IDE)
cmd-cobigen=Werkzeug Kommando für Cobigen.
cmd-jasypt=Werkzeug Kommando für Jasypt
cmd-sonar=Werkzeug Kommando für SonarQube.
val-args=Die Kommandozeilen-Argumente zur Übergabe an das Werkzeug.
val-edition=Die Werkzeug Edition.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.devonfw.tools.ide.tool.jasypt;

import com.devonfw.tools.ide.commandlet.InstallCommandlet;
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;

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

private static final String PROJECT_JASYPT = "jasypt";

@Test
public void testJasyptInstallCommandlet() {

// arrange
IdeTestContext context = newContext(PROJECT_JASYPT);
InstallCommandlet install = context.getCommandletManager().getCommandlet(InstallCommandlet.class);
install.tool.setValueAsString("jasypt", context);
// act
install.run();

// assert
checkInstallation(context);
}

@Test
public void testJasyptInstall() {

// arrange
IdeTestContext context = newContext(PROJECT_JASYPT);

Jasypt commandlet = new Jasypt(context);

// act
commandlet.install();

// assert
checkInstallation(context);
}

@Test
public void testJasyptRun() {

// arrange
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.");
checkInstallation(context);
}

private void checkInstallation(IdeTestContext context) {

// install - java
assertThat(context.getSoftwarePath().resolve("java/bin/java")).exists();

// commandlet - jasypt
assertThat(context.getSoftwarePath().resolve("jasypt/jasypt-1.9.3.jar")).hasContent("This is a jar file.");
assertThat(context.getSoftwarePath().resolve("jasypt/.ide.software.version")).exists().hasContent("1.9.3");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed jasypt in version 1.9.3");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the download metadata
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the users HOME directory
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/jasypt/project/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_HOME directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JAVA_VERSION=17.0.10_7
JASYPT_VERSION=1.9.3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the main workspace of jmc test case
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/jasypt/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_ROOT directory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a jar file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "executing java:"
cat $2 # .jar file

0 comments on commit be4ab47

Please sign in to comment.