Compare commits

..

2 Commits

Author SHA1 Message Date
RenFengJiang cb5e6b2931 新增:日志文件检查规则 10 months ago
RenFengJiang 6fb1fd3963 新增:log文件识别 10 months ago
  1. 3
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxPlugin.java
  2. 4
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/CxxSquidSensor.java
  3. 37
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/LogLanguage.java
  4. 36
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/LogChecker.java
  5. 35
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/LogCheckerTest.java
  6. 2454
      sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/LogChecker.log

@ -98,6 +98,8 @@ public final class CxxPlugin implements Plugin {
static private List<Object> getSensorsImpl() { static private List<Object> getSensorsImpl() {
var l = new ArrayList<Object>(); var l = new ArrayList<Object>();
l.add(LogLanguage.class);
// utility classes // utility classes
l.add(CxxUnitTestResultsAggregator.class); l.add(CxxUnitTestResultsAggregator.class);
l.add(RulesDefinitionXmlLoader.class); l.add(RulesDefinitionXmlLoader.class);
@ -152,6 +154,7 @@ public final class CxxPlugin implements Plugin {
l.add(CxxClangTidyRuleRepository.class); l.add(CxxClangTidyRuleRepository.class);
l.add(CxxClangSARuleRepository.class); l.add(CxxClangSARuleRepository.class);
// post jobs // post jobs
l.add(FinalReport.class); l.add(FinalReport.class);

@ -286,7 +286,7 @@ public class CxxSquidSensor implements ProjectSensor {
public void describe(SensorDescriptor descriptor) { public void describe(SensorDescriptor descriptor) {
descriptor descriptor
.name("CXX") .name("CXX")
.onlyOnLanguage(CxxLanguage.KEY) .onlyOnLanguages(CxxLanguage.KEY, LogLanguage.KEY)
.onlyOnFileType(InputFile.Type.MAIN) .onlyOnFileType(InputFile.Type.MAIN)
.onlyWhenConfiguration(conf -> !conf.getBoolean(SQUID_DISABLED_KEY).orElse(false)); .onlyWhenConfiguration(conf -> !conf.getBoolean(SQUID_DISABLED_KEY).orElse(false));
} }
@ -373,7 +373,7 @@ public class CxxSquidSensor implements ProjectSensor {
private Iterable<InputFile> getInputFiles(SensorContext context, CxxSquidConfiguration squidConfig) { private Iterable<InputFile> getInputFiles(SensorContext context, CxxSquidConfiguration squidConfig) {
Iterable<InputFile> inputFiles = context.fileSystem().inputFiles( Iterable<InputFile> inputFiles = context.fileSystem().inputFiles(
context.fileSystem().predicates().and( context.fileSystem().predicates().and(
context.fileSystem().predicates().hasLanguage(CxxLanguage.KEY), context.fileSystem().predicates().hasLanguages(CxxLanguage.KEY, LogLanguage.KEY),
context.fileSystem().predicates().hasType(InputFile.Type.MAIN) context.fileSystem().predicates().hasType(InputFile.Type.MAIN)
) )
); );

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称信息安全性设计准则检查插件
* 项目描述用于检查源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx;
import org.sonar.api.resources.AbstractLanguage;
import java.lang.module.Configuration;
/**
* TODO LogLanguage
*
* @author RenFengJiang
* @date 2024/1/16
*/
public final class LogLanguage extends AbstractLanguage {
public static final String NAME = "LOG";
public static final String KEY = "log";
private final Configuration config;
public LogLanguage( Configuration config) {
super(KEY, NAME);
this.config = config;
}
@Override
public String[] getFileSuffixes() {
String[] arr = new String[1];
arr[0] = ".log";
return arr;
}
}

@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称信息安全性设计准则检查插件
* 项目描述用于检查源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules.checkers;
import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.Grammar;
import org.sonar.check.Rule;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import javax.annotation.Nullable;
/**
* TODO LogChecker
*
* @author RenFengJiang
* @date 2024/1/16
*/
@Rule(key = "LogChecker", name = "日志文件检查", description = "在产品发布之前移除产生日志文件的代码")
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class LogChecker extends SquidCheck<Grammar> {
@Override
public void visitFile(@Nullable AstNode astNode) {
super.visitFile(astNode);
if(getContext().getInputFile().filename().endsWith(".log")){
getContext().createFileViolation(this, "日志文件检查");
}
}
}

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称信息安全性设计准则检查插件
* 项目描述用于检查源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules.checkers;
import com.keyware.sonar.cxx.CxxFileTesterHelper;
import org.junit.jupiter.api.Test;
import org.sonar.cxx.CxxAstScanner;
import org.sonar.cxx.squidbridge.api.SourceFile;
import org.sonar.cxx.squidbridge.checks.CheckMessagesVerifier;
import java.io.IOException;
/**
* TODO LogCheckerTest
*
* @author RenFengJiang
* @date 2024/1/16
*/
public class LogCheckerTest {
@Test
public void checkTest() throws IOException {
var checker = new LogChecker();
var tester = CxxFileTesterHelper.create("LogChecker.log");
SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().withMessage("日志文件检查");
}
}
Loading…
Cancel
Save