From fd6042573ad89f0ecfe371e17a280d63d7d80ba4 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 29 Jul 2014 04:39:03 +0200 Subject: [PATCH] Return fingerprint along with the public key for repository --- lib/travis/api/v2/http/ssh_key.rb | 5 ++--- lib/travis/api/v2/http/ssl_key.rb | 7 ++++++- lib/travis/private_key.rb | 16 ++++++++++++++++ spec/unit/api/v2/http/ssl_key_spec.rb | 10 ++++++++-- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 lib/travis/private_key.rb diff --git a/lib/travis/api/v2/http/ssh_key.rb b/lib/travis/api/v2/http/ssh_key.rb index 25908dd6..afff8987 100644 --- a/lib/travis/api/v2/http/ssh_key.rb +++ b/lib/travis/api/v2/http/ssh_key.rb @@ -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 diff --git a/lib/travis/api/v2/http/ssl_key.rb b/lib/travis/api/v2/http/ssl_key.rb index fa356e17..51754f12 100644 --- a/lib/travis/api/v2/http/ssl_key.rb +++ b/lib/travis/api/v2/http/ssl_key.rb @@ -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 diff --git a/lib/travis/private_key.rb b/lib/travis/private_key.rb new file mode 100644 index 00000000..924bbee3 --- /dev/null +++ b/lib/travis/private_key.rb @@ -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 + "" + end +end diff --git a/spec/unit/api/v2/http/ssl_key_spec.rb b/spec/unit/api/v2/http/ssl_key_spec.rb index fc7f988e..5be3da1c 100644 --- a/spec/unit/api/v2/http/ssl_key_spec.rb +++ b/spec/unit/api/v2/http/ssl_key_spec.rb @@ -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