Icinga / icinga2

The core of our monitoring platform with a powerful configuration language and REST API.
https://icinga.com/docs/icinga2/latest
GNU General Public License v2.0
2k stars 574 forks source link

Add user object to the filter namespace #10051

Closed wolneykien closed 4 months ago

wolneykien commented 5 months ago

The current evaluation context of the filter function defined for an ApiUser doesn't provide any way to access the user object itself.

With the patch applied the problem of having an ApiUser per Host (see https://community.icinga.com/t/per-host-api-user-for-passive-check-submission/9053/3 , for instance) can be solved by first defining a template like this:

template ApiUser "usersync" {
  permissions = [{
    permission = "actions/process-check-result"
    filter = () => {
      return host.name == user.client_cn
    }
  }]
}

And then by creating an ApiUser that imports it, possibly using the REST API. I. e.:

curl ... -X PUT 'https://localhost:5665/v1/objects/apiusers/userforhost1' -d '{"attrs": {"client_cn": "host1"}, "templates": ["usersync"]}'
cla-bot[bot] commented 5 months ago

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: Paul Wolneykien. This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails
Al2Klimov commented 4 months ago

Didn't test, but I could imagine that it also works like this:

wolneykien commented 4 months ago

Didn't test, but I could imagine that it also works like this:

* At the beginning of the template get the user: `var user = this`

* Capture that var in the filter closure: `use(user)`

Wow, thanks. It seems to work. I'll re-test today on a "vanilla" icinga2 instance.

wolneykien commented 3 months ago

The closure approach works well. And this is the project where the mentioned ApiUser template is used: https://github.com/wolneykien/icinga2-usersyncd .