Skip to content

Commit

Permalink
update to aspect
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceilzcx committed Nov 10, 2024
1 parent 6f285bc commit efaa041
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.common.constants;

import org.apache.hertzbeat.common.util.DesensitizedUtil;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({ FIELD})
@Retention(RUNTIME)
@Documented
public @interface DesensitizedField {
DesensitizedUtil.DesensitizedType desensitizedType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.constants.DesensitizedField;
import org.apache.hertzbeat.common.util.DesensitizedUtil;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -84,12 +86,14 @@ public class NoticeReceiver {
description = "Mobile number: Valid when the notification method is SMS",
example = "18923435643", accessMode = READ_WRITE)
@Size(max = 100)
@DesensitizedField(desensitizedType= DesensitizedUtil.DesensitizedType.MOBILE_PHONE)
private String phone;

@Schema(title = "Email account: Valid when the notification method is email",
description = "Email account: Valid when the notification method is email",
example = "[email protected]", accessMode = READ_WRITE)
@Size(max = 100)
@DesensitizedField(desensitizedType= DesensitizedUtil.DesensitizedType.EMAIL)
private String email;

@Schema(title = "URL address: The notification method is valid for webhook",
Expand Down Expand Up @@ -148,6 +152,7 @@ public class NoticeReceiver {
@Schema(title = "Enterprise weChat secret: The notification method is valid for Enterprise WeChat app message",
description = "Enterprise weChat secret: The notification method is valid for Enterprise WeChat app message",
example = "oUydwn92ey0lnuY02MixNa57eNK-20dJn5NEOG-u2uE", accessMode = READ_WRITE)
@DesensitizedField(desensitizedType= DesensitizedUtil.DesensitizedType.PASSWORD)
private String appSecret;

@Schema(title = "Enterprise weChat party id: The notification method is valid for Enterprise WeChat app message",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@
*/
public class DesensitizedUtil {

/**
* desensitize field
*/
public enum DesensitizedType {
MOBILE_PHONE,
EMAIL,
PASSWORD
}

/**
* desensitize field
* @param type field type
* @param str field value
* @return desensitized value
*/
public static String desensitize(DesensitizedType type, String str) {
if (type == null || StringUtils.isEmpty(str)) {
return str;
}
switch (type) {
case MOBILE_PHONE -> str = mobilePhone(str);
case EMAIL -> str = email(str);
case PASSWORD -> str = password(str);
default -> {}
}
return str;
}

/**
* desensitize mobile phone
* @param str field value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.manager.aspect;

import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.constants.DesensitizedField;
import org.apache.hertzbeat.common.entity.manager.NoticeReceiver;
import org.apache.hertzbeat.common.util.DesensitizedUtil;
import org.apache.hertzbeat.manager.dao.NoticeReceiverDao;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

@Aspect
@Component
@Slf4j
public class DesensitizedAspect {
@Resource
private NoticeReceiverDao noticeReceiverDao;

@Around("execution(* org.apache.hertzbeat.manager.service.NoticeConfigService.*(..)))")
public Object around(ProceedingJoinPoint point) {
try {
unDesensitized(point.getArgs());
Object result = point.proceed();
return desensitized(result);
} catch (Throwable e) {
log.error(e.getMessage(), e);
}
return null;
}

private void unDesensitized(Object[] args) throws IllegalAccessException {
if (args == null || args.length == 0) {
return;
}
Long receiverId = null;
for (Object arg : args) {
if (arg instanceof NoticeReceiver argNoticeReceiver) {
receiverId = argNoticeReceiver.getId();
break;
}
}
if (receiverId == null) {
return;
}
NoticeReceiver noticeReceiver = noticeReceiverDao.findById(receiverId).orElse(null);
if (noticeReceiver == null) {
return;
}
for (Object arg : args) {
if (arg instanceof NoticeReceiver argNoticeReceiver) {
for (Field field : argNoticeReceiver.getClass().getDeclaredFields()) {
DesensitizedField annotation = field.getAnnotation(DesensitizedField.class);
if (annotation != null) {
field.setAccessible(true);
DesensitizedUtil.DesensitizedType desensitizedType = annotation.desensitizedType();
String desensitizedValue = DesensitizedUtil.desensitize(desensitizedType, field.get(noticeReceiver).toString());
if (field.get(argNoticeReceiver) != null && field.get(argNoticeReceiver).equals(desensitizedValue)) {
field.set(argNoticeReceiver, field.get(noticeReceiver));
}
}
}
}
}
}

private Object desensitized(Object result) throws IllegalAccessException {
if (result == null) {
return null;
}
if (result instanceof List<?>) {
for (Object item : ((List<?>) result)) {
desensitizedField(item);
}
} else {
desensitizedField(result);
}
return result;
}

private void desensitizedField(Object result) throws IllegalAccessException {
for (Field field : result.getClass().getDeclaredFields()) {
DesensitizedField annotation = field.getAnnotation(DesensitizedField.class);
if (annotation != null) {
DesensitizedUtil.DesensitizedType desensitizedType = annotation.desensitizedType();
field.setAccessible(true);
field.set(result, DesensitizedUtil.desensitize(desensitizedType, field.get(result).toString()));
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.hertzbeat.common.entity.manager.NoticeReceiver;
import org.apache.hertzbeat.common.entity.manager.NoticeRule;
import org.apache.hertzbeat.common.entity.manager.NoticeTemplate;
import org.apache.hertzbeat.common.util.DesensitizedUtil;
import org.apache.hertzbeat.manager.component.alerter.DispatcherAlarm;
import org.apache.hertzbeat.manager.dao.NoticeReceiverDao;
import org.apache.hertzbeat.manager.dao.NoticeRuleDao;
Expand Down Expand Up @@ -96,15 +95,7 @@ public List<NoticeReceiver> getNoticeReceivers(String name) {
}
return predicate;
};
List<NoticeReceiver> noticeReceivers = noticeReceiverDao.findAll(specification);
if (CollectionUtils.isNotEmpty(noticeReceivers)) {
noticeReceivers.forEach(noticeReceiver -> {
noticeReceiver.setPhone(DesensitizedUtil.mobilePhone(noticeReceiver.getPhone()));
noticeReceiver.setEmail(DesensitizedUtil.email(noticeReceiver.getEmail()));
noticeReceiver.setAppSecret(DesensitizedUtil.password(noticeReceiver.getAppSecret()));
});
}
return noticeReceivers;
return noticeReceiverDao.findAll(specification);
}

@Override
Expand Down

0 comments on commit efaa041

Please sign in to comment.