Commit 2b67968

mo khan <mo@mokhan.ca>
2015-02-10 03:40:06
protect from forgery and cache ip and hostname on fake agent.
1 parent 41c8858
app/controllers/api/agents/events_controller.rb
@@ -1,6 +1,6 @@
 module Api
   module Agents
-    class EventsController < ApplicationController
+    class EventsController < ApiController
       def create
         @agent = Agent.find(params[:agent_id])
         publish(EventMessage.new(
app/controllers/api/api_controller.rb
@@ -1,5 +1,10 @@
 module Api
-  class ApiController < ApplicationController
+  class ApiController < ActionController::Base
     protect_from_forgery with: :null_session
+    protected
+
+    def publish(message)
+      Publisher.publish(message)
+    end
   end
 end
app/controllers/application_controller.rb
@@ -1,7 +1,7 @@
 class ApplicationController < ActionController::Base
   # Prevent CSRF attacks by raising an exception.
   # For APIs, you may want to use :null_session instead.
-  #protect_from_forgery with: :exception
+  protect_from_forgery with: :exception
 
   protected
 
lib/fake_agent.rb
@@ -10,7 +10,7 @@ class FakeAgent
   end
 
   def register
-    response = Typhoeus.post(registration_url, body: { agent: { hostname: Socket.gethostname } })
+    response = Typhoeus.post(registration_url, body: { agent: { hostname: hostname } })
     json = JSON.parse(response.body)
     @id = json["id"]
   end
@@ -80,7 +80,7 @@ class FakeAgent
           data: {
             fingerprint: fingerprint_for(file),
             path: file,
-            hostname: Socket.gethostname,
+            hostname: hostname,
             ip_addresses: ip_addresses,
           }
         }
@@ -98,8 +98,12 @@ class FakeAgent
     sha
   end
 
+  def hostname
+    @hostname ||= Socket.gethostname
+  end
+
   def ip_addresses
-    Socket.ip_address_list.find_all { |x| x.ipv4? }.map { |x| x.ip_address }
+    @ipaddresses ||= Socket.ip_address_list.find_all { |x| x.ipv4? }.map { |x| x.ip_address }
   end
 
   def disposition_for(file)