Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of New filters #29

Merged
merged 11 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/otto_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go 1.13
- name: Set up Go 1.22.5
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.22.5
id: go
- name: Get Otto
run: |
export PATH=$PATH:$(go env GOPATH)/bin
go get -v github.com/robertkrimen/otto/otto
go install github.com/robertkrimen/otto/otto@latest
- name: Test
run: |
export PATH=$PATH:$(go env GOPATH)/bin
cat lib/has_contact.js otto_tests/has_contact_otto_tests.js lib/no_contacts.js otto_tests/no_contacts_otto_tests.js > otto_tests/test.js
cat lib/has_check_contact.js otto_tests/has_check_contact_otto_tests.js lib/has_entity_contact.js otto_tests/has_entity_contact_otto_tests.js lib/has_contact.js otto_tests/has_contact_otto_tests.js lib/no_contacts.js otto_tests/no_contacts_otto_tests.js > otto_tests/test.js
if otto otto_tests/test.js | tee /dev/stderr | grep FAILED; then false; else true; fi
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased](https://github.com/sensu/sensu-go-has-contact-filter/tree/HEAD)
chavakula marked this conversation as resolved.
Show resolved Hide resolved
[Full Changelog](https://github.com/sensu/sensu-go-has-contact-filter/compare/0.3.0...HEAD)
- Added `has_check_contact` function. Use this to filter contacts in check contacts.
- Added `has_entity_contact` function. Use this to filter contacts in entity contacts.
- Added `no_check_contact` function. Use this when defining a "default" handler which is used when no check contacts are defined.
- Added `no_entity_contact` function. Use this when defining a "default" handler which is used when no entity contacts are defined.
- Updated readme for examples of `has_check_contact` and `has_entity_contact`


## [0.3.0](https://github.com/sensu/sensu-go-has-contact-filter/tree/0.3.0) (2020-12-23)
[Full Changelog](https://github.com/sensu/sensu-go-has-contact-filter/compare/0.2.0...0.3.0)
Expand Down
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ When contacts are present in the event's check labels, any contacts defined at i

The `no_contacts` function accepts the event object as its sole argument. This function returns true if the event does not contain a `contacts` label under either the event's entity or check scopes, or if those labels values contain only blank space.

### has_entity_contact

The `has_entity_contact` function requires two arguments: the event object and a contact name string. Given these inputs, the function evaluates whether or not the specified contact name appears in comma separated values for the `contacts` label, under the entity scope.

### no_entity_contact

The `no_entity_contact` function accepts the event object as its sole argument. This function returns true if the event does not contain a `contacts` label under the event's entity or if those labels values contain only blank space.

### has_check_contact

The `has_check_contact` function requires two arguments: the event object and a contact name string. Given these inputs, the function evaluates whether or not the specified contact name appears in comma separated values for the `contacts` label, under check scope.

### no_check_contact

The `no_check_contact` function accepts the event object as its sole argument. This function returns true if the event does not contain a `contacts` label under the event's check scopes, or if those labels values contain only blank space.

## Installation

Expand Down Expand Up @@ -60,6 +75,40 @@ spec:
- has_contact(event, "ops")
```

if you want to check contacts only in check and ignore entity contacts

``` yaml
---
type: EventFilter
api_version: core/v2
metadata:
name: contact_ops
namespace: default
spec:
action: allow
runtime_assets:
- sensu-go-has-contact-filter
expressions:
- has_check_contact(event, "ops")
```

if you want to check contacts only in entity and ignore check contacts

``` yaml
---
type: EventFilter
api_version: core/v2
metadata:
name: contact_ops
namespace: default
spec:
action: allow
runtime_assets:
- sensu-go-has-contact-filter
expressions:
- has_entity_contact(event, "ops")
```

To complement filters which test for specific contact names, you may also wish to have a filter which handles events without explicitly defined contacts:

``` yaml
Expand Down Expand Up @@ -164,7 +213,7 @@ spec:
- email_default
```

With a handler set like this one in place, check results configured with the `email` handler will spawn a handler pipeline for each handler in the set, each pipeline using the `has_contact` function to evaluate whether or not the event matches a named contact. Based on the above examples, a check result with "dev" and/or "ops" will be routed to those contacts respective email addresses, whereas any check result without contacts defined will be handled by the `email_default` handler.
With a handler set like this one in place, check results configured with the `email` handler will spawn a handler pipeline for each handler in the set, each pipeline using the `has_contact`, `has_entity_contact`, `has_check_contact` function to evaluate whether or not the event matches a named contact. Based on the above examples, a check result with "dev" and/or "ops" will be routed to those contacts respective email addresses, whereas any check result without contacts defined will be handled by the `email_default` handler.

See the included [contact routing pattern diagram][diagram] for a visual description of intended use cases.

Expand Down
27 changes: 0 additions & 27 deletions bin/build.sh

This file was deleted.

73 changes: 0 additions & 73 deletions bin/github-release-upload.sh

This file was deleted.

25 changes: 25 additions & 0 deletions lib/has_check_contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function trimit(item, index, arr) {
arr[index] = item.trim();
}

function has_check_contact(event, contact) {
var check_contacts = [];

if (event.hasOwnProperty("check") && event.check.hasOwnProperty("labels") && event.check.labels.hasOwnProperty("contacts")) {
check_contacts = event.check.labels.contacts.split(",");
check_contacts.forEach(trimit);
}

// if there are no contacts, the event is not allowed
if (check_contacts.length == 0) {
return false;
}

// allow the event if contact is present in check contacts
if (check_contacts.indexOf(contact.trim()) >= 0) {
return true;
}

// otherwise event is not allowed
return false;
}
26 changes: 26 additions & 0 deletions lib/has_entity_contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function trimit(item, index, arr) {
arr[index] = item.trim();
}

function has_entity_contact(event, contact) {
var entity_contacts = [];

if (event.hasOwnProperty("entity") && event.entity.hasOwnProperty("labels") && event.entity.labels.hasOwnProperty("contacts")) {
entity_contacts = event.entity.labels.contacts.split(",");
entity_contacts.forEach(trimit);
}

// if there are no contacts, the event is not allowed
if (entity_contacts.length == 0) {
return false;
}

// allow the event if contact is present in check contacts
if (entity_contacts.indexOf(contact.trim()) >= 0) {
return true;
}

// otherwise event is not allowed
return false;

}
14 changes: 14 additions & 0 deletions lib/no_check_contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function no_check_contact(event) {
var check_contacts = [];

if (event.hasOwnProperty("check") && event.check.hasOwnProperty("labels") && event.check.labels.hasOwnProperty("contacts")) {
check_contacts = event.check.labels.contacts.split(",");
// filter out any elements which do not contain at least one non-whitespace character
check_contacts = check_contacts.filter(function(entry) { return /\S/.test(entry); });
}

if (check_contacts.length == 0) {
return true;
}
return false;
}
12 changes: 12 additions & 0 deletions lib/no_entity_contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function no_entity_contact(event) {
var entity_contacts = [];
if (event.hasOwnProperty("entity") && event.entity.hasOwnProperty("labels") && event.entity.labels.hasOwnProperty("contacts")) {
entity_contacts = event.entity.labels.contacts.split(",");
// filter out any elements which do not contain at least one non-whitespace character
entity_contacts = entity_contacts.filter(function(entry) { return /\S/.test(entry); });
}
if (entity_contacts.length == 0) {
return true;
}
return false;
}
Loading