summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorVarac <varac@leap.se>2016-12-21 08:45:58 +0000
committerVarac <varac@leap.se>2016-12-21 08:45:58 +0000
commit4116559c43b36cf69dc128769cbae857c53f094f (patch)
tree09aa68188b268957fecfda1967b57614f1b315ee /lib
parent9dd91e45703e129838bdc491afceb183e4bbab02 (diff)
parente767aa460fc64a317551012f1781c2105c572158 (diff)
Merge branch 'feature/sshkey-info' into 'master'
feature: add troubleshooting info to `leap user ls` It is hard to get ssh key setup right. This change makes it much easier to debug what the problem is. See merge request !58
Diffstat (limited to 'lib')
-rw-r--r--lib/leap_cli/commands/user.rb25
-rw-r--r--lib/leap_cli/ssh/key.rb11
2 files changed, 31 insertions, 5 deletions
diff --git a/lib/leap_cli/commands/user.rb b/lib/leap_cli/commands/user.rb
index 1ca92719..a10d5163 100644
--- a/lib/leap_cli/commands/user.rb
+++ b/lib/leap_cli/commands/user.rb
@@ -113,6 +113,20 @@ module LeapCli
def do_list_users(global, options, args)
require 'leap_cli/ssh'
+ ssh_keys = {}
+ Dir.glob("#{ENV['HOME']}/.ssh/*.pub").each do |keyfile|
+ key = SSH::Key.load(keyfile)
+ ssh_keys[key.fingerprint] = key if key
+ end
+
+ ssh_agent_keys = {}
+ if !`which ssh-add`.empty?
+ `ssh-add -L`.split("\n").each do |keystring|
+ key = SSH::Key.load(keystring)
+ ssh_agent_keys[key.fingerprint] = key if key
+ end
+ end
+
Dir.glob(path([:user_ssh, '*'])).each do |keyfile|
username = File.basename(File.dirname(keyfile))
log username, :color => :cyan do
@@ -121,6 +135,14 @@ module LeapCli
log 'SSH MD5 fingerprint: ' + key.fingerprint(:digest => :md5, :type => :ssh, :encoding => :hex)
log 'SSH SHA256 fingerprint: ' + key.fingerprint(:digest => :sha256, :type => :ssh, :encoding => :base64)
log 'DER MD5 fingerprint: ' + key.fingerprint(:digest => :md5, :type => :der, :encoding => :hex)
+ if ssh_keys[key.fingerprint]
+ log 'Matches local key: ' + ssh_keys[key.fingerprint].filename, color: :green
+ if ssh_agent_keys[key.fingerprint]
+ log 'Matches ssh-agent key: ' + ssh_agent_keys[key.fingerprint].summary(encoding: :base64), color: :green
+ else
+ log :error, 'No matching key in the ssh-agent'
+ end
+ end
end
end
end
@@ -154,6 +176,9 @@ module LeapCli
end
else
key_index = 0
+ log "Picking the only compatible ssh key: "+ ssh_keys[key_index].filename do
+ log ssh_keys[key_index].summary
+ end
end
return ssh_keys[key_index]
diff --git a/lib/leap_cli/ssh/key.rb b/lib/leap_cli/ssh/key.rb
index 76223b7e..108b6137 100644
--- a/lib/leap_cli/ssh/key.rb
+++ b/lib/leap_cli/ssh/key.rb
@@ -254,9 +254,9 @@ module LeapCli
end
if digest == "MD5" && encoding == :hex
- return fp.scan(/../).join(':')
+ return fp.strip.scan(/../).join(':')
else
- return fp
+ return fp.strip
end
end
@@ -267,11 +267,12 @@ module LeapCli
Net::SSH::Buffer.from(:key, @key).to_s.split("\001\000").last.size * 8
end
- def summary
+ def summary(type: :ssh, digest: :sha256, encoding: :hex)
+ fp = digest.to_s.upcase + ":" + self.fingerprint(type: type, digest: digest, encoding: encoding)
if self.filename
- "%s %s %s (%s)" % [self.type, self.bits, self.fingerprint, File.basename(self.filename)]
+ "%s %s %s (%s)" % [self.type, self.bits, fp, File.basename(self.filename)]
else
- "%s %s %s" % [self.type, self.bits, self.fingerprint]
+ "%s %s %s" % [self.type, self.bits, fp]
end
end