# changing Notice::policy mechanism

**URL:** <https://community.zeek.org/t/changing-notice-policy-mechanism/2478>\
**Category:** Development\
**Tags:** development\
**Created:** [November 1, 2012, 8:35pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478 "2012-11-01T20:35:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 1, 2012, 8:35pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/1 "2012-11-01T20:35:17Z")

</div>

I've discussed changing the Notice::policy notice handling mechanism with a few people and I think that generally everyone agrees that the current mechanism, while very powerful, sucks from a usability perspective. This is a good thing to address now since we're either on the precipice of repeating a mistake to more frameworks or adapting to a style that is easier to understand. I'm going to give an example of how I think it could work in an evented model now. Please shoot holes. 🙂

These implement the decidedly silly case of sending a notice to an email if the SSH connection originated locally.

event Notice::policy(n: Notice::Info)  
&nbsp;&nbsp;{  
&nbsp;&nbsp;if ( n$note == SSH::Login &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Site::is\_local\_host(n$id$orig\_h) )  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;add n$actions[Notice::ACTION\_EMAIL];  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}

That would be replacement to the current model of this:

redef Notice::policy += {  
&nbsp;&nbsp;[$pred(n: Notice::Info) = {  
&nbsp;&nbsp;&nbsp;&nbsp;return ( n$note == SSH::Login && Site::is\_local\_host(n$id$orig\_h) );  
&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;$action = Notice::ACTION\_EMAIL],  
};

Here are some random thoughts about these two approaches in no particular order:

- The evented model (top one) is more Bro-y and easier which is a BIG plus.

- The PolicyItem model (bottom one) has the ability to halt further processing with the $halt attribute of PolicyItems. I don't think I'm convinced that this is a huge issue.

- The evented model has latency from the event queue, but I don't think this is a huge issue. The latency is normally ok. Jon, is it an issue for the file analysis framework? I don't remember. The actions being applied would be processed through an event queue too so they will be processed after the policy events anyway.

- Code block prioritization is built into the evented model using the &priority attribute. It's specifically implemented for PolicyItem model.

Any thoughts?

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![Daniel\_Thayer](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@Daniel\_Thayer](https://community.zeek.org/u/Daniel_Thayer)\
**Post date:** [November 1, 2012, 9:01pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/2 "2012-11-01T21:01:40Z")

</div>

I like this idea (it seems MUCH easier to use). How would  
"suppress\_for" be implemented in the evented model?

---

<div class="post-metadata">

**Author:** ![Siwek\_Jon](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Siwek\_Jon](https://community.zeek.org/u/Siwek_Jon)\
**Post date:** [November 1, 2012, 9:47pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/3 "2012-11-01T21:47:39Z")

</div>

> - The PolicyItem model (bottom one) has the ability to halt further processing with the $halt attribute of PolicyItems. I don't think I'm convinced that this is a huge issue.

I think the same thing could be done with the event-model -- if Notice::Info just had a boolean field that each event handler body could check and then bail out early if necessary. And that field could even be something left up to a user to define in a redef.

> - The evented model has latency from the event queue, but I don't think this is a huge issue. The latency is normally ok. Jon, is it an issue for the file analysis framework?

I think the latency is a problem in that case since immediate feedback is helpful if certain actions are to work without buffering incoming file data. E.g. if the only thing I want to happen is for files to be extracted to disk, then I wouldn't expect that to require buffering.

It may be workable, but I think the latency does make things more complicated so it might not make sense to re-use this policy model for the file analysis framework, but that shouldn't be a showstopper for using it for notices. I think it's fine to have the policy models be inconsistent, as long as both are easy to understand and use for their purpose.

&nbsp;&nbsp;&nbsp;&nbsp;Jon

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 2:07am UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/4 "2012-11-02T02:07:13Z")

</div>

Suppression is implemented at a pretty low level in the notice framework. That Notice::policy event would never be generated for suppressed notices (just like the notice policy variable currently isn't evaluated for suppressed notices).

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 2:28am UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/5 "2012-11-02T02:28:38Z")

</div>

As we discussed earlier and I never brought up in my overview, would it be better if we had some mechanism sitting between events and functions? "Synchronous events"? "Multi-bodied functions"? Here's an example of how a synchronous event might look at the script land…

global awesome: event(a: count) &synchronous;  
global foobar: event(a: count);

event bro\_init()  
&nbsp;&nbsp;{  
&nbsp;&nbsp;event foobar(1);  
&nbsp;&nbsp;event awesome(5);  
&nbsp;&nbsp;}

event awesome(a: count) &priority=5  
&nbsp;&nbsp;{  
&nbsp;&nbsp;print fmt("awesome: %d", a+5);  
&nbsp;&nbsp;}

event awesome(a: count) &priority=2  
&nbsp;&nbsp;{  
&nbsp;&nbsp;print fmt("awesome: %d", a+2);  
&nbsp;&nbsp;}

event foobar(a: count)  
&nbsp;&nbsp;{  
&nbsp;&nbsp;print "foobar handler!";  
&nbsp;&nbsp;}

Assuming synchronous events existed, the output would be this:

awesome: 10  
awesome: 7  
foobar handler!

I would really kind of like to move away from this policy model. The more I think about it, the less Bro-y it feels and I have trouble remembering how exactly to use them too (which I don't think bodes well for anyone!). Focusing on simplicity and consistency at the script layer is something that we'll probably have to continue to struggle and fight for forever. Although, I'm generally ok with complexity anywhere in Bro as long as we find a clear and obvious way to expose it to users.

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 2:44am UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/6 "2012-11-02T02:44:45Z")

</div>

Aaand, to reply to myself… Now that I implemented that sample code, I don't like it. Events presume the event queue. If we bypass that, it just seems bad and inconsistent. Maybe we just need a mechanism for multi-bodied functions? It would basically be functions that could be defined over and over, could have a &priority attribute applied to them, and would be enforced to have no return value.

global foobar: function(a: count) &multibody;

function foobar(a: count) &priority=5  
&nbsp;&nbsp;{  
&nbsp;&nbsp;print "foobar at priority 5";  
&nbsp;&nbsp;}

function foobar(a: count) &priority=8  
&nbsp;&nbsp;{  
&nbsp;&nbsp;print "foobar at priority 8";  
&nbsp;&nbsp;}

print foobar(1); // \<-- Maybe it could return a count to indicate number of bodies executed?

Sample output

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 3:11am UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/7 "2012-11-02T03:11:29Z")

</div>

One more reply to myself. 🙂

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 3:19am UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/8 "2012-11-02T03:19:12Z")

</div>

Dammit.

> There would be some work that would need to be done to deal with edge cases brought about by smashing scopes together like this, but I think it should be possible to watch for locals being created and making sure that there aren't any conflicting names (Bro should probably not start up if there are conflicting names).

Actually, nevermind. I'm not fully thinking through the scope smashing. We could just create a scope for each of the function implementations…

> > function foobar(a: count) &priority=5  
> > &nbsp;&nbsp;{  
> > &nbsp;&nbsp;local test = "nope";  
> > &nbsp;&nbsp;print test;  
> > &nbsp;&nbsp;}
> > 
> > function foobar(a: count) &priority=8  
> > &nbsp;&nbsp;{  
> > &nbsp;&nbsp;local test = "yup";  
> > &nbsp;&nbsp;print test;  
> > &nbsp;&nbsp;}

Those would inline into this…

function foobar(a: count)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;local test = "nope";  
&nbsp;&nbsp;print test;  
&nbsp;&nbsp;}  
&nbsp;&nbsp;{  
&nbsp;&nbsp;local test = "yup";  
&nbsp;&nbsp;print test;  
&nbsp;&nbsp;}  
&nbsp;&nbsp;}

