CloudformationQualityProfile.java

  1. /*
  2.  * Cloudformation Plugin for SonarQube
  3.  * Copyright (C) 2019 James Pether Sörling
  4.  * james@hack23.com
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 3 of the License, or (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public License
  17.  * along with this program; if not, write to the Free Software Foundation,
  18.  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  19.  */
  20. package com.hack23.sonar.cloudformation;

  21. import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
  22. import org.sonar.api.server.rule.RulesDefinition.Repository;
  23. import org.sonar.api.server.rule.RulesDefinition.Rule;

  24. /**
  25.  * The Class CloudformationQualityProfile.
  26.  */
  27. public final class CloudformationQualityProfile implements BuiltInQualityProfilesDefinition {


  28.     /** The Constant CLOUDFORMATION_RULES. */
  29.     private static final String CLOUDFORMATION_RULES = "Cloudformation(cfn-nag,checkov) Rules";

  30.     /** The Constant TERRAFORM_RULES. */
  31.     private static final String TERRAFORM_RULES = "Terraform(checkov) Rules";

  32.     /** The Constant SERVERLESS. */
  33.     private static final String SERVERLESS = "serverless";

  34.     /** The Constant CLOUDFORMATION. */
  35.     private static final String CLOUDFORMATION = "cloudformation";

  36.     /** The cloudformation rules definition. */
  37.     private final CloudformationRulesDefinition cloudformationRulesDefinition;

  38.     /**
  39.      * Instantiates a new cloudformation quality profile.
  40.      *
  41.      * @param cloudformationRulesDefinition the cloudformation rules definition
  42.      */
  43.     public CloudformationQualityProfile(final CloudformationRulesDefinition cloudformationRulesDefinition) {
  44.         super();
  45.         this.cloudformationRulesDefinition = cloudformationRulesDefinition;
  46.     }

  47.     /**
  48.      * Define.
  49.      *
  50.      * @param context the context
  51.      */
  52.     @Override
  53.     public void define(final Context context) {

  54.         createCloudformationQualityProfile(context, "cloudformation");
  55.         createTerraformQualityProfile(context, "terraform");
  56.     }


  57.     /**
  58.      * Creates the terraform quality profile.
  59.      *
  60.      * @param context the context
  61.      * @param language the language
  62.      */
  63.     private void createTerraformQualityProfile(final Context context, final String language) {
  64.         final NewBuiltInQualityProfile iacProfile = context.createBuiltInQualityProfile(TERRAFORM_RULES, language);
  65.         for (final Repository repository : cloudformationRulesDefinition.getContext().repositories()) {
  66.             if (repository.key().contains("cloudformation-plugin-terraform")) {
  67.                 for (final Rule rule : repository.rules()) {
  68.                     if (rule.tags().contains(CLOUDFORMATION) || rule.tags().contains(SERVERLESS) || rule.tags().contains("terraform")) {
  69.                         iacProfile.activateRule(repository.key(), rule.key());
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         iacProfile.done();
  75.     }

  76.     /**
  77.      * Creates the cloudformation quality profile.
  78.      *
  79.      * @param context the context
  80.      * @param language the language
  81.      */
  82.     private void createCloudformationQualityProfile(final Context context, final String language) {
  83.         final NewBuiltInQualityProfile cloudFormationprofile = context
  84.                 .createBuiltInQualityProfile(CLOUDFORMATION_RULES, language);
  85.         for (final Repository repository : cloudformationRulesDefinition.getContext().repositories()) {
  86.             if (repository.key().contains("cloudformation-plugin-cfn")) {
  87.                 for (final Rule rule : repository.rules()) {
  88.                     if (rule.tags().contains(CLOUDFORMATION) || rule.tags().contains(SERVERLESS) || rule.tags().contains("cfn-nag")) {
  89.                         cloudFormationprofile.activateRule(repository.key(), rule.key());
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.         cloudFormationprofile.done();
  95.     }
  96. }