Skip to content

Commit

Permalink
Merge pull request #216 from Nizernizer/main
Browse files Browse the repository at this point in the history
Add log
  • Loading branch information
exexute authored Jan 7, 2022
2 parents 2b532a0 + 833920e commit 3815f66
Show file tree
Hide file tree
Showing 54 changed files with 704 additions and 400 deletions.
10 changes: 8 additions & 2 deletions iast-agent/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -53,6 +53,12 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>

<dependency>
<groupId>com.secnium.iast</groupId>
<artifactId>iast-log</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>

<build>
Expand Down
10 changes: 5 additions & 5 deletions iast-agent/src/main/java/com/secnium/iast/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.net.URL;
import java.net.URLClassLoader;

import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.log.DongTaiLog;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand Down Expand Up @@ -47,16 +47,16 @@ public static void main(String[] args) {
String jdkVersion = getJdkVersion();
if ("1".equals(jdkVersion) && appendToolsPath()) {
AttachLauncher.attach(pid, attachArgs);
LogUtils.info("engine " + attachArgs + " successfully. pid: " + pid);
DongTaiLog.info("engine " + attachArgs + " successfully. pid: " + pid);
} else {
AttachLauncher.attach(pid, attachArgs);
LogUtils.info("engine " + attachArgs + " successfully. pid: " + pid);
DongTaiLog.info("engine " + attachArgs + " successfully. pid: " + pid);
}
} else {
formatter.printHelp("java -jar agent.jar", attachOptions, true);
}
} catch (Throwable t) {
LogUtils.error("Start DongTai Agent failed, exception stack trace: ");
DongTaiLog.error("Start DongTai Agent failed, exception stack trace: ");
t.printStackTrace();
System.exit(-1);
}
Expand All @@ -69,7 +69,7 @@ public static void main(String[] args) {
*/
public static String getJdkVersion() {
String jdkVersion = System.getProperty("java.version", "1.8");
LogUtils.info("current jdk version is : " + jdkVersion);
DongTaiLog.info("current jdk version is : " + jdkVersion);
String[] jdkVersionItem = jdkVersion.split("\\.");
boolean isHighJdk = true;
if (jdkVersionItem.length > 1 && ("6".equals(jdkVersionItem[1]) || "7".equals(jdkVersionItem[1]) || "8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.secnium.iast.agent.monitor.EngineMonitor;
import com.secnium.iast.agent.monitor.MonitorDaemonThread;
import com.secnium.iast.agent.report.AgentRegisterReport;
import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.log.DongTaiLog;

import java.lang.instrument.Instrumentation;
import java.util.concurrent.TimeUnit;

/**
* @author dongzhiyong@huoxian.cn
Expand Down Expand Up @@ -40,7 +42,7 @@ public static void premain(String args, Instrumentation inst) {
*/
public static void agentmain(String featureString, Instrumentation inst) {
if ("uninstall".equals(featureString)) {
LogUtils.info("Engine is about to be uninstalled");
DongTaiLog.info("Engine is about to be uninstalled");
uninstall();
} else {
LAUNCH_MODE = LAUNCH_MODE_ATTACH;
Expand Down Expand Up @@ -72,17 +74,17 @@ private static void install(final Instrumentation inst) {
IastProperties iastProperties = IastProperties.getInstance();
Boolean send = AgentRegisterReport.send();
if (send) {
LogUtils.info("Agent has successfully registered with " + iastProperties.getBaseUrl());
DongTaiLog.info("Agent has successfully registered with " + iastProperties.getBaseUrl());
Boolean agentStat = AgentRegisterReport.agentStat();
if (!agentStat) {
EngineMonitor.isCoreRegisterStart = false;
LogUtils.info("The agent was not audited. Disable enabling.");
DongTaiLog.info("The agent was not audited. Disable enabling.");
} else {
EngineMonitor.isCoreRegisterStart = true;
}
loadEngine(inst);
} else {
LogUtils.error("Agent register failed. Start without DongTai IAST.");
DongTaiLog.error("Agent register failed. Start without DongTai IAST.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.secnium.iast.agent;

import com.secnium.iast.agent.util.JavaVersionUtils;
import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.log.DongTaiLog;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.IOException;
import java.util.Properties;

Expand All @@ -17,25 +18,25 @@ public class AttachLauncher {
public static void attach(String pid, String args) throws Exception {
VirtualMachine vmObj = null;
try {
LogUtils.info("trying attach to process " + pid + ", agent address is " + AGENT_PATH);
DongTaiLog.info("trying attach to process " + pid + ", agent address is " + AGENT_PATH);
vmObj = VirtualMachine.attach(pid);
Properties targetSystemProperties = vmObj.getSystemProperties();
String targetJavaVersion = JavaVersionUtils.javaVersionStr(targetSystemProperties);
String currentJavaVersion = JavaVersionUtils.javaVersionStr();
if (targetJavaVersion != null && currentJavaVersion != null) {
if (!targetJavaVersion.equals(currentJavaVersion)) {
LogUtils.error(
DongTaiLog.error(
"Current VM java version: " + currentJavaVersion + " do not match target VM java version: "
+ targetJavaVersion + ", attach may fail.");
LogUtils.error(
DongTaiLog.error(
"Target VM JAVA_HOME is " + targetSystemProperties.getProperty("java.home")
+ ", DongTai-Agent JAVA_HOME is " + System.getProperty("java.home")
+ ", try to set the same JAVA_HOME.");
}
}

vmObj.loadAgent(AGENT_PATH, args);
LogUtils.info("attach to process " + pid + " success.");
DongTaiLog.info("attach to process " + pid + " success.");
} finally {
if (null != vmObj) {
vmObj.detach();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.secnium.iast.agent;

import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.log.DongTaiLog;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void init(String path) throws ClassNotFoundException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
cfg.load(inputStream);

LogUtils.info(
DongTaiLog.info(
"The engine configuration file is initialized successfully. file is " + propertiesFile.toString());
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.secnium.iast.agent;

import com.secnium.iast.agent.monitor.MonitorDaemonThread;
import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.log.DongTaiLog;

public class ShutdownThread extends Thread {

@Override
public void run() {
LogUtils.info("enter shutdown thread");
DongTaiLog.info("enter shutdown thread");
MonitorDaemonThread.isExit = true;
LogUtils.info("exit shutdown thread");
DongTaiLog.info("exit shutdown thread");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.secnium.iast.agent.*;
import com.secnium.iast.agent.report.AgentRegisterReport;
import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.agent.util.http.HttpClientUtils;
import com.secnium.iast.log.DongTaiLog;

import java.io.BufferedInputStream;
import java.io.File;
Expand Down Expand Up @@ -134,18 +134,18 @@ private boolean downloadJarPackageToCacheFromUrl(String fileUrl, String fileName
final File classPath = new File(new File(fileName).getParent());

if (!classPath.mkdirs() && !classPath.exists()) {
LogUtils.info("Check or create local file cache path, path is " + classPath);
DongTaiLog.info("Check or create local file cache path, path is " + classPath);
}
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
LogUtils.info("The remote file " + fileUrl + " was successfully written to the local cache.");
DongTaiLog.info("The remote file " + fileUrl + " was successfully written to the local cache.");
status = true;
} catch (Exception ignore) {
LogUtils.error("The remote file " + fileUrl + " download failure, please check the iast-token.");
DongTaiLog.error("The remote file " + fileUrl + " download failure, please check the iast-token.");
}
return status;
}
Expand All @@ -164,7 +164,7 @@ public boolean updateEnginePackage() {

public boolean downloadEnginePackage() {
if (engineNotExist(getInjectPackageCachePath()) || engineNotExist(getEnginePackageCachePath())) {
LogUtils.info("Engine does not exist in local cache, the engine will be downloaded.");
DongTaiLog.info("Engine does not exist in local cache, the engine will be downloaded.");
return updateEnginePackage();
} else {
return true;
Expand All @@ -184,11 +184,11 @@ public boolean install() {
.invoke(null, launchMode, this.properties.getPropertiesFilePath(), AgentRegisterReport.getAgentFlag(), inst, agentPath);
return true;
} catch (IOException e) {
LogUtils.error("DongTai engine start failed, please contact staff for help.");
DongTaiLog.error("DongTai engine start failed, please contact staff for help.");
} catch (ClassNotFoundException e) {
LogUtils.error(" DongTai engine start failed, please contact staff for help.");
DongTaiLog.error(" DongTai engine start failed, please contact staff for help.");
} catch (Throwable throwable) {
LogUtils.error("DongTai engine start failed, please contact staff for help.");
DongTaiLog.error("DongTai engine start failed, please contact staff for help.");
throwable.printStackTrace();
}
return false;
Expand All @@ -202,18 +202,18 @@ public boolean start() {
try {
if (classOfEngine != null) {
classOfEngine.getMethod("start").invoke(null);
LogUtils.info("DongTai engine start successfully.");
DongTaiLog.info("DongTai engine start successfully.");
return true;
}
return false;
} catch (InvocationTargetException e) {
LogUtils.error("DongTai engine start failed, please contact staff for help.");
DongTaiLog.error("DongTai engine start failed, please contact staff for help.");
} catch (NoSuchMethodException e) {
LogUtils.error("DongTai engine start failed, please contact staff for help.");
DongTaiLog.error("DongTai engine start failed, please contact staff for help.");
} catch (IllegalAccessException e) {
LogUtils.error("DongTai engine start failed, please contact staff for help.");
DongTaiLog.error("DongTai engine start failed, please contact staff for help.");
} catch (Throwable throwable) {
LogUtils.error("DongTai engine start failed, please contact staff for help.");
DongTaiLog.error("DongTai engine start failed, please contact staff for help.");
throwable.printStackTrace();
}
return false;
Expand All @@ -229,23 +229,23 @@ public boolean stop() {
try {
if (classOfEngine != null) {
classOfEngine.getMethod("stop").invoke(null);
LogUtils.info("DongTai engine stop successfully.");
DongTaiLog.info("DongTai engine stop successfully.");
return true;
}
return false;
} catch (InvocationTargetException e) {
LogUtils.error("DongTai engine stop failed, please contact staff for help.");
DongTaiLog.error("DongTai engine stop failed, please contact staff for help.");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
LogUtils.error(sw.toString());
DongTaiLog.error(sw.toString());
} catch (NoSuchMethodException e) {
LogUtils.error("DongTai engine stop failed, please contact staff for help.");
DongTaiLog.error("DongTai engine stop failed, please contact staff for help.");
} catch (IllegalAccessException e) {
LogUtils.error("DongTai engine stop failed, please contact staff for help.");
DongTaiLog.error("DongTai engine stop failed, please contact staff for help.");
e.printStackTrace();
} catch (Throwable throwable) {
LogUtils.error("DongTai engine stop failed, please contact staff for help.");
DongTaiLog.error("DongTai engine stop failed, please contact staff for help.");
throwable.printStackTrace();
}
return false;
Expand Down Expand Up @@ -329,10 +329,10 @@ private static synchronized ClassLoader loadOrDefineClassLoader(final String cor
* @return true-引擎不存在;false-引擎存在
*/
private boolean engineNotExist(final String jarPath) {
LogUtils.info("Check if the engine[" + jarPath + "] needs to be updated");
DongTaiLog.info("Check if the engine[" + jarPath + "] needs to be updated");

if (properties.isDebug()) {
LogUtils.info("current mode: debug, load engine from " + jarPath);
DongTaiLog.info("current mode: debug, load engine from " + jarPath);
File tempFile = new File(jarPath);
return !tempFile.exists();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.secnium.iast.agent.*;
import com.secnium.iast.agent.manager.EngineManager;
import com.secnium.iast.agent.report.AgentRegisterReport;
import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.agent.util.http.HttpClientUtils;
import com.secnium.iast.log.DongTaiLog;
import org.json.JSONObject;

/**
Expand All @@ -31,10 +31,10 @@ public void check() {
isCoreRegisterStart = true;
startEngine();
}else if ("coreStop".equals(status) && isCoreRegisterStart) {
LogUtils.info("engine stop");
DongTaiLog.info("engine stop");
engineManager.stop();
} else if ("coreStart".equals(status) && isCoreRegisterStart) {
LogUtils.info("engine start");
DongTaiLog.info("engine start");
engineManager.start();
}
}
Expand All @@ -57,7 +57,7 @@ public void startEngine() {
status = status && engineManager.install();
status = status && engineManager.start();
if (!status) {
LogUtils.info("DongTai IAST started failure");
DongTaiLog.info("DongTai IAST started failure");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.secnium.iast.agent.monitor;

import com.secnium.iast.agent.manager.EngineManager;
import com.secnium.iast.agent.util.LogUtils;
import com.secnium.iast.log.DongTaiLog;

import java.util.ArrayList;

/**
Expand All @@ -22,10 +23,10 @@ public MonitorDaemonThread(EngineManager engineManager) {
this.engineManager = engineManager;
try {
delayTime = Integer.parseInt(System.getProperty("iast.engine.delay.time", "0"));
LogUtils.info("engine delay time is " + delayTime + " s");
DongTaiLog.info("engine delay time is " + delayTime + " s");
delayTime = delayTime * 1000;
} catch (Exception e) {
LogUtils.error("engine delay time must be int,eg: 10、20");
DongTaiLog.error("engine delay time must be int,eg: 10、20");
delayTime = 0;
}
}
Expand Down Expand Up @@ -65,7 +66,7 @@ public void startEngine() {
status = status && engineManager.install();
status = status && engineManager.start();
if (!status) {
LogUtils.info("DongTai IAST started failure");
DongTaiLog.info("DongTai IAST started failure");
}
}
}
Loading

0 comments on commit 3815f66

Please sign in to comment.