ssl_certificate.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SSLCertificate < ApplicationModel
  3. validate :valid_ssl_certificate
  4. before_validation :extract_metadata, on: :create
  5. def certificate_parsed
  6. @certificate_parsed ||= Certificate::X509::SSL.new(certificate)
  7. rescue OpenSSL::X509::CertificateError
  8. raise Exceptions::UnprocessableEntity, __('This is not a valid X509 certificate. Please check the certificate format.')
  9. end
  10. def filter_attributes(attributes)
  11. super.except! 'certificate'
  12. end
  13. private
  14. def extract_metadata
  15. cert = certificate_parsed
  16. self.fingerprint = cert.fingerprint
  17. self.subject = cert.extensions_as_hash.fetch('subjectAltName', [cert.subject]).join(',')
  18. self.not_before = cert.not_before
  19. self.not_after = cert.not_after
  20. self.ca = cert.ca?
  21. end
  22. def valid_ssl_certificate
  23. certificate_parsed.valid_ssl_certificate!
  24. rescue Exceptions::UnprocessableEntity => e
  25. errors.add(:base, e.message)
  26. end
  27. end