diff --git a/app/models/ssh_key.rb b/app/models/ssh_key.rb index 710158419..c33dffc39 100644 --- a/app/models/ssh_key.rb +++ b/app/models/ssh_key.rb @@ -10,8 +10,9 @@ class SshKey < ActiveRecord::Base before_validation lambda { self.key = key.strip if key.present? } before_validation :set_fingerprint - validates :name, :presence => true, :length => {:maximum => 255} - validates :key, :presence => true, :length => {:maximum => 5000}, format: { :with => /ssh-.{3} / }, uniqueness: true + validates :name, :length => {:maximum => 255} + validates :key, :length => {:maximum => 5000}, format: { :with => /ssh-.{3} / } # Public key? + validates :fingerprint, uniqueness: true, :presence => { :message => I18n.t('activerecord.errors.ssh_key.wrong_key') } after_create :add_key before_destroy :remove_key @@ -21,21 +22,29 @@ class SshKey < ActiveRecord::Base def set_fingerprint return false unless key - file = Tempfile.new('key_file') + file = Tempfile.new('key_file', "#{APP_CONFIG['root_path']}/tmp") begin file.puts key file.rewind fingerprint_output = `ssh-keygen -lf #{file.path} 2>&1` # Catch stderr. + exitstatus = $?.exitstatus ensure file.close file.unlink # deletes the temp file end - error_message = I18n.t('activerecord.errors.ssh_key.wrong_key') - if $?.exitstatus != 0 - errors.add :key, error_message + if exitstatus != 0 + self.fingerprint = nil else - self.fingerprint = fingerprint_output.split.try(:[], 1) - errors.add(:key, error_message) if fingerprint.blank? + self.fingerprint = fingerprint_output.split.try :[], 1 + if name.blank? + s = fingerprint_output.split.try :[], 2 + if File.exist? s # no identificator + start = key =~ /ssh-.{3} / + self.name = key[start..start+26] # taken first 26 characters + else + self.name = s + end + end end end diff --git a/config/locales/models/ssh_key.en.yml b/config/locales/models/ssh_key.en.yml index 2100b8a75..fe0886f0d 100644 --- a/config/locales/models/ssh_key.en.yml +++ b/config/locales/models/ssh_key.en.yml @@ -4,14 +4,14 @@ en: activerecord: errors: ssh_key: - wrong_key: Wrong public key + wrong_key: '^Wrong public key' models: ssh_key: SSH Key attributes: ssh_key: id: Id name: Name - fingerprint: Fingerprint + fingerprint: Key created_at: Created updated_at: Updated user_id: User diff --git a/config/locales/models/ssh_key.ru.yml b/config/locales/models/ssh_key.ru.yml index 425e320b8..3892dc136 100644 --- a/config/locales/models/ssh_key.ru.yml +++ b/config/locales/models/ssh_key.ru.yml @@ -4,14 +4,14 @@ ru: activerecord: errors: ssh_key: - wrong_key: Неправильный публичный ключ + wrong_key: '^Неправильный публичный ключ' models: ssh_key: SSH ключ attributes: ssh_key: id: Id name: Имя - fingerprint: Отпечаток + fingerprint: Ключ created_at: Создано updated_at: Обновлено user_id: Пользователь diff --git a/db/migrate/20130227102900_create_ssh_keys.rb b/db/migrate/20130227102900_create_ssh_keys.rb index c06929945..7e652627f 100644 --- a/db/migrate/20130227102900_create_ssh_keys.rb +++ b/db/migrate/20130227102900_create_ssh_keys.rb @@ -1,7 +1,7 @@ class CreateSshKeys < ActiveRecord::Migration def change create_table :ssh_keys do |t| - t.string :name, :null => false + t.string :name t.text :key, :null => false t.string :fingerprint, :null => false t.integer :user_id, :null => false @@ -9,7 +9,7 @@ class CreateSshKeys < ActiveRecord::Migration t.timestamps end - add_index :ssh_keys, :key, :unique => true + add_index :ssh_keys, :fingerprint, :unique => true add_index :ssh_keys, :user_id end end diff --git a/db/schema.rb b/db/schema.rb index 01b180546..bf7ec9c65 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -454,7 +454,7 @@ ActiveRecord::Schema.define(:version => 20130227102900) do end create_table "ssh_keys", :force => true do |t| - t.string "name", :null => false + t.string "name" t.text "key", :null => false t.string "fingerprint", :null => false t.integer "user_id", :null => false @@ -462,7 +462,7 @@ ActiveRecord::Schema.define(:version => 20130227102900) do t.datetime "updated_at", :null => false end - add_index "ssh_keys", ["key"], :name => "index_ssh_keys_on_key", :unique => true + add_index "ssh_keys", ["fingerprint"], :name => "index_ssh_keys_on_fingerprint", :unique => true add_index "ssh_keys", ["user_id"], :name => "index_ssh_keys_on_user_id" create_table "subscribes", :force => true do |t|