Search in sources :

Example 56 with RuleDefinitionDto

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());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 57 with RuleDefinitionDto

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Profiler(org.sonar.api.utils.log.Profiler) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto)

Example 58 with RuleDefinitionDto

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);
    }
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleKey(org.sonar.api.rule.RuleKey) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 59 with RuleDefinitionDto

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();
}
Also used : ArrayList(java.util.ArrayList) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto)

Example 60 with RuleDefinitionDto

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();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Aggregations

RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)654 Test (org.junit.Test)583 ComponentDto (org.sonar.db.component.ComponentDto)305 IssueDto (org.sonar.db.issue.IssueDto)219 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)144 UserDto (org.sonar.db.user.UserDto)96 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)93 DbTester (org.sonar.db.DbTester)93 System2 (org.sonar.api.utils.System2)91 UserSessionRule (org.sonar.server.tester.UserSessionRule)84 List (java.util.List)80 Rule (org.junit.Rule)77 DbClient (org.sonar.db.DbClient)68 RuleParamDto (org.sonar.db.rule.RuleParamDto)67 Mockito.mock (org.mockito.Mockito.mock)65 TestRequest (org.sonar.server.ws.TestRequest)64 Consumer (java.util.function.Consumer)63 EsTester (org.sonar.server.es.EsTester)61 Random (java.util.Random)60 RuleType (org.sonar.api.rules.RuleType)60