fog/fog — Issue #636: Document SSH key management
Author: pfleidi
Created: 2011-12-02 13:22:03+00:00
State: open

Hello everyone,

some days ago I was using Fog to bootstrap a virtual machine using Compute::AWS. I was running into some strange timeout errors until I discovered, that these errors are caused by a failed SSH authentication.

As it turns out, a colleague of mine was using [bootstrap](https://github.com/fog/fog/blob/master/lib/fog/aws/models/compute/servers.rb#L74) with the same AWS credentials but different SSH keys. So the fog_default public key was already registered to the public key of my colleague and the attempts to log in with my key pair failed.

It took me quite a bit of time to grep through the fog source to figure out what I was doing wrong. While reading through the fog code, I found two ways to work around this problem:

1. Use connection.import_key_pair() to register a custom key and pass in :key_name to bootstrap()
2. Set Fog.credential to a custom name so the SSH public key gets registered using fog_#{custom_name}

Using the first workaround my code looked like:

```ruby
Fog.credentials = Fog.credentials.merge({
:private_key_path => "./keys/my_custom_key",
:public_key_path => "./keys/my_custom_key.pub"
})

if connection.key_pairs.get(my_custom_key).nil?
public_key = IO.read('./keys/my_custom_key.pub')
connection.import_key_pair('my_custom_key', public_key)
end

server = connection.servers.bootstrap(
:key_name => my_custom_key,
...
)
```
Using the second, the code got much simpler code:

```ruby
Fog.credential = my_custom_key

connection.servers.bootstrap(
:private_key_path => './keys/my_custom_key',
:public_key_path => './keys/my_custom_key.pub',
...
)
```

It would be really great if this behavior was documented somewhere, so people wouldn't run into strange errors caused my wrong key management.