Class: Gitlab::KeyFingerprint

Inherits:
Object
  • Object
show all
Includes:
Popen
Defined in:
lib/gitlab/key_fingerprint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Popen

#popen

Constructor Details

#initialize(key) ⇒ KeyFingerprint

Returns a new instance of KeyFingerprint


7
8
9
# File 'lib/gitlab/key_fingerprint.rb', line 7

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key


5
6
7
# File 'lib/gitlab/key_fingerprint.rb', line 5

def key
  @key
end

Instance Method Details

#fingerprintObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/key_fingerprint.rb', line 11

def fingerprint
  cmd_status = 0
  cmd_output = ''

  Tempfile.open('gitlab_key_file') do |file|
    file.puts key
    file.rewind

    cmd = []
    cmd.push *%W(ssh-keygen)
    cmd.push *%W(-E md5) if explicit_fingerprint_algorithm?
    cmd.push *%W(-lf #{file.path})

    cmd_output, cmd_status = popen(cmd, '/tmp')
  end

  return nil unless cmd_status.zero?

  # 16 hex bytes separated by ':', optionally starting with "MD5:"
  fingerprint_matches = cmd_output.match(/(MD5:)?(?<fingerprint>(\h{2}:){15}\h{2})/)
  return nil unless fingerprint_matches

  fingerprint_matches[:fingerprint]
end