|
| 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