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

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

29 lines
621 B
Ruby
Raw Normal View History

# frozen_string_literal: true
# CertificateKeyValidator
2016-02-09 17:06:55 +00:00
#
# Custom validator for private keys.
#
# class Project < ActiveRecord::Base
# validates :certificate_key, certificate_key: true
# end
#
class CertificateKeyValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless valid_private_key_pem?(value)
record.errors.add(attribute, "must be a valid PEM private key")
end
end
private
def valid_private_key_pem?(value)
2016-02-16 13:40:54 +00:00
return false unless value
pkey = OpenSSL::PKey.read(value)
2016-02-09 17:06:55 +00:00
pkey.private?
rescue OpenSSL::PKey::PKeyError
false
end
end