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

Violation Metrics Export Sidecar for Gatekeeper #137

Conversation

mlarrousse
Copy link

At T-Mobile we use this sidecar to scrape the gatekeeper audit-loop logs from the audit container and then expose them on a prometheus metrics endpoint, we'd like to contribute it back for anyone who is looking to use this or a similar pattern for getting audited violation details out of Gatekeeper.

Contained in this PR is:

A Dockerfile for building the mtail image
Mtail code largely written by @ghsbhatia
a kustomization.yaml and patches to deploy gatekeeper with the sidecar.

Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! A few nits but looks great 👍 When comments have been addressed (or dismissed :D), please also squash all commits into one. Thanks for contributing!

mtail_violations_exporter/README.md Outdated Show resolved Hide resolved
mtail_violations_exporter/Makefile Outdated Show resolved Hide resolved
mtail_violations_exporter/kustomization.yaml Outdated Show resolved Hide resolved
Copy link

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool!

One thought about the potential for the JSON to change.

text context

# pattern for opa violation attributes
const VIOLATION /{.*\\"constraint_kind\\"\s*:\s*\\"(?P<constraint_kind>.*?)\\".*\\"constraint_name\\"\s*:\s*\\"(?P<constraint_name>.*?)\\".*\\"resource_kind\\"\s*:\s*\\"(?P<resource_kind>.*?)\\".*\\"resource_namespace\\"\s*:\s*\\"(?P<resource_namespace>.*?)\\".*\\"resource_name\\"\s*:\s*\\"(?P<resource_name>.*?)\\".*}/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Golang's built in JSON marshaler does have stably sorted map keys:

https://golang.org/pkg/encoding/json/#Marshal

So this looks like it should work. Care should be taken here though, as adding new fields will break this scraper.

If it's possible to semantically process the JSON (e.g. actually convert this string to a JSON object and pull out values of interest), then this would be less brittle against the adding of new fields to the log line (which would be considered a backwards compatible change).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mtail is an unstructured parser, so no semantic JSON processing.

If you add a new field, these should still be sorted and it will just ignore your new field as it does today with constraint_action.

That said I think it would be better to break out each of these fields into their own const & not rely on the sorting. Does this work for now for you & we can break apart this VIOLATION parse the next time we update this program?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants