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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

26 lines
575 B
Ruby
Raw Normal View History

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