lib/onering/api.rb in onering-client-0.0.83 vs lib/onering/api.rb in onering-client-0.0.84
- old
+ new
@@ -81,17 +81,22 @@
end
end
# arm the hack
TCPSocket._hack_local_ip = options.get('config.source')
+
+ # sound the siren
+ Onering::Logger.info("Using local interface #{options.get('config.source')} to connect", "Onering::API")
+
else
raise "Invalid source IP address #{options.get('config.source')}"
end
end
# set API connectivity details
Onering::API.base_uri(options.get('config.url', Onering::Config.get(:url, DEFAULT_BASE)))
+ Onering::Logger.info("Server URL is #{Onering::API.base_uri}", "Onering::API")
# add default parameters
options.get('config.params',{}).each do |k,v|
_default_param(k,v)
end
@@ -102,17 +107,24 @@
def connect(options={})
# setup authentication
_setup_auth()
+ Onering::Logger.debug("Connection setup complete", "Onering::API")
return self
end
def request(method, endpoint, options={})
endpoint = [Onering::Config.get(:path, DEFAULT_PATH).strip, endpoint.sub(/^\//,'')].join('/')
+ Onering::Logger.debug("#{method.to_s.upcase} #{endpoint}#{(options[:query] || {}).empty? ? '' : '?'+options[:query].join('=', '&')}", "Onering::API")
+ options.get(:headers,[]).each do |name, value|
+ next if name == 'Content-Type' and value == 'application/json'
+ Onering::Logger.debug("+#{name}: #{value}", "Onering::API")
+ end
+
case (method.to_sym rescue method)
when :post
rv = Onering::API.post(endpoint, options)
when :put
rv = Onering::API.put(endpoint, options)
@@ -214,23 +226,31 @@
end
# -----------------------------------------------------------------------------
def _setup_auth_ssl()
begin
+ Onering::Logger.info("Using SSL authentication mechanism", "Onering::API")
+
# get first keyfile found
key = (([Onering::Config.get('authentication.keyfile')] + DEFAULT_CLIENT_PEM).compact.select{|i|
- (File.exists?(File.expand_path(i)) rescue false)
+ rv = (File.readable?(File.expand_path(i)) rescue false)
+ Onering::Logger.debug("SSL keyfile found at #{File.expand_path(i)}", "Onering::API") if rv === true
+ rv
}).first
# SSL client key not found, attempt autoregistration...
if key.nil?
if Onering::Config.get('authentication.autoregister', true)
+ Onering::Logger.warn("SSL keyfile not found, attempting to autoregister client", "Onering::API")
+
validation_key = Onering::Config.get('authentication.validation_keyfile', DEFAULT_VALIDATION_PEM)
validation_key = (File.expand_path(validation_key) rescue validation_key)
# if validation key exists, autoregister
if File.size?(validation_key)
+ Onering::Logger.debug("Using validation key at #{validation_key}", "Onering::API")
+
# set the authentication PEM to validation.pem
Onering::API.pem(File.read(validation_key))
# attempt to create client.pem from least-specific to most, first writable path wins
clients = [{
@@ -255,19 +275,20 @@
next unless File.writable?(client[:path])
Dir.mkdir(client[:path]) unless File.directory?(client[:path])
next if File.exists?(keyfile)
# attempt to create/download the keyfile
+ Onering::Logger.debug("Requesting SSL keyfile as client #{client[:name].strip}, key #{client[:keyname]}", "Onering::API")
response = self.class.get("/api/users/#{client[:name].strip}/keys/#{client[:keyname]}")
# if successful, write the file
if response.code < 400 and response.body
File.open(keyfile, 'w').puts(response.body)
raise Actions::Retry.new
else
# all errors are fatal at this stage
- raise Errors::ClientError.new("Cannot autoregister client: HTTP #{response.code} - #{(response.parsed_response || {}).get('error.message', 'Unknown error')}")
+ Onering::Logger.fatal("Cannot autoregister client: HTTP #{response.code} - #{(response.parsed_response || {}).get('error.message', 'Unknown error')}", "Onering::API")
end
end
# it is an error to not have created a client.pem by now
raise Errors::AuthenticationMissing.new("Cannot autoregister client: keyfile not created")
@@ -279,19 +300,22 @@
else
raise Errors::AuthenticationMissing.new("Cannot find SSL key and autoregistration is disabled")
end
else
Onering::API.pem(File.read((File.expand_path(key) rescue key)))
+ Onering::Logger.debug("Using SSL keyfile #{File.expand_path(key) rescue key}", "Onering::API")
end
rescue Actions::Retry
retry
end
end
# -----------------------------------------------------------------------------
def _setup_auth_token()
+ Onering::Logger.info("Using token authentication mechanism", "Onering::API")
+
# get first keyfile found
key = Onering::Config.get('authentication.key')
raise Errors::AuthenticationMissing.new("Cannot find an API token") if key.nil?
# set auth mechanism
\ No newline at end of file