Return fingerprint along with the public key for repository
This commit is contained in:
parent
d7c6edec18
commit
fd6042573a
|
@ -1,4 +1,5 @@
|
|||
require 'openssl'
|
||||
require 'travis/private_key'
|
||||
|
||||
module Travis
|
||||
module Api
|
||||
|
@ -14,9 +15,7 @@ module Travis
|
|||
def fingerprint
|
||||
value = object.value.decrypt
|
||||
return unless value
|
||||
key = OpenSSL::PKey::RSA.new(value)
|
||||
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(':')
|
||||
PrivateKey.new(value).fingerprint
|
||||
rescue OpenSSL::PKey::RSAError
|
||||
nil
|
||||
end
|
||||
|
|
|
@ -9,9 +9,14 @@ module Travis
|
|||
@key = key
|
||||
end
|
||||
|
||||
def fingerprint
|
||||
PrivateKey.new(key.private_key).fingerprint
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
'key' => key.public_key
|
||||
'key' => key.public_key,
|
||||
'fingerprint' => fingerprint
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
16
lib/travis/private_key.rb
Normal file
16
lib/travis/private_key.rb
Normal 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
|
|
@ -4,9 +4,15 @@ describe Travis::Api::V2::Http::Repository do
|
|||
include Travis::Testing::Stubs
|
||||
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['fingerprint'].should == '57:78:65:c2:c9:c8:c9:f7:dd:2b:35:39:40:27:d2:40'
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user