summaryrefslogtreecommitdiff
path: root/lib/gitlab/config/entry/validators.rb
blob: 374f929878e2fddd2929985d17b91c8da5368200 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# frozen_string_literal: true

module Gitlab
  module Config
    module Entry
      module Validators
        class AllowedKeysValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            unknown_keys = value.try(:keys).to_a - options[:in]

            if unknown_keys.any?
              record.errors.add(attribute, "contains unknown keys: " +
                                            unknown_keys.join(', '))
            end
          end
        end

        class DisallowedKeysValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            present_keys = value.try(:keys).to_a & options[:in]

            if present_keys.any?
              message = options[:message] || "contains disallowed keys"
              message += ": #{present_keys.join(', ')}"

              record.errors.add(attribute, message)
            end
          end
        end

        class RequiredKeysValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            present_keys = options[:in] - value.try(:keys).to_a

            if present_keys.any?
              record.errors.add(attribute, "missing required keys: " +
                present_keys.join(', '))
            end
          end
        end

        class AllowedValuesValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            unless options[:in].include?(value.to_s)
              record.errors.add(attribute, "unknown value: #{value}")
            end
          end
        end

        class AllowedArrayValuesValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            unknown_values = value - options[:in]
            unless unknown_values.empty?
              record.errors.add(attribute, "contains unknown values: " +
                                            unknown_values.join(', '))
            end
          end
        end

        class ArrayOfStringsValidator < ActiveModel::EachValidator
          include LegacyValidationHelpers

          def validate_each(record, attribute, value)
            unless validate_array_of_strings(value)
              record.errors.add(attribute, 'should be an array of strings')
            end
          end
        end

        class ArrayOfHashesValidator < ActiveModel::EachValidator
          include LegacyValidationHelpers

          def validate_each(record, attribute, value)
            unless value.is_a?(Array) && value.map { |hsh| hsh.is_a?(Hash) }.all?
              record.errors.add(attribute, 'should be an array of hashes')
            end
          end
        end

        class ArrayOrStringValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            unless value.is_a?(Array) || value.is_a?(String)
              record.errors.add(attribute, 'should be an array or a string')
            end
          end
        end

        class BooleanValidator < ActiveModel::EachValidator
          include LegacyValidationHelpers

          def validate_each(record, attribute, value)
            unless validate_boolean(value)
              record.errors.add(attribute, 'should be a boolean value')
            end
          end
        end

        class DurationValidator < ActiveModel::EachValidator
          include LegacyValidationHelpers

          def validate_each(record, attribute, value)
            unless validate_duration(value)
              record.errors.add(attribute, 'should be a duration')
            end

            if options[:limit]
              unless validate_duration_limit(value, options[:limit])
                record.errors.add(attribute, 'should not exceed the limit')
              end
            end
          end
        end

        class HashOrStringValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            unless value.is_a?(Hash) || value.is_a?(String)
              record.errors.add(attribute, 'should be a hash or a string')
            end
          end
        end

        class HashOrIntegerValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            unless value.is_a?(Hash) || value.is_a?(Integer)
              record.errors.add(attribute, 'should be a hash or an integer')
            end
          end
        end

        class KeyValidator < ActiveModel::EachValidator
          include LegacyValidationHelpers

          def validate_each(record, attribute, value)
            if validate_string(value)
              validate_path(record, attribute, value)
            else
              record.errors.add(attribute, 'should be a string or symbol')
            end
          end

          private

          def validate_path(record, attribute, value)
            path = CGI.unescape(value.to_s)

            if path.include?('/')
              record.errors.add(attribute, 'cannot contain the "/" character')
            elsif path == '.' || path == '..'
              record.errors.add(attribute, 'cannot be "." or ".."')
            end
          end
        end

        class RegexpValidator < ActiveModel::EachValidator
          include LegacyValidationHelpers

          def validate_each(record, attribute, value)
            unless validate_regexp(value)
              record.errors.add(attribute, 'must be a regular expression')
            end
          end

          protected

          def fallback
            false
          end

          private

          def matches_syntax?(value)
            Gitlab::UntrustedRegexp::RubySyntax.matches_syntax?(value)
          end

          def validate_regexp(value)
            matches_syntax?(value) &&
              Gitlab::UntrustedRegexp::RubySyntax.valid?(value, fallback: fallback)
          end
        end

        class ArrayOfStringsOrRegexpsValidator < RegexpValidator
          def validate_each(record, attribute, value)
            unless validate_array_of_strings_or_regexps(value)
              record.errors.add(attribute, 'should be an array of strings or regexps')
            end
          end

          private

          def validate_array_of_strings_or_regexps(values)
            values.is_a?(Array) && values.all?(&method(:validate_string_or_regexp))
          end

          def validate_string_or_regexp(value)
            return false unless value.is_a?(String)
            return validate_regexp(value) if matches_syntax?(value)

            true
          end
        end

        class ArrayOfStringsOrRegexpsWithFallbackValidator < ArrayOfStringsOrRegexpsValidator
          protected

          def fallback
            true
          end
        end

        class ArrayOfStringsOrStringValidator < RegexpValidator
          def validate_each(record, attribute, value)
            unless validate_array_of_strings_or_string(value)
              record.errors.add(attribute, 'should be an array of strings or a string')
            end
          end

          private

          def validate_array_of_strings_or_string(values)
            validate_array_of_strings(values) || validate_string(values)
          end
        end

        class TypeValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            type = options[:with]
            raise unless type.is_a?(Class)

            unless value.is_a?(type)
              message = options[:message] || "should be a #{type.name}"
              record.errors.add(attribute, message)
            end
          end
        end

        class VariablesValidator < ActiveModel::EachValidator
          include LegacyValidationHelpers

          def validate_each(record, attribute, value)
            unless validate_variables(value)
              record.errors.add(attribute, 'should be a hash of key value pairs')
            end
          end
        end

        class ExpressionValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            unless value.is_a?(String) && ::Gitlab::Ci::Pipeline::Expression::Statement.new(value).valid?
              record.errors.add(attribute, 'Invalid expression syntax')
            end
          end
        end

        class PortNamePresentAndUniqueValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            return unless value.is_a?(Array)

            ports_size = value.count
            return if ports_size <= 1

            named_ports = value.select { |e| e.is_a?(Hash) }.map { |e| e[:name] }.compact.map(&:downcase)

            if ports_size != named_ports.size
              record.errors.add(attribute, 'when there is more than one port, a unique name should be added')
            end

            if ports_size != named_ports.uniq.size
              record.errors.add(attribute, 'each port name must be different')
            end
          end
        end

        class PortUniqueValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            value = ports(value)
            return unless value.is_a?(Array)

            ports_size = value.count
            return if ports_size <= 1

            if transform_ports(value).size != ports_size
              record.errors.add(attribute, 'each port number can only be referenced once')
            end
          end

          private

          def ports(current_data)
            current_data
          end

          def transform_ports(raw_ports)
            raw_ports.map do |port|
              case port
              when Integer
                port
              when Hash
                port[:number]
              end
            end.uniq
          end
        end

        class JobPortUniqueValidator < PortUniqueValidator
          private

          def ports(current_data)
            return unless current_data.is_a?(Hash)

            (image_ports(current_data) + services_ports(current_data)).compact
          end

          def image_ports(current_data)
            return [] unless current_data[:image].is_a?(Hash)

            current_data.dig(:image, :ports).to_a
          end

          def services_ports(current_data)
            current_data.dig(:services).to_a.flat_map { |service| service.is_a?(Hash) ? service[:ports] : nil }
          end
        end

        class ServicesWithPortsAliasUniqueValidator < ActiveModel::EachValidator
          def validate_each(record, attribute, value)
            current_aliases = aliases(value)
            return if current_aliases.empty?

            unless aliases_unique?(current_aliases)
              record.errors.add(:config, 'alias must be unique in services with ports')
            end
          end

          private

          def aliases(value)
            value.select { |s| s.is_a?(Hash) && s[:ports] }.pluck(:alias) # rubocop:disable CodeReuse/ActiveRecord
          end

          def aliases_unique?(aliases)
            aliases.size == aliases.uniq.size
          end
        end
      end
    end
  end
end