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

Remove the deprecated Authentication#getSourceRealm method #92222

Conversation

ywangd
Copy link
Member

@ywangd ywangd commented Dec 8, 2022

This PR removes the deprecated Authentication#getSourceRealm method. Its usage is mostly replaced by #getEffectiveSubject#getRealm except for ApiKeyService#getCreatorRealmName and ApiKeyService#getCreatorRealmType which has a special handling to return authenticatingSubject's realm when run-as lookup fails. This is to maintain BWC since these information is used in audit logs. Therefore, even it is technically incorrect, we should not break it without careful planning.

Relates: #88494

This PR removes the deprecated Authentication#getSourceRealm method. Its
usage is mostly replaced by #getEffectiveSubject#getRealm except for
ApiKeyService#getCreatorRealmName and ApiKeyService#getCreatorRealmType
which has a special handling to return authenticatingSubject's realm
when run-as lookup fails. This is to maintain BWC since these
information is used in audit logs. Therefore, even it is technically
incorrect, we should not break it without careful planning.
@ywangd ywangd added >refactoring :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) v8.7.0 labels Dec 8, 2022
@ywangd ywangd requested a review from n1v0lg December 8, 2022 07:18
@elasticsearchmachine elasticsearchmachine added the Team:Security Meta label for security team label Dec 8, 2022
@elasticsearchmachine
Copy link
Collaborator

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

Comment on lines 73 to 88
*
* The authentication is consisted of two {@link Subject}s
* <ul>
* <li>{@link #authenticatingSubject}</li> performs the authentication, i.e. it provides a credential.</li>
* <li>{@link #effectiveSubject} The subject that {@link #authenticatingSubject} impersonates ({@link #isRunAs()})</li>
* </ul>
* If {@link #isRunAs()} is {@code false}, the two {@link Subject}s will be the same object.
*
* Authentication also has a {@link #type} that tells which mechanism the {@link #authenticatingSubject}
* uses to perform the authentication.
*
* The Authentication's version is its {@link Subject}'s version, i.e. {@code getEffectiveSubject().getVersion()}.
* It is guaranteed that the versions are identical for the two Subjects. Hence {@code getAuthenticatingSubject().getVersion()}
* will give out the same result. But using {@code getEffectiveSubject()} is more idiomatic since most callers
* of this class should just need to know about the {@link #effectiveSubject}. That is, often times, the caller
* begins with {@code authentication.getEffectiveSubject()} for interrogating an Authentication object.
Copy link
Member Author

Choose a reason for hiding this comment

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

Added some more javadoc as discussed in #91067 (comment)

Comment on lines -231 to -233
if (isAssignedToDomain() && false == newAuthentication.isAssignedToDomain()) {
logger.info("Rewriting authentication [" + this + "] without domain");
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Relocated this logging inside the RealmRef itself which I think is a better location and also get rid of the usages of isAssignedToDomain

Comment on lines -343 to +346
public @Nullable RealmDomain getDomain() {
return getSourceRealm().getDomain();
@Nullable
RealmDomain getDomain() {
Copy link
Member Author

Choose a reason for hiding this comment

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

I changed this method (also isAssignedToDomain) to package private because:

  1. It is not really used in production code
  2. I am not sure whether we want them. Since we removed getRealm method from Authentication, having a getDomain feels going backwards.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed that it doesn't make sense to expose either of the domain methods beyond tests.

Copy link
Contributor

@n1v0lg n1v0lg left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines -343 to +346
public @Nullable RealmDomain getDomain() {
return getSourceRealm().getDomain();
@Nullable
RealmDomain getDomain() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed that it doesn't make sense to expose either of the domain methods beyond tests.

@@ -40,30 +40,6 @@

public class AuthenticationTests extends ESTestCase {

public void testWillGetLookedUpByWhenItExists() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: would cover isFailedRunAs here

Copy link
Member Author

@ywangd ywangd Dec 8, 2022

Choose a reason for hiding this comment

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

Good point! I adapted the tests for isFailedRunAs. Also added new assertions in ApiKeyServiceTests to ensure the behaviours of getCreatorRealmName and getCreatorRealmType do not change.

Co-authored-by: Nikolaj Volgushev <n1v0lg@users.noreply.github.com>
@ywangd ywangd added the auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Dec 8, 2022
@ywangd
Copy link
Member Author

ywangd commented Dec 9, 2022

@elasticmachine update branch

@ywangd
Copy link
Member Author

ywangd commented Dec 11, 2022

CI failure is unrelated and already tracked at #91800

@elasticsearchmachine elasticsearchmachine merged commit a598e6b into elastic:main Dec 11, 2022
@ywangd ywangd deleted the remove-authentication-get-source-realm branch December 11, 2022 23:39
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!) >refactoring :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) Team:Security Meta label for security team v8.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants