diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/key.rb | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/app/models/key.rb b/app/models/key.rb index a87ea4943e9..0f2fc45abc9 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -15,6 +15,8 @@ require 'digest/md5' class Key < ActiveRecord::Base + include Gitlab::Popen + belongs_to :user attr_accessible :key, :title @@ -34,16 +36,10 @@ class Key < ActiveRecord::Base def fingerprintable_key return true unless key # Don't test if there is no key. - file = Tempfile.new('key_file') - begin - file.puts key - file.rewind - fingerprint_output = `ssh-keygen -lf #{file.path} 2>&1` # Catch stderr. - ensure - file.close - file.unlink # deletes the temp file + unless generate_fingerpint + errors.add(:key, "can't be fingerprinted") + false end - errors.add(:key, "can't be fingerprinted") if $?.exitstatus != 0 end # projects that has this key @@ -54,4 +50,30 @@ class Key < ActiveRecord::Base def shell_id "key-#{id}" end + + private + + def generate_fingerpint + cmd_status = 0 + cmd_output = '' + file = Tempfile.new('gitlab_key_file') + + begin + file.puts key + file.rewind + cmd_output, cmd_status = popen("ssh-keygen -lf #{file.path}", '/tmp') + ensure + file.close + file.unlink # deletes the temp file + end + + if cmd_status.zero? + cmd_output.gsub /([\d\h]{2}:)+[\d\h]{2}/ do |match| + self.fingerprint = match + end + true + else + false + end + end end |
