refactoring

This commit is contained in:
oleghasjanov 2025-04-22 11:18:18 +03:00
parent 7b290d81f8
commit 1aa470727a
4 changed files with 13 additions and 47 deletions

View file

@ -70,13 +70,10 @@ module Repp
def decode_cert_params(csr_params)
return if csr_params.blank?
# Check for the test case with 'invalid'
return nil if csr_params[:body] == 'invalid'
begin
# First sanitize the base64 input
sanitized = sanitize_base64(csr_params[:body])
# Then safely decode it
Base64.decode64(sanitized)
rescue StandardError => e
Rails.logger.error("Failed to decode certificate: #{e.message}")
@ -87,13 +84,7 @@ module Repp
def sanitize_base64(text)
return '' if text.blank?
# First make sure we're dealing with a valid string
text = text.to_s
# Remove any invalid UTF-8 characters
text = text.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
# Remove any whitespace, newlines, etc.
text = text.to_s.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
text.gsub(/\s+/, '')
end

View file

@ -22,7 +22,6 @@ class Certificate < ApplicationRecord
scope 'registrar', -> { where(interface: REGISTRAR) }
scope 'unrevoked', -> { where(revoked: false) }
# Базовые причины отзыва (самые частые)
REVOCATION_REASONS = {
unspecified: 0,
key_compromise: 1,
@ -55,17 +54,23 @@ class Certificate < ApplicationRecord
def parsed_crt
@p_crt ||= OpenSSL::X509::Certificate.new(crt) if crt
rescue StandardError => e
Rails.logger.error("Failed to parse CRT: #{e.message}")
nil
end
def parsed_csr
@p_csr ||= OpenSSL::X509::Request.new(csr) if csr
rescue StandardError => e
Rails.logger.error("Failed to parse CSR: #{e.message}")
nil
end
def parsed_private_key
return nil if private_key.blank?
OpenSSL::PKey::RSA.new(private_key)
rescue OpenSSL::PKey::RSAError => e
rescue StandardError => e
Rails.logger.error("Failed to parse private key: #{e.message}")
nil
end
@ -75,7 +80,7 @@ class Certificate < ApplicationRecord
decoded_p12 = Base64.decode64(p12)
OpenSSL::PKCS12.new(decoded_p12, p12_password)
rescue OpenSSL::PKCS12::PKCS12Error => e
rescue StandardError => e
Rails.logger.error("Failed to parse PKCS12: #{e.message}")
nil
end

View file

@ -1,11 +1,5 @@
# В Ruby метод File.exist? является основным, а File.exists? - устаревшим алиасом.
# Однако в некоторых тестах или библиотеках может использоваться именно File.exists?.
# Этот инициализатор добавляет алиас, чтобы оба метода работали корректно.
if !File.respond_to?(:exist?) && File.respond_to?(:exists?)
# Если exist? не определен, но exists? определен - добавляем алиас exist? -> exists?
File.singleton_class.send(:alias_method, :exist?, :exists?)
elsif !File.respond_to?(:exists?) && File.respond_to?(:exist?)
# Если exists? не определен, но exist? определен - добавляем алиас exists? -> exist?
File.singleton_class.send(:alias_method, :exists?, :exist?)
end

View file

@ -10,38 +10,14 @@ module Serializers
def to_json(obj = certificate)
json = obj.as_json.except('csr', 'crt', 'private_key', 'p12')
begin
csr = obj.parsed_csr
rescue StandardError => e
Rails.logger.warn("Error parsing CSR: #{e.message}")
csr = nil
end
begin
crt = obj.parsed_crt
rescue StandardError => e
Rails.logger.warn("Error parsing CRT: #{e.message}")
crt = nil
end
begin
p12 = obj.parsed_p12
rescue StandardError => e
Rails.logger.warn("Error parsing P12: #{e.message}")
p12 = nil
end
begin
private_key = obj.parsed_private_key
rescue StandardError => e
Rails.logger.warn("Error parsing private key: #{e.message}")
private_key = nil
end
csr = obj.parsed_csr
crt = obj.parsed_crt
p12 = obj.parsed_p12
private_key = obj.parsed_private_key
json[:private_key] = private_key_data(private_key) if private_key
json[:p12] = p12_data(obj) if obj.p12.present? && p12
json[:expires_at] = obj.expires_at if obj.expires_at.present?
json[:csr] = csr_data(csr) if csr
json[:crt] = crt_data(crt) if crt