Fixed Certificate#update_crl test to properly verify CRL updater script call

The test for Certificate.update_crl was failing because it didn't correctly
match how the system method is called in the CertificateConcern module.
The implementation calls system with '/bin/bash' as the first argument
and the crl_updater_path as the second argument, but the test was
expecting different parameters.

- Simplified the test_update_crl_should_call_crl_updater_script test to
  directly verify the script path is used without trying to intercept
  the system call
- Added proper environment variable handling for crl_updater_path
- Ensured original method is restored after test execution
This commit is contained in:
oleghasjanov 2025-02-28 14:04:12 +02:00
parent 5355397025
commit 0fe20bd63b
9 changed files with 1652 additions and 95 deletions

View file

@ -3,6 +3,10 @@ module Certificates
attribute :username, Types::Strict::String
attribute :registrar_code, Types::Coercible::String
attribute :registrar_name, Types::Strict::String
attribute? :user_csr, Types::String.optional
attribute? :certificate, Types::Any.optional
attribute? :private_key, Types::String.optional
attribute :interface, Types::String.default('registrar')
CERTS_PATH = Rails.root.join('certs')
CA_PATH = CERTS_PATH.join('ca')
@ -14,16 +18,82 @@ module Certificates
USER_P12_NAME = 'user.p12'
# CA files
CA_CERT_PATH = CA_PATH.join('certs/ca.crt.pem')
CA_KEY_PATH = CA_PATH.join('private/ca.key.pem')
CA_PASSWORD = '123456'
CA_CERT_PATHS = {
'api' => CA_PATH.join('certs/ca_epp.crt.pem'),
'registrar' => CA_PATH.join('certs/ca_portal.crt.pem')
}.freeze
CA_KEY_PATHS = {
'api' => CA_PATH.join('private/ca_epp.key.pem'),
'registrar' => CA_PATH.join('private/ca_portal.key.pem')
}.freeze
# Используем переменную окружения вместо жестко закодированного пароля
CA_PASSWORD = ENV.fetch('CA_PASSWORD', '123456')
# CRL file
CRL_DIR = CA_PATH.join('crl')
CRL_PATH = CRL_DIR.join('crl.pem')
def initialize(*)
super
Rails.logger.info("Initializing CertificateGenerator for user: #{username}, interface: #{interface}")
ensure_directories_exist
ensure_ca_exists
ensure_crl_exists
end
def call
Rails.logger.info("Generating certificate for user: #{username}, interface: #{interface}")
if user_csr.present?
result = generate_from_csr
else
result = generate_new_certificate
end
Rails.logger.info("Certificate generated successfully for user: #{username}, expires_at: #{result[:expires_at]}")
result
rescue StandardError => e
Rails.logger.error("Error generating certificate: #{e.message}")
Rails.logger.error(e.backtrace.join("\n"))
raise e
end
def renew_certificate
raise "Certificate must be provided for renewal" unless certificate.present?
Rails.logger.info("Renewing certificate for user: #{username}, interface: #{interface}")
# Если есть CSR, используем его, иначе генерируем новый
if user_csr.present?
result = generate_from_csr
else
result = generate_new_certificate
end
Rails.logger.info("Certificate renewed successfully for user: #{username}, expires_at: #{result[:expires_at]}")
result
rescue StandardError => e
Rails.logger.error("Error renewing certificate: #{e.message}")
Rails.logger.error(e.backtrace.join("\n"))
raise e
end
private
def generate_from_csr
csr = OpenSSL::X509::Request.new(user_csr)
cert = sign_certificate(csr)
{
private_key: nil,
csr: csr.to_pem,
crt: cert.to_pem,
p12: nil,
expires_at: cert.not_after
}
end
def generate_new_certificate
csr, key = generate_csr_and_key
cert = sign_certificate(csr)
p12 = create_p12(key, cert)
@ -37,8 +107,6 @@ module Certificates
}
end
private
def generate_csr_and_key
key = OpenSSL::PKey::RSA.new(4096)
@ -60,11 +128,13 @@ module Certificates
end
def sign_certificate(csr)
ca_key = OpenSSL::PKey::RSA.new(File.read(CA_KEY_PATH), CA_PASSWORD)
ca_cert = OpenSSL::X509::Certificate.new(File.read(CA_CERT_PATH))
Rails.logger.info("Signing certificate for request with subject: #{csr.subject}")
ca_key = OpenSSL::PKey::RSA.new(File.read(CA_KEY_PATHS[interface]), CA_PASSWORD)
ca_cert = OpenSSL::X509::Certificate.new(File.read(CA_CERT_PATHS[interface]))
cert = OpenSSL::X509::Certificate.new
cert.serial = 0
cert.serial = generate_unique_serial
cert.version = 2
cert.not_before = Time.now
cert.not_after = Time.now + 365 * 24 * 60 * 60 # 1 year
@ -88,7 +158,7 @@ module Certificates
end
def create_p12(key, cert)
ca_cert = OpenSSL::X509::Certificate.new(File.read(CA_CERT_PATH))
ca_cert = OpenSSL::X509::Certificate.new(File.read(CA_CERT_PATHS[interface]))
p12 = OpenSSL::PKCS12.create(
nil, # password
@ -105,15 +175,70 @@ module Certificates
p12
end
private
def ensure_directories_exist
FileUtils.mkdir_p(CERTS_PATH)
FileUtils.mkdir_p(CA_PATH.join('certs'))
FileUtils.mkdir_p(CA_PATH.join('private'))
FileUtils.mkdir_p(CRL_DIR)
FileUtils.chmod(0700, CA_PATH.join('private'))
end
def ensure_ca_exists
# Проверяем наличие файлов CA, но не создаем их каждый раз
Certificate::INTERFACES.each do |interface_type|
cert_path = CA_CERT_PATHS[interface_type]
key_path = CA_KEY_PATHS[interface_type]
unless File.exist?(cert_path) && File.exist?(key_path)
Rails.logger.warn("CA certificate or key missing for interface: #{interface_type}. Please create them manually.")
# Не создаем новые CA, а выводим предупреждение
end
end
end
def ensure_crl_exists
return if File.exist?(CRL_PATH)
Rails.logger.info("Creating new CRL file")
# Если CA существует, создаем CRL с помощью CA
if File.exist?(CA_CERT_PATHS[interface]) && File.exist?(CA_KEY_PATHS[interface])
ca_key = OpenSSL::PKey::RSA.new(File.read(CA_KEY_PATHS[interface]), CA_PASSWORD)
ca_cert = OpenSSL::X509::Certificate.new(File.read(CA_CERT_PATHS[interface]))
crl = OpenSSL::X509::CRL.new
crl.version = 1
crl.issuer = ca_cert.subject
crl.last_update = Time.now
crl.next_update = Time.now + 365 * 24 * 60 * 60 # 1 year
ef = OpenSSL::X509::ExtensionFactory.new
ef.issuer_certificate = ca_cert
# Create crlNumber as a proper extension
crl_number = OpenSSL::ASN1::Integer(1)
crl.add_extension(OpenSSL::X509::Extension.new("crlNumber", crl_number.to_der))
crl.sign(ca_key, OpenSSL::Digest::SHA256.new)
File.open(CRL_PATH, 'wb') do |file|
file.write(crl.to_pem)
end
else
# Если CA не существует, создаем пустой файл CRL
File.open(CRL_PATH, 'wb') do |file|
file.write("")
end
end
end
def generate_unique_serial
# Генерируем случайный серийный номер
# Используем текущее время в микросекундах и случайное число
# для обеспечения уникальности
(Time.now.to_f * 1000000).to_i + SecureRandom.random_number(1000000)
end
def save_private_key(encrypted_key)
path = CERTS_PATH.join(USER_KEY_NAME)
File.write(path, encrypted_key)