Skip to content

Commit fc86a1c

Browse files
committed
Create and import agent_bootstrap
1 parent 6414305 commit fc86a1c

5 files changed

Lines changed: 89 additions & 1 deletion

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ build: clean check_binaries
1414
./gradlew agent_api:shadowJar
1515
cp agent_api/build/libs/agent*-all.jar dist/agent_api.jar
1616

17+
18+
./gradlew agent_bootstrap:shadowJar
19+
cp agent_bootstrap/build/libs/agent*-all.jar dist/agent_bootstrap.jar
20+
1721
mock_init:
1822
docker kill mock_core && docker rm mock_core
1923
cd end2end/server && docker build -t mock_core .

agent/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55

66
dependencies {
77
implementation project(':agent_api')
8+
compileOnly project(':agent_bootstrap')
89
implementation 'net.bytebuddy:byte-buddy:1.15.11'
910
// Compile only for interface types :
1011
compileOnly 'jakarta.servlet:jakarta.servlet-api:6.1.0' // spring 3 -> jakarta

agent_bootstrap/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'java'
3+
id 'com.github.johnrengelman.shadow' version '7.1.0'
4+
}
5+
6+
dependencies {
7+
}
8+
9+
shadowJar {
10+
mergeServiceFiles()
11+
manifest {
12+
}
13+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package dev.aikido.agent_bootstrap;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.net.InetAddress;
6+
import java.net.MalformedURLException;
7+
import java.net.URL;
8+
import java.net.URLClassLoader;
9+
10+
public final class AikidoBootstrapClass {
11+
// Define some constants for the collectors we use:
12+
public final static Method URL_COLLECTOR_REPORT =
13+
load("URLCollector", "report", URL.class);
14+
15+
public final static Method FILE_COLLECTOR_REPORT =
16+
load("FileCollector", "report", Object.class, String.class);
17+
18+
public final static Method REDIRECT_COLLECTOR_REPORT =
19+
load("RedirectCollector", "report", URL.class, URL.class);
20+
21+
public final static Method DNS_RECORD_COLLECTOR_REPORT =
22+
load("DNSRecordCollector", "report", String.class, InetAddress[].class);
23+
24+
public final static Method COMMAND_COLLECTOR_REPORT =
25+
load("CommandCollector", "report", Object.class);
26+
27+
// Since we have to wrap a native Java Class stuff gets more complicated
28+
// The classpath is not the same anymore, and we can't import our modules directly.
29+
// To bypass this issue we load collectors from a .jar file
30+
public static Method load(String className, String expectedMethodName, Class<?>... parameterTypes) {
31+
try {
32+
Class<?> requestedClass = loadClass("dev.aikido.agent_api.collectors." + className);
33+
return requestedClass.getMethod(expectedMethodName, parameterTypes);
34+
} catch (Throwable e) {
35+
System.out.println("AIKIDO: " + e.getMessage());
36+
}
37+
return null;
38+
}
39+
40+
public static Object invoke(Method method, Object... arguments) throws Throwable {
41+
try {
42+
// Do not wrap calls that happen in the background process.
43+
if (Thread.currentThread().getClass().toString()
44+
.equals("class dev.aikido.agent_api.background.BackgroundProcess")) {
45+
return null;
46+
}
47+
48+
return method.invoke(null, arguments);
49+
} catch (InvocationTargetException invocationTargetException) {
50+
if (invocationTargetException.getCause().toString().startsWith("dev.aikido.agent_api.vulnerabilities")) {
51+
throw invocationTargetException.getCause();
52+
}
53+
// Ignore non-aikido exceptions
54+
System.out.println("AIKIDO: " + invocationTargetException.getTargetException().getMessage());
55+
} catch (Throwable e) {
56+
System.out.println("AIKIDO: " + e.getMessage());
57+
}
58+
return null;
59+
}
60+
61+
/**
62+
* Load class path from the aikido agent api jar file
63+
*/
64+
private static Class<?> loadClass(String classPath) throws MalformedURLException, ClassNotFoundException {
65+
String jarFilePath = System.getProperty("AIK_agent_api_jar");
66+
URL[] urls = {new URL(jarFilePath)};
67+
URLClassLoader classLoader = new URLClassLoader(urls);
68+
return classLoader.loadClass(classPath);
69+
}
70+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
rootProject.name = 'firewall-java'
2-
include 'agent', 'agent_api'
2+
include 'agent', 'agent_api', 'agent_bootstrap'

0 commit comments

Comments
 (0)