Class: Tddium::Ssh
- Inherits:
-
Object
- Object
- Tddium::Ssh
- Extended by:
- TddiumConstant
- Defined in:
- lib/tddium/ssh.rb
Class Method Summary collapse
- .generate_keypair(name, output_dir) ⇒ Object
- .load_ssh_key(ssh_file, name) ⇒ Object
- .validate_keys(name, path, tddium_api, generate_new_key = false) ⇒ Object
Class Method Details
.generate_keypair(name, output_dir) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tddium/ssh.rb', line 28 def generate_keypair(name, output_dir) filename = File.(File.join(output_dir, "identity.tddium.#{name}")) pub_filename = filename + ".pub" if File.exists?(filename) then raise TddiumError.new(Text::Error::KEY_ALREADY_EXISTS % filename) end cmd = "ssh-keygen -q -t rsa -P '' -C 'tddium.#{name}' -f #{filename}" exit_failure Text::Error::KEYGEN_FAILED % name unless system(cmd) {:name=>name, :pub=>File.read(pub_filename), :hostname=>`hostname`, :fingerprint=>`ssh-keygen -lf #{pub_filename}`} end |
.load_ssh_key(ssh_file, name) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/tddium/ssh.rb', line 8 def load_ssh_key(ssh_file, name) begin data = File.open(File.(ssh_file)) {|file| file.read} rescue Errno::ENOENT => e raise TddiumError.new(Text::Error::INACCESSIBLE_SSH_PUBLIC_KEY % [ssh_file, e]) end if data =~ /^-+BEGIN \S+ PRIVATE KEY-+/ then raise TddiumError.new(Text::Error::INVALID_SSH_PUBLIC_KEY % ssh_file) end if data !~ /^\s*ssh-(dss|rsa)/ && data !~ /^\s*ecdsa-/ then raise TddiumError.new(Text::Error::INVALID_SSH_PUBLIC_KEY % ssh_file) end {:name=>name, :pub=>data, :hostname=>`hostname`, :fingerprint=>`ssh-keygen -lf #{ssh_file}`} end |
.validate_keys(name, path, tddium_api, generate_new_key = false) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tddium/ssh.rb', line 42 def validate_keys name, path, tddium_api, generate_new_key = false keys_details, keydata = tddium_api.get_keys, nil # key name should be unique if keys_details.count{|x|x['name'] == name} > 0 abort Text::Error::ADD_KEYS_DUPLICATE % name end unless generate_new_key # check out key's content uniqueness keydata = self.load_ssh_key(path, name) duplicate_keys = keys_details.select{|key| key['pub'] == keydata[:pub] } unless duplicate_keys.empty? abort Text::Error::ADD_KEY_CONTENT_DUPLICATE % duplicate_keys.first['name'] end else # generate new key keydata = self.generate_keypair(name, path) end keydata end |