Skip to content

Fix can access resource checks for API Keys with run as #84277

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

Merged

Conversation

albertzaharovits
Copy link
Contributor

@albertzaharovits albertzaharovits commented Feb 23, 2022

This fixes two things for the "can access" authz check:

  • API Keys running as, have access to the resources created by the effective run as user
  • tokens created by API Keys (with the client credentials) have access to the API Key's resources

In addition, this PR moves some of the authz plumbing code from the Async and Scroll services classes under the Security Context class (as a minor refactoring).

ywangd added a commit to ywangd/elasticsearch that referenced this pull request Feb 24, 2022
API Key can run-as since elastic#79809. There are places in the code where we
assume API key cannot run-as. Most of them are corrected in elastic#81564.
But there are still a few things got missed. This PR fixes the methods
for checking owner user realm for API key.

Note the resource sharing check (canAccessResourcesOf) also needs to be
fixed, this will be handled by elastic#84277
@albertzaharovits albertzaharovits self-assigned this Feb 27, 2022
@albertzaharovits albertzaharovits added v8.1.0 :Security/Authorization Roles, Privileges, DLS/FLS, RBAC/ABAC labels Feb 27, 2022
@albertzaharovits albertzaharovits marked this pull request as ready for review February 27, 2022 20:46
@elasticmachine elasticmachine added the Team:Security Meta label for security team label Feb 27, 2022
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security (Team:Security)

ywangd added a commit that referenced this pull request Feb 28, 2022
API Key can run-as since #79809. There are places in the code where we
assume API key cannot run-as. Most of them are corrected in #81564.
But there are still a few things got missed. This PR fixes the methods
for checking owner user realm for API key.

This means, when API Keys "running-as" (impersonating other users),
we do not expose the authenticating key ID and name to the end-user
such as the Authenticate API and the SetSecurityUseringest processor.
Only the effective user is revealed, just like in the regular case of a realm
user run as. For audit logging, the key's ID and name are not exposed
either. But this is mainly because there are no existing fields suitable for
these information. We do intend to add them later (#84394) because
auditing logging is to consumed by system admin instead of end-users.

Note the resource sharing check (canAccessResourcesOf) also needs to be
fixed, this will be handled by #84277
Copy link
Member

@ywangd ywangd left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks for handling this. Backport will be a challenge. 🙏

Comment on lines 198 to 206
if ((false == FileRealmSettings.TYPE.equals(realmRef.getType()) && false == FileRealmSettings.TYPE.equals(otherRealmRef.getType()))
|| (false == NativeRealmSettings.TYPE.equals(realmRef.getType())
&& false == NativeRealmSettings.TYPE.equals(otherRealmRef.getType()))) {
Authentication runAs4 = original.runAs(user, otherRealmRef);
assertCannotAccessResources(original, runAs4);
assertCannotAccessResources(original.token(), runAs4);
assertCannotAccessResources(original, runAs4.token());
assertCannotAccessResources(original.token(), runAs4.token());
}
Copy link
Member

Choose a reason for hiding this comment

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

This if check does not seem to be useful since otherRealmRef is created as:

final RealmRef otherRealmRef = randomValueOtherThan(realmRef, () -> randomRealmRef(false));

So it is never equal to realmRef.

Copy link
Contributor Author

@albertzaharovits albertzaharovits Feb 28, 2022

Choose a reason for hiding this comment

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

The if is needed, but it was wrong. I've fixed it, and left a comment.

The idea is that the realm refs are different but if they're both file or both native, then the ownership check should pass.

@ywangd
Copy link
Member

ywangd commented Feb 28, 2022

Btw, I think we probably can drop the >bug label since API key run-as was not yet released and token created by API key wasn't really working before 8.1. What do you think?

ywangd added a commit to ywangd/elasticsearch that referenced this pull request Feb 28, 2022
API Key can run-as since elastic#79809. There are places in the code where we
assume API key cannot run-as. Most of them are corrected in elastic#81564.
But there are still a few things got missed. This PR fixes the methods
for checking owner user realm for API key.

This means, when API Keys "running-as" (impersonating other users),
we do not expose the authenticating key ID and name to the end-user
such as the Authenticate API and the SetSecurityUseringest processor.
Only the effective user is revealed, just like in the regular case of a realm
user run as. For audit logging, the key's ID and name are not exposed
either. But this is mainly because there are no existing fields suitable for
these information. We do intend to add them later (elastic#84394) because
auditing logging is to consumed by system admin instead of end-users.

Note the resource sharing check (canAccessResourcesOf) also needs to be
fixed, this will be handled by elastic#84277
elasticsearchmachine pushed a commit that referenced this pull request Feb 28, 2022
* Fix owner user realm check for API key authentication (#84325)

API Key can run-as since #79809. There are places in the code where we
assume API key cannot run-as. Most of them are corrected in #81564.
But there are still a few things got missed. This PR fixes the methods
for checking owner user realm for API key.

This means, when API Keys "running-as" (impersonating other users),
we do not expose the authenticating key ID and name to the end-user
such as the Authenticate API and the SetSecurityUseringest processor.
Only the effective user is revealed, just like in the regular case of a realm
user run as. For audit logging, the key's ID and name are not exposed
either. But this is mainly because there are no existing fields suitable for
these information. We do intend to add them later (#84394) because
auditing logging is to consumed by system admin instead of end-users.

Note the resource sharing check (canAccessResourcesOf) also needs to be
fixed, this will be handled by #84277

* fix test
@ywangd
Copy link
Member

ywangd commented Feb 28, 2022

Backport will be a challenge

I added a few helper methods to AuthenticationTests while doing my backports. There are now methods like randomRealmAuthentication etc. It also has methods like AuthenticationTests#toRunAs and AuthenticationTests#toToken to mirror the new methods added to Authentication. I added them to the Test file because I don't want to change production code for tests. I hope they could be useful for your backport as well.

@albertzaharovits
Copy link
Contributor Author

Btw, I think we probably can drop the >bug label since API key run-as was not yet released and token created by API key wasn't really working before 8.1. What do you think?

Okay, I'll label this >non-issue.

@albertzaharovits albertzaharovits added auto-backport-and-merge auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) labels Feb 28, 2022
@elasticsearchmachine elasticsearchmachine merged commit 2f0733d into elastic:master Feb 28, 2022
@albertzaharovits albertzaharovits deleted the can-access-resource-of branch February 28, 2022 14:01
albertzaharovits added a commit to albertzaharovits/elasticsearch that referenced this pull request Feb 28, 2022
This fixes two things for the "can access" authz check: * API Keys
running as, have access to the resources created by the effective run as
user * tokens created by API Keys (with the client credentials) have
access to the API Key's resources

In addition, this PR moves some of the authz plumbing code from the
Async and Scroll services classes under the Security Context class (as a
minor refactoring).
elasticsearchmachine pushed a commit that referenced this pull request Feb 28, 2022
* Fix can access resource checks for API Keys with run as (#84277)

This fixes two things for the "can access" authz check: * API Keys
running as, have access to the resources created by the effective run as
user * tokens created by API Keys (with the client credentials) have
access to the API Key's resources

In addition, this PR moves some of the authz plumbing code from the
Async and Scroll services classes under the Security Context class (as a
minor refactoring).

* spotless

* Merge fallout

* AuthenticationTests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) >non-issue :Security/Authorization Roles, Privileges, DLS/FLS, RBAC/ABAC Team:Security Meta label for security team v8.1.0 v8.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants