Commit e34cef0c authored by Alexis Reigel's avatar Alexis Reigel

extract gpg functionality to lib class

parent 7b4d29f4
......@@ -33,12 +33,8 @@ class GpgKey < ActiveRecord::Base
private
def extract_fingerprint
import = GPGME::Key.import(key)
return if import.considered == 0
# we can assume that the result only contains one item as the validation
# only allows one key
self.fingerprint = import.imports.first.fingerprint
self.fingerprint = Gitlab::Gpg.fingerprints_from_key(key).first
end
end
module Gitlab
module Gpg
extend self
def fingerprints_from_key(key)
using_tmp_keychain do
import = GPGME::Key.import(key)
return [] if import.imported == 0
import.imports.map(&:fingerprint)
end
end
def using_tmp_keychain
Dir.mktmpdir do |dir|
@original_dirs ||= [GPGME::Engine.dirinfo('homedir')]
@original_dirs.push(dir)
GPGME::Engine.home_dir = dir
return_value = yield
@original_dirs.pop
GPGME::Engine.home_dir = @original_dirs[-1]
return_value
end
end
end
end
require 'rails_helper'
describe Gitlab::Gpg do
describe '.fingerprints_from_key' do
it 'returns the fingerprint' do
expect(
described_class.fingerprints_from_key(GpgHelpers.public_key)
).to eq ['4F4840A503964251CF7D7F5DC728AF10972E97C0']
end
it 'returns an empty array when the key is invalid' do
expect(
described_class.fingerprints_from_key('bogus')
).to eq []
end
end
describe '.add_to_keychain' do
end
end
......@@ -143,14 +143,8 @@ RSpec.configure do |config|
end
config.around(:each, :gpg) do |example|
Dir.mktmpdir do |dir|
original_dir = GPGME::Engine.dirinfo('homedir')
GPGME::Engine.home_dir = dir
Gitlab::Gpg.using_tmp_keychain do
example.run
GPGME::Engine.home_dir = original_dir
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment