lib/onering/api.rb in onering-client-0.0.44 vs lib/onering/api.rb in onering-client-0.0.45
- old
+ new
@@ -76,77 +76,82 @@
rescue Errors::ClientPemNotFound
# client PEM not present, attempt autoregistration
STDERR.puts("Onering client.pem not found, attempting automatic registration...")
- @_validation = ([options[:validationfile], @_config['validationfile']]+DEFAULT_VALIDATION_PEM).compact.select{|i|
- (File.exists?((File.expand_path(i) rescue i)) rescue nil)
- }.compact.first
+ begin
+ @_validation = ([options[:validationfile], @_config['validationfile']]+DEFAULT_VALIDATION_PEM).compact.select{|i|
+ (File.exists?((File.expand_path(i) rescue i)) rescue nil)
+ }.compact.first
- if @_validation.nil?
- raise Errors::ClientError.new("Cannot automatically register client, cannot find validation.pem")
- end
+ if @_validation.nil?
+ raise Errors::ClientError.new("Cannot automatically register client, cannot find validation.pem")
+ end
- @_validation = File.read(@_validation)
+ @_validation = File.read(@_validation)
- @rest = RestClient::Resource.new("#{@_uri.scheme}://#{@_uri.host}:#{@_uri.port || 443}", {
- :timeout => 120,
- :open_timeout => 30,
- :ssl_client_cert => OpenSSL::X509::Certificate.new(@_validation),
- :ssl_client_key => OpenSSL::PKey::RSA.new(@_validation),
- :verify_peer => OpenSSL::SSL::VERIFY_PEER
- })
+ @rest = RestClient::Resource.new("#{@_uri.scheme}://#{@_uri.host}:#{@_uri.port || 443}", {
+ :timeout => 120,
+ :open_timeout => 30,
+ :ssl_client_cert => OpenSSL::X509::Certificate.new(@_validation),
+ :ssl_client_key => OpenSSL::PKey::RSA.new(@_validation),
+ :verify_peer => OpenSSL::SSL::VERIFY_PEER
+ })
- clients = [{
- :path => "/etc/onering",
- :name => (@_config['id'] || File.read("/etc/hardware.id")).strip.chomp,
- :keyname => 'system',
- :autodelete => true
- },{
- :path => "~/.onering",
- :name => ENV['USER'],
- :keyname => 'cli',
- :autodelete => false
- }]
+ clients = [{
+ :path => "/etc/onering",
+ :name => (@_config['id'] || File.read("/etc/hardware.id")).strip.chomp,
+ :keyname => 'system',
+ :autodelete => true
+ },{
+ :path => "~/.onering",
+ :name => ENV['USER'],
+ :keyname => 'cli',
+ :autodelete => false
+ }]
- # attempt to autoregister clients from least specific to most (machine account then user account)
- clients.each do |client|
- # determine if we can create this client
- client[:path] = (File.expand_path(client[:path]) rescue client[:path])
- next unless File.writable?(File.dirname(client[:path]))
- Dir.mkdir(client[:path]) unless File.directory?(client[:path])
- next unless File.writable?(client[:path])
+ # attempt to autoregister clients from least specific to most (machine account then user account)
+ clients.each do |client|
+ # determine if we can create this client
+ client[:path] = (File.expand_path(client[:path]) rescue client[:path])
+ next unless File.writable?(File.dirname(client[:path]))
+ Dir.mkdir(client[:path]) unless File.directory?(client[:path])
+ next unless File.writable?(client[:path])
- begin
- response = @rest["/api/users/#{client[:name]}/keys/#{client[:keyname]}"].get({
- :params => {
- :cert => 'pem',
- :autodelete => client[:autodelete]
- }
- })
+ begin
+ response = @rest["/api/users/#{client[:name]}/keys/#{client[:keyname]}"].get({
+ :params => {
+ :cert => 'pem',
+ :autodelete => client[:autodelete]
+ }
+ })
- rescue RestClient::Forbidden
- STDERR.puts("Cannot re-download key '#{client[:keyname]}' for client #{client[:name]}. Please remove the client key from Onering and try again.")
- next
+ rescue RestClient::Forbidden
+ STDERR.puts("Cannot re-download key '#{client[:keyname]}' for client #{client[:name]}. Please remove the client key from Onering and try again.")
+ next
- rescue RestClient::Exception => e
- raise Errors::ClientError.new("HTTP #{e.http_code}: #{e.message}")
- end
+ rescue RestClient::Exception => e
+ raise Errors::ClientError.new("HTTP #{e.http_code}: #{e.message}")
+ end
- File.open("#{client[:path]}/client.pem", "w") do |file|
- file.puts(response.to_str)
- STDERR.puts("Successfully registered client key #{client[:name]}:#{client[:keyname]}, key is at #{file.path}")
- break
+ File.open("#{client[:path]}/client.pem", "w") do |file|
+ file.puts(response.to_str)
+ STDERR.puts("Successfully registered client key #{client[:name]}:#{client[:keyname]}, key is at #{file.path}")
+ break
+ end
end
- end
- if clients.select{|i| p = "#{i[:path]}/client.pem"; File.exists?((File.expand_path(p) rescue p)) }.empty?
- raise Errors::ClientError.new("Unable to register a Onering client.")
- end
+ if clients.select{|i| p = "#{i[:path]}/client.pem"; File.exists?((File.expand_path(p) rescue p)) }.empty?
+ raise Errors::ClientError.new("Unable to register a Onering client.")
+ end
- retry
+ retry
+
+ rescue Exception => e
+ STDERR.puts("Error occurred during autoregistration: #{e.class.name} - #{e.message}")
+ end
end
else
raise Errors::ClientError.new("Could not parse API URL.")
end