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

Add run-as support for OAuth2 tokens #86680

Merged
merged 5 commits into from
May 20, 2022
Merged

Conversation

ywangd
Copy link
Member

@ywangd ywangd commented May 11, 2022

Authentication with an OAuth2 token can now perform run-as with changes
in this PR. This is in addition to the existing run-as support for realm
and API key authentication.

NB there are additional constraints on whether an OAuth2 token is
indeed qualified for run-as:

  1. The token cannot itself already be a run-as (this can happen when the token is created using run-as and client_credentials grant)
  2. The token cannot be created by anonymous or internal users

Service accounts cannot create tokens which meant run-as is not
supported for service accounts (none of existing service accounts
requires it anyway).

Relates: #84336

Authentication with an OAuth2 token can now perform run-as with changes
in this PR. This is in addition to the existing run-as support for realm
and API key authentication.

NB there are additional constraints on whether an OAuth2 token is
indeed qualified for run-as:
1. The token cannot itself cannot already be a run-as
2. The token cannot be created by anonymous or internal users

Service accounts cannot create tokens which meant run-as is not
supported for service accounts (none of existing service accounts
requires it anyway).
@ywangd ywangd added >enhancement :Security/Security Security issues without another label v8.3.0 labels May 11, 2022
@ywangd ywangd requested a review from tvernum May 11, 2022 13:32
@elasticmachine elasticmachine added the Team:Security Meta label for security team label May 11, 2022
@elasticmachine
Copy link
Collaborator

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

@elasticsearchmachine
Copy link
Collaborator

Hi @ywangd, I've created a changelog YAML for you.

@justincr-elastic
Copy link
Contributor

Can you clarify this in the description?

The token cannot itself cannot already be a run-as

// anonymous user?
if (getUser().equals(anonymousUser)) {
assert ANONYMOUS_REALM_TYPE.equals(getAuthenticatingSubject().getRealm().getType())
&& ANONYMOUS_REALM_NAME.equals(getAuthenticatingSubject().getRealm().getName());
Copy link
Contributor

@justincr-elastic justincr-elastic May 11, 2022

Choose a reason for hiding this comment

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

Should this be || based on your comment that a custom realm can use __anonymous for its name and type?

Copy link
Member Author

Choose a reason for hiding this comment

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

The intention is to make sure we only treat ES's own anonymous user as the true anonymous user. So the user's realm must match both of the name and type of ES anonymous realm.

authentication.getAuthenticationType().name().toLowerCase(Locale.ROOT)
);
if (false == authentication.supportsRunAs(anonymousUser)) {
logger.info("ignore run-as header since it is currently not supported for authentication [{}]", authentication);
Copy link
Contributor

Choose a reason for hiding this comment

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

Log parameter changed from authentication.getAuthenticationType().name().toLowerCase(Locale.ROOT) to authentication. Is that design intent?

Copy link
Member Author

Choose a reason for hiding this comment

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

With the changes here, determine whether an authentication supports run-as is more involved and not just based on the authenticationType. So I changed it to log the whole authentication object, which will be easier for us to diagnose if the logging message ever comes to us in a SDH.

Technically we can log multiple different message but more precise about exactly which part of authentication preventing run-as. But I am not sure if it's worth it. Let me know if you feel strongly about it.

@justincr-elastic
Copy link
Contributor

I left a couple of drive by comments.

@justincr-elastic
Copy link
Contributor

Are there any OAuth2 tests that use Access Tokens formatted as JWTs?

@ywangd
Copy link
Member Author

ywangd commented May 11, 2022

Are there any OAuth2 tests that use Access Tokens formatted as JWTs?

Not intentionally. But practically the JwtRestIT can be considered as one because for a JWT realm to work successfully, the ES OAuth2 token authenticator must have decided that it should yield from the authentication chain.

@lockewritesdocs
Copy link
Contributor

lockewritesdocs commented May 19, 2022

@ywangd, as part of #86411, I'm clarifying that:

Tokens that are created with the get token API are incompatible with the run_as privilege because OAuth2 tokens do not support this privilege.

This is true until your PR is merged. As part of your PR, I think that we should remove that sentence and then provide examples in the run_as section about how to generate OAuth2 tokens and use them with the run_as privilege.

@ywangd
Copy link
Member Author

ywangd commented May 20, 2022

As part of your PR, I think that we should remove that sentence and then provide examples in the run_as section about how to generate OAuth2 tokens and use them with the run_as privilege.

Makse sense. I'll get to it when your PR is merged. Thanks for the reminder!

Copy link
Contributor

@tvernum tvernum left a comment

Choose a reason for hiding this comment

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

LGTM

…security/authc/Authentication.java

Co-authored-by: Tim Vernum <tim@adjective.org>
@ywangd
Copy link
Member Author

ywangd commented May 20, 2022

@lockewritesdocs This PR is now ready to be merged. Just need a round of CI to pass. In this case, would it make more sense for you to update your PR on the docs? I am happy to help out with providing technical details if needed. Thanks!

@lockewritesdocs
Copy link
Contributor

@ywangd, go ahead and merge your PR and I'll remove the note about OAauth2. Please let me know if you have examples that we can use for run_as.

@ywangd
Copy link
Member Author

ywangd commented May 20, 2022

go ahead and merge your PR and I'll remove the note about OAauth2. Please let me know if you have examples that we can use for run_as.

Thanks. I'll message you for examples in a separate channel.

@ywangd ywangd merged commit 8c67c3a into elastic:master May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Security/Security Security issues without another label Team:Security Meta label for security team v8.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants