Commit 113d2ff525b6005e1cc7ff86a5a0189c4ab3d0e4
1 parent
05a7e8b9
Exists in
master
and in
4 other branches
store and display public key fingerprint
Showing
6 changed files
with
60 additions
and
13 deletions
Show diff stats
app/models/key.rb
| @@ -15,6 +15,8 @@ | @@ -15,6 +15,8 @@ | ||
| 15 | require 'digest/md5' | 15 | require 'digest/md5' |
| 16 | 16 | ||
| 17 | class Key < ActiveRecord::Base | 17 | class Key < ActiveRecord::Base |
| 18 | + include Gitlab::Popen | ||
| 19 | + | ||
| 18 | belongs_to :user | 20 | belongs_to :user |
| 19 | 21 | ||
| 20 | attr_accessible :key, :title | 22 | attr_accessible :key, :title |
| @@ -34,16 +36,10 @@ class Key < ActiveRecord::Base | @@ -34,16 +36,10 @@ class Key < ActiveRecord::Base | ||
| 34 | def fingerprintable_key | 36 | def fingerprintable_key |
| 35 | return true unless key # Don't test if there is no key. | 37 | return true unless key # Don't test if there is no key. |
| 36 | 38 | ||
| 37 | - file = Tempfile.new('key_file') | ||
| 38 | - begin | ||
| 39 | - file.puts key | ||
| 40 | - file.rewind | ||
| 41 | - fingerprint_output = `ssh-keygen -lf #{file.path} 2>&1` # Catch stderr. | ||
| 42 | - ensure | ||
| 43 | - file.close | ||
| 44 | - file.unlink # deletes the temp file | 39 | + unless generate_fingerpint |
| 40 | + errors.add(:key, "can't be fingerprinted") | ||
| 41 | + false | ||
| 45 | end | 42 | end |
| 46 | - errors.add(:key, "can't be fingerprinted") if $?.exitstatus != 0 | ||
| 47 | end | 43 | end |
| 48 | 44 | ||
| 49 | # projects that has this key | 45 | # projects that has this key |
| @@ -54,4 +50,30 @@ class Key < ActiveRecord::Base | @@ -54,4 +50,30 @@ class Key < ActiveRecord::Base | ||
| 54 | def shell_id | 50 | def shell_id |
| 55 | "key-#{id}" | 51 | "key-#{id}" |
| 56 | end | 52 | end |
| 53 | + | ||
| 54 | + private | ||
| 55 | + | ||
| 56 | + def generate_fingerpint | ||
| 57 | + cmd_status = 0 | ||
| 58 | + cmd_output = '' | ||
| 59 | + file = Tempfile.new('gitlab_key_file') | ||
| 60 | + | ||
| 61 | + begin | ||
| 62 | + file.puts key | ||
| 63 | + file.rewind | ||
| 64 | + cmd_output, cmd_status = popen("ssh-keygen -lf #{file.path}", '/tmp') | ||
| 65 | + ensure | ||
| 66 | + file.close | ||
| 67 | + file.unlink # deletes the temp file | ||
| 68 | + end | ||
| 69 | + | ||
| 70 | + if cmd_status.zero? | ||
| 71 | + cmd_output.gsub /([\d\h]{2}:)+[\d\h]{2}/ do |match| | ||
| 72 | + self.fingerprint = match | ||
| 73 | + end | ||
| 74 | + true | ||
| 75 | + else | ||
| 76 | + false | ||
| 77 | + end | ||
| 78 | + end | ||
| 57 | end | 79 | end |
app/views/profiles/keys/_key.html.haml
| 1 | %li | 1 | %li |
| 2 | = link_to profile_key_path(key) do | 2 | = link_to profile_key_path(key) do |
| 3 | %strong= key.title | 3 | %strong= key.title |
| 4 | + %span | ||
| 5 | + (#{key.fingerprint}) | ||
| 4 | %span.cgray | 6 | %span.cgray |
| 5 | added | 7 | added |
| 6 | = time_ago_in_words(key.created_at) | 8 | = time_ago_in_words(key.created_at) |
| 7 | ago | 9 | ago |
| 8 | - = link_to 'Remove', profile_key_path(key), confirm: 'Are you sure?', method: :delete, class: "btn btn-small btn-remove delete-key pull-right" | ||
| 9 | 10 | ||
| 11 | + = link_to 'Remove', profile_key_path(key), confirm: 'Are you sure?', method: :delete, class: "btn btn-small btn-remove delete-key pull-right" |
app/views/profiles/keys/show.html.haml
db/schema.rb
| @@ -11,7 +11,7 @@ | @@ -11,7 +11,7 @@ | ||
| 11 | # | 11 | # |
| 12 | # It's strongly recommended to check this file into your version control system. | 12 | # It's strongly recommended to check this file into your version control system. |
| 13 | 13 | ||
| 14 | -ActiveRecord::Schema.define(:version => 20130622115340) do | 14 | +ActiveRecord::Schema.define(:version => 20130624162710) do |
| 15 | 15 | ||
| 16 | create_table "deploy_keys_projects", :force => true do |t| | 16 | create_table "deploy_keys_projects", :force => true do |t| |
| 17 | t.integer "deploy_key_id", :null => false | 17 | t.integer "deploy_key_id", :null => false |
| @@ -77,11 +77,10 @@ ActiveRecord::Schema.define(:version => 20130622115340) do | @@ -77,11 +77,10 @@ ActiveRecord::Schema.define(:version => 20130622115340) do | ||
| 77 | t.datetime "updated_at" | 77 | t.datetime "updated_at" |
| 78 | t.text "key" | 78 | t.text "key" |
| 79 | t.string "title" | 79 | t.string "title" |
| 80 | - t.string "identifier" | ||
| 81 | t.string "type" | 80 | t.string "type" |
| 81 | + t.string "fingerprint" | ||
| 82 | end | 82 | end |
| 83 | 83 | ||
| 84 | - add_index "keys", ["identifier"], :name => "index_keys_on_identifier" | ||
| 85 | add_index "keys", ["user_id"], :name => "index_keys_on_user_id" | 84 | add_index "keys", ["user_id"], :name => "index_keys_on_user_id" |
| 86 | 85 | ||
| 87 | create_table "merge_requests", :force => true do |t| | 86 | create_table "merge_requests", :force => true do |t| |
| @@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
| 1 | +desc "GITLAB | Migrate SSH Keys" | ||
| 2 | +task migrate_keys: :environment do | ||
| 3 | + puts "This will add fingerprint to ssh keys in db" | ||
| 4 | + ask_to_continue | ||
| 5 | + | ||
| 6 | + Key.find_each(batch_size: 20) do |key| | ||
| 7 | + if key.valid? && key.save | ||
| 8 | + print '.' | ||
| 9 | + else | ||
| 10 | + print 'F' | ||
| 11 | + end | ||
| 12 | + end | ||
| 13 | +end | ||
| 14 | + | ||
| 15 | + |