Skip to content

Commit f18f749

Browse files
authored
Merge pull request #322 from Nizernizer/main
Release 1.7.0
2 parents 0c3b1a7 + 521ba64 commit f18f749

File tree

6 files changed

+390
-12
lines changed

6 files changed

+390
-12
lines changed

dongtai-agent/src/test/java/com/secnium/iast/agent/AgentTest.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.secnium.iast.agent;
22

3+
import java.io.*;
34
import java.lang.management.ManagementFactory;
45
import java.lang.management.RuntimeMXBean;
6+
import java.net.InetAddress;
7+
import java.net.UnknownHostException;
58
import java.util.Arrays;
9+
import java.util.Scanner;
610

11+
import io.dongtai.iast.agent.IastProperties;
12+
import io.dongtai.iast.agent.report.AgentRegisterReport;
713
import io.dongtai.log.DongTaiLog;
814
import org.junit.Test;
915

@@ -21,7 +27,75 @@ public void appendToolsPath() {
2127
}
2228
}
2329

30+
@Test
31+
public void changeFile() {
32+
replace("/Users/erzhuangniu/workspace/DongTai-agent-java/dongtai-agent/src/main/resources/bin/fluent.conf");
33+
}
34+
35+
public static void replace(String path) {
36+
String temp = "";
37+
38+
try {
39+
File file = new File(path);
40+
FileInputStream fis = new FileInputStream(file);
41+
InputStreamReader isr = new InputStreamReader(fis);
42+
BufferedReader br = new BufferedReader(isr);
43+
StringBuffer buf = new StringBuffer();
44+
// 保存该行前面的内容
45+
while ((temp = br.readLine()) != null) {
46+
if (temp.contains("${HOSTNAME_AGENT_ID}")){
47+
temp.replace("${HOSTNAME_AGENT_ID}", AgentRegisterReport.getInternalHostName()+"-"+AgentRegisterReport.getAgentFlag().toString());
48+
}else if (temp.contains("${HOSTNAME}")){
49+
temp.replace("${HOSTNAME}",AgentRegisterReport.getInternalHostName());
50+
}else if (temp.contains("${AGENT_ID}")){
51+
temp.replace("${AGENT_ID}",AgentRegisterReport.getAgentFlag().toString());
52+
}else if (temp.contains("${OPENAPI}")){
53+
temp.replace("${OPENAPI}", IastProperties.getInstance().getBaseUrl());
54+
}else if (temp.contains("${LOG_PORT}")){
55+
temp.replace("${LOG_PORT}",IastProperties.getInstance().getLogPort());
56+
}else if (temp.contains("${LOG_PATH}")){
57+
temp.replace("${LOG_PATH}", System.getProperty("dongtai.log.path")+File.separator+"dongtai_javaagent.log");
58+
}
59+
buf = buf.append(temp);
60+
buf = buf.append(System.getProperty("line.separator"));
61+
}
62+
br.close();
63+
FileOutputStream fos = new FileOutputStream(file);
64+
PrintWriter pw = new PrintWriter(fos);
65+
pw.write(buf.toString().toCharArray());
66+
pw.flush();
67+
pw.close();
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
73+
@Test
74+
public void doFluent() {
75+
String[] execution = {
76+
"nohup",
77+
"tail",
78+
"-f",
79+
"/var/folders/xy/xyx56h3s29z6376gvk32621h0000gn/T//gunsTest001-042401-8579dc8d088d4a1680977352f6652aba/iast/fluent.conf"
80+
};
81+
try {
82+
Runtime.getRuntime().exec(execution);
83+
System.out.println("aasdasdsa");
84+
} catch (IOException e) {
85+
DongTaiLog.error(e);
86+
}
87+
}
88+
89+
@Test
90+
public void doAaaa() {
91+
String s = "https://iast.io/openapi";
92+
int i = s.indexOf("://");
93+
int i1 = s.indexOf("/openapi");
94+
System.out.println();
95+
}
96+
2497
public static void main(String[] args) {
25-
System.out.println(System.getProperty("java.io.tmpdir.dongtai"));
98+
String a = "52.81.92.214:30158";
99+
System.out.println(a.substring(a.indexOf(":")+1));
26100
}
27101
}

dongtai-core/src/main/java/io/dongtai/iast/core/utils/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
*/
66
public class Constants {
7-
public static final String AGENT_VERSION_VALUE = "v1.7.2";
7+
public static final String AGENT_VERSION_VALUE = "v1.7.0";
88
public final static String API_REPORT_UPLOAD = "/api/v1/report/upload";
99
public final static String SERVER_ADDRESS = "/api/v1/agent/update";
1010
public final static String API_HOOK_PROFILE = "/api/v1/profiles";
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
package com.secnium.iast.core;
22

3+
import io.dongtai.iast.core.utils.threadlocal.BooleanThreadLocal;
34
import io.dongtai.log.DongTaiLog;
4-
5-
import java.util.concurrent.TimeUnit;
5+
import org.junit.Test;
66

77
public class AgentEngineTest {
88

99
public static void main(String[] args) {
10-
new AgentEngineTest().a();
10+
1111
}
1212

13-
public AgentEngineTest(){
13+
public AgentEngineTest() {
1414

1515
}
1616

17-
public void a(){
17+
@Test
18+
public void a() {
19+
BooleanThreadLocal booleanThreadLocal = new BooleanThreadLocal(false);
20+
System.out.println(booleanThreadLocal.isEnterEntry());
21+
booleanThreadLocal.set(true);
22+
System.out.println(booleanThreadLocal.isEnterEntry());
23+
booleanThreadLocal.remove();
24+
System.out.println(booleanThreadLocal.isEnterEntry());
25+
}
26+
27+
@Test
28+
public void b(boolean a) {
1829
try {
19-
System.out.println("b");
20-
TimeUnit.SECONDS.sleep(10);
21-
System.out.println("a");
22-
} catch (InterruptedException e) {
23-
DongTaiLog.error(e);
30+
if (a){
31+
try {
32+
throw new IllegalStateException("DongTai agent request replay");
33+
}catch (RuntimeException e){
34+
System.out.println("DongTai agent request replay, please ignore");
35+
}
36+
}
37+
} catch (NullPointerException e) {
38+
DongTaiLog.info("DongTai agent request replay, please ignore");
2439
}
2540
}
2641

42+
2743
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.secnium.iast.core;
2+
3+
import java.util.concurrent.atomic.AtomicLong;
4+
5+
public class Test {
6+
7+
private static final ThreadLocal<Long> responseTime = new ThreadLocal<>();
8+
9+
public void execute(int count){
10+
for (int i = 0; i < count; i++) {
11+
responseTime.set(System.currentTimeMillis());
12+
String testLine = "response time:"+(System.currentTimeMillis()-responseTime.get())+"ms";
13+
}
14+
}
15+
16+
@org.junit.Test
17+
public void test(){
18+
Long start = System.currentTimeMillis();
19+
execute(10000);
20+
Long end = System.currentTimeMillis();
21+
System.out.println(end-start);
22+
}
23+
24+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package io.dongtai.iast.core;
2+
3+
import com.google.gson.reflect.TypeToken;
4+
import io.dongtai.iast.common.entity.performance.PerformanceMetrics;
5+
import io.dongtai.iast.common.entity.performance.metrics.CpuInfoMetrics;
6+
import io.dongtai.iast.common.entity.performance.metrics.MemoryUsageMetrics;
7+
import io.dongtai.iast.common.entity.performance.metrics.ThreadInfoMetrics;
8+
import io.dongtai.iast.common.entity.response.PlainResult;
9+
import io.dongtai.iast.core.bytecode.enhance.plugin.fallback.FallbackSwitch;
10+
import io.dongtai.iast.core.utils.config.entity.RemoteConfigEntityV2;
11+
import io.dongtai.iast.core.utils.config.entity.PerformanceEntity;
12+
import io.dongtai.iast.core.utils.config.entity.PerformanceLimitThreshold;
13+
import io.dongtai.iast.core.utils.json.GsonUtils;
14+
import io.dongtai.iast.core.utils.threadlocal.BooleanThreadLocal;
15+
import io.dongtai.log.DongTaiLog;
16+
import org.json.JSONObject;
17+
import org.junit.Test;
18+
19+
import java.util.List;
20+
21+
public class RemoteConfig {
22+
23+
/**
24+
* 全局配置
25+
*/
26+
private static String existsRemoteConfigMeta = "{}";
27+
private static Boolean enableAutoFallback;
28+
/**
29+
* 高频hook限流相关配置
30+
*/
31+
private static Double hookLimitTokenPerSecond;
32+
private static Double hookLimitInitBurstSeconds;
33+
/**
34+
* 高频流量限流相关配置
35+
*/
36+
private static Double heavyTrafficLimitTokenPerSecond;
37+
private static Double heavyTrafficLimitInitBurstSeconds;
38+
private static Integer heavyTrafficBreakerWaitDuration;
39+
/**
40+
* 性能熔断阈值相关配置
41+
*/
42+
private static Integer performanceBreakerWindowSize;
43+
private static Double performanceBreakerFailureRate;
44+
private static Integer performanceBreakerWaitDuration;
45+
private static Integer performanceLimitRiskMaxMetricsCount;
46+
private static List<PerformanceMetrics> performanceLimitRiskThreshold;
47+
private static List<PerformanceMetrics> performanceLimitMaxThreshold;
48+
/**
49+
* 二次降级阈值相关配置
50+
*/
51+
private static Double secondFallbackFrequencyTokenPerSecond;
52+
private static Double secondFallbackFrequencyInitBurstSeconds;
53+
private static Long secondFallbackDuration;
54+
55+
56+
private static final String REMOTE_CONFIG_DEFAULT_META = "{}";
57+
private static final String REMOTE_CONFIG_NEW_META = "{\"status\":201,\"msg\":\"\\u64cd\\u4f5c\\u6210\\u529f\",\"data\":{\"enableAutoFallback\":true,\"performanceLimitRiskMaxMetricsCount\":30,\"systemIsUninstall\":true,\"jvmIsUninstall\": true,\"applicationIsUninstall\": true,\"system\":[{\"fallbackName\":\"cpuUsagePercentage\",\"conditions\":\"greater\",\"value\":100,\"description\":\"系统 CPU 使用率阈值\"},{\"fallbackName\":\"sysMemUsagePercentage\",\"conditions\":\"greater\",\"value\":100,\"description\":\"系统内存使用率阈值\"},{\"fallbackName\":\"sysMemUsageUsed\",\"conditions\":\"greater\",\"value\":100000000000,\"description\":\"系统内存使用值阈值\"}],\"jvm\":[{\"fallbackName\":\"jvmMemUsagePercentage\",\"conditions\":\"greater\",\"value\":100,\"description\":\"JVM 内存使用率阈值\"},{\"fallbackName\":\"jvmMemUsageUsed\",\"conditions\":\"greater\",\"value\":100000000000,\"description\":\"JVM 内存使用值阈值\"},{\"fallbackName\":\"threadCount\",\"conditions\":\"greater\",\"value\":100000,\"description\":\"总线程数阈值\"},{\"fallbackName\":\"daemonThreadCount\",\"conditions\":\"greater\",\"value\":1000000,\"description\":\"守护线程数阈值\"},{\"fallbackName\":\"dongTaiThreadCount\",\"conditions\":\"greater\",\"value\":1000000,\"description\":\"洞态IAST线程数阈值\"}],\"appliaction\":[{\"fallbackName\":\"hookLimitTokenPerSecond\",\"conditions\":\"greater\",\"value\":10000,\"description\":\"单请求 HOOK 限流\"},{\"fallbackName\":\"heavyTrafficLimitTokenPerSecond\",\"conditions\":\"greater\",\"value\":100000000,\"description\":\"高频 HOOK 限流\"}]}}";
58+
59+
/**
60+
* 解析远程配置响应
61+
*/
62+
private static RemoteConfigEntityV2 parseRemoteConfigResponse(String remoteResponse) {
63+
try {
64+
// 默认响应标识调用失败
65+
if (REMOTE_CONFIG_DEFAULT_META.equals(remoteResponse)) {
66+
FallbackSwitch.setPerformanceFallback(false);
67+
return null;
68+
}
69+
if (REMOTE_CONFIG_DEFAULT_META.equals(new JSONObject(remoteResponse).get("data"))){
70+
FallbackSwitch.setPerformanceFallback(false);
71+
return null;
72+
}
73+
PlainResult<RemoteConfigEntityV2> result = GsonUtils.toObject(remoteResponse, new TypeToken<PlainResult<RemoteConfigEntityV2>>() {
74+
}.getType());
75+
// 服务端响应成功状态码
76+
if (result.isSuccess()) {
77+
return result.getData();
78+
} else {
79+
DongTaiLog.warn("remoteConfig request not success, status:{}, msg:{},response:{}", result.getStatus(), result.getMsg(),
80+
GsonUtils.toJson(remoteResponse));
81+
return null;
82+
}
83+
} catch (Throwable t) {
84+
DongTaiLog.warn("remoteConfig parse failed: msg:{}, err:{}, response:{}", t.getMessage(), t.getCause(), GsonUtils.toJson(remoteResponse));
85+
return null;
86+
}
87+
}
88+
89+
public void syncRemoteConfig() {
90+
try {
91+
// 远端有配置且和上次配置内容不一致时,重新更新配置文件
92+
String remoteResponse = REMOTE_CONFIG_NEW_META;
93+
RemoteConfigEntityV2 remoteConfigEntity = parseRemoteConfigResponse(remoteResponse);
94+
List<PerformanceEntity> application = remoteConfigEntity.getApplication();
95+
List<PerformanceEntity> jvm = remoteConfigEntity.getJvm();
96+
List<PerformanceEntity> system = remoteConfigEntity.getSystem();
97+
PerformanceLimitThreshold performanceLimitThreshold = new PerformanceLimitThreshold();
98+
MemoryUsageMetrics memoryUsage = new MemoryUsageMetrics();
99+
ThreadInfoMetrics threadInfoMetrics = new ThreadInfoMetrics();
100+
CpuInfoMetrics cpuInfoMetrics = new CpuInfoMetrics();
101+
MemoryUsageMetrics memoryNoHeapUsage = new MemoryUsageMetrics();
102+
103+
104+
if (null != remoteConfigEntity && !remoteResponse.equals(existsRemoteConfigMeta)) {
105+
if (remoteConfigEntity.getEnableAutoFallback() != null) {
106+
enableAutoFallback = remoteConfigEntity.getEnableAutoFallback();
107+
}
108+
if (remoteConfigEntity.getPerformanceLimitRiskMaxMetricsCount() != null) {
109+
performanceLimitRiskMaxMetricsCount = remoteConfigEntity.getPerformanceLimitRiskMaxMetricsCount()/30 + remoteConfigEntity.getPerformanceLimitRiskMaxMetricsCount()%30==0?0:1;
110+
}
111+
112+
performanceLimitRiskMaxMetricsCount = remoteConfigEntity.getPerformanceLimitRiskMaxMetricsCount();
113+
if (remoteConfigEntity.getApplication() != null) {
114+
for (PerformanceEntity performanceEntity:application){
115+
switch (performanceEntity.getFallbackName()){
116+
case "hookLimitTokenPerSecond":
117+
hookLimitTokenPerSecond = performanceEntity.getValue();
118+
break;
119+
case "heavyTrafficLimitTokenPerSecond":
120+
heavyTrafficLimitTokenPerSecond = performanceEntity.getValue();
121+
break;
122+
}
123+
}
124+
}
125+
126+
if (remoteConfigEntity.getJvm() != null) {
127+
for (PerformanceEntity performanceEntity:jvm){
128+
switch (performanceEntity.getFallbackName()){
129+
case "jvmMemUsagePercentage":{
130+
memoryUsage.setMemUsagePercentage(performanceEntity.getValue());
131+
break;
132+
}
133+
case "jvmMemUsageUsed":{
134+
memoryUsage.setUsed(performanceEntity.getValue().longValue());
135+
break;
136+
}
137+
case "threadCount":{
138+
threadInfoMetrics.setThreadCount(performanceEntity.getValue().intValue());
139+
break;
140+
}
141+
case "daemonThreadCount":{
142+
threadInfoMetrics.setDaemonThreadCount(performanceEntity.getValue().intValue());
143+
break;
144+
}
145+
case "dongTaiThreadCount":{
146+
threadInfoMetrics.setDongTaiThreadCount(performanceEntity.getValue().intValue());
147+
break;
148+
}
149+
}
150+
}
151+
}
152+
153+
if (remoteConfigEntity.getSystem() != null) {
154+
for (PerformanceEntity performanceEntity:system){
155+
switch (performanceEntity.getFallbackName()){
156+
case "cpuUsagePercentage":{
157+
cpuInfoMetrics.setCpuUsagePercentage(performanceEntity.getValue());
158+
break;
159+
}
160+
case "sysMemUsagePercentage":{
161+
memoryNoHeapUsage.setMemUsagePercentage(performanceEntity.getValue());
162+
break;
163+
}
164+
case "sysMemUsageUsed":{
165+
memoryNoHeapUsage.setUsed(performanceEntity.getValue().longValue());
166+
break;
167+
}
168+
}
169+
}
170+
}
171+
172+
threadInfoMetrics.setPeakThreadCount(1000000000);
173+
memoryUsage.setMax(1000000000000L);
174+
memoryNoHeapUsage.setMax(1000000000000L);
175+
performanceLimitThreshold.setThreadInfo(threadInfoMetrics);
176+
performanceLimitThreshold.setMemoryUsage(memoryUsage);
177+
performanceLimitThreshold.setMemoryNoHeapUsage(memoryNoHeapUsage);
178+
performanceLimitThreshold.setCpuUsage(cpuInfoMetrics);
179+
performanceLimitRiskThreshold = performanceLimitRiskThreshold;
180+
existsRemoteConfigMeta = remoteResponse;
181+
DongTaiLog.debug("Sync remote config successful.");
182+
}
183+
} catch (Throwable t) {
184+
DongTaiLog.warn("Sync remote config failed, msg: {}, error: {}", t.getMessage(), t.getCause());
185+
}
186+
}
187+
188+
private static final BooleanThreadLocal HEAVY_HOOK_FALLBACK = new BooleanThreadLocal(false);
189+
190+
@Test
191+
public void testJson(){
192+
HEAVY_HOOK_FALLBACK.remove();
193+
System.out.println(HEAVY_HOOK_FALLBACK.get());
194+
HEAVY_HOOK_FALLBACK.remove();
195+
System.out.println(HEAVY_HOOK_FALLBACK.get());
196+
HEAVY_HOOK_FALLBACK.remove();
197+
System.out.println(HEAVY_HOOK_FALLBACK.get());
198+
}
199+
200+
}

0 commit comments

Comments
 (0)