The only problem is that this doesn't currently work, but it begs the question… should it?

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![Siwek\_Jon](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Siwek\_Jon](https://community.zeek.org/u/Siwek_Jon)\
**Post date:** [November 2, 2012, 4:06pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/9 "2012-11-02T16:06:14Z")

</div>

> > > function foobar(a: count) &priority=5  
> > > &nbsp;&nbsp;{  
> > > &nbsp;&nbsp;local test = "nope";  
> > > &nbsp;&nbsp;print test;  
> > > &nbsp;&nbsp;}
> > > 
> > > function foobar(a: count) &priority=8  
> > > &nbsp;&nbsp;{  
> > > &nbsp;&nbsp;local test = "yup";  
> > > &nbsp;&nbsp;print test;  
> > > &nbsp;&nbsp;}

Adding the &multibody attribute to do this I think might work alright. And I think it's not much to implement it -- I just did some quick hacks as a proof of concept and so far it didn't look like a problem.

> Those would inline into this…
> 
> function foobar(a: count)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;local test = "nope";  
> &nbsp;&nbsp;print test;  
> &nbsp;&nbsp;}  
> &nbsp;&nbsp;{  
> &nbsp;&nbsp;local test = "yup";  
> &nbsp;&nbsp;print test;  
> &nbsp;&nbsp;}  
> &nbsp;&nbsp;}
> 
> The only problem is that this doesn't currently work, but it begs the question… should it?

That probably should work, but it's also not related to implementing the proposed &multibody attribute for functions since the bodies are already parsed separately with different scopes and can then be internally stored as separate bodies/scopes, there's no need to do the mashing together under a common scope -- just keep them separate and iterate over the bodies according to priority.

This code already exists because of the similarity of functions and events, it's just that functions are only allowed multiple body definitions if they have &redef, and in that case, the later definition \*replaces\* earlier ones instead of being pushed on to the vector of bodies.

&nbsp;&nbsp;&nbsp;&nbsp;Jon

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 4:20pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/10 "2012-11-02T16:20:10Z")

</div>

Ahh, that makes sense.

I actually thought about it a bit more and I'm still not completely attached to the idea. I sort of don't like how the documentation for that would look where we have to say that normally you shouldn't implement a function over and over, but in special cases it's just how something works.

I almost wonder if we should have another type of code block in addition to events and functions. It's really just a difference in semantics at this point, but it would be much easier to document if there was a completely different name. We could document the use and behavior of each of them very clearly with the third name.

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![robin](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/robin/32/599_2.png) [@robin](https://community.zeek.org/u/robin)\
**Post date:** [November 2, 2012, 4:38pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/11 "2012-11-02T16:38:59Z")

</div>

Can't say I like the multi-body functions; it's an unusual concept and  
they'd come with untuitive limitations (like the no return value  
constraint). Or put differently, I believe what you call multi-body  
functions here are actually just the synchronous events you proposed  
first. 🙂

