优化准则:通过用户名口令、数据证书等其他手段对用户身份进行验证。

wuhaoyang
wuhaoyang 8 months ago
parent 24ccac44f7
commit 32dba7da4a
  1. 90
      sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/ConfigurationFileChecker.java

@ -29,16 +29,15 @@ import java.util.Scanner;
/** /**
* TODO ConfigurationFileChecker * 通过用户名口令数据证书等其他手段对用户身份进行验证
* *
* @author WuHaoYang * @author WuHaoYang
* @date 2024/1/22 * @date 2024/1/22
*/ */
@Rule(key = "ConfigurationFileChecker") @Rule(key = "ConfigurationFileChecker")
public class ConfigurationFileChecker implements ConfigCheck { public class ConfigurationFileChecker implements ConfigCheck{
@Override
public void execute(SensorContext context, InputFile inputFile, RuleKey ruleKey){ public void execute(SensorContext context, InputFile inputFile, RuleKey ruleKey){
//文件名称 //文件名称
String filename = inputFile.filename(); String filename = inputFile.filename();
@ -76,19 +75,14 @@ public class ConfigurationFileChecker implements ConfigCheck {
if (filename.endsWith(".ini")){ if (filename.endsWith(".ini")){
// 获取当前输入文件的绝对路径 // 获取当前输入文件的绝对路径
File file1 = inputFile.file(); File file1 = inputFile.file();
File absoluteFile = file1.getAbsoluteFile();
// 构建目录路径 // 构建目录路径
File folder = new File(String.valueOf(absoluteFile)).getParentFile(); System.out.println("---------------ini文件路径----------------"+file1);
System.out.println("---------------ini文件路径----------------"+folder);
File[] listOfFiles = folder.listFiles();
int lineNum = 1; int lineNum = 1;
for (File file : listOfFiles) {
if (file.isFile() && file.getName().endsWith(".ini")) {
Properties properties = new Properties(); Properties properties = new Properties();
try (FileInputStream fileInput = new FileInputStream(file)) { try (FileInputStream fileInput = new FileInputStream(file1)) {
properties.load(fileInput); properties.load(fileInput);
String password = properties.getProperty("password"); String password = properties.getProperty("password");
System.out.println("password=" + password); System.out.println("password=" + password);
@ -102,33 +96,25 @@ public class ConfigurationFileChecker implements ConfigCheck {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
lineNum++; lineNum++;
} }
}
if (filename.endsWith(".conf")){ if (filename.endsWith(".conf")){
// 获取当前输入文件的绝对路径 // 获取当前输入文件的绝对路径
File file1 = inputFile.file(); File file1 = inputFile.file();
File absoluteFile = file1.getAbsoluteFile();
// 构建目录路径 // 构建目录路径
File folder = new File(String.valueOf(absoluteFile)).getParentFile(); System.out.println("---------------conf文件路径----------------"+file1);
System.out.println("---------------conf文件路径----------------"+folder);
File[] listOfFiles = folder.listFiles((dir, name) -> name.endsWith(".conf"));
int lineNum = 1; int lineNum = 1;
for (File file : listOfFiles) {
if (file.isFile()) {
Properties prop = new Properties(); Properties prop = new Properties();
InputStream input = null; InputStream input = null;
try { try {
input = new FileInputStream(file); input = new FileInputStream(file1);
prop.load(input); prop.load(input);
if (prop.containsKey("password")) { if (prop.containsKey("password")) {
@ -141,7 +127,6 @@ public class ConfigurationFileChecker implements ConfigCheck {
.at(inputFile.selectLine(lineNum))) .at(inputFile.selectLine(lineNum)))
.save(); .save();
} }
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} finally { } finally {
@ -153,10 +138,8 @@ public class ConfigurationFileChecker implements ConfigCheck {
} }
} }
} }
}
lineNum++; lineNum++;
} }
}
@ -167,23 +150,17 @@ public class ConfigurationFileChecker implements ConfigCheck {
// 构建目录路径 // 构建目录路径
File dir = new File(String.valueOf(absoluteFile)).getParentFile(); File dir = new File(String.valueOf(absoluteFile)).getParentFile();
System.out.println("---------------xml文件路径----------------"+dir); System.out.println("---------------xml文件路径----------------"+file1);
FilenameFilter filter = new FilenameFilter() { File xmlFile = new File(dir, filename);
public boolean accept(File dir, String name) {
return name.endsWith(".xml");
}
};
String[] children = dir.list(filter);
if (children == null) {
System.out.println("目录不存在或不是目录");
} else {
for (int i = 0; i < children.length; i++) {
String filename1 = children[i];
File xmlFile = new File(dir, filename1);
processXML(xmlFile); processXML(xmlFile);
} int lineNum = 1;
} NewIssue newIssue = context.newIssue();
newIssue
.forRule(ruleKey)
.at(newIssue.newLocation()
.on(inputFile)
.at(inputFile.selectLine(lineNum)))
.save();
} }
@ -195,18 +172,19 @@ public class ConfigurationFileChecker implements ConfigCheck {
// 构建目录路径 // 构建目录路径
File folder = new File(String.valueOf(absoluteFile)).getParentFile(); File folder = new File(String.valueOf(absoluteFile)).getParentFile();
System.out.println("---------------json文件路径----------------"+folder);
File[] listOfFiles = folder.listFiles();
if (listOfFiles != null) { System.out.println("---------------json文件路径----------------"+file1);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
for (File file : listOfFiles) { JsonNode rootNode = mapper.readTree(file1);
if (file.isFile() && file.getName().endsWith(".json")) {
JsonNode rootNode = mapper.readTree(file);
extractPassword(rootNode); extractPassword(rootNode);
} int lineNum = 1;
} NewIssue newIssue = context.newIssue();
} newIssue
.forRule(ruleKey)
.at(newIssue.newLocation()
.on(inputFile)
.at(inputFile.selectLine(lineNum)))
.save();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -221,24 +199,28 @@ public class ConfigurationFileChecker implements ConfigCheck {
// 构建目录路径 // 构建目录路径
File dir = new File(String.valueOf(absoluteFile)).getParentFile(); File dir = new File(String.valueOf(absoluteFile)).getParentFile();
System.out.println("---------------yml文件路径----------------"+dir); System.out.println("---------------yml文件路径----------------"+file1);
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
for (File file : dir.listFiles()) { try (FileInputStream fis = new FileInputStream(file1)) {
if (file.isFile() && file.getName().endsWith(".yml")) {
try (FileInputStream fis = new FileInputStream(file)) {
Map<String, Object> obj = yaml.load(fis); Map<String, Object> obj = yaml.load(fis);
if (obj != null){ if (obj != null){
String password = searchPassword(obj); String password = searchPassword(obj);
if (password != null) { if (password != null) {
System.out.println("password="+password); System.out.println("password="+password);
int lineNum = 1;
NewIssue newIssue = context.newIssue();
newIssue
.forRule(ruleKey)
.at(newIssue.newLocation()
.on(inputFile)
.at(inputFile.selectLine(lineNum)))
.save();
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
}
} }
public static void processXML(File xmlFile) { public static void processXML(File xmlFile) {

Loading…
Cancel
Save