|
| 1 | +ruleset io.picolabs.policy { |
| 2 | + meta { |
| 3 | + shares __testing, policies, policy, event_policy, query_policy |
| 4 | + , channels_using_policy, channels_using_policy_count, ui |
| 5 | + } |
| 6 | + global { |
| 7 | + __testing = { |
| 8 | + "queries": [ { "name": "__testing" } |
| 9 | + , { "name": "policies" } |
| 10 | + , { "name": "policy", "args": [ "id" ] } |
| 11 | + , { "name": "event_policy", "args": [ "id" ] } |
| 12 | + , { "name": "query_policy", "args": [ "id" ] } |
| 13 | + , { "name": "channels_using_policy", "args": [ "id" ] } |
| 14 | + , { "name": "channels_using_policy_count", "args": [ "id" ] } |
| 15 | + ] |
| 16 | + , |
| 17 | + "events": [ { "domain": "policy", "type": "wish_to_deny", "attrs": [ "domain", "type" ] } |
| 18 | + ] |
| 19 | + } |
| 20 | + policies = engine:listPolicies().map(function(p){p{"id"}}).values() |
| 21 | + policy = function(id) { |
| 22 | + engine:listPolicies().filter(function(p){p{"id"}==id}).head() |
| 23 | + } |
| 24 | + event_policy = function(id) { |
| 25 | + policy(id){"event"} |
| 26 | + } |
| 27 | + query_policy = function(id) { |
| 28 | + policy(id){"query"} |
| 29 | + } |
| 30 | + channels_using_policy = function(id) { |
| 31 | + engine:listChannels().filter(function(c){c{"policy_id"}==id}) |
| 32 | + } |
| 33 | + channels_using_policy_count = function(id) { |
| 34 | + channels_using_policy(id).length() |
| 35 | + } |
| 36 | + ui = function() { |
| 37 | + all = engine:listPolicies() |
| 38 | + .collect(function(v){v{"id"}}) |
| 39 | + .map(function(v){v.head()}) |
| 40 | + .map(function(v){v.put("event",v{"event"}.encode())}) |
| 41 | + .map(function(v){v.put("query",v{"query"}.encode())}); |
| 42 | + {"used":all.filter(function(v,k){channels_using_policy_count(k)}), |
| 43 | + "unused":all.filter(function(v,k){channels_using_policy_count(k)==0})} |
| 44 | + } |
| 45 | + } |
| 46 | + rule make_deny_policy { |
| 47 | + select when policy wish_to_deny |
| 48 | + pre { |
| 49 | + domain = event:attr("domain").klog("domain"); |
| 50 | + type = event:attr("type").klog("type"); |
| 51 | + } |
| 52 | + engine:newPolicy({ |
| 53 | + "name": <<blacklist #{domain}:#{type}>>, |
| 54 | + "event": { |
| 55 | + "allow": [{}], //allow any |
| 56 | + "deny": [{ "domain": domain, "type": type }] |
| 57 | + } |
| 58 | + }) setting(policy) |
| 59 | + fired { |
| 60 | + ent:policies{policy{"id"}} := policy.klog("policy") |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments