debian-mirror-gitlab/app/validators/certificate_key_validator.rb

29 lines
621 B
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
# CertificateKeyValidator
2017-08-17 22:00:37 +05:30
#
# 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)
return false unless value
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
pkey = OpenSSL::PKey.read(value)
2017-08-17 22:00:37 +05:30
pkey.private?
rescue OpenSSL::PKey::PKeyError
false
end
end