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

ObjectQueryHandler: Check user permissions on joined relations #9408

Merged
merged 3 commits into from
Oct 11, 2022

Conversation

yhabteab
Copy link
Member

fixes #9339

@cla-bot cla-bot bot added the cla/signed label Jun 20, 2022
@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch from d2acb74 to 21ffdac Compare June 21, 2022 13:53
@yhabteab yhabteab requested a review from Al2Klimov June 21, 2022 13:54
lib/remote/filterutility.cpp Show resolved Hide resolved
lib/remote/filterutility.cpp Outdated Show resolved Hide resolved
@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch 3 times, most recently from b5a0291 to 952d404 Compare June 23, 2022 06:55
Copy link
Member

@Al2Klimov Al2Klimov left a comment

Choose a reason for hiding this comment

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

Much better! Now as we have a clear view, let’s have a look...

@@ -166,17 +166,43 @@ void FilterUtility::CheckPermission(const ApiUser::Ptr& user, const String& perm
else
*permissionFilter = new LogicalOrExpression(std::unique_ptr<Expression>(*permissionFilter), std::unique_ptr<Expression>(fexpr));
}

break;
Copy link
Member

Choose a reason for hiding this comment

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

This is a bad idea. Figure out and explain in your own words why.

Copy link
Member Author

@yhabteab yhabteab Jun 23, 2022

Choose a reason for hiding this comment

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

Is it probably due to the fact that the same permission might be used with different filters repeatedly? I don't know, why should one even do something like that!

Copy link
Member

Choose a reason for hiding this comment

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

Oh, not necessarily the same. Think of query/* and query/Host e.g..

// has been found!!
continue;
}

Copy link
Member

Choose a reason for hiding this comment

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

OK, in case of insufficient permission you skip it... but otherwise you unconditionally include it, right? This is a also bad idea. Figure out and explain in your own words why.

Copy link
Member Author

Choose a reason for hiding this comment

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

Have to evaluate the permission filters as well!

Copy link
Member

Choose a reason for hiding this comment

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

Exactly. You know what to do...

Copy link
Contributor

Choose a reason for hiding this comment

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

This comment should be resolved, shouldn't it?

@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch 2 times, most recently from 254df92 to 6483659 Compare June 23, 2022 11:00
lib/remote/filterutility.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Show resolved Hide resolved
@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch 2 times, most recently from fe6ef0b to 62698d1 Compare June 23, 2022 16:15
@yhabteab yhabteab requested a review from Al2Klimov June 23, 2022 16:15
@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch from ae93849 to b90f8f8 Compare June 24, 2022 10:25
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
@yhabteab yhabteab requested a review from Al2Klimov July 18, 2022 07:40
Copy link
Member

@Al2Klimov Al2Klimov left a comment

Choose a reason for hiding this comment

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

Riddle 1

The default config with icinga2 api setup and

object ApiUser "root" {
  password = "123456"

  var perm = {
    permission = "objects/query/checkcommand"
    filter = {{ true }}
  }

  var fperm = () use (perm) => perm

  permissions = [ "*" ] + range(10000).map(fperm)
}

leaks memory with

curl -ksSu root:123456 -H 'Accept: application/json' -X GET -d '{"joins":["check_command"],"pretty":true}' https://127.0.0.1:5665/v1/objects/hosts

Why?

Riddle 2

The default config with icinga2 api setup and

object ApiUser "root" {
  password = "123456"

  permissions = [ {
    permission = "objects/query/service"
  }, {
    permission = "objects/query/checkcommand"
    filter = {{ false }}
  } ]
}

outputs some of the check commands with

curl -ksSu root:123456 -H 'Accept: application/json' -X GET -d '{"joins":["check_command", "host"],"pretty":true}' https://127.0.0.1:5665/v1/objects/services

Why?

Please only explanation in own words for now, thx.

@yhabteab
Copy link
Member Author

Riddle 1

Isn't that obvious to you? When you specify the same permission 10K times with a callable filter, what would you expect to happen? I also get a stack overflow error but that is not something being added by this PR. Just try querying the CheckCommands directly and see what happens there.

[2022-07-22 11:31:32 +0200] information/ApiListener: New client connection from [::ffff:127.0.0.1]:50112 (no client certificate)
Stack overflow while evaluating expression: Recursion level too deep.
[2022-07-22 11:31:32 +0200] information/HttpServerConnection: Request: GET /v1/objects/checkcommands (from [::ffff:127.0.0.1]:50112), user: dummy, agent: curl/7.79.1, status: Not Found).
[2022-07-22 11:31:32 +0200] information/HttpServerConnection: HTTP client disconnected (from [::ffff:127.0.0.1]:50112)

Riddle 2

Yep, need to cache the permission filter of the individual types as well.

@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch 2 times, most recently from 34e0c31 to f8bd84e Compare July 25, 2022 08:23
@Al2Klimov
Copy link
Member

Isn't that obvious to you?

Oh, I exactly know why is what happening here. That’s why I called it a riddle. :-)

When you specify the same permission 10K times with a callable filter, what would you expect to happen? I also get a stack overflow error but that is not something being added by this PR.

Absolutely. But Icinga even manages to properly handle that (pseudo) stack overflow. So not leaking memory is the least thing Icinga shall do. I'm joining check commands and only they have a such filter, so your join permission check is clearly responsible. Also I have no doubt it also works with a smaller filter, but mine leaks memory faster.

@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch from f8bd84e to 44ea4bc Compare July 25, 2022 10:34
@yhabteab yhabteab requested a review from Al2Klimov July 25, 2022 10:36
@Al2Klimov
Copy link
Member

Riddle 2

Still happening.

@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch from 44ea4bc to cefeb5a Compare July 25, 2022 11:24
Copy link
Contributor

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

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

@yhabteab @Al2Klimov Are you done with these riddles (i.e. do both of you agree that they are (re)solved)?

lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
// has been found!!
continue;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This comment should be resolved, shouldn't it?

lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
lib/remote/filterutility.cpp Outdated Show resolved Hide resolved
lib/remote/filterutility.cpp Show resolved Hide resolved
@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch from cefeb5a to 7243f5c Compare October 5, 2022 15:53
Copy link
Contributor

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

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

Tested it and worked fine. Just some formatting and details in the comments left (unless you want to do the extended suggestion within this PR).

lib/remote/filterutility.cpp Outdated Show resolved Hide resolved
lib/remote/objectqueryhandler.cpp Outdated Show resolved Hide resolved
@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch from 4e29174 to 6aa6338 Compare October 7, 2022 08:01
Copy link
Contributor

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

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

Code looks fine and my tests were successful, just a small formatting issue in one of the comments left.

lib/remote/filterutility.cpp Outdated Show resolved Hide resolved
@yhabteab yhabteab force-pushed the bugfix/match-api-permissions-against-join-relations branch from 6aa6338 to 72e6894 Compare October 10, 2022 10:33
@Al2Klimov Al2Klimov added this to the 2.14.0 milestone Oct 11, 2022
@Al2Klimov Al2Klimov merged commit 363f4d3 into master Oct 11, 2022
@icinga-probot icinga-probot bot deleted the bugfix/match-api-permissions-against-join-relations branch October 11, 2022 11:42
@yhabteab yhabteab added the backported Fix was included in a bugfix release label Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api REST API backported Fix was included in a bugfix release cla/signed ref/NC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

icinga api permissions do not affect joins
3 participants