gitlab-org--gitlab-foss/app/validators/certificate_validator.rb

26 lines
575 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-02-09 12:06:55 -05:00
# UrlValidator
#
# Custom validator for private keys.
#
# class Project < ActiveRecord::Base
# validates :certificate_key, certificate: true
2016-02-09 12:06:55 -05:00
# end
#
class CertificateValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless valid_certificate_pem?(value)
2016-02-09 12:06:55 -05:00
record.errors.add(attribute, "must be a valid PEM certificate")
end
end
private
def valid_certificate_pem?(value)
2016-02-16 08:40:54 -05:00
OpenSSL::X509::Certificate.new(value).present?
2016-02-09 12:06:55 -05:00
rescue OpenSSL::X509::CertificateError
2016-02-16 08:40:54 -05:00
false
2016-02-09 12:06:55 -05:00
end
end