#953 fixed errors with key dublicates; fix error messages; small refactoring;
This commit is contained in:
parent
8756a6d28a
commit
e97cda730f
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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: Пользователь
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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|
|
||||
|
|
Loading…
Reference in New Issue