allow credentials to provide the code

Andrew Cantino 10 years ago
parent
commit
62b60d1413
2 changed files with 49 additions and 4 deletions
  1. 20 3
      app/models/agents/java_script_agent.rb
  2. 29 1
      spec/models/agents/java_script_agent_spec.rb

+ 20 - 3
app/models/agents/java_script_agent.rb

@@ -21,11 +21,15 @@ module Agents
21 21
       * `this.options(key)`
22 22
       * `this.log(message)`
23 23
       * `this.error(message)`
24
-
25 24
     MD
26 25
 
27 26
     def validate_options
28
-      errors.add(:base, "The 'code' option is required") unless options['code'].present?
27
+      cred_name = credential_referenced_by_code
28
+      if cred_name
29
+        errors.add(:base, "The credential '#{cred_name}' referenced by code cannot be found") unless credential(cred_name).present?
30
+      else
31
+        errors.add(:base, "The 'code' option is required") unless options['code'].present?
32
+      end
29 33
     end
30 34
 
31 35
     def working?
@@ -99,10 +103,23 @@ module Agents
99 103
         end
100 104
       end
101 105
 
102
-      context.eval(options['code'])
106
+      context.eval(code)
103 107
       context.eval("Agent.#{js_function}();")
104 108
     end
105 109
 
110
+    def code
111
+      cred = credential_referenced_by_code
112
+      if cred
113
+        credential(cred) || 'Agent.check = function() { this.error("Unable to find credential"); };'
114
+      else
115
+        options['code']
116
+      end
117
+    end
118
+
119
+    def credential_referenced_by_code
120
+      options['code'] =~ /\Acredential:(.*)\Z/ && $1
121
+    end
122
+
106 123
     def setup_javascript
107 124
       <<-JS
108 125
         function Agent() {};

+ 29 - 1
spec/models/agents/java_script_agent_spec.rb

@@ -22,6 +22,14 @@ describe Agents::JavaScriptAgent do
22 22
       @agent.options.delete('code')
23 23
       @agent.should_not be_valid
24 24
     end
25
+
26
+    it "accepts a credential, but it must exist" do
27
+      @agent.should be_valid
28
+      @agent.options['code'] = 'credential:foo'
29
+      @agent.should_not be_valid
30
+      users(:jane).user_credentials.create! :credential_name => "foo", :credential_value => "bar"
31
+      @agent.reload.should be_valid
32
+    end
25 33
   end
26 34
 
27 35
   describe "#working?" do
@@ -55,7 +63,7 @@ describe Agents::JavaScriptAgent do
55 63
   describe "executing code" do
56 64
     it "works by default" do
57 65
       @agent.options = @agent.default_options
58
-      @agent.options['make_event'] = true;
66
+      @agent.options['make_event'] = true
59 67
       @agent.save!
60 68
 
61 69
       lambda {
@@ -66,6 +74,26 @@ describe Agents::JavaScriptAgent do
66 74
       }.should change { Event.count }.by(2)
67 75
     end
68 76
 
77
+
78
+    describe "using credentials as code" do
79
+      before do
80
+        @agent.user.user_credentials.create :credential_name => 'code-foo', :credential_value => 'Agent.check = function() { this.log("ran it"); };'
81
+        @agent.options['code'] = 'credential:code-foo'
82
+        @agent.save!
83
+      end
84
+
85
+      it "accepts credentials" do
86
+        @agent.check
87
+        AgentLog.last.message.should == "ran it"
88
+      end
89
+
90
+      it "logs an error when the credential goes away" do
91
+        @agent.user.user_credentials.delete_all
92
+        @agent.reload.check
93
+        AgentLog.last.message.should == "Unable to find credential"
94
+      end
95
+    end
96
+
69 97
     describe "error handling" do
70 98
       it "should log an error when V8 has issues" do
71 99
         @agent.options['code'] = 'syntax error!'