Skip to content

Commit

Permalink
feat: alarm rule checker (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
masaimu authored Mar 27, 2024
1 parent 86f7856 commit d251dcc
Show file tree
Hide file tree
Showing 39 changed files with 1,325 additions and 929 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
import io.holoinsight.server.home.biz.service.EnvironmentService;
import io.holoinsight.server.home.biz.service.TenantInitService;
import io.holoinsight.server.home.biz.service.UserinfoVerificationService;
import io.holoinsight.server.home.biz.service.impl.AlarmHistoryServiceImpl;
import io.holoinsight.server.home.biz.service.impl.AlertRuleServiceImpl;
import io.holoinsight.server.home.biz.service.impl.DefaultEnvironmentServiceImpl;
import io.holoinsight.server.home.biz.service.impl.DefaultTenantInitServiceImpl;
import io.holoinsight.server.home.biz.service.impl.UserinfoVerificationServiceImpl;
import io.holoinsight.server.home.common.service.RequestContextAdapter;
import io.holoinsight.server.home.common.service.RequestContextAdapterImpl;
import io.holoinsight.server.home.facade.utils.ApiSecurityService;
import io.holoinsight.server.home.web.security.ApiSecurityService;
import io.holoinsight.server.home.web.controller.TraceAgentFacadeImpl;
import io.holoinsight.server.home.web.security.ApiSecurityServiceImpl;
import io.holoinsight.server.home.web.security.ParameterSecurityService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public interface RequestContextAdapter {
String getTenantFromContext(RequestContext.Context context);

String getWorkspaceFromContext(RequestContext.Context context);

String getSimpleWorkspaceFromContext(RequestContext.Context context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ public String getTenantFromContext(RequestContext.Context context) {
public String getWorkspaceFromContext(RequestContext.Context context) {
return context.ms.workspace;
}

@Override
public String getSimpleWorkspaceFromContext(RequestContext.Context context) {
return getWorkspaceFromContext(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,19 @@
*/
package io.holoinsight.server.home.dal.model.dto;

import io.holoinsight.server.home.common.service.SpringContext;
import io.holoinsight.server.home.facade.ApiSecurity;
import io.holoinsight.server.home.facade.utils.ApiSecurityService;
import io.holoinsight.server.home.facade.utils.CreateCheck;
import io.holoinsight.server.home.facade.utils.ExistCheck;
import io.holoinsight.server.home.facade.utils.ParaCheckUtil;
import io.holoinsight.server.home.facade.utils.UpdateCheck;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.Date;

import static io.holoinsight.server.home.facade.utils.CheckCategory.CUSTOM;
import static io.holoinsight.server.home.facade.utils.CheckCategory.IS_NULL;
import static io.holoinsight.server.home.facade.utils.CheckCategory.NOT_NULL;

/**
* @author wangsiyuan
* @date 2022/6/15 4:52 下午
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class AlarmBlockDTO extends ApiSecurity {
public class AlarmBlockDTO {
/**
* id
*/
@CreateCheck(IS_NULL)
@UpdateCheck({NOT_NULL, CUSTOM})
@ExistCheck(column = {"id", "tenant", "workspace"}, mapper = "alarmBlockMapper")
private Long id;

/**
Expand Down Expand Up @@ -69,8 +51,6 @@ public class AlarmBlockDTO extends ApiSecurity {
/**
* 告警id
*/
@CreateCheck(CUSTOM)
@UpdateCheck(CUSTOM)
private String uniqueId;

/**
Expand All @@ -81,7 +61,6 @@ public class AlarmBlockDTO extends ApiSecurity {
/**
* 租户id
*/
@UpdateCheck({NOT_NULL, CUSTOM})
private String tenant;

/**
Expand All @@ -108,44 +87,4 @@ public class AlarmBlockDTO extends ApiSecurity {
* 结束时间
*/
private Date endTime;

@Override
public void customCheckRead(Field field, String tenant, String workspace) {

}

@Override
public void customCheckUpdate(Field field, String tenant, String workspace) {
ApiSecurityService apiSecurityService = SpringContext.getBean(ApiSecurityService.class);
String fieldName = field.getName();
switch (fieldName) {
case "uniqueId":
if (StringUtils.isNotEmpty(this.uniqueId)) {
ParaCheckUtil.checkParaBoolean(
apiSecurityService.checkRuleTenantAndWorkspace(this.uniqueId, tenant, workspace),
"uniqueId do not belong to this tenant or workspace");
}
break;
case "tenant":
if (!StringUtils.equals(this.tenant, tenant)) {
throwMonitorException("tenant is illegal");
}
break;
}
}

@Override
public void customCheckCreate(Field field, String tenant, String workspace) {
ApiSecurityService apiSecurityService = SpringContext.getBean(ApiSecurityService.class);
String fieldName = field.getName();
switch (fieldName) {
case "uniqueId":
if (StringUtils.isNotEmpty(this.uniqueId)) {
ParaCheckUtil.checkParaBoolean(
apiSecurityService.checkRuleTenantAndWorkspace(this.uniqueId, tenant, workspace),
"uniqueId do not belong to this tenant or workspace");
}
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
import io.holoinsight.server.home.facade.trigger.DataSource;
import io.holoinsight.server.home.facade.trigger.Filter;
import io.holoinsight.server.home.facade.trigger.Trigger;
import io.holoinsight.server.home.facade.utils.ApiSecurityService;
import io.holoinsight.server.home.facade.utils.CreateCheck;
import io.holoinsight.server.home.facade.utils.ExistCheck;
import io.holoinsight.server.home.facade.utils.UpdateCheck;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

Expand All @@ -28,24 +23,18 @@
import java.util.Map;

import static io.holoinsight.server.home.facade.utils.CheckCategory.CUSTOM;
import static io.holoinsight.server.home.facade.utils.CheckCategory.IS_NULL;
import static io.holoinsight.server.home.facade.utils.CheckCategory.NOT_NULL;
import static io.holoinsight.server.home.facade.utils.CheckCategory.SQL_NAME;

/**
* @author wangsiyuan
* @date 2022/4/12 9:38 下午
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class AlarmRuleDTO extends ApiSecurity {
public class AlarmRuleDTO {

/**
* id
*/
@CreateCheck(IS_NULL)
@UpdateCheck({NOT_NULL, CUSTOM})
@ExistCheck(column = {"id", "tenant", "workspace"}, mapper = "alarmRuleMapper")
private Long id;

/**
Expand All @@ -61,8 +50,6 @@ public class AlarmRuleDTO extends ApiSecurity {
/**
* 规则名称
*/
@CreateCheck({NOT_NULL, SQL_NAME})
@UpdateCheck({SQL_NAME})
private String ruleName;

/**
Expand All @@ -83,7 +70,6 @@ public class AlarmRuleDTO extends ApiSecurity {
/**
* 告警级别
*/
@CreateCheck(NOT_NULL)
private String alarmLevel;

/**
Expand All @@ -94,13 +80,11 @@ public class AlarmRuleDTO extends ApiSecurity {
/**
* 规则是否生效
*/
@CreateCheck(NOT_NULL)
private Byte status;

/**
* 合并是否开启
*/
@CreateCheck(NOT_NULL)
private Byte isMerge;

/**
Expand All @@ -111,7 +95,6 @@ public class AlarmRuleDTO extends ApiSecurity {
/**
* 恢复通知是否开启
*/
@CreateCheck(NOT_NULL)
private Byte recover;

/**
Expand All @@ -127,7 +110,6 @@ public class AlarmRuleDTO extends ApiSecurity {
/**
* 租户id
*/
@UpdateCheck({NOT_NULL, CUSTOM})
private String tenant;

/**
Expand All @@ -138,14 +120,11 @@ public class AlarmRuleDTO extends ApiSecurity {
/**
* 告警规则
*/
@CreateCheck({NOT_NULL, CUSTOM})
@UpdateCheck({CUSTOM})
private Map<String, Object> rule;

/**
* 生效时间
*/
@CreateCheck(NOT_NULL)
private Map<String, Object> timeFilter;

/**
Expand Down Expand Up @@ -324,57 +303,4 @@ public List<String> getMetric() {
}
return filters;
}

@Override
public void customCheckRead(Field field, String tenant, String workspace) {

}

@Override
public void customCheckCreate(Field field, String tenant, String workspace) {
String fieldName = field.getName();
switch (fieldName) {
case "rule":
checkMetrics(tenant, workspace);
break;
}
}

@Override
public void customCheckUpdate(Field field, String tenant, String workspace) {
String fieldName = field.getName();
switch (fieldName) {
case "tenant":
if (!StringUtils.equals(this.tenant, tenant)) {
throwMonitorException("tenant is illegal");
}
break;
case "rule":
checkMetrics(tenant, workspace);
break;
}
}

private void checkMetrics(String tenant, String workspace) {
List<String> metrics = getMetric();
if (CollectionUtils.isEmpty(metrics)) {
return;
}
ApiSecurityService apiSecurityService = SpringContext.getBean(ApiSecurityService.class);
for (String metric : metrics) {
if (apiSecurityService.isGlobalMetric(metric)) {
Map<String /* tagk */, List<Object> /* tagvs */> filter = getFilters(metric);
boolean checkResult = apiSecurityService.checkFilter(metric, filter, tenant, workspace);
if (!checkResult) {
throwMonitorException("the tenant or workspace of " + metric + " is invalid.");
}
} else {
boolean checkResult =
apiSecurityService.checkMetricTenantAndWorkspace(metric, tenant, workspace);
if (!checkResult) {
throwMonitorException("the tenant or workspace of " + metric + " is invalid.");
}
}
}
}
}
Loading

0 comments on commit d251dcc

Please sign in to comment.