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

Fix owner user realm check for API key authentication #84325

Merged
merged 3 commits into from
Feb 28, 2022

Conversation

ywangd
Copy link
Member

@ywangd ywangd commented Feb 24, 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

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
@ywangd ywangd added >non-issue :Security/Security Security issues without another label v8.1.0 v8.2.0 labels Feb 24, 2022
@elasticmachine elasticmachine added the Team:Security Meta label for security team label Feb 24, 2022
@elasticmachine
Copy link
Collaborator

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

Comment on lines -408 to 411
if (isAuthenticatedWithApiKey()) {
if (isApiKey()) {
this.assertApiKeyMetadata();
final String apiKeyId = (String) this.metadata.get(AuthenticationField.API_KEY_ID_KEY);
final String apiKeyName = (String) this.metadata.get(AuthenticationField.API_KEY_NAME_KEY);
Copy link
Member Author

Choose a reason for hiding this comment

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

This change means we are not going to show API key ID and name in the authentication API response if the request is made by an API key run-as some other user. I think this makes sense. Ideally once we figure out what the effective user is, it should behave mostly the same as the user authentication itself. Also, it is plausible for certain setup, the underlying API key is not mean to reveal to the end-user.

Comment on lines -371 to 374
if (isAuthenticatedWithServiceAccount()) {
if (isServiceAccount()) {
final String tokenName = (String) getMetadata().get(ServiceAccountSettings.TOKEN_NAME_FIELD);
assert tokenName != null : "token name cannot be null";
final String tokenSource = (String) getMetadata().get(ServiceAccountSettings.TOKEN_SOURCE_FIELD);
Copy link
Member Author

Choose a reason for hiding this comment

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

Similar to the above: If the service account run-as another user, the service token name and type won't show up in the authentication response. This in theory could be a "breaking change" except we don't have any service account that has "run-as" privilege. So the change here is safe.

Comment on lines 1488 to 1491
// API key can run-as, when that happens, the following field will be _es_api_key,
// not the API key owner user's realm.
.with(PRINCIPAL_RUN_BY_REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName());
// TODO: API key can run-as which means we could use extra fields
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't have an existing field that can be used for logging API related information when an API key performs run-as. I think this should be tackled in a separate PR. The information is currently lacking, but not wrong, which makes sense to add more in future. Besides, we need more time to decide about exactly what fields these new info should take.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind raising a GH issue for it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I have also realized that we audit the service token identifier in the case of it running-as some other user, but the format is probably not consistent.
Would it be more appropriate to also leave that as a TODO (also a GH issue)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes we should discuss both issues and I raised #84394

Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, also added a TODO.

Copy link
Contributor

@albertzaharovits albertzaharovits left a comment

Choose a reason for hiding this comment

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

LGTM

I would polish a bit the PR message, eg:
"
For API Keys "running-as" (impersonating other users), do not expose (in the_security/_authenticate, security APIs, audit logging, and the Set Security User ingest processor) the authenticating key id and name. Hence only the effective user is revealed, just like in the regular case of a user run as.
"

@ywangd
Copy link
Member Author

ywangd commented Feb 27, 2022

I would polish a bit the PR message, eg: " For API Keys "running-as" (impersonating other users), do not expose (in the_security/_authenticate, security APIs, audit logging, and the Set Security User ingest processor) the authenticating key id and name. Hence only the effective user is revealed, just like in the regular case of a user run as. "

I updated the PR description based on your suggestion. Thanks!

@ywangd ywangd merged commit ba958e2 into elastic:master Feb 28, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Security/Security Security issues without another label 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.

3 participants