diff --git a/CHANGELOG.md b/CHANGELOG.md index 75876e1df..18b79cc43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,7 @@ BUG FIXES: - providers/docker: Fix issue where multiple identical proxy VMs would be created. [GH-3963] - providers/docker: Multiple links with the same name work. [GH-4571] + - providers/docker: Add support of SSH agent forwarding. [GH-4905] - providers/virtualbox: Show a human-friendly error if VirtualBox didn't clean up an existing VM. [GH-4681] - providers/virtualbox: Detect case when VirtualBox reports 0.0.0.0 as diff --git a/plugins/providers/docker/action/prepare_ssh.rb b/plugins/providers/docker/action/prepare_ssh.rb index 1a7298a66..450b139c6 100644 --- a/plugins/providers/docker/action/prepare_ssh.rb +++ b/plugins/providers/docker/action/prepare_ssh.rb @@ -19,14 +19,19 @@ module VagrantPlugins # Modify the SSH options for when we `vagrant ssh`... ssh_opts = env[:ssh_opts] || {} - # Build the command we'll execute within the host machine + # Build the command we'll execute within the Docker host machine: ssh_command = env[:machine].communicate.container_ssh_command if !Array(ssh_opts[:extra_args]).empty? ssh_command << " #{Array(ssh_opts[:extra_args]).join(" ")}" end + # Modify the SSH options for the original command: # Append "-t" to force a TTY allocation ssh_opts[:extra_args] = ["-t"] + # Enable Agent forwarding when requested for the target VM + if env[:machine].ssh_info[:forward_agent] + ssh_opts[:extra_args] << "-o ForwardAgent=yes" + end ssh_opts[:extra_args] << ssh_command # Set the opts diff --git a/plugins/providers/docker/communicator.rb b/plugins/providers/docker/communicator.rb index 657789575..fb6c9fe9e 100644 --- a/plugins/providers/docker/communicator.rb +++ b/plugins/providers/docker/communicator.rb @@ -137,18 +137,21 @@ module VagrantPlugins info[:port] ||= 22 # Make sure our private keys are synced over to the host VM - key_args = sync_private_keys(info).map do |path| + ssh_args = sync_private_keys(info).map do |path| "-i #{path}" - end.join(" ") + end + + # Use ad-hoc SSH options for the hop on the docker proxy + if info[:forward_agent] + ssh_args << "-o ForwardAgent=yes" + end + ssh_args.concat(["-o Compression=yes", + "-o ConnectTimeout=5", + "-o StrictHostKeyChecking=no", + "-o UserKnownHostsFile=/dev/null"]) # Build the SSH command - "ssh #{key_args} " + - "-o Compression=yes " + - "-o ConnectTimeout=5 " + - "-o StrictHostKeyChecking=no " + - "-o UserKnownHostsFile=/dev/null " + - "-p#{info[:port]} " + - "#{info[:username]}@#{info[:host]}" + "ssh #{info[:username]}@#{info[:host]} -p#{info[:port]} #{ssh_args.join(" ")}" end protected