|
| 1 | +# Public key support |
| 2 | + |
| 3 | +Bootleg supports the use of SSH identity files (keys) for passwordless connections. |
| 4 | + |
| 5 | +Please note the difference in terms as used in this document: |
| 6 | + |
| 7 | +* `password` refers to a password-protected user account |
| 8 | +* `passphrase` refers to a passphrase-protected private SSH key |
| 9 | + |
| 10 | +## Private keys |
| 11 | + |
| 12 | +In many cases, private keys are not protected by a passphrase and do not need to be unlocked before use. |
| 13 | +This is ideal for tools like Bootleg which lack a user interface. |
| 14 | + |
| 15 | +When defining your roles and hosts, simply add an `identity` option pointing to your private SSH key. |
| 16 | + |
| 17 | +```elixir |
| 18 | +role :app, "example.com", identity: "~/.ssh/id_rsa" |
| 19 | +``` |
| 20 | + |
| 21 | +The SSH private key will be used in remote builds (Git push) and for execution of remote commands. |
| 22 | + |
| 23 | +## Passphrase-protected private keys |
| 24 | + |
| 25 | +Private keys that are protected by a passphrase need to be unlocked before use. This is natively |
| 26 | +supported by the `ssh_client_key_api` package using Bootleg's `passphrase` or `passphrase_provider` options. |
| 27 | + |
| 28 | +However, the remote build scenario uses a Git push, which as an external process **does not** work seamlessly |
| 29 | +with the aforementioned Bootleg options. See "[Remote builds](#remote-builds)" below for solutions. |
| 30 | + |
| 31 | +### Options for protected private keys |
| 32 | + |
| 33 | +#### `passphrase` |
| 34 | + |
| 35 | +When configuring your role, set the `passphrase` option to the string that unlocks your private key. |
| 36 | + |
| 37 | +#### `passphrase_provider` |
| 38 | + |
| 39 | +Instead of setting a `passphrase`, you may set `passphrase_provider` to something that returns the string to unlock your private key. When using a provider, the returned value is then set as the `passphrase` option at time of `SSH.init/3`. |
| 40 | + |
| 41 | +##### Anonymous function |
| 42 | + |
| 43 | +```elixir |
| 44 | +role(:app, "example.com", identity: "protected_id_rsa", passphrase_provider: fn -> "foobar" end) |
| 45 | +``` |
| 46 | + |
| 47 | +##### Module and function reference |
| 48 | + |
| 49 | +```elixir |
| 50 | +defmodule Test.Foo do |
| 51 | + def bar do |
| 52 | + "foobar" |
| 53 | + end |
| 54 | +end |
| 55 | +role(:app, "example.com", identity: "protected_id_rsa", passphrase_provider: {Test.Foo, :bar}) |
| 56 | +``` |
| 57 | + |
| 58 | +##### System command and arguments |
| 59 | + |
| 60 | +```elixir |
| 61 | +role(:app, "example.com", identity: "protected_id_rsa", passphrase_provider: {"/bin/echo", ["foobar"]}) |
| 62 | +``` |
| 63 | + |
| 64 | +### Local builds |
| 65 | + |
| 66 | +When your build server is the same machine you're running Bootleg on, you may define the passphrase alongside the identity. |
| 67 | + |
| 68 | +```elixir |
| 69 | +role :app, "example.com", identity: "~/.ssh/protected_id_rsa", passphrase: "secretsauce" |
| 70 | +``` |
| 71 | + |
| 72 | +### Remote builds |
| 73 | + |
| 74 | +When your build server is another machine, the build process will attempt to do a Git push to it. |
| 75 | +This requires that you unlock your private key in one of two ways: |
| 76 | + |
| 77 | +#### Using the `insecure_agent` Bootleg role option (preferred) |
| 78 | + |
| 79 | +To use this option, set a passphrase options above, but also set `insecure_agent` on the role. |
| 80 | + |
| 81 | +During the build process, the passphrase will be temporarily written to the filesystem in order |
| 82 | +to unlock the key using `ssh-add`. This file is then removed immediately after the Git push operation. |
| 83 | + |
| 84 | +```elixir |
| 85 | +role :build, "example.com", identity: "~/.ssh/protected_id_rsa", passphrase: "secretsauce", insecure_agent: true |
| 86 | +``` |
| 87 | + |
| 88 | +#### Using `ssh-agent` (external to Bootleg) |
| 89 | + |
| 90 | +With ssh-agent, the Git push command will succeed but a passphrase is still needed for Bootleg to use your private key during execution of remote commands. |
| 91 | + |
| 92 | +```elixir |
| 93 | +role :build, "example.com", identity: "~/.ssh/protected_id_rsa", passphrase: "secretsauce" |
| 94 | +``` |
| 95 | + |
| 96 | +Here you would run `$ ssh-add ~/.ssh/protected_id_rsa` before invoking Bootleg to provide the passphrase that unlocks your private key. After specifying the correct passphrase your key is added to the SSH Agent and Git push operations will succeed as expected. |
| 97 | + |
| 98 | +Then run Bootleg as you would. |
0 commit comments