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

Various changes to support automatically pulling sets of paths to use… #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ build/

# Gradle
.gradle/

.idea/.name
.idea/compiler.xml
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions src/main/groovy/com/chrisney/enigma/EnigmaPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,53 @@ class EnigmaPlugin implements Plugin<Project> {
enabled = extension.enabled
rootProject = project.rootDir.absolutePath
pathSrc = project.rootDir.absolutePath + extension.srcJava

pathSrcs = []
project.rootProject.getAllprojects().forEach { proj ->
if(proj.rootDir.absolutePath != proj.projectDir.absolutePath) {
pathSrcs += proj.projectDir.absolutePath + "/src/main/java/"
}
else {
println("Ignoring root: " + proj.projectDir.absolutePath)
}
}

debug = extension.debug
}

project.task('backup', type: BackupTask) {
enabled = extension.enabled
rootProject = project.rootDir.absolutePath
pathSrc = project.rootDir.absolutePath + extension.srcJava

pathSrcs = []
project.rootProject.getAllprojects().forEach { proj ->
if(proj.rootDir.absolutePath != proj.projectDir.absolutePath) {
pathSrcs += proj.projectDir.absolutePath + "/src/main/java/"
}
else {
println("Ignoring root: " + proj.projectDir.absolutePath)
}
}

debug = extension.debug
}

project.task('injectCode', type: InjectCodeTask) {
enabled = extension.enabled
rootProject = project.rootDir.absolutePath
pathSrc = project.rootDir.absolutePath + extension.srcJava

pathSrcs = []
project.rootProject.getAllprojects().forEach { proj ->
if(proj.rootDir.absolutePath != proj.projectDir.absolutePath) {
pathSrcs += proj.projectDir.absolutePath + "/src/main/java/"
}
else {
println("Ignoring root: " + proj.projectDir.absolutePath)
}
}

hash = extension.hash
customFunction = extension.customFunction
debug = extension.debug
Expand All @@ -65,6 +98,17 @@ class EnigmaPlugin implements Plugin<Project> {
enabled = extension.enabled
rootProject = project.rootDir.absolutePath
pathSrc = project.rootDir.absolutePath + extension.srcJava

pathSrcs = []
project.rootProject.getAllprojects().forEach { proj ->
if(proj.rootDir.absolutePath != proj.projectDir.absolutePath) {
pathSrcs += proj.projectDir.absolutePath + "/src/main/java/"
}
else {
println("Ignoring root: " + proj.projectDir.absolutePath)
}
}

hash = extension.hash
ignoredClasses = extension.ignoredClasses
classes = extension.classes
Expand All @@ -78,6 +122,17 @@ class EnigmaPlugin implements Plugin<Project> {
enabled = extension.enabled
rootProject = project.rootDir.absolutePath
pathSrc = project.rootDir.absolutePath + extension.srcJava

pathSrcs = []
project.rootProject.getAllprojects().forEach { proj ->
if(proj.rootDir.absolutePath != proj.projectDir.absolutePath) {
pathSrcs += proj.projectDir.absolutePath + "/src/main/java/"
}
else {
println("Ignoring root: " + proj.projectDir.absolutePath)
}
}

debug = extension.debug
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/chrisney/enigma/parser/JavaCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ public void injectFakeKeys() {
int sizeValue = Utils.getRandomNumberInRange(10, 30);
String randomValue = TextUtils.getRandomString(sizeValue, TextUtils.KEY_CHARACTERS);

System.out.println("Injecting fake param: " + fakeParamName);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugging stuff, to be removed.

System.out.println("Injecting fake randomValue: " + randomValue);

injectFakeKeys(fakeParamName, randomValue);
}

Expand All @@ -398,8 +401,13 @@ public void injectFakeKeys(String fakeParamName, String randomValue) {
ArrayList<CodeBlock> classBlocks = getBlocksByType(CodeBlock.BlockType.Class);
ArrayList<CodeBlock> functions = getFunctions();

System.out.println("classBlocks size: " + classBlocks.size());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

System.out.println("functions size: " + functions.size());

if (Utils.arrayNotEmpty(classBlocks) && Utils.arrayNotEmpty(functions)) {

System.out.println("Utils.arrayNotEmpty(classBlocks) && Utils.arrayNotEmpty(functions) PASSED");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


// Generate fake code:
String fakeAttribute = getFakeAttribute(fakeParamName, randomValue);
CodeBlock fakeCode = getFakeCode(fakeParamName);
Expand Down
42 changes: 39 additions & 3 deletions src/main/java/com/chrisney/enigma/tasks/AbstractTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.apache.commons.io.FileUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Internal;
import org.gradle.internal.Pair;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -29,6 +31,7 @@ public class AbstractTask extends DefaultTask {
public boolean debug = false;
public String rootProject;
public String pathSrc;
public String[] pathSrcs;

public AbstractTask() {
this.setGroup("enigma");
Expand All @@ -39,8 +42,32 @@ public AbstractTask() {
* @return Collection of all JAVA files (*.java)
*/
@Internal
protected Collection<File> getAllJavaFiles() {
return Utils.listFileTree(new File(pathSrc), ".java");
protected Collection<Pair<Integer, File>> getAllJavaFiles() {
System.out.println("Enigma pathSrcs:[");
for(String src : pathSrcs) {
System.out.println("\tEnigma src: " + src);
}
System.out.println("Enigma ]");

Collection<Pair<Integer, File>> ret = new java.util.ArrayList<>(Collections.emptyList());
if(pathSrcs.length <= 0) {
Collection<File> files = Utils.listFileTree(new File(pathSrc), ".java");
for(File file : files) {
ret.add(Pair.of(0, file));
}
return ret;
}
else {
int index = 0;
for(String src : pathSrcs) {
Collection<File> files = Utils.listFileTree(new File(src), ".java");
for(File file : files) {
ret.add(Pair.of(index, file));
}
index++;
}
return ret;
}
}

/**
Expand All @@ -50,7 +77,16 @@ protected Collection<File> getAllJavaFiles() {
*/
@Internal
protected Collection<File> getAllXmlFiles() {
return Utils.listFileTree(new File(pathSrc), ".xml");
if(pathSrcs.length <= 0) {
Utils.listFileTree(new File(pathSrc), ".xml");
}

Collection<File> ret = new java.util.ArrayList<>(Collections.emptyList());

for(String src : pathSrcs) {
ret.addAll(Utils.listFileTree(new File(src), ".xml"));
}
return ret;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/chrisney/enigma/tasks/BackupTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.io.FileUtils;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.Pair;

import javax.inject.Inject;
import java.io.File;
Expand All @@ -24,8 +25,9 @@ public void backup() throws IOException {
if (!checkSCM()) return;
this.removeBackupDir();
this.createBackupDir();
for (File javaFile : this.getAllJavaFiles()) {
this.backupFile(javaFile);
for (Pair<Integer, File> pair : this.getAllJavaFiles()) {
assert pair.right != null;
this.backupFile(pair.right);
}
}

Expand Down
27 changes: 22 additions & 5 deletions src/main/java/com/chrisney/enigma/tasks/EnigmaTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.apache.commons.io.FileUtils;
import org.gradle.internal.Pair;

import javax.inject.Inject;
import java.io.File;
Expand Down Expand Up @@ -47,16 +48,19 @@ public void encrypt() throws Exception {
return;
}

for (File javaFile : this.getAllJavaFiles()) {
for (Pair<Integer, File> pair : this.getAllJavaFiles()) {
File javaFile = pair.right;
assert javaFile != null;
if (!isSelected(javaFile) || isIgnored(javaFile)) {
System.out.println("\uD83D\uDEAB️ " + javaFile.getName() + " ignored");
} else {
encryptJavaFile(javaFile);
encryptJavaFile(pair);
}
}
}

private boolean isSelected(File javaFile) {
System.out.println("Enigma: Checking isSelected: " + javaFile.getAbsolutePath());
if (this.classes != null) {
for (String ignored : this.classes) {
String path = ignored.replace(".", File.separator)
Expand All @@ -71,6 +75,7 @@ private boolean isSelected(File javaFile) {
}

private boolean isIgnored(File javaFile) {
System.out.println("Enigma: Checking isIgnored: " + javaFile.getAbsolutePath());
if (this.ignoredClasses != null) {
for (String ignored : this.ignoredClasses) {
String path = ignored.replace(".", File.separator)
Expand All @@ -83,7 +88,13 @@ private boolean isIgnored(File javaFile) {
return false;
}

private void encryptJavaFile(File srcFile) throws Exception {
private void encryptJavaFile(Pair<Integer, File> pair) throws Exception {
assert pair != null;
assert pair.right != null;
assert pair.left != null;

File srcFile = pair.right;
int srcIteration = pair.left;

if (isEnigmaFile(srcFile)) return;
if (isEnigmatized(srcFile)) {
Expand All @@ -96,10 +107,16 @@ private void encryptJavaFile(File srcFile) throws Exception {
JavaParser p = new JavaParser();
JavaCode code = p.parse(contents);

code.addImport(InjectCodeTask.PACKAGE_NAME + "." + InjectCodeTask.CLASS_NAME);
// Change the statement per the 'iteration' of the src
String importPackageStatement = InjectCodeTask.PACKAGE_NAME + "_" + srcIteration + "." + InjectCodeTask.CLASS_NAME;

code.addImport(importPackageStatement);
code.encryptStrings(hash, InjectCodeTask.FUNCTION_NAME);

if (injectFakeKeys) code.injectFakeKeys();
if (injectFakeKeys) {
System.out.println("INJECTING FAKE KEYS");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

code.injectFakeKeys();
}

String contentSecured = code.toCode();
FileUtils.writeStringToFile(srcFile, contentSecured, "UTF-8");
Expand Down
53 changes: 45 additions & 8 deletions src/main/java/com/chrisney/enigma/tasks/InjectCodeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,36 @@
*/
public class InjectCodeTask extends AbstractTask {

class AddFileRunnable implements Runnable {
private final String src;
private final int iteration;

AddFileRunnable(int iteration, String src) {
this.iteration = iteration;
this.src = src;
}

@Override
public void run() {
try {
addFile(this.iteration, this.src);
} catch (IOException e) {
throw new RuntimeException("Failed to AddFile, src: " + src, e);
}
}
}

public static final String PACKAGE_NAME = "com.chrisney.enigma";
public static final String CLASS_NAME = "EnigmaUtils";
public static final String FUNCTION_NAME = InjectCodeTask.CLASS_NAME + ".enigmatization";
public static final String IMPORT_NAME = "import " + InjectCodeTask.PACKAGE_NAME + "." + InjectCodeTask.CLASS_NAME + ";";

public static final String SOURCE_CODE = "package " + PACKAGE_NAME + ";\n" +
"\n" +
"import javax.crypto.Cipher;\n" +
public static final String SOURCE_CODE_PACKAGE_STATEMENT = "package " + PACKAGE_NAME;
public static final String getSourceCodePackageStatement(int iteration) {
return SOURCE_CODE_PACKAGE_STATEMENT + "_" + iteration + ";\n\n";
}

public static final String SOURCE_CODE = "import javax.crypto.Cipher;\n" +
"import javax.crypto.SecretKey;\n" +
"import javax.crypto.spec.SecretKeySpec;\n" +
"import javax.crypto.spec.IvParameterSpec;\n" +
Expand Down Expand Up @@ -65,6 +87,16 @@ public InjectCodeTask() {
super();
}

private void addFile(int iteration, String src) throws IOException {
File packageName = new File(src + File.separator + PACKAGE_NAME.replace(".", File.separator) + "_" + iteration);
if (!packageName.exists()) packageName.mkdir();

File codeFile = new File(packageName.getAbsolutePath() + File.separator + CLASS_NAME + ".java");
String data = encodeHash(getSourceCodePackageStatement(iteration) + SOURCE_CODE);
System.out.println("addFile, adding with package statement: " + getSourceCodePackageStatement(iteration));
FileUtils.writeStringToFile(codeFile, data, "UTF-8");
}

@TaskAction
public void addCode() throws IOException {
if (!enabled) return;
Expand All @@ -76,12 +108,17 @@ public void addCode() throws IOException {
System.out.println("⚠️ Missing Hash value to inject Enigma code");
return;
}
File packageName = new File(pathSrc + File.separator + PACKAGE_NAME.replace(".", File.separator));
if (!packageName.exists()) packageName.mkdir();

File codeFile = new File(packageName.getAbsolutePath() + File.separator + CLASS_NAME + ".java");
String data = encodeHash(SOURCE_CODE);
FileUtils.writeStringToFile(codeFile, data, "UTF-8");
if(pathSrcs.length <= 0) {
new AddFileRunnable(0, pathSrc).run();
}
else {
int index = 0;
for(String src : pathSrcs) {
new AddFileRunnable(index, src).run();
index++;
}
}

System.out.println("✏️ Add Enigma code");
}
Expand Down
Loading