diff --git a/templates/commands/ssh_config/config.erb b/templates/commands/ssh_config/config.erb index 2cdb2dbf4..42b9a8f38 100644 --- a/templates/commands/ssh_config/config.erb +++ b/templates/commands/ssh_config/config.erb @@ -6,11 +6,7 @@ Host <%= host_key %> StrictHostKeyChecking no PasswordAuthentication no <% private_key_path.each do |path| %> -<% if path.include?(" ") -%> - IdentityFile "<%= path %>" -<% else -%> - IdentityFile <%= path %> -<% end -%> + IdentityFile <%= path.inspect %> <% end -%> IdentitiesOnly yes LogLevel FATAL diff --git a/test/unit/plugins/commands/ssh_config/command_test.rb b/test/unit/plugins/commands/ssh_config/command_test.rb index e458776ec..0922c9a7a 100644 --- a/test/unit/plugins/commands/ssh_config/command_test.rb +++ b/test/unit/plugins/commands/ssh_config/command_test.rb @@ -92,8 +92,8 @@ Host #{machine.name} subject.execute - expect(output).to include("IdentityFile foo") - expect(output).to include("IdentityFile bar") + expect(output).to include('IdentityFile "foo"') + expect(output).to include('IdentityFile "bar"') end it "puts quotes around an identityfile path if it has a space" do @@ -107,5 +107,17 @@ Host #{machine.name} expect(output).to include('IdentityFile "with a space"') end + + it "escapes special characters" do + allow(machine).to receive(:ssh_info) { ssh_info.merge(private_key_path: ['/private/tmp/test of "vagrant" plugin/']) } + output = "" + allow(subject).to receive(:safe_puts) do |data| + output += data if data + end + + subject.execute + + expect(output).to include('"/private/tmp/test of \"vagrant\" plugin/"') + end end end