File: parse_dotenv_artifact_service.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (82 lines) | stat: -rw-r--r-- 2,209 bytes parent folder | download
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
# frozen_string_literal: true

module Ci
  class ParseDotenvArtifactService < ::BaseService
    include ::Gitlab::Utils::StrongMemoize
    include ::Gitlab::EncodingHelper

    SizeLimitError = Class.new(StandardError)
    ParserError = Class.new(StandardError)

    def execute(artifact)
      validate!(artifact)

      variables = parse!(artifact)
      Ci::JobVariable.bulk_insert!(variables)

      success
    rescue SizeLimitError, ParserError, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique => error
      Gitlab::ErrorTracking.track_exception(error, job_id: artifact.job_id)
      error(error.message, :bad_request)
    end

    private

    def validate!(artifact)
      unless artifact&.dotenv?
        raise ArgumentError, 'Artifact is not dotenv file type'
      end

      unless artifact.file.size < dotenv_size_limit
        raise SizeLimitError,
          "Dotenv Artifact Too Big. Maximum Allowable Size: #{dotenv_size_limit}"
      end
    end

    def parse!(artifact)
      variables = {}

      artifact.each_blob do |blob|
        # Windows powershell may output UTF-16LE files, so convert the whole file
        # to UTF-8 before proceeding.
        blob = strip_bom(encode_utf8_with_replacement_character(blob))

        blob.each_line do |line|
          key, value = scan_line!(line)

          variables[key] = Ci::JobVariable.new(
            job_id: artifact.job_id,
            source: :dotenv,
            key: key,
            value: value,
            raw: false,
            project_id: artifact.project_id
          )
        end
      end

      if variables.size > dotenv_variable_limit
        raise SizeLimitError,
          "Dotenv files cannot have more than #{dotenv_variable_limit} variables"
      end

      variables.values
    end

    def scan_line!(line)
      result = line.scan(/^(.*?)=(.*)$/).last

      raise ParserError, 'Invalid Format' if result.nil?

      result.each(&:strip!)
    end

    def dotenv_variable_limit
      strong_memoize(:dotenv_variable_limit) { project.actual_limits.dotenv_variables }
    end

    def dotenv_size_limit
      strong_memoize(:dotenv_size_limit) { project.actual_limits.dotenv_size }
    end
  end
end