Let me try to structure this a bit. The current policy approach gives  
us four things I believe (let me know if I'm missing something):

&nbsp;&nbsp;&nbsp;&nbsp;(1) Immediate execution to completion (that's the synchronous).

&nbsp;&nbsp;&nbsp;&nbsp;(2) The ability for an item to prevent others from being considered.

&nbsp;&nbsp;&nbsp;&nbsp;(3) Returning values back to the code running executing the policy  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(actions, suppress\_for, etc.)

&nbsp;&nbsp;&nbsp;&nbsp;(4) Items sorted by priority.

Events currently only provide (4), but I can see changing them to  
do (1) and (2):

&nbsp;&nbsp;&nbsp;&nbsp;- (1): That's the "synchronous" part. There's actually nothing  
&nbsp;&nbsp;&nbsp;&nbsp;internally that would prevent us from executing event handlers  
&nbsp;&nbsp;&nbsp;&nbsp;immediately, rather than queuing them first. In some sense, they  
&nbsp;&nbsp;&nbsp;&nbsp;would jump to the head of the queue and be processed immediately.  
&nbsp;&nbsp;&nbsp;&nbsp;We could indeed introduce a new attribute for such events, though  
&nbsp;&nbsp;&nbsp;&nbsp;I'm not sure I like &synchronous (but no better idea right now).

&nbsp;&nbsp;&nbsp;&nbsp;This would vioalate the current FIFO order of events, but with  
&nbsp;&nbsp;&nbsp;&nbsp;explicit marking, I think that'd be ok.

&nbsp;&nbsp;&nbsp;&nbsp;- (2): Jon suggested a boolean flag but I don't like that because  
&nbsp;&nbsp;&nbsp;&nbsp;now each handler would need check for it, which is easy to forget  
&nbsp;&nbsp;&nbsp;&nbsp;and generally repetetive. Another idea for this has been floating  
&nbsp;&nbsp;&nbsp;&nbsp;around already: allow event handlers to stop execution of further  
&nbsp;&nbsp;&nbsp;&nbsp;handlers of the same kind for an event. Something like:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event foo()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event stop; # No further handlers for this event.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

(3) is tricky though, not sure how to do that without return values?  
Question is: can we do without? We don't need it for actions (they can  
just turn into function calls), but what about suppress-for style  
stuff. Seth, you said that suppress\_for is not a problem either?

Robin

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 4:51pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/12 "2012-11-02T16:51:24Z")

</div>

> &nbsp;&nbsp;- (1): That's the "synchronous" part. There's actually nothing  
> &nbsp;&nbsp;&nbsp;internally that would prevent us from executing event handlers  
> &nbsp;&nbsp;&nbsp;immediately, rather than queuing them first. In some sense, they  
> &nbsp;&nbsp;&nbsp;would jump to the head of the queue and be processed immediately.  
> &nbsp;&nbsp;&nbsp;We could indeed introduce a new attribute for such events, though  
> &nbsp;&nbsp;&nbsp;I'm not sure I like &synchronous (but no better idea right now).

I don't like that attribute either.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event foo()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event stop; # No further handlers for this event.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

I actually don't like this. I like to be able to assume that all event handlers will execute and I can see users causing some major debugging headaches for us with it.

> (3) is tricky though, not sure how to do that without return values?  
> Question is: can we do without? We don't need it for actions (they can  
> just turn into function calls), but what about suppress-for style  
> stuff. Seth, you said that suppress\_for is not a problem either?

Correct, suppress\_for isn't a problem and I don't think we need (3) anyway.

What about my proposal I sent out a little bit ago? Make a third type of code block with all of the characteristics that we want? We don't have to compromise on existing and good designs for functions or events that way.

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![Siwek\_Jon](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Siwek\_Jon](https://community.zeek.org/u/Siwek_Jon)\
**Post date:** [November 2, 2012, 4:59pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/13 "2012-11-02T16:59:19Z")

</div>

> (3) is tricky though, not sure how to do that without return values?  
> Question is: can we do without?

I think so. People could design the "return value" as something expected as a side effect of the call ? e.g. the value of a record field could get modified in the bodies and then inspected after.

&nbsp;&nbsp;&nbsp;&nbsp;Jon

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 2, 2012, 7:09pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/14 "2012-11-02T19:09:17Z")

</div>

Continuing the discussion from the group call, I think that we're starting to converge on adding a "policy" keyword to the language to represent something between a function and an event. Jon and I chatted about it some more and we couldn't come to a more abstracted and still clear keyword than "policy". (I really like the policy keyword here)

As a concrete example of the existing policy mechanism in the notice framework and the reworked mechanism I'll give an equivalent implementation of both.

# New style under discussion.  
policy Notice::trigger(n: Notice::Info) &priority=5  
&nbsp;&nbsp;{  
&nbsp;&nbsp;if (n$note == Whatever && Site::is\_local\_host(n$conn$id$orig\_h)  
&nbsp;&nbsp;&nbsp;&nbsp;add n$actions[ACTION\_BLAH];  
&nbsp;&nbsp;}

# Existing style  
redef Notice::policy += {  
&nbsp;&nbsp;[$pred(n: Notice::Info) = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (n$note == Whatever && Site::is\_local\_host(n$conn$id$orig\_h);  
&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;$priority = 5,  
&nbsp;&nbsp;&nbsp;$action = ACTION\_BLAH]  
};

To address your various points about how this needs to work:

&nbsp;&nbsp;1. Immediate policy handler execution. Not even by jumping to the beginning of the  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event queue and waiting for the queue to be flushed, just direct execution.

&nbsp;&nbsp;2. Adding a "break"-like keyword to halt policy execution (only valid in policy handlers).

&nbsp;&nbsp;3. No return value(s) when calling policy handlers. We'll reuse the mutability mechanism  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that I subversively began using all over the place by modifying records in place.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Calling policy handlers would look like this:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy Notice::trigger(n);

&nbsp;&nbsp;4. Policy handlers sorted by priority. Equivalent priority has undefined execution order,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;but I don't see that as a problem since it's already a concern for event handlers.

.Seth

---

<div class="post-metadata">

**Author:** ![Vern](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/vern/32/630_2.png) [@Vern](https://community.zeek.org/u/Vern)\
**Post date:** [November 5, 2012, 7:13am UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/15 "2012-11-05T07:13:06Z")

</div>

> Continuing the discussion from the group call, I think that we're starting  
> to converge on adding a "policy" keyword to the language to represent  
> something between a function and an event.

Hmmmm. This is sounding weird. I think it's telling that you're heading  
towards introducing a concept that isn't (in terms of the name that comes  
to mind for it) about anything relating to language semantics, but rather  
to intended use, i.e., that this mechanism is for honing policy. That's  
often a sign that it's time to stop & rethink things.

Let's start with what's deficient about the current style. The overarching  
problem is it's hard to understand/explain, correct? How much of this is  
the funky [$pred...] syntax, vs. that we've wound up adding a bunch of  
separate hooks into the framework so it's hard to know which hook to use,  
and/or it's hard to understand what a given collection of hooks does?

I feel like we need to better determine just what problem we want/need to  
solve in order to then think about how to coherently provide better support  
for it.

&nbsp;&nbsp;&nbsp;&nbsp;Vern

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 5, 2012, 4:23pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/16 "2012-11-05T16:23:52Z")

</div>

> Let's start with what's deficient about the current style. The overarching  
> problem is it's hard to understand/explain, correct?

Yep.

> How much of this is  
> the funky [$pred...] syntax, vs. that we've wound up adding a bunch of  
> separate hooks into the framework so it's hard to know which hook to use,  
> and/or it's hard to understand what a given collection of hooks does?

Personally, it's mostly the syntax. There are also some cases where it's hard to express what you want in a single policy item too. Some of this could probably be fixed within the notice policy now, but then we'd essentially be turning the notice policy into a set of functions which starts to look suspiciously similar to events being executed immediately without the familiar syntax of events.

> I feel like we need to better determine just what problem we want/need to  
> solve in order to then think about how to coherently provide better support  
> for it.

I suspect that the reason the notice policy mechanism was first created was due to need for immediately evaluated events. The event handling mechanism feels very natural in Bro after using it for a while and the existing notice policy ends up feeling weird because you have to do a lot of uncommon and unfamiliar syntax. I'm not completely tied to the "policy" keyword, but does it seem unreasonable to have something like this immediately executed event that we've been discussing?

As

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [November 5, 2012, 4:28pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/17 "2012-11-05T16:28:09Z")

</div>

I think I was done. I'm not sure what the stray "As" is there for. 🙂

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![Vern](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/vern/32/630_2.png) [@Vern](https://community.zeek.org/u/Vern)\
**Post date:** [November 5, 2012, 4:41pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/18 "2012-11-05T16:41:10Z")

</div>

> I'm not completely tied to the "policy" keyword, but does it seem  
> unreasonable to have something like this immediately executed event that  
> we've been discussing?

Yeah, it seems somewhat strange and confusing.

If we back up, it seems to me there are several things the framework  
tries to achieve.

&nbsp;&nbsp;(1) Associate predicates with different possible cases. This  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is the "n$note == Whatever" clause that's currently included  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in basically every policy element.

&nbsp;&nbsp;(2) A predicate that applies logic when that case holds to yield  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a classification decision.

&nbsp;&nbsp;(3) Priorities to decide which predicate "wins" when multiple ones  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of them match the case.

&nbsp;&nbsp;(4) The ability for an early winner to short-circuit processing by  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;others that match the case, either for efficiency or to prevent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;their side-effects.

What I wonder about is having a starting point of dealing with (1) differently.  
Conceptually, one would like to say:

&nbsp;&nbsp;switch n$note {

&nbsp;&nbsp;case Notice::Sneakiness:  
&nbsp;&nbsp;&nbsp;&nbsp;if ( pred1() ) ...  
&nbsp;&nbsp;&nbsp;&nbsp;if ( pred2() ) ...

&nbsp;&nbsp;}

but the problem is that we want refinement of the code inside each  
case, without having to edit the overall code block.

I know that languages going as far back as CLOS had mechanisms for  
doing this sort of refinement. Not being a CLOS hacker, this is just  
from ancient memory, but I believe the CLOS equivalent for what we  
want would be:

&nbsp;&nbsp;function policy\_action(n: Notice::Info &case n$note==Notice::Sneakiness,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other\_arguments\_here ...)  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;}

That seems like too much verbiage. But it has me wondering whether there  
might be some sort of prioritized case construct that works better than  
"match ... using". Ideally this would be a language construct that nicely  
applies in other switch-like contexts, too.

&nbsp;&nbsp;&nbsp;&nbsp;Vern

---

<div class="post-metadata">

**Author:** ![Siwek\_Jon](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Siwek\_Jon](https://community.zeek.org/u/Siwek_Jon)\
**Post date:** [November 5, 2012, 4:43pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/19 "2012-11-05T16:43:05Z")

</div>

> 2. Adding a "break"-like keyword to halt policy execution (only valid in policy handlers).
> 
> 3. No return value(s) when calling policy handlers.

Just an alternative idea I had -- maybe they could be required to return a boolean value that's used internally to determine whether to keep traversing the vector of handler bodies. Then you don't need to add a new keyword for the halt/break functionality.

&nbsp;&nbsp;&nbsp;&nbsp;Jon

---

<div class="post-metadata">

**Author:** ![Siwek\_Jon](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Siwek\_Jon](https://community.zeek.org/u/Siwek_Jon)\
**Post date:** [November 5, 2012, 4:57pm UTC](https://community.zeek.org/t/changing-notice-policy-mechanism/2478/20 "2012-11-05T16:57:55Z")

</div>

> I feel like we need to better determine just what problem we want/need to  
> solve in order to then think about how to coherently provide better support  
> for it.

I think the construct we need to solve the problem already exists purely in the script-layer:

&nbsp;&nbsp;&nbsp;&nbsp;type TriggerFunc: function(n: Notice::Info): bool;  
&nbsp;&nbsp;&nbsp;&nbsp;global Notice::policy: vector of set[TriggerFunc];

I think that the issue is it being cumbersome to manipulate/redef? So we're working around that with the new proposal, but maybe we can do more to make redefining something like that syntactically clean and easy?

&nbsp;&nbsp;&nbsp;&nbsp;Jon

[Next page](https://community.zeek.org/t/changing-notice-policy-mechanism/2478.md?page=2)
