-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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 deprecated Authentication#getAuthenticatedBy #91104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ protected void doExecute(Task task, OpenIdConnectLogoutRequest request, ActionLi | |
final String token = request.getToken(); | ||
tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap(tuple -> { | ||
final Authentication authentication = tuple.v1(); | ||
assert false == authentication.isRunAs() : "oidc realm authentication cannot have run-as"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, oidc realm authentication cannot have run-as either. |
||
final Map<String, Object> tokenMetadata = tuple.v2(); | ||
validateAuthenticationAndMetadata(authentication, tokenMetadata); | ||
tokenService.invalidateAccessToken(token, ActionListener.wrap(result -> { | ||
|
@@ -86,7 +87,7 @@ protected void doExecute(Task task, OpenIdConnectLogoutRequest request, ActionLi | |
|
||
private OpenIdConnectLogoutResponse buildResponse(Authentication authentication, Map<String, Object> tokenMetadata) { | ||
final String idTokenHint = (String) getFromMetadata(tokenMetadata, "id_token_hint"); | ||
final Realm realm = this.realms.realm(authentication.getAuthenticatedBy().getName()); | ||
final Realm realm = this.realms.realm(authentication.getEffectiveSubject().getRealm().getName()); | ||
Comment on lines
-89
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and the other two places in this file are the same stroy as the saml realm authentication. |
||
final JWT idToken; | ||
try { | ||
idToken = JWTParser.parse(idTokenHint); | ||
|
@@ -108,11 +109,11 @@ private void validateAuthenticationAndMetadata(Authentication authentication, Ma | |
throw new ElasticsearchSecurityException("No active user"); | ||
} | ||
|
||
final Authentication.RealmRef ref = authentication.getAuthenticatedBy(); | ||
final Authentication.RealmRef ref = authentication.getEffectiveSubject().getRealm(); | ||
if (ref == null || Strings.isNullOrEmpty(ref.getName())) { | ||
throw new ElasticsearchSecurityException("Authentication {} has no authenticating realm", authentication); | ||
} | ||
final Realm realm = this.realms.realm(authentication.getAuthenticatedBy().getName()); | ||
final Realm realm = this.realms.realm(authentication.getEffectiveSubject().getRealm().getName()); | ||
if (realm == null) { | ||
throw new ElasticsearchSecurityException("Authenticating realm {} does not exist", ref.getName()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ protected void doExecute(Task task, SamlAuthenticateRequest request, ActionListe | |
return; | ||
} | ||
assert authentication != null : "authentication should never be null at this point"; | ||
assert false == authentication.isRunAs() : "saml realm authentication cannot have run-as"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assertion for saml realm not having run-as. |
||
@SuppressWarnings("unchecked") | ||
final Map<String, Object> tokenMeta = (Map<String, Object>) result.getMetadata().get(SamlRealm.CONTEXT_TOKEN_DATA); | ||
tokenService.createOAuth2Tokens( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ protected void doExecute(Task task, SamlLogoutRequest request, ActionListener<Sa | |
final String token = request.getToken(); | ||
tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap(tuple -> { | ||
Authentication authentication = tuple.v1(); | ||
assert false == authentication.isRunAs() : "saml realm authentication cannot have run-as"; | ||
final Map<String, Object> tokenMetadata = tuple.v2(); | ||
SamlLogoutResponse response = buildResponse(authentication, tokenMetadata); | ||
tokenService.invalidateAccessToken(token, ActionListener.wrap(created -> { | ||
|
@@ -134,9 +135,9 @@ private String getMetadataString(Map<String, Object> metadata, String key) { | |
} | ||
|
||
private SamlRealm findRealm(Authentication authentication) { | ||
final Authentication.RealmRef ref = authentication.getAuthenticatedBy(); | ||
final Authentication.RealmRef ref = authentication.getEffectiveSubject().getRealm(); | ||
if (ref == null || Strings.isNullOrEmpty(ref.getName())) { | ||
throw SamlUtils.samlException("Authentication {} has no authenticating realm", authentication); | ||
throw SamlUtils.samlException("Authentication {} has no effective realm", authentication); | ||
Comment on lines
-137
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here as well. |
||
} | ||
final Realm realm = realms.realm(ref.getName()); | ||
if (realm == null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1513,17 +1513,20 @@ private static Optional<ElasticsearchSecurityException> checkClientCanRefresh( | |
clientAuthentication.getEffectiveSubject().getUser().principal() | ||
); | ||
return Optional.of(invalidGrantException("tokens must be refreshed by the creating client")); | ||
} else if (clientAuthentication.getAuthenticatedBy().getName().equals(refreshToken.getAssociatedRealm()) == false) { | ||
logger.warn( | ||
"[{}] created the refresh token while authenticated by [{}] but is now authenticated by [{}]", | ||
refreshToken.getAssociatedUser(), | ||
refreshToken.getAssociatedRealm(), | ||
clientAuthentication.getAuthenticatedBy().getName() | ||
); | ||
return Optional.of(invalidGrantException("tokens must be refreshed by the creating client")); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} else if (clientAuthentication.getAuthenticatingSubject() | ||
.getRealm() | ||
.getName() | ||
.equals(refreshToken.getAssociatedRealm()) == false) { | ||
Comment on lines
+1516
to
+1519
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is inconsistent, i.e. it first checks effective user but then checks authenticating realm. But it is an existing bug (the token was created by capturing effective user + authenticating realm). So this is kept as is. Also it does not really matter much in practice because we now capture full authentication object for new tokens and these logics are not really used much. |
||
logger.warn( | ||
"[{}] created the refresh token while authenticated by [{}] but is now authenticated by [{}]", | ||
refreshToken.getAssociatedUser(), | ||
refreshToken.getAssociatedRealm(), | ||
clientAuthentication.getAuthenticatingSubject().getRealm().getName() | ||
); | ||
return Optional.of(invalidGrantException("tokens must be refreshed by the creating client")); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1795,9 +1798,9 @@ static BytesReference createTokenDocument( | |
builder.field("authentication", originatingClientAuth.maybeRewriteForOlderVersion(userToken.getVersion()).encode()); | ||
} else { | ||
builder.field("user", originatingClientAuth.getEffectiveSubject().getUser().principal()) | ||
.field("realm", originatingClientAuth.getAuthenticatedBy().getName()); | ||
if (originatingClientAuth.getAuthenticatedBy().getDomain() != null) { | ||
builder.field("realm_domain", originatingClientAuth.getAuthenticatedBy().getDomain()); | ||
.field("realm", originatingClientAuth.getAuthenticatingSubject().getRealm().getName()); | ||
if (originatingClientAuth.getAuthenticatingSubject().getRealm().getDomain() != null) { | ||
builder.field("realm_domain", originatingClientAuth.getAuthenticatingSubject().getRealm().getDomain()); | ||
} | ||
} | ||
builder.endObject().endObject(); | ||
|
@@ -2546,7 +2549,7 @@ static final class RefreshTokenStatus { | |
this.invalidated = invalidated; | ||
// not used, filled-in for consistency's sake | ||
this.associatedUser = associatedAuthentication.getEffectiveSubject().getUser().principal(); | ||
this.associatedRealm = associatedAuthentication.getAuthenticatedBy().getName(); | ||
this.associatedRealm = associatedAuthentication.getAuthenticatingSubject().getRealm().getName(); | ||
this.associatedAuthentication = associatedAuthentication; | ||
this.refreshed = refreshed; | ||
this.refreshInstant = refreshInstant; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,7 +214,8 @@ private void buildUser(X509AuthenticationToken token, String principal, ActionLi | |
"pki_delegated_by_user", | ||
token.getDelegateeAuthentication().getEffectiveSubject().getUser().principal(), | ||
"pki_delegated_by_realm", | ||
token.getDelegateeAuthentication().getAuthenticatedBy().getName() | ||
// TODO: this should be the realm of effective subject | ||
token.getDelegateeAuthentication().getAuthenticatingSubject().getRealm().getName() | ||
Comment on lines
-217
to
+218
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bug. I'll have a separate PR just for it so it is not blended in a pure refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Happy to review that one, too. |
||
); | ||
} else { | ||
metadata = Map.of("pki_dn", token.dn()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This technically should be effectiveSubject. So I changed it be so. In practice, it does not matter because SAML realm authentication cannot have run-as. I add such assertion in other places (closer to where authentication is created).