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

Chore/improve precision of archunit, add translaiton #34

Merged
merged 20 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
17 changes: 4 additions & 13 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- name: Checkout Repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up JDK 21
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'adopt'
Expand All @@ -32,15 +32,6 @@ jobs:

- name: Build and Test with Maven
run: |
mvn -B package --file pom.xml -DskipTests
mvn clean
mvn package -DskipTests
sarpsahinalp marked this conversation as resolved.
Show resolved Hide resolved
mvn test

- name: Run Tests on Student Submission
run: |
git config --global url.https://github.com/.insteadOf git://github.com/
git clone https://github.com/sarpsahinalp/test-student-submission.git
mkdir test-student-submission/libs
cp target/*.jar test-student-submission/libs
cd test-student-submission
./gradlew build
./gradlew test
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
<version>3.5.1</version>
<configuration>
<forkCount>1</forkCount>
<argLine>-javaagent:${project.build.directory}/ares-2.0.0-SNAPSHOT-agent.jar -Xbootclasspath/a:${user.home}/.m2/repository/org/aspectj/aspectjrt/${aspectj.version}/aspectjrt-${aspectj.version}.jar</argLine>
<argLine>-javaagent:${project.build.directory}${file.separator}ares-2.0.0-SNAPSHOT-agent.jar -Xbootclasspath/a:${user.home}${file.separator}.m2${file.separator}repository${file.separator}org${file.separator}aspectj${file.separator}aspectjrt${file.separator}${aspectj.version}${file.separator}aspectjrt-${aspectj.version}.jar</argLine>
</configuration>
</plugin>
<!-- Shade Plugin to package the agent JAR -->
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/de/tum/cit/ase/ares/api/aop/java/JavaAOPMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.List;
import java.util.stream.Stream;

import static de.tum.cit.ase.ares.api.aop.java.instrumentation.advice.JavaInstrumentationAdviceToolbox.localize;

/**
* Enum representing the different modes of Aspect-Oriented Programming (AOP)
* available for Java in Ares.
Expand Down Expand Up @@ -212,16 +214,16 @@ public void reset() {
method.setAccessible(false);

} catch (ClassNotFoundException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Creation): The class for the specific security test case settings could not be found. Ensure the class name is correct and the class is available at runtime.", e);
throw new SecurityException(localize("security.creation.reset.class.not.found.exception"), e);

} catch (NoSuchMethodException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Creation): The 'reset' method could not be found in the specified class. Ensure the method exists and is correctly named.", e);
throw new SecurityException(localize("security.creation.reset.no.method.exception"), e);

} catch (IllegalAccessException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Creation): Access to the 'reset' method was denied. Ensure the method is public and accessible.", e);
throw new SecurityException(localize("security.creation.reset.illegal.access.exception"), e);

} catch (InvocationTargetException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Creation): An error occurred while invoking the 'reset' method. This could be due to an underlying issue within the method implementation.", e);
throw new SecurityException(localize("security.creation.reset.invocation.target.exception"), e);
}
}
//</editor-fold>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private static String generateAdviceSettingValue(@Nonnull String dataType, @Nonn

return switch (dataType) {
case "String" -> {
// TODO: Refactor the error messages here
if (!(value instanceof String)) {
throw new SecurityException(String.format(
"Ares Security Error (Reason: Ares-Code; Stage: Creation): "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import java.lang.reflect.InaccessibleObjectException;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;

import static de.tum.cit.ase.ares.api.localization.Messages.localized;

public aspect JavaAspectJFileSystemAdviceDefinitions {

//<editor-fold desc="Tool methods">
Expand All @@ -30,17 +32,17 @@ public aspect JavaAspectJFileSystemAdviceDefinitions {
field.setAccessible(false);
return value;
} catch (LinkageError e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Execution): Linkage error while accessing field '" + fieldName + "' in AdviceSettings", e);
throw new SecurityException(localized("security.advice.linkage.exception", fieldName), e);
} catch (ClassNotFoundException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Execution): Could not find 'JavaSecurityTestCaseSettings' class to access field '" + fieldName + "'", e);
throw new SecurityException(localized("security.advice.class.not.found.exception", fieldName), e);
} catch (NoSuchFieldException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Execution): Field '" + fieldName + "' not found in AdviceSettings", e);
throw new SecurityException(localized("security.advice.no.such.field.exception", fieldName), e);
} catch (NullPointerException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Execution): Null pointer exception while accessing field '" + fieldName + "' in AdviceSettings", e);
throw new SecurityException(localized("security.advice.null.pointer.exception", fieldName), e);
} catch (IllegalAccessException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Execution): Field '" + fieldName + "' is not accessible in AdviceSettings", e);
throw new SecurityException(localized("security.advice.illegal.access.exception", fieldName), e);
} catch (InaccessibleObjectException e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Execution): Field '" + fieldName + "' is inaccessible in AdviceSettings", e);
throw new SecurityException(localized("security.advice.inaccessible.object.exception", fieldName), e);
}
}
// </editor-fold>
Expand Down Expand Up @@ -109,30 +111,30 @@ public aspect JavaAspectJFileSystemAdviceDefinitions {
*/
private static Path variableToPath(Object variableValue) {
if (variableValue == null) {
throw new InvalidPathException("null", "Cannot transform to path");
throw new InvalidPathException("null", localized("security.advice.transform.path.exception"));
} else if (variableValue instanceof Path) {
Path path = (Path) variableValue;
try {
return path.normalize().toAbsolutePath();
} catch (InvalidPathException e) {
throw new InvalidPathException(path.toString(), "Cannot transform to path");
throw new InvalidPathException(path.toString(), localized("security.advice.transform.path.exception"));
}
} else if (variableValue instanceof String) {
String string = (String) variableValue;
try {
return Path.of(string).normalize().toAbsolutePath();
} catch (InvalidPathException e) {
throw new InvalidPathException(string, "Cannot transform to path");
throw new InvalidPathException(string, localized("security.advice.transform.path.exception"));
}
} else if (variableValue instanceof File) {
File file = (File) variableValue;
try {
return Path.of(file.toURI()).normalize().toAbsolutePath();
} catch (InvalidPathException e) {
throw new InvalidPathException(file.toString(), "Cannot transform to path");
throw new InvalidPathException(file.toString(), localized("security.advice.transform.path.exception"));
}
} else {
throw new InvalidPathException(variableValue.toString(), "Cannot transform to path");
throw new InvalidPathException(variableValue.toString(), localized("security.advice.transform.path.exception"));
}
}

Expand Down Expand Up @@ -214,7 +216,7 @@ public aspect JavaAspectJFileSystemAdviceDefinitions {
if (illegallyReadingMethod != null) {
String illegallyReadPath = (parameters == null || parameters.length == 0) ? null : checkIfVariableCriteriaIsViolated(parameters, allowedPaths);
if (illegallyReadPath != null) {
throw new SecurityException("Ares Security Error (Reason: Student-Code; Stage: Execution):" + illegallyReadingMethod + " tried to illegally " + action + " from " + illegallyReadPath + " via " + fullMethodSignature + "but was blocked by Ares.");
throw new SecurityException(localized("security.advice.illegal.method.execution", illegallyReadingMethod, action, illegallyReadPath, fullMethodSignature));
}
}
}
Expand All @@ -232,7 +234,7 @@ public aspect JavaAspectJFileSystemAdviceDefinitions {
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.bufferedReaderInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.scannerInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.fileReaderInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.lineNumberReaderInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.desktopExecuteMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.randomAccessFileInitMethods() {
checkFileSystemInteraction("read", thisJoinPoint);
}
Expand All @@ -246,11 +248,7 @@ public aspect JavaAspectJFileSystemAdviceDefinitions {
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.fileHandlerMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.fileSystemProviderWriteMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.printWriterInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.bufferedWriterInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.outputStreamWriterInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.dataOutputStreamInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.objectOutputStreamInitMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.printStreamInitMethods() {
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.desktopExecuteMethods() {
checkFileSystemInteraction("write", thisJoinPoint);
}

Expand All @@ -268,6 +266,7 @@ public aspect JavaAspectJFileSystemAdviceDefinitions {
before():
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.fileDeleteMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.filesDeleteMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.desktopExecuteMethods() ||
de.tum.cit.ase.ares.api.aop.java.aspectj.adviceandpointcut.JavaAspectJFileSystemPointcutDefinitions.fileSystemProviderDeleteMethods() {
checkFileSystemInteraction("delete", thisJoinPoint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public aspect JavaAspectJFileSystemPointcutDefinitions {
(call(* java.io.File.canExecute(..)) ||
call(* java.io.File.renameTo(..)) ||
call(* java.io.File.toPath(..)) ||
call(* java.io.File.toURI(..)) ||
call(* java.io.File.mkdirs(..)) ||
call(* java.io.File.listFiles(..)));

Expand Down Expand Up @@ -222,20 +221,8 @@ public aspect JavaAspectJFileSystemPointcutDefinitions {

pointcut fileReaderInitMethods(): call(java.io.FileReader.new(..));

pointcut lineNumberReaderInitMethods(): call(java.io.LineNumberReader.new(..));

pointcut printWriterInitMethods(): call(java.io.PrintWriter.new(..));

pointcut bufferedWriterInitMethods(): call(java.io.BufferedWriter.new(..));

pointcut outputStreamWriterInitMethods(): call(java.io.OutputStreamWriter.new(..));

pointcut dataOutputStreamInitMethods(): call(java.io.DataOutputStream.new(..));

pointcut objectOutputStreamInitMethods(): call(java.io.ObjectOutputStream.new(..));

pointcut printStreamInitMethods(): call(java.io.PrintStream.new(..));

//TODO Sarp: These should definitely be considered in different pointcut files or this one
pointcut desktopExecuteMethods():
(call(* java.awt.Desktop.browse(..)) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.matcher.ElementMatchers;

import static de.tum.cit.ase.ares.api.aop.java.instrumentation.advice.JavaInstrumentationAdviceToolbox.localize;

/**
* This class is the entry point for the Java instrumentation agent.
* It installs the agent builder for the different types of file operations.
Expand Down Expand Up @@ -58,7 +60,7 @@ private static void installAgentBuilder(
.transform(transformer)
.installOn(inst);
} catch (Exception e) {
throw new SecurityException("Ares Security Error (Reason: Ares-Code; Stage: Creation): Failed to install agent builder on " + String.join(", ", methodsMap.keySet()) + ".", e);
throw new SecurityException(localize("security.instrumentation.agent.installation.error", String.join(", ", methodsMap.keySet())), e);

}
}
Expand Down
Loading
Loading