add pubkey_verifier class to framework

this class provides a new way to do
public key only verification tests
for SSH

7321
bug/bundler_fix
David Maloney 2016-09-19 14:35:59 -05:00
parent 26491eed1a
commit 2f17ae0946
No known key found for this signature in database
GPG Key ID: DEDBA9DC3A913DB2
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
require 'net/ssh'
module Net
module SSH
class PubkeyVerifier
include Net::SSH::Transport::Constants
include Net::SSH::Authentication::Constants
attr_accessor :host, :key, :options, :user
def initialize(host,user, opts)
@host = host
# Parse Keyfile out into a PKey object
pubkey_file = opts.fetch(:pubkey_file)
@key = Net::SSH::KeyFactory.load_public_key(pubkey_file)
@user = user
# Always set auth methods to ONLY publickey regardless
# of what the user sends
opts[:auth_methods] = ['publickey']
@options = Net::SSH.configuration_for(host, opts.fetch(:config, true)).merge(opts)
end
def auth_session(transport)
Net::SSH::Authentication::Session.new(transport,options)
end
def ssh_transport
Net::SSH::Transport::Session.new(host,options)
end
def verify
transport = ssh_transport
auth = auth_session(transport)
transport.send_message(transport.service_request("ssh-userauth"))
auth.expect_message(SERVICE_ACCEPT)
# The initial public key exchange
pubkey_method = Net::SSH::Authentication::Methods::Publickey.new(auth)
pubkey_method.send(:send_request, key,user, "ssh-connection")
# Check the response to see if the public key is good
response_message = auth.next_message
case response_message.type
when USERAUTH_PK_OK
true
when USERAUTH_FAILURE
false
else
raise Net::SSH::Exception, "unexpected reply to USERAUTH_REQUEST: #{response_message.type} (#{response_message.inspect})"
end
end
end
end
end