Class: LeapCli::SshKey

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/leap_cli/ssh_key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rsa_key) ⇒ SshKey

Returns a new instance of SshKey.



72
73
74
# File 'lib/leap_cli/ssh_key.rb', line 72

def initialize(rsa_key)
  @key = rsa_key
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



14
15
16
# File 'lib/leap_cli/ssh_key.rb', line 14

def comment
  @comment
end

#filenameObject

Returns the value of attribute filename.



13
14
15
# File 'lib/leap_cli/ssh_key.rb', line 13

def filename
  @filename
end

Class Method Details

.load(arg1, arg2 = nil) ⇒ Object

CLASS METHODS



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/leap_cli/ssh_key.rb', line 20

def self.load(arg1, arg2=nil)
  key = nil
  if arg1.is_a? OpenSSL::PKey::RSA
    key = SshKey.new arg1
  elsif arg1.is_a? String
    if arg1 =~ /^ssh-/
      type, data = arg1.split(' ')
      key = SshKey.new load_from_data(data, type)
    elsif File.exists? arg1
      key = SshKey.new load_from_file(arg1)
      key.filename = arg1
    else
      key = SshKey.new load_from_data(arg1, arg2)
    end
  end
  return key
end

.load_from_data(data, type = 'ssh-rsa') ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/leap_cli/ssh_key.rb', line 52

def self.load_from_data(data, type='ssh-rsa')
  public_key = nil
  private_key = nil
  begin
    public_key = Net::SSH::KeyFactory.load_data_public_key("#{type} #{data}")
  rescue NotImplementedError, Net::SSH::Exception, OpenSSL::PKey::PKeyError
    begin
      private_key = Net::SSH::KeyFactory.load_data_private_key("#{type} #{data}")
    rescue NotImplementedError, Net::SSH::Exception, OpenSSL::PKey::PKeyError
    end
  end
  public_key || private_key
end

.load_from_file(filename) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/leap_cli/ssh_key.rb', line 38

def self.load_from_file(filename)
  public_key = nil
  private_key = nil
  begin
    public_key = Net::SSH::KeyFactory.load_public_key(filename)
  rescue NotImplementedError, Net::SSH::Exception, OpenSSL::PKey::PKeyError
    begin
      private_key = Net::SSH::KeyFactory.load_private_key(filename)
    rescue NotImplementedError, Net::SSH::Exception, OpenSSL::PKey::PKeyError
    end
  end
  public_key || private_key
end

Instance Method Details

#==(other_key) ⇒ Object



114
115
116
117
118
# File 'lib/leap_cli/ssh_key.rb', line 114

def ==(other_key)
  return false if other_key.nil?
  return false if self.class != other_key.class
  return self.to_text == other_key.to_text
end

#bitsObject

not sure if this will always work, but is seems to for now.



98
99
100
# File 'lib/leap_cli/ssh_key.rb', line 98

def bits
  Net::SSH::Buffer.from(:key, @key).to_s.split("\001\000").last.size * 8
end

#in_known_hosts?(*identifiers) ⇒ Boolean

Returns:



120
121
122
123
124
125
126
127
# File 'lib/leap_cli/ssh_key.rb', line 120

def in_known_hosts?(*identifiers)
  identifiers.each do |identifier|
    Net::SSH::KnownHosts.search_for(identifier).each do |key|
      return true if self == key
    end
  end
  return false
end

#keyObject



110
111
112
# File 'lib/leap_cli/ssh_key.rb', line 110

def key
  [Net::SSH::Buffer.from(:key, @key).to_s].pack("m*").gsub(/\s/, "")
end

#private_keyObject



91
92
93
# File 'lib/leap_cli/ssh_key.rb', line 91

def private_key
  SshKey.new(@key.private_key)
end

#public_keyObject



87
88
89
# File 'lib/leap_cli/ssh_key.rb', line 87

def public_key
  SshKey.new(@key.public_key)
end

#summaryObject



102
103
104
# File 'lib/leap_cli/ssh_key.rb', line 102

def summary
  "%s %s %s (%s)" % [self.type, self.bits, self.fingerprint, self.filename || self.comment || '']
end

#to_sObject



106
107
108
# File 'lib/leap_cli/ssh_key.rb', line 106

def to_s
  self.type + " " + self.key
end