use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_security_owaspTop10_return_vulnerabilities_and_hotspots_only.
@Test
public void search_by_security_owaspTop10_return_vulnerabilities_and_hotspots_only() {
RuleDefinitionDto rule1 = createRule(setSecurityStandards(of("owaspTop10:a1", "owaspTop10:a10", "cwe:543")), r -> r.setType(VULNERABILITY));
RuleDefinitionDto rule2 = createRule(setSecurityStandards(of("owaspTop10:a10", "cwe:543")), r -> r.setType(SECURITY_HOTSPOT));
createRule(setSecurityStandards(of("cwe:543")), r -> r.setType(CODE_SMELL));
index();
RuleQuery query = new RuleQuery().setOwaspTop10(of("a5", "a10"));
SearchIdResult<String> results = underTest.search(query, new SearchOptions().addFacets("owaspTop10"));
assertThat(results.getUuids()).containsOnly(rule1.getUuid(), rule2.getUuid());
}
use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class RegisterRules method start.
@Override
public void start() {
Profiler profiler = Profiler.create(LOG).startInfo("Register rules");
try (DbSession dbSession = dbClient.openSession(true)) {
RulesDefinition.Context ruleDefinitionContext = defLoader.load();
List<RulesDefinition.Repository> repositories = ruleDefinitionContext.repositories();
RegisterRulesContext registerRulesContext = createRegisterRulesContext(dbSession);
verifyRuleKeyConsistency(repositories, registerRulesContext);
for (RulesDefinition.ExtendedRepository repoDef : repositories) {
if (languages.get(repoDef.language()) != null) {
registerRules(registerRulesContext, repoDef.rules(), dbSession);
dbSession.commit();
}
}
processRemainingDbRules(registerRulesContext, dbSession);
List<ActiveRuleChange> changes = removeActiveRulesOnStillExistingRepositories(dbSession, registerRulesContext, repositories);
dbSession.commit();
persistRepositories(dbSession, ruleDefinitionContext.repositories());
// FIXME lack of resiliency, active rules index is corrupted if rule index fails
// to be updated. Only a single DB commit should be executed.
ruleIndexer.commitAndIndex(dbSession, registerRulesContext.getAllModified().map(RuleDefinitionDto::getUuid).collect(toSet()));
activeRuleIndexer.commitAndIndex(dbSession, changes);
registerRulesContext.getRenamed().forEach(e -> LOG.info("Rule {} re-keyed to {}", e.getValue(), e.getKey().getKey()));
profiler.stopDebug();
if (!registerRulesContext.hasDbRules()) {
Stream.concat(ruleIndexer.getIndexTypes().stream(), activeRuleIndexer.getIndexTypes().stream()).forEach(t -> metadataIndex.setInitialized(t, true));
}
webServerRuleFinder.startCaching();
}
}
use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class RegisterRules method registerRules.
private void registerRules(RegisterRulesContext context, List<RulesDefinition.Rule> ruleDefs, DbSession session) {
Map<RulesDefinition.Rule, RuleDefinitionDto> dtos = new LinkedHashMap<>(ruleDefs.size());
for (RulesDefinition.Rule ruleDef : ruleDefs) {
RuleKey ruleKey = RuleKey.of(ruleDef.repository().key(), ruleDef.key());
RuleDefinitionDto ruleDefinitionDto = context.getDbRuleFor(ruleDef).orElseGet(() -> {
RuleDefinitionDto newRule = createRuleDto(ruleDef, session);
context.created(newRule);
return newRule;
});
dtos.put(ruleDef, ruleDefinitionDto);
// we must detect renaming __before__ we modify the DTO
if (!ruleDefinitionDto.getKey().equals(ruleKey)) {
context.renamed(ruleDefinitionDto);
ruleDefinitionDto.setRuleKey(ruleKey);
}
if (mergeRule(ruleDef, ruleDefinitionDto)) {
context.updated(ruleDefinitionDto);
}
if (mergeDebtDefinitions(ruleDef, ruleDefinitionDto)) {
context.updated(ruleDefinitionDto);
}
if (mergeTags(ruleDef, ruleDefinitionDto)) {
context.updated(ruleDefinitionDto);
}
if (mergeSecurityStandards(ruleDef, ruleDefinitionDto)) {
context.updated(ruleDefinitionDto);
}
if (context.isUpdated(ruleDefinitionDto) || context.isRenamed(ruleDefinitionDto)) {
update(session, ruleDefinitionDto);
} else if (!context.isCreated(ruleDefinitionDto)) {
context.unchanged(ruleDefinitionDto);
}
}
for (Map.Entry<RulesDefinition.Rule, RuleDefinitionDto> e : dtos.entrySet()) {
mergeParams(context, e.getKey(), e.getValue(), session);
updateDeprecatedKeys(context, e.getKey(), e.getValue(), session);
}
}
use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class RegisterRules method processRemainingDbRules.
private void processRemainingDbRules(RegisterRulesContext recorder, DbSession dbSession) {
// custom rules check status of template, so they must be processed at the end
List<RuleDefinitionDto> customRules = new ArrayList<>();
recorder.getRemaining().forEach(rule -> {
if (rule.isCustomRule()) {
customRules.add(rule);
} else if (!rule.isAdHoc() && rule.getStatus() != RuleStatus.REMOVED) {
removeRule(dbSession, recorder, rule);
}
});
for (RuleDefinitionDto customRule : customRules) {
String templateUuid = customRule.getTemplateUuid();
checkNotNull(templateUuid, "Template uuid of the custom rule '%s' is null", customRule);
Optional<RuleDefinitionDto> template = dbClient.ruleDao().selectDefinitionByUuid(templateUuid, dbSession);
if (template.isPresent() && template.get().getStatus() != RuleStatus.REMOVED) {
if (updateCustomRuleFromTemplateRule(customRule, template.get())) {
recorder.updated(customRule);
update(dbSession, customRule);
}
} else {
removeRule(dbSession, recorder, customRule);
}
}
dbSession.commit();
}
use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class RegisterRulesTest method insert_then_remove_rule.
@Test
public void insert_then_remove_rule() {
String ruleKey = randomAlphanumeric(5);
// register one rule
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
repo.createRule(ruleKey).setName(randomAlphanumeric(5)).setHtmlDescription(randomAlphanumeric(20));
repo.done();
});
// verify db
List<RuleDefinitionDto> rules = dbClient.ruleDao().selectAllDefinitions(db.getSession());
assertThat(rules).extracting(RuleDefinitionDto::getKey).extracting(RuleKey::rule).containsExactly(ruleKey);
RuleDefinitionDto rule = rules.iterator().next();
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).containsExactly(rule.getUuid());
verifyIndicesMarkedAsInitialized();
// register no rule
execute(context -> context.createRepository("fake", "java").done());
// verify db
assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).extracting(RuleDefinitionDto::getKey).extracting(RuleKey::rule).containsExactly(ruleKey);
assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).extracting(RuleDefinitionDto::getStatus).containsExactly(REMOVED);
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).isEmpty();
verifyIndicesNotMarkedAsInitialized();
}
Aggregations