summaryrefslogtreecommitdiff
path: root/qa/qa/runtime/key/base.rb
blob: c7e5ebada7b0bd46431e772b6bcb2bec21e8e442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module QA
  module Runtime
    module Key
      class Base
        attr_reader :name, :bits, :private_key, :public_key, :fingerprint

        def initialize(name, bits)
          @name = name
          @bits = bits

          Dir.mktmpdir do |dir|
            path = "#{dir}/id_#{name}"

            ssh_keygen(name, bits, path)
            populate_key_data(path)
          end
        end

        private

        def ssh_keygen(name, bits, path)
          cmd = %W[ssh-keygen -t #{name} -b #{bits} -f #{path} -N] << ''

          Service::Shellout.shell(cmd)
        end

        def populate_key_data(path)
          @private_key = File.binread(path)
          @public_key = File.binread("#{path}.pub")
          @fingerprint =
            `ssh-keygen -l -E md5 -f #{path} | cut -d' ' -f2 | cut -d: -f2-`.chomp
        end
      end
    end
  end
end