Return fingerprint along with the public key for repository

This commit is contained in:
Piotr Sarnacki 2014-07-29 04:39:03 +02:00
parent d7c6edec18
commit fd6042573a
4 changed files with 32 additions and 6 deletions

View File

@ -1,4 +1,5 @@
require 'openssl' require 'openssl'
require 'travis/private_key'
module Travis module Travis
module Api module Api
@ -14,9 +15,7 @@ module Travis
def fingerprint def fingerprint
value = object.value.decrypt value = object.value.decrypt
return unless value return unless value
key = OpenSSL::PKey::RSA.new(value) PrivateKey.new(value).fingerprint
ssh_rsa = "\x00\x00\x00\x07ssh-rsa" + key.e.to_s(0) + key.n.to_s(0)
OpenSSL::Digest::MD5.new(ssh_rsa).hexdigest.scan(/../).join(':')
rescue OpenSSL::PKey::RSAError rescue OpenSSL::PKey::RSAError
nil nil
end end

View File

@ -9,9 +9,14 @@ module Travis
@key = key @key = key
end end
def fingerprint
PrivateKey.new(key.private_key).fingerprint
end
def data def data
{ {
'key' => key.public_key 'key' => key.public_key,
'fingerprint' => fingerprint
} }
end end
end end

16
lib/travis/private_key.rb Normal file
View File

@ -0,0 +1,16 @@
class PrivateKey
attr_reader :key
def initialize(key)
@key = key
end
def fingerprint
rsa_key = OpenSSL::PKey::RSA.new(key)
public_ssh_rsa = "\x00\x00\x00\x07ssh-rsa" + rsa_key.e.to_s(0) + rsa_key.n.to_s(0)
OpenSSL::Digest::MD5.new(public_ssh_rsa).hexdigest.scan(/../).join(':')
end
def inspect
"<PrivateKey #{fingerprint}>"
end
end

View File

@ -4,9 +4,15 @@ describe Travis::Api::V2::Http::Repository do
include Travis::Testing::Stubs include Travis::Testing::Stubs
include Support::Formats include Support::Formats
let(:data) { Travis::Api::V2::Http::SslKey.new(stub_key).data } let(:key) {
key = stub_key
key.stubs(:private_key).returns(TEST_PRIVATE_KEY)
key
}
let(:data) { Travis::Api::V2::Http::SslKey.new(key).data }
it 'key' do it 'returns data' do
data['key'].should == '-----BEGIN PUBLIC KEY-----' data['key'].should == '-----BEGIN PUBLIC KEY-----'
data['fingerprint'].should == '57:78:65:c2:c9:c8:c9:f7:dd:2b:35:39:40:27:d2:40'
end end
end end