#953 fixed errors with key dublicates; fix error messages; small refactoring;

This commit is contained in:
Alexander Machehin 2013-03-01 23:35:33 +06:00
parent 8756a6d28a
commit e97cda730f
5 changed files with 25 additions and 16 deletions

View File

@ -10,8 +10,9 @@ class SshKey < ActiveRecord::Base
before_validation lambda { self.key = key.strip if key.present? } before_validation lambda { self.key = key.strip if key.present? }
before_validation :set_fingerprint before_validation :set_fingerprint
validates :name, :presence => true, :length => {:maximum => 255} validates :name, :length => {:maximum => 255}
validates :key, :presence => true, :length => {:maximum => 5000}, format: { :with => /ssh-.{3} / }, uniqueness: true 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 after_create :add_key
before_destroy :remove_key before_destroy :remove_key
@ -21,21 +22,29 @@ class SshKey < ActiveRecord::Base
def set_fingerprint def set_fingerprint
return false unless key return false unless key
file = Tempfile.new('key_file') file = Tempfile.new('key_file', "#{APP_CONFIG['root_path']}/tmp")
begin begin
file.puts key file.puts key
file.rewind file.rewind
fingerprint_output = `ssh-keygen -lf #{file.path} 2>&1` # Catch stderr. fingerprint_output = `ssh-keygen -lf #{file.path} 2>&1` # Catch stderr.
exitstatus = $?.exitstatus
ensure ensure
file.close file.close
file.unlink # deletes the temp file file.unlink # deletes the temp file
end end
error_message = I18n.t('activerecord.errors.ssh_key.wrong_key') if exitstatus != 0
if $?.exitstatus != 0 self.fingerprint = nil
errors.add :key, error_message
else else
self.fingerprint = fingerprint_output.split.try(:[], 1) self.fingerprint = fingerprint_output.split.try :[], 1
errors.add(:key, error_message) if fingerprint.blank? 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
end end

View File

@ -4,14 +4,14 @@ en:
activerecord: activerecord:
errors: errors:
ssh_key: ssh_key:
wrong_key: Wrong public key wrong_key: '^Wrong public key'
models: models:
ssh_key: SSH Key ssh_key: SSH Key
attributes: attributes:
ssh_key: ssh_key:
id: Id id: Id
name: Name name: Name
fingerprint: Fingerprint fingerprint: Key
created_at: Created created_at: Created
updated_at: Updated updated_at: Updated
user_id: User user_id: User

View File

@ -4,14 +4,14 @@ ru:
activerecord: activerecord:
errors: errors:
ssh_key: ssh_key:
wrong_key: Неправильный публичный ключ wrong_key: '^Неправильный публичный ключ'
models: models:
ssh_key: SSH ключ ssh_key: SSH ключ
attributes: attributes:
ssh_key: ssh_key:
id: Id id: Id
name: Имя name: Имя
fingerprint: Отпечаток fingerprint: Ключ
created_at: Создано created_at: Создано
updated_at: Обновлено updated_at: Обновлено
user_id: Пользователь user_id: Пользователь

View File

@ -1,7 +1,7 @@
class CreateSshKeys < ActiveRecord::Migration class CreateSshKeys < ActiveRecord::Migration
def change def change
create_table :ssh_keys do |t| create_table :ssh_keys do |t|
t.string :name, :null => false t.string :name
t.text :key, :null => false t.text :key, :null => false
t.string :fingerprint, :null => false t.string :fingerprint, :null => false
t.integer :user_id, :null => false t.integer :user_id, :null => false
@ -9,7 +9,7 @@ class CreateSshKeys < ActiveRecord::Migration
t.timestamps t.timestamps
end end
add_index :ssh_keys, :key, :unique => true add_index :ssh_keys, :fingerprint, :unique => true
add_index :ssh_keys, :user_id add_index :ssh_keys, :user_id
end end
end end

View File

@ -454,7 +454,7 @@ ActiveRecord::Schema.define(:version => 20130227102900) do
end end
create_table "ssh_keys", :force => true do |t| create_table "ssh_keys", :force => true do |t|
t.string "name", :null => false t.string "name"
t.text "key", :null => false t.text "key", :null => false
t.string "fingerprint", :null => false t.string "fingerprint", :null => false
t.integer "user_id", :null => false t.integer "user_id", :null => false
@ -462,7 +462,7 @@ ActiveRecord::Schema.define(:version => 20130227102900) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end 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" add_index "ssh_keys", ["user_id"], :name => "index_ssh_keys_on_user_id"
create_table "subscribes", :force => true do |t| create_table "subscribes", :force => true do |t|