Class: Kitchen::Driver::Aws::InstanceConnect

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/driver/aws/instance_connect.rb

Overview

Pushes short-lived SSH public keys to an instance using EC2 Instance Connect, so that a connection can be made without a long-lived key pair.

A pushed key is accepted for roughly sixty seconds, so it is sent again before each connection rather than once at create time.

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ InstanceConnect

Returns a new instance of InstanceConnect.

Parameters:

  • config (Hash)

    the driver config

  • logger (Kitchen::Logger)

    the logger to report through



30
31
32
33
34
# File 'lib/kitchen/driver/aws/instance_connect.rb', line 30

def initialize(config, logger)
  @config = config
  @logger = logger
  @client = ::Aws::EC2InstanceConnect::Client.new(region: config[:region])
end

Instance Method Details

#send_ssh_public_key(instance_id, username, public_key) ⇒ void

This method returns an undefined value.

Push an SSH public key to an instance for a given user.

Parameters:

  • instance_id (String)

    the target instance, e.g. "i-0123abcd"

  • username (String)

    the OS account to authorize the key for

  • public_key (String)

    the OpenSSH-format public key

Raises:

  • (Aws::EC2InstanceConnect::Errors::ServiceError)

    when the key is rejected; there is no usable connection in that case, so the error is not swallowed



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kitchen/driver/aws/instance_connect.rb', line 45

def send_ssh_public_key(instance_id, username, public_key)
  @logger.info("Sending SSH public key to instance #{instance_id} for user #{username}")

  @client.send_ssh_public_key({
    instance_id: instance_id,
    instance_os_user: username,
    ssh_public_key: public_key,
    availability_zone: @config[:availability_zone],
  })

  @logger.debug("SSH public key successfully sent to instance")
end