def self.get_ssh_keys(libra_server, rhlogin, password, net_http)
data = {'rhlogin' => rhlogin, 'action' => 'list-keys'}
if @mydebug
data[:debug] = true
end
print_post_data(data)
json_data = generate_json(data)
url = URI.parse("https://#{libra_server}/broker/ssh_keys")
response = http_post(net_http, url, json_data, password)
unless response.code == '200'
if response.code == '401'
puts "Invalid user credentials"
exit 97
else
print_response_err(response)
end
exit 1
end
begin
json_resp = json_decode(response.body)
rescue RHC::JsonError
exit 1
end
update_server_api_v(json_resp)
begin
ssh_keys = (json_decode(json_resp['data'].to_s))
rescue RHC::JsonError
exit 1
end
begin
if ssh_keys['ssh_type'].nil? or ssh_keys['ssh_type'].empty?
ssh_keys['fingerprint'] = nil
else
ssh_keys['fingerprint'] = \
Net::SSH::KeyFactory.load_data_public_key(
"#{ssh_keys['ssh_type']} #{ssh_keys['ssh_key']}").fingerprint
end
rescue NoMethodError
tempfile = `mktemp /tmp/openshift.XXXXXXXX`
`echo "#{ssh_keys['ssh_type']} #{ssh_keys['ssh_key']}" > #{tempfile}`
ssh_keys['fingerprint'] = `ssh-keygen -lf #{tempfile}`.split(' ')[1]
rescue Net::SSH::Exception, NotImplementedError, OpenSSL::PKey::PKeyError
ssh_keys['fingerprint'] = 'Key type is not recognized. Please check this key is valid.'
end
if ssh_keys['keys'] && ssh_keys['keys'].kind_of?(Hash)
ssh_keys['keys'].each do |name, keyval|
type = keyval['type']
key = keyval['key']
begin
ssh_keys['keys'][name]['fingerprint'] = \
Net::SSH::KeyFactory.load_data_public_key(
"#{type} #{key}").fingerprint
rescue NoMethodError
tempfile = `mktemp /tmp/openshift.XXXXXXXX`
`echo "#{type} #{key}" > #{tempfile}`
ssh_keys['keys'][name]['fingerprint'] = `ssh-keygen -lf #{tempfile}`.split(' ')[1]
rescue NotImplementedError, Net::SSH::Exception, OpenSSL::PKey::PKeyError
ssh_keys['keys'][name]['fingerprint'] = 'Key type is not recognized. Please check this key is valid.'
end
end
end
ssh_keys
end