From 9bc1d212ce6d05eff8fbee56f461f69e33daf6a2 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Wed, 28 Apr 2021 06:57:52 +0000 Subject: [PATCH] Fixes #3528 - Allow to add S/MIME certificate chains from single file. --- .../integration/smime_controller.rb | 6 +++-- spec/requests/integration/smime_spec.rb | 4 ++-- spec/system/system/integration/smime_spec.rb | 23 ++++++++++++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/controllers/integration/smime_controller.rb b/app/controllers/integration/smime_controller.rb index 42005ddc9..4751557dc 100644 --- a/app/controllers/integration/smime_controller.rb +++ b/app/controllers/integration/smime_controller.rb @@ -42,11 +42,13 @@ class Integration::SMIMEController < ApplicationController string = params[:file].read.force_encoding('utf-8') end - item = SMIMECertificate.create!(public_key: string) + items = string.scan(/.+?-+END(?: TRUSTED)? CERTIFICATE-+/mi).each_with_object([]) do |cert, result| + result << SMIMECertificate.create!(public_key: cert) + end render json: { result: 'ok', - response: item, + response: items, } rescue => e unprocessable_entity(e) diff --git a/spec/requests/integration/smime_spec.rb b/spec/requests/integration/smime_spec.rb index 42d077281..d8d5d8a69 100644 --- a/spec/requests/integration/smime_spec.rb +++ b/spec/requests/integration/smime_spec.rb @@ -30,7 +30,7 @@ RSpec.describe 'Integration SMIME', type: :request do end.to change(SMIMECertificate, :count).by(1) expect(response).to have_http_status(:ok) - expect(DateTime.parse(json_response['response']['not_after_at'])).to eq(parsed_certificate.not_after) + expect(DateTime.parse(json_response['response'][0]['not_after_at'])).to eq(parsed_certificate.not_after) end it 'adds certificate by file' do @@ -39,7 +39,7 @@ RSpec.describe 'Integration SMIME', type: :request do end.to change(SMIMECertificate, :count).by(1) expect(response).to have_http_status(:ok) - expect(DateTime.parse(json_response['response']['not_after_at'])).to eq(parsed_certificate.not_after) + expect(DateTime.parse(json_response['response'][0]['not_after_at'])).to eq(parsed_certificate.not_after) end end diff --git a/spec/system/system/integration/smime_spec.rb b/spec/system/system/integration/smime_spec.rb index 19147946b..2d8cf5edc 100644 --- a/spec/system/system/integration/smime_spec.rb +++ b/spec/system/system/integration/smime_spec.rb @@ -14,11 +14,14 @@ RSpec.describe 'Manage > Integration > S/MIME', type: :system do File.read(Rails.root.join("spec/fixtures/smime/#{fixture}.secret")).strip end - it 'enabling and adding of public and private key' do + before do visit 'system/integration/smime' # enable S/MIME click 'label[for=setting-switch]' + end + + it 'enabling and adding of public and private key' do # add cert click '.js-addCertificate' @@ -40,4 +43,22 @@ RSpec.describe 'Manage > Integration > S/MIME', type: :system do expect( SMIMECertificate.last.raw ).to be_present expect( SMIMECertificate.last.private_key ).to be_present end + + it 'adding of multiple certificates at once' do + multiple_certificates = [ + File.read(Rails.root.join('spec/fixtures/smime/ChainCA.crt')), + File.read(Rails.root.join('spec/fixtures/smime/IntermediateCA.crt')), + File.read(Rails.root.join('spec/fixtures/smime/RootCA.crt')), + ].join + + # add cert + click '.js-addCertificate' + fill_in 'Paste Certificate', with: multiple_certificates + click '.js-submit' + + # wait for ajax + expect(page).to have_text('ChainCA') + expect(page).to have_text('IntermediateCA') + expect(page).to have_text('RootCA') + end end