summaryrefslogtreecommitdiff
path: root/app/services/security/ci_configuration/sast_parser_service.rb
blob: cae9a90f0a0c0fa2c8f24158b0be2d05462df160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

module Security
  module CiConfiguration
    # This class parses SAST template file and .gitlab-ci.yml to populate default and current values into the JSON
    # read from app/validators/json_schemas/security_ci_configuration_schemas/sast_ui_schema.json
    class SastParserService < ::BaseService
      include Gitlab::Utils::StrongMemoize

      SAST_UI_SCHEMA_PATH = 'app/validators/json_schemas/security_ci_configuration_schemas/sast_ui_schema.json'

      def initialize(project)
        @project = project
      end

      def configuration
        result = Gitlab::Json.parse(File.read(Rails.root.join(SAST_UI_SCHEMA_PATH))).with_indifferent_access
        populate_default_value_for(result, :global)
        populate_default_value_for(result, :pipeline)
        fill_current_value_with_default_for(result, :global)
        fill_current_value_with_default_for(result, :pipeline)
        populate_current_value_for(result, :global)
        populate_current_value_for(result, :pipeline)

        fill_current_value_with_default_for_analyzers(result)
        populate_current_value_for_analyzers(result)

        result
      end

      private

      def sast_template_content
        Gitlab::Template::GitlabCiYmlTemplate.find('SAST').content
      end

      def populate_default_value_for(config, level)
        set_each(config[level], key: :default_value, with: sast_template_attributes)
      end

      def populate_current_value_for(config, level)
        set_each(config[level], key: :value, with: gitlab_ci_yml_attributes)
      end

      def fill_current_value_with_default_for(config, level)
        set_each(config[level], key: :value, with: sast_template_attributes)
      end

      def set_each(config_attributes, key:, with:)
        config_attributes.each do |entity|
          entity[key] = with[entity[:field]] if with[entity[:field]]
        end
      end

      def fill_current_value_with_default_for_analyzers(result)
        result[:analyzers].each do |analyzer|
          analyzer[:variables].each do |entity|
            entity[:value] = entity[:default_value] if entity[:default_value]
          end
        end
      end

      def populate_current_value_for_analyzers(result)
        result[:analyzers].each do |analyzer|
          analyzer[:enabled] = analyzer_enabled?(analyzer[:name])
          populate_current_value_for(analyzer, :variables)
        end
      end

      def analyzer_enabled?(analyzer_name)
        # Unless explicitly listed in the excluded analyzers, consider it enabled
        sast_excluded_analyzers.exclude?(analyzer_name)
      end

      def sast_excluded_analyzers
        strong_memoize(:sast_excluded_analyzers) do
          excluded_analyzers = gitlab_ci_yml_attributes["SAST_EXCLUDED_ANALYZERS"] || sast_template_attributes["SAST_EXCLUDED_ANALYZERS"]
          excluded_analyzers.split(',').map(&:strip) rescue []
        end
      end

      def sast_template_attributes
        @sast_template_attributes ||= build_sast_attributes(sast_template_content)
      end

      def gitlab_ci_yml_attributes
        @gitlab_ci_yml_attributes ||= begin
          config_content = @project.repository.blob_data_at(@project.repository.root_ref_sha, ci_config_file)
          return {} unless config_content

          build_sast_attributes(config_content)
        end
      end

      def ci_config_file
        '.gitlab-ci.yml'
      end

      def build_sast_attributes(content)
        options = { project: @project, user: current_user, sha: @project.repository.commit.sha }
        yaml_result = Gitlab::Ci::YamlProcessor.new(content, options).execute
        return {} unless yaml_result.valid?

        extract_required_attributes(yaml_result)
      end

      def extract_required_attributes(yaml_result)
        result = {}

        yaml_result.yaml_variables_for(:sast).each do |variable|
          result[variable[:key]] = variable[:value]
        end

        result[:stage] = yaml_result.stage_for(:sast)
        result.with_indifferent_access
      end
    end
  end
end