Class: Cript::KeyInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/cript/key_info.rb

Instance Method Summary collapse

Instance Method Details

#commentObject



28
29
30
# File 'lib/cript/key_info.rb', line 28

def comment
  @opt[:comment] || key_info[:comment]
end

#fingerprintObject



24
25
26
# File 'lib/cript/key_info.rb', line 24

def fingerprint
  @opt[:fingerprint] || key_info[:fingerprint]
end

#key_content(type = :public) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cript/key_info.rb', line 32

def key_content(type = :public)
  type = :private unless type == :public
  if @opt[:"#{type}_key_content"]
    @opt[:"#{type}_key_content"]
  elsif @opt[:"#{type}_key_path"]
    content = File.read(@opt[:"#{type}_key_path"])
    if content.include?("#{type.to_s.upcase} KEY")
      content
    else
      ssh_key_to_pem(@opt[:"#{type}_key_path"])
    end
  else
    raise Cript::Cripter::Error, "No #{type} key content"
  end
end

#key_infoObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/cript/key_info.rb', line 48

def key_info
  @key_info ||= begin
    if @opt[:public_key_path]
      info = ssh_keygen("-l -f \"#{@opt[:public_key_path]}\"").split(/[\s]+/)
      if info.length == 4
        { size: info[0].to_i, fingerprint: info[1], comment: info[2], type: info[3].match(/([\w]+)/)[1].downcase }
      end
    end
  end
end

#sizeObject



20
21
22
# File 'lib/cript/key_info.rb', line 20

def size
  @opt[:size] || key_info[:size]
end

#ssh_key_to_pem(path) ⇒ Object



3
4
5
# File 'lib/cript/key_info.rb', line 3

def ssh_key_to_pem(path)
  ssh_keygen("-f \"#{path}\" -e -m pem")
end

#ssh_keygen(cmd) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/cript/key_info.rb', line 7

def ssh_keygen(cmd)
  ssh_keygen = %x{ which ssh-keygen }.to_s.strip
  if ssh_keygen != ""
    %x{ #{ssh_keygen} #{cmd} }.to_s.strip
  else
    raise "ssh-keygen not available"
  end
end

#typeObject



16
17
18
# File 'lib/cript/key_info.rb', line 16

def type
  @opt[:type] || key_info[:type]
end