Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use ssh agent if no password or key files have been specified #165

Merged
merged 1 commit into from
Nov 29, 2016

Conversation

alexpop
Copy link
Contributor

@alexpop alexpop commented Nov 21, 2016

If no password or private key is specified for ssh connections, try to use the ssh agent keys.

How it works:

[05:42:37 ~/git/train (ap/add-agent-login)]$ ssh-add
Identity added: /Users/apop/.ssh/id_rsa (/Users/apop/.ssh/id_rsa)

[05:42:50 ~/git/train (ap/add-agent-login)]$ bundle exec pry
[1] pry(main)> require 'train'
=> true
[2] pry(main)> train = Train.create('ssh', host: 'ap-cs6.opschef.tv', port: 22, user: 'root')
=> #<Train::Transports::SSH:0x007fcefa482c10
 @logger=#<Logger:0x007fcefa482b48 @default_formatter=#<Logger::Formatter:0x007fcefa482b20 @datetime_format=nil>, @formatter=nil, @level=0, @logdev=#<Logger::LogDevice:0x007fcefa482ad0 @dev=#<IO:<STDOUT>>, @filename=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007fcefa482a80>, @mon_owner=nil, @shift_age=nil, @shift_size=nil>, @progname=nil>,
 @options=
  {:host=>"ap-cs6.opschef.tv",
   :port=>22,
   :user=>"root",
   :shell=>false,
   :shell_options=>nil,
   :shell_command=>nil,
   :sudo=>false,
   :sudo_options=>nil,
   :sudo_password=>nil,
   :sudo_command=>nil,
   :key_files=>nil,
   :password=>nil,
   :keepalive=>true,
   :keepalive_interval=>60,
   :connection_timeout=>15,
   :connection_retries=>5,
   :connection_retry_sleep=>1,
   :max_wait_until_ready=>600,
   :compression=>false,
   :pty=>false,
   :compression_level=>0}>
[3] pry(main)> train.connection.run_command('hostname')
pick b6f42e5 try to use ssh agent if no password or key files have been specified Signed-off-by: Alex Pop <apop@chef.io>
D, [2016-11-21T17:44:48.843869 #9308] DEBUG -- : [SSH] Using Agent keys as no password or key file have been specified
...
D, [2016-11-21T17:44:49.549664 #9308] DEBUG -- : [SSH] root@ap-cs6.opschef.tv<{:user_known_hosts_file=>"/dev/null", :paranoid=>false, :port=>22, :compression=>false, :compression_level=>0, :keepalive=>true, :keepalive_interval=>60, :timeout=>15, :auth_methods=>["none", "publickey"], :keys_only=>nil, :keys=>nil, :password=>"<hidden>", :forward_agent=>nil, :user=>"root"}> (hostname)
=> #<struct Train::Extras::CommandResult stdout="ap-cs6.opschef.tv\n", stderr="", exit_status=0>

This enables tools like InSpec to run remote scans without having to link to a private key or use a password.

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great -- just one question

def ssh_known_identities
return @manager.known_identities if @manager
# Force KeyManager to load the key(s)
@manager = Net::SSH::Authentication::KeyManager.new(nil?).each_identity {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm interesting. Can you fill me in here -- what is nil?? It seems to resolve to false,

irb(main):004:0> nil?
=> false

or am I missing something?

@alexpop
Copy link
Contributor Author

alexpop commented Nov 22, 2016

Thanks Stephan!
You are right, should have been just nil there. Updated.

Copy link
Contributor

@arlimus arlimus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see this come in, thank you @alexpop !!
Just one comment

return @manager.known_identities if @manager
# Force KeyManager to load the key(s)
@manager = Net::SSH::Authentication::KeyManager.new(nil).each_identity {}
@manager.known_identities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 3-liner could be simplified to:

@manager ||= Net::SSH...blah
@manager.known_identities

DRY 😉

@alexpop
Copy link
Contributor Author

alexpop commented Nov 23, 2016

Nice one Dom, rebased and ready to go!

@alexpop
Copy link
Contributor Author

alexpop commented Nov 24, 2016

The Ubuntu suite failing in travis works locally:

$ SUITE="test:docker config=test-travis-ubuntu.yml"
[07:22:54 ~/git/train (ap/add-agent-login)]$

$ bundle exec rake $SUITE
sh -c cd /Users/apop/git/train/test/integration && ruby -I ../../lib docker_test.rb tests/*
--> schedule docker ubuntu:10.04
--> pull docker images ubuntu:10.04 (this may take a while)
... start pull image ubuntu:10.04
--> schedule docker ubuntu:12.04
--> schedule docker ubuntu:16.04
--> schedule docker ubuntu:16.10
--> pull docker images ubuntu:12.04 (this may take a while)
--> pull docker images ubuntu:16.04 (this may take a while)
--> schedule docker ubuntu:14.04
... start pull image ubuntu:12.04
--> pull docker images ubuntu:16.10 (this may take a while)
--> provision docker ubuntu:14.04
    script ./bootstrap.sh
--> start docker ubuntu:14.04
--> run test on docker 6f1b55532d5c0f22e8d74ac6540cfa2b77f53669f57c624100eccef7a5d86f52
Running tests:
- tests/path_block_device_test.rb
- tests/path_character_device_test.rb
- tests/path_file_test.rb
- tests/path_folder_test.rb
- tests/path_missing_test.rb
- tests/path_pipe_test.rb
- tests/path_symlink_test.rb
- tests/run_command_test.rb

Run options: --seed 15282

# Running:

...........--> pull docker images finished for ubuntu:12.04
--> provision docker ubuntu:12.04
... start pull image ubuntu:16.04
    script ./bootstrap.sh
.--> start docker ubuntu:12.04
.--> run test on docker 70111af617ac1e57a80b45da63fff131cfadf8ab056cff7287aedb428c3b7559
Running tests:
- tests/path_block_device_test.rb
- tests/path_character_device_test.rb
- tests/path_file_test.rb
- tests/path_folder_test.rb
- tests/path_missing_test.rb
- tests/path_pipe_test.rb
- tests/path_symlink_test.rb
- tests/run_command_test.rb

.Run options: --seed 11936

# Running:

--> pull docker images finished for ubuntu:10.04
... start pull image ubuntu:16.10
--> provision docker ubuntu:10.04
    script ./bootstrap.sh
--> start docker ubuntu:10.04
..--> run test on docker 953bb9812dd28f358d4751b693d124416a883eac512f123134c5438b07e27879
Running tests:
- tests/path_block_device_test.rb
- tests/path_character_device_test.rb
- tests/path_file_test.rb
- tests/path_folder_test.rb
- tests/path_missing_test.rb
- tests/path_pipe_test.rb
- tests/path_symlink_test.rb
- tests/run_command_test.rb

.Run options: --seed 52835

# Running:

..--> pull docker images finished for ubuntu:16.04
--> provision docker ubuntu:16.04
    script ./bootstrap.sh
..--> start docker ubuntu:16.04
--> run test on docker 2721043ca46f09504b2ba1b9f6c3ba731f90f2be96748e292d4faae23f5d737e
.Running tests:
- tests/path_block_device_test.rb
- tests/path_character_device_test.rb
- tests/path_file_test.rb
- tests/path_folder_test.rb
- tests/path_missing_test.rb
- tests/path_pipe_test.rb
- tests/path_symlink_test.rb
- tests/run_command_test.rb

.--> pull docker images finished for ubuntu:16.10
--> provision docker ubuntu:16.10
    script ./bootstrap.sh
.--> start docker ubuntu:16.10
--> run test on docker 53bb96b5b1e04b4e4d3d5f69a403dd52b57050b83c1fce4d4a15429860c9e46e
Running tests:
- tests/path_block_device_test.rb
- tests/path_character_device_test.rb
- tests/path_file_test.rb
- tests/path_folder_test.rb
- tests/path_missing_test.rb
- tests/path_pipe_test.rb
- tests/path_symlink_test.rb
- tests/run_command_test.rb

Run options: --seed 27687

# Running:

...Run options: --seed 28730

# Running:

...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Finished in 373.014210s, 0.3244 runs/s, 0.3485 assertions/s.

121 runs, 130 assertions, 0 failures, 0 errors, 0 skips
--> killrm docker 6f1b55532d5c0f22e8d74ac6540cfa2b77f53669f57c624100eccef7a5d86f52
..........................................................

Finished in 391.802122s, 0.3088 runs/s, 0.3318 assertions/s.

121 runs, 130 assertions, 0 failures, 0 errors, 0 skips
--> killrm docker 70111af617ac1e57a80b45da63fff131cfadf8ab056cff7287aedb428c3b7559
......

Finished in 389.114266s, 0.3110 runs/s, 0.3341 assertions/s.

121 runs, 130 assertions, 0 failures, 0 errors, 0 skips
--> killrm docker 953bb9812dd28f358d4751b693d124416a883eac512f123134c5438b07e27879
......

Finished in 383.685920s, 0.3154 runs/s, 0.3388 assertions/s.

121 runs, 130 assertions, 0 failures, 0 errors, 0 skips
--> killrm docker 2721043ca46f09504b2ba1b9f6c3ba731f90f2be96748e292d4faae23f5d737e
.

Finished in 381.146294s, 0.3175 runs/s, 0.3411 assertions/s.

121 runs, 130 assertions, 0 failures, 0 errors, 0 skips
--> killrm docker 53bb96b5b1e04b4e4d3d5f69a403dd52b57050b83c1fce4d4a15429860c9e46e

[07:30:01 ~/git/train (ap/add-agent-login)]$
$ echo $?
0

@chris-rock chris-rock merged commit 7cca326 into master Nov 29, 2016
@chris-rock chris-rock deleted the ap/add-agent-login branch November 29, 2016 10:50
@chris-rock
Copy link
Contributor

Awesome work @alexpop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants