internetee-registry/app/controllers/repp/v1/certificates_controller.rb
oleghasjanov 0fe20bd63b 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
2025-04-16 11:47:52 +03:00

106 lines
3.9 KiB
Ruby

require 'serializers/repp/certificate'
module Repp
module V1
class CertificatesController < BaseController
before_action :find_certificate, only: %i[show download]
load_and_authorize_resource param_method: :cert_params
THROTTLED_ACTIONS = %i[show create download].freeze
include Shunter::Integration::Throttle
api :GET, '/repp/v1/api_users/:api_user_id/certificates/:id'
desc "Get a specific api user's specific certificate data"
def show
serializer = Serializers::Repp::Certificate.new(@certificate)
render_success(data: { cert: serializer.to_json })
end
api :POST, '/repp/v1/certificates'
desc 'Submit a new api user certificate signing request'
def create
@api_user = current_user.registrar.api_users.find(cert_params[:api_user_id])
csr = decode_cert_params(cert_params[:csr])
interface = cert_params[:interface].presence || 'api'
# Validate interface
unless Certificate::INTERFACES.include?(interface)
render_error(I18n.t('errors.invalid_interface'), :unprocessable_entity) and return
end
@certificate = @api_user.certificates.build(csr: csr, interface: interface)
if @certificate.save
# Автоматически подписываем CSR
begin
generator = Certificates::CertificateGenerator.new(
username: @api_user.username,
registrar_code: @api_user.registrar.code,
registrar_name: @api_user.registrar.name,
user_csr: csr,
interface: interface
)
result = generator.call
@certificate.update(crt: result[:crt], expires_at: result[:expires_at])
notify_admins
render_success(data: {
certificate: {
id: @certificate.id,
common_name: @certificate.common_name,
expires_at: @certificate.expires_at,
interface: @certificate.interface,
status: @certificate.status
}
})
rescue StandardError => e
Rails.logger.error("Certificate generation error: #{e.message}")
@certificate.destroy # Удаляем частично созданный сертификат
render_error(I18n.t('errors.certificate_generation_failed'), :unprocessable_entity)
end
else
handle_non_epp_errors(@certificate)
end
end
api :get, '/repp/v1/api_users/:api_user_id/certificates/:id/download'
desc "Download a specific api user's specific certificate"
param :type, String, required: true, desc: 'Type of certificate (csr or crt)'
def download
filename = "#{@api_user.username}_#{Time.zone.today.strftime('%y%m%d')}_portal.#{params[:type]}.pem"
send_data @certificate[params[:type].to_s], filename: filename
end
private
def find_certificate
@api_user = current_user.registrar.api_users.find(params[:api_user_id])
@certificate = @api_user.certificates.find(params[:id])
end
def cert_params
params.require(:certificate).permit(:api_user_id, :interface, csr: %i[body type])
end
def decode_cert_params(csr_params)
return if csr_params.blank?
Base64.decode64(csr_params[:body])
end
def notify_admins
admin_users_emails = User.admin.pluck(:email).reject(&:blank?)
return if admin_users_emails.empty?
admin_users_emails.each do |email|
CertificateMailer.certificate_signing_requested(email: email,
api_user: @api_user,
csr: @certificate)
.deliver_now
end
end
end
end
end