Class: Katello::Validators::GpgKeyContentValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/lib/katello/validators/gpg_key_content_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate_line_length(record, attribute, value) ⇒ Object



34
35
36
37
38
# File 'app/lib/katello/validators/gpg_key_content_validator.rb', line 34

def self.validate_line_length(record, attribute, value)
  value.each_line do |line|
    record.errors[attribute] << _("must contain valid  Public GPG Key") if line.length > GpgKey::MAX_CONTENT_LINE_LENGTH
  end
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



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
# File 'app/lib/katello/validators/gpg_key_content_validator.rb', line 6

def validate_each(record, attribute, value)
  if value
    GpgKeyContentValidator.validate_line_length(record, attribute, value)
    gpg_key_line_array = value.split("\n")
    gpg_key_line_array.delete("")

    if gpg_key_line_array.first.match(/-{5}BEGIN PGP PUBLIC KEY BLOCK-{5}/) && gpg_key_line_array.last.match(/-{5}END PGP PUBLIC KEY BLOCK-{5}/)

      gpg_key_line_array.shift
      gpg_key_line_array.pop

      if gpg_key_line_array.empty?
        record.errors[attribute] << _("must contain valid Public GPG Key")
      else
        unless gpg_key_line_array.drop(1).join.match(/[a-zA-Z0-9+\/=]*/)
          record.errors[attribute] << _("must contain valid Public GPG Key")
        end
      end

    else
      record.errors[attribute] << _("must contain valid Public GPG Key")
    end

  else
    record.errors[attribute] << _("must contain GPG Key")
  end
end