Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desensitize sensitive information #2800

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,7 @@

import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY;
import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,91 @@
/*
* 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.util;

import org.apache.commons.lang3.StringUtils;

/**
* desensitize field utility
*/
public class DesensitizedUtil {

/**
* desensitize mobile phone
* @param str field value
* @return desensitized value
*/
public static String mobilePhone(String str) {
if (StringUtils.isEmpty(str)) {
return "";
}
return desensitized(str, 3, str.length() - 4);
}

/**
* desensitize email
* @param str field value
* @return desensitized value
*/
public static String email(String str) {
if (StringUtils.isEmpty(str)) {
return "";
}
int index = str.indexOf("@");
if (index <= 1) {
return str;
}
return desensitized(str, 1, index);
}

/**
* desensitize password
* @param str field value
* @return desensitized value
*/
public static String password(String str) {
if (StringUtils.isEmpty(str)) {
return "";
}
return desensitized(str, 0, str.length());
}

/**
* replace char to *
* @param str field value
* @param start start index
* @param end end index
* @return desensitized value
*/
private static String desensitized(String str, int start, int end) {
if (StringUtils.isEmpty(str)) {
return "";
}
if (start < 0 || end < 0 || end > str.length() || start >= end) {
return str;
}
final StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i ) {
if (i >= start && i < end) {
result.append("*");
} else {
result.append(str.charAt(i));
}
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 40,7 @@
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 @@ -95,7 96,15 @@ public List<NoticeReceiver> getNoticeReceivers(String name) {
}
return predicate;
};
return noticeReceiverDao.findAll(specification);
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;
}

@Override
Expand Down
Loading