From 502e31bec9af080bcb483b0d57c8b52aeb507f93 Mon Sep 17 00:00:00 2001 From: Alexis Reigel Date: Thu, 15 Jun 2017 13:37:03 +0200 Subject: [PATCH] memoize verified_signature call --- lib/gitlab/gpg/commit.rb | 25 +++++++++++++++++-------- spec/lib/gitlab/gpg/commit_spec.rb | 12 ++++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/gitlab/gpg/commit.rb b/lib/gitlab/gpg/commit.rb index 8d2e6269618..8bc430db715 100644 --- a/lib/gitlab/gpg/commit.rb +++ b/lib/gitlab/gpg/commit.rb @@ -19,6 +19,19 @@ module Gitlab cached_signature = GpgSignature.find_by(commit_sha: commit.sha) return cached_signature if cached_signature.present? + using_keychain do |gpg_key| + if gpg_key + Gitlab::Gpg::CurrentKeyChain.add(gpg_key.key) + @verified_signature = nil + end + + create_cached_signature!(gpg_key) + end + end + + private + + def using_keychain Gitlab::Gpg.using_tmp_keychain do # first we need to get the keyid from the signature to query the gpg # key belonging to the keyid. @@ -30,27 +43,23 @@ module Gitlab Gitlab::Gpg::CurrentKeyChain.add(gpg_key.key) end - create_cached_signature!(gpg_key) + yield gpg_key end end - private - def verified_signature - GPGME::Crypto.new.verify(@signature_text, signed_text: @signed_text) do |verified_signature| + @verified_signature ||= GPGME::Crypto.new.verify(@signature_text, signed_text: @signed_text) do |verified_signature| return verified_signature end end def create_cached_signature!(gpg_key) - verified_signature_result = verified_signature - GpgSignature.create!( commit_sha: commit.sha, project: commit.project, gpg_key: gpg_key, - gpg_key_primary_keyid: gpg_key&.primary_keyid || verified_signature_result.fingerprint, - valid_signature: !!(gpg_key && gpg_key.verified? && verified_signature_result.valid?) + gpg_key_primary_keyid: gpg_key&.primary_keyid || verified_signature.fingerprint, + valid_signature: !!(gpg_key && gpg_key.verified? && verified_signature.valid?) ) end end diff --git a/spec/lib/gitlab/gpg/commit_spec.rb b/spec/lib/gitlab/gpg/commit_spec.rb index 448b16a656e..387ce8f74b4 100644 --- a/spec/lib/gitlab/gpg/commit_spec.rb +++ b/spec/lib/gitlab/gpg/commit_spec.rb @@ -38,11 +38,11 @@ RSpec.describe Gitlab::Gpg::Commit do it 'returns the cached signature on second call' do gpg_commit = described_class.new(commit) - expect(gpg_commit).to receive(:verified_signature).twice.and_call_original + expect(gpg_commit).to receive(:using_keychain).and_call_original gpg_commit.signature # consecutive call - expect(gpg_commit).not_to receive(:verified_signature).and_call_original + expect(gpg_commit).not_to receive(:using_keychain).and_call_original gpg_commit.signature end end @@ -73,11 +73,11 @@ RSpec.describe Gitlab::Gpg::Commit do it 'returns the cached signature on second call' do gpg_commit = described_class.new(commit) - expect(gpg_commit).to receive(:verified_signature).and_call_original + expect(gpg_commit).to receive(:using_keychain).and_call_original gpg_commit.signature # consecutive call - expect(gpg_commit).not_to receive(:verified_signature).and_call_original + expect(gpg_commit).not_to receive(:using_keychain).and_call_original gpg_commit.signature end end @@ -108,11 +108,11 @@ RSpec.describe Gitlab::Gpg::Commit do it 'returns the cached signature on second call' do gpg_commit = described_class.new(commit) - expect(gpg_commit).to receive(:verified_signature).and_call_original + expect(gpg_commit).to receive(:using_keychain).and_call_original gpg_commit.signature # consecutive call - expect(gpg_commit).not_to receive(:verified_signature).and_call_original + expect(gpg_commit).not_to receive(:using_keychain).and_call_original gpg_commit.signature end end