View Javadoc
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  
22  import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
23  import org.sonar.api.server.rule.RulesDefinition.Repository;
24  import org.sonar.api.server.rule.RulesDefinition.Rule;
25  
26  /**
27   * The Class CloudformationQualityProfile.
28   */
29  public final class CloudformationQualityProfile implements BuiltInQualityProfilesDefinition {
30  
31  
32  	/** The Constant CLOUDFORMATION_RULES. */
33  	private static final String CLOUDFORMATION_RULES = "Cloudformation(cfn-nag,checkov) Rules";
34  
35  	/** The Constant TERRAFORM_RULES. */
36  	private static final String TERRAFORM_RULES = "Terraform(checkov) Rules";
37  
38  	/** The Constant SERVERLESS. */
39  	private static final String SERVERLESS = "serverless";
40  
41  	/** The Constant CLOUDFORMATION. */
42  	private static final String CLOUDFORMATION = "cloudformation";
43  
44  	/** The cloudformation rules definition. */
45  	private final CloudformationRulesDefinition cloudformationRulesDefinition;
46  
47  	/**
48  	 * Instantiates a new cloudformation quality profile.
49  	 *
50  	 * @param cloudformationRulesDefinition the cloudformation rules definition
51  	 */
52  	public CloudformationQualityProfile(final CloudformationRulesDefinition cloudformationRulesDefinition) {
53  		super();
54  		this.cloudformationRulesDefinition = cloudformationRulesDefinition;
55  	}
56  
57  	/**
58  	 * Define.
59  	 *
60  	 * @param context the context
61  	 */
62  	@Override
63  	public void define(final Context context) {
64  
65  		createCloudformationQualityProfile(context, "cloudformation");
66  		createTerraformQualityProfile(context, "terraform");
67  	}
68  
69  
70  	/**
71  	 * Creates the terraform quality profile.
72  	 *
73  	 * @param context the context
74  	 * @param language the language
75  	 */
76  	private void createTerraformQualityProfile(final Context context, final String language) {
77  		final NewBuiltInQualityProfile iacProfile = context.createBuiltInQualityProfile(TERRAFORM_RULES, language);
78  		for (final Repository repository : cloudformationRulesDefinition.getContext().repositories()) {
79  			if (repository.key().contains("cloudformation-plugin-terraform")) {
80  				for (final Rule rule : repository.rules()) {
81  					if (rule.tags().contains(CLOUDFORMATION) || rule.tags().contains(SERVERLESS) || rule.tags().contains("terraform")) {
82  						iacProfile.activateRule(repository.key(), rule.key());
83  					}
84  				}
85  			}
86  		}
87  		iacProfile.done();
88  	}
89  
90  	/**
91  	 * Creates the cloudformation quality profile.
92  	 *
93  	 * @param context the context
94  	 * @param language the language
95  	 */
96  	private void createCloudformationQualityProfile(final Context context, final String language) {
97  		final NewBuiltInQualityProfile cloudFormationprofile = context
98  				.createBuiltInQualityProfile(CLOUDFORMATION_RULES, language);
99  		for (final Repository repository : cloudformationRulesDefinition.getContext().repositories()) {
100 			if (repository.key().contains("cloudformation-plugin-cfn")) {
101 				for (final Rule rule : repository.rules()) {
102 					if (rule.tags().contains(CLOUDFORMATION) || rule.tags().contains(SERVERLESS) || rule.tags().contains("cfn-nag")) {
103 						cloudFormationprofile.activateRule(repository.key(), rule.key());
104 					}
105 				}
106 			}
107 		}
108 		cloudFormationprofile.done();
109 	}
110 }