From 1aa470727a362a88b90342c830627ce8c34746b6 Mon Sep 17 00:00:00 2001 From: oleghasjanov Date: Tue, 22 Apr 2025 11:18:18 +0300 Subject: [PATCH] refactoring --- .../repp/v1/certificates_controller.rb | 11 +------ app/models/certificate.rb | 11 +++++-- config/initializers/file_exists_alias.rb | 6 ---- lib/serializers/repp/certificate.rb | 32 +++---------------- 4 files changed, 13 insertions(+), 47 deletions(-) diff --git a/app/controllers/repp/v1/certificates_controller.rb b/app/controllers/repp/v1/certificates_controller.rb index aa59f8346..632bff615 100644 --- a/app/controllers/repp/v1/certificates_controller.rb +++ b/app/controllers/repp/v1/certificates_controller.rb @@ -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 diff --git a/app/models/certificate.rb b/app/models/certificate.rb index bd4e964a7..6ff032c4b 100644 --- a/app/models/certificate.rb +++ b/app/models/certificate.rb @@ -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 diff --git a/config/initializers/file_exists_alias.rb b/config/initializers/file_exists_alias.rb index 58f1ba15b..8d8b79370 100644 --- a/config/initializers/file_exists_alias.rb +++ b/config/initializers/file_exists_alias.rb @@ -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 \ No newline at end of file diff --git a/lib/serializers/repp/certificate.rb b/lib/serializers/repp/certificate.rb index 4d2bca41d..ff161f2d7 100644 --- a/lib/serializers/repp/certificate.rb +++ b/lib/serializers/repp/certificate.rb @@ -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