Class: Vagrant::Util::Keypair

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/keypair.rb

Defined Under Namespace

Classes: Ecdsa, Ecdsa256, Ecdsa384, Ecdsa521, Ed25519, Rsa

Constant Summary collapse

AUTH_MAGIC =

Magic string header

"openssh-key-v1".freeze
PRIVATE_KEY_START =

Header of private key file content

"-----BEGIN OPENSSH PRIVATE KEY-----\n".freeze
PRIVATE_KEY_END =

Footer of private key file content

"-----END OPENSSH PRIVATE KEY-----\n".freeze
VALID_TYPES =

Supported key types.

{
  ecdsa256: Ecdsa256,
  ecdsa384: Ecdsa384,
  ecdsa521: Ecdsa521,
  ed25519: Ed25519,
  rsa: Rsa
}.freeze
PREFER_KEY_TYPES =

Ordered mapping of openssh key type name to lookup name. The order defined here is based on preference. Note that ecdsa ordering is based on performance

{
  Ed25519::KEY_TYPE => :ed25519,
  Ecdsa256::KEY_TYPE => :ecdsa256,
  Ecdsa521::KEY_TYPE => :ecdsa521,
  Ecdsa384::KEY_TYPE => :ecdsa384,
  Rsa::KEY_TYPE => :rsa,
}.freeze

Class Method Summary collapse

Class Method Details

.available_typesArray<Symbol>

Returns list of supported key types.

Returns:

  • (Array<Symbol>)

    list of supported key types



29
30
31
# File 'lib/vagrant/util/keypair.rb', line 29

def self.available_types
  PREFER_KEY_TYPES.values
end

.create(password = nil, type: :rsa) ⇒ Array<String, String, String>

Create a new keypair

Parameters:

  • password (String) (defaults to: nil)

    Password for the key or nil for no password (only supported for rsa type)

  • type (Symbol) (defaults to: :rsa)

    Key type to generate

Returns:

  • (Array<String, String, String>)

    Public key, openssh private key, openssh public key with comment



38
39
40
41
42
43
44
45
# File 'lib/vagrant/util/keypair.rb', line 38

def self.create(password=nil, type: :rsa)
  if !VALID_TYPES.key?(type)
    raise ArgumentError,
          "Invalid key type requested (supported types: #{available_types.map(&:inspect).join(", ")})"
  end

  VALID_TYPES[type].create(password)
end

.valid_type?(key) ⇒ Boolean

Check if provided key is a supported key type

Parameters:

  • key (Symbol)

    Key type to check

Returns:

  • (Boolean)

    key type is supported



24
25
26
# File 'lib/vagrant/util/keypair.rb', line 24

def self.valid_type?(key)
  VALID_TYPES.keys.include?(key)
end