Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cyclonedx_schema_validator.rb « validators « sbom « parsers « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8d3ef1d6b5feedf3775bbaab63fff9aa8f93030 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Parsers
      module Sbom
        module Validators
          class CyclonedxSchemaValidator
            SUPPORTED_SPEC_VERSIONS = %w[1.4 1.5].freeze

            SCHEMA_BASE_PATH = Rails.root.join('app', 'validators', 'json_schemas', 'cyclonedx').freeze

            def initialize(report_data)
              @report_data = report_data
            end

            def valid?
              errors.empty?
            end

            def errors
              @errors ||= validate!
            end

            private

            def validate!
              if spec_version_valid?
                pretty_errors
              else
                [format("Unsupported CycloneDX spec version. Must be one of: %{versions}",
                  versions: SUPPORTED_SPEC_VERSIONS.join(', '))]
              end
            end

            def spec_version_valid?
              SUPPORTED_SPEC_VERSIONS.include?(spec_version)
            end

            def spec_version
              @report_data['specVersion']
            end

            def raw_errors
              JSONSchemer.schema(SCHEMA_BASE_PATH.join("bom-#{spec_version}.schema.json")).validate(@report_data)
            end

            def pretty_errors
              raw_errors.map { |error| JSONSchemer::Errors.pretty(error) }
            end
          end
        end
      end
    end
  end
end