Module: Kitchen::Driver::Oci::Mixin::SshKeys::RSA

Defined in:
lib/kitchen/driver/oci/mixin/ssh_keys.rb

Overview

Mixins required to generate a RSA key pair.

Author:

  • Justin Steele <justin.steele@oracle.com>

Instance Method Summary collapse

Instance Method Details

#generate_key_pairObject

Generates an RSA key pair to be used to SSH to the instance and updates the state with the full path to the private key.



76
77
78
79
80
81
# File 'lib/kitchen/driver/oci/mixin/ssh_keys.rb', line 76

def generate_key_pair
  rsa_key = OpenSSL::PKey::RSA.new(4096)
  write_private_key(rsa_key)
  write_public_key(rsa_key)
  state.store(:ssh_key, private_key_file)
end

#write_private_key(rsa_key) ⇒ Object

Writes the private key.

Parameters:

  • rsa_key (OpenSSL::PKey::RSA)

    the generated RSA key.



86
87
88
89
# File 'lib/kitchen/driver/oci/mixin/ssh_keys.rb', line 86

def write_private_key(rsa_key)
  File.open(private_key_file, "wb") { |k| k.write(rsa_key.to_pem) }
  File.chmod(0600, private_key_file)
end

#write_public_key(rsa_key) ⇒ Object

Writes the encoded private key as a public key.

Parameters:

  • rsa_key (OpenSSL::PKey::RSA)

    the generated RSA key.



94
95
96
97
98
# File 'lib/kitchen/driver/oci/mixin/ssh_keys.rb', line 94

def write_public_key(rsa_key)
  public_key = ["#{[7].pack("N")}#{algorithm}#{rsa_key.e.to_s(0)}#{rsa_key.n.to_s(0)}"].pack("m0")
  File.open(public_key_file, "wb") { |k| k.write("#{algorithm} #{public_key} #{config[:instance_name]}") }
  File.chmod(0600, public_key_file)
end