Skip to content

Commit 0ac81ce

Browse files
authored
Remove deprecated Authentication#getAuthenticatedBy (#91104)
This PR removes the deprecated Authentication#getAuthenticatedBy method and replaces its usages with #getAuthenticatingSubject#getRealm Relates: #88494
1 parent 4a575e7 commit 0ac81ce

File tree

18 files changed

+82
-83
lines changed

18 files changed

+82
-83
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public SamlAuthenticateResponse(StreamInput in) throws IOException {
4444

4545
public SamlAuthenticateResponse(Authentication authentication, String tokenString, String refreshToken, TimeValue expiresIn) {
4646
this.principal = authentication.getEffectiveSubject().getUser().principal();
47-
this.realm = authentication.getAuthenticatedBy().getName();
47+
this.realm = authentication.getEffectiveSubject().getRealm().getName();
4848
this.tokenString = tokenString;
4949
this.refreshToken = refreshToken;
5050
this.expiresIn = expiresIn;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,6 @@ public boolean isRunAs() {
167167
return authenticatingSubject != effectiveSubject;
168168
}
169169

170-
/**
171-
* Use {@code getAuthenticatingSubject().getRealm()} instead.
172-
*/
173-
@Deprecated
174-
public RealmRef getAuthenticatedBy() {
175-
return authenticatingSubject.getRealm();
176-
}
177-
178170
/**
179171
* The use case for this method is largely trying to tell whether there is a run-as user
180172
* and can be replaced by {@code isRunAs}
@@ -367,7 +359,7 @@ public boolean isAssignedToDomain() {
367359
}
368360

369361
public boolean isAuthenticatedWithServiceAccount() {
370-
return ServiceAccountSettings.REALM_TYPE.equals(getAuthenticatedBy().getType());
362+
return ServiceAccountSettings.REALM_TYPE.equals(getAuthenticatingSubject().getRealm().getType());
371363
}
372364

373365
/**
@@ -568,12 +560,12 @@ public void toXContentFragment(XContentBuilder builder) throws IOException {
568560
builder.field(User.Fields.METADATA.getPreferredName(), user.metadata());
569561
builder.field(User.Fields.ENABLED.getPreferredName(), user.enabled());
570562
builder.startObject(User.Fields.AUTHENTICATION_REALM.getPreferredName());
571-
builder.field(User.Fields.REALM_NAME.getPreferredName(), getAuthenticatedBy().getName());
572-
builder.field(User.Fields.REALM_TYPE.getPreferredName(), getAuthenticatedBy().getType());
563+
builder.field(User.Fields.REALM_NAME.getPreferredName(), getAuthenticatingSubject().getRealm().getName());
564+
builder.field(User.Fields.REALM_TYPE.getPreferredName(), getAuthenticatingSubject().getRealm().getType());
573565
// domain name is generally ambiguous, because it can change during the lifetime of the authentication,
574566
// but it is good enough for display purposes (including auditing)
575-
if (getAuthenticatedBy().getDomain() != null) {
576-
builder.field(User.Fields.REALM_DOMAIN.getPreferredName(), getAuthenticatedBy().getDomain().name());
567+
if (getAuthenticatingSubject().getRealm().getDomain() != null) {
568+
builder.field(User.Fields.REALM_DOMAIN.getPreferredName(), getAuthenticatingSubject().getRealm().getDomain().name());
577569
}
578570
builder.endObject();
579571
builder.startObject(User.Fields.LOOKUP_REALM.getPreferredName());
@@ -584,10 +576,10 @@ public void toXContentFragment(XContentBuilder builder) throws IOException {
584576
builder.field(User.Fields.REALM_DOMAIN.getPreferredName(), getLookedUpBy().getDomain().name());
585577
}
586578
} else {
587-
builder.field(User.Fields.REALM_NAME.getPreferredName(), getAuthenticatedBy().getName());
588-
builder.field(User.Fields.REALM_TYPE.getPreferredName(), getAuthenticatedBy().getType());
589-
if (getAuthenticatedBy().getDomain() != null) {
590-
builder.field(User.Fields.REALM_DOMAIN.getPreferredName(), getAuthenticatedBy().getDomain().name());
579+
builder.field(User.Fields.REALM_NAME.getPreferredName(), getAuthenticatingSubject().getRealm().getName());
580+
builder.field(User.Fields.REALM_TYPE.getPreferredName(), getAuthenticatingSubject().getRealm().getType());
581+
if (getAuthenticatingSubject().getRealm().getDomain() != null) {
582+
builder.field(User.Fields.REALM_DOMAIN.getPreferredName(), getAuthenticatingSubject().getRealm().getDomain().name());
591583
}
592584
}
593585
builder.endObject();

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void testIsServiceAccount() {
224224
authentication = AuthenticationTestHelper.builder().serviceAccount().build();
225225
} else {
226226
authentication = randomValueOtherThanMany(
227-
authc -> "_service_account".equals(authc.getAuthenticatedBy().getName()),
227+
authc -> "_service_account".equals(authc.getAuthenticatingSubject().getRealm().getName()),
228228
() -> AuthenticationTestHelper.builder().build()
229229
);
230230
}

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmIntegTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@ public void testOperationsOnReservedUsers() throws Exception {
745745
Collections.singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword()))
746746
).execute(AuthenticateAction.INSTANCE, AuthenticateRequest.INSTANCE).get();
747747
assertThat(authenticateResponse.authentication().getEffectiveSubject().getUser().principal(), is(username));
748-
assertThat(authenticateResponse.authentication().getAuthenticatedBy().getName(), equalTo("reserved"));
749-
assertThat(authenticateResponse.authentication().getAuthenticatedBy().getType(), equalTo("reserved"));
748+
assertThat(authenticateResponse.authentication().getAuthenticatingSubject().getRealm().getName(), equalTo("reserved"));
749+
assertThat(authenticateResponse.authentication().getAuthenticatingSubject().getRealm().getType(), equalTo("reserved"));
750750
assertNull(authenticateResponse.authentication().getLookedUpBy());
751751
}
752752

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected void doExecute(Task task, OpenIdConnectLogoutRequest request, ActionLi
6666
final String token = request.getToken();
6767
tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap(tuple -> {
6868
final Authentication authentication = tuple.v1();
69+
assert false == authentication.isRunAs() : "oidc realm authentication cannot have run-as";
6970
final Map<String, Object> tokenMetadata = tuple.v2();
7071
validateAuthenticationAndMetadata(authentication, tokenMetadata);
7172
tokenService.invalidateAccessToken(token, ActionListener.wrap(result -> {
@@ -86,7 +87,7 @@ protected void doExecute(Task task, OpenIdConnectLogoutRequest request, ActionLi
8687

8788
private OpenIdConnectLogoutResponse buildResponse(Authentication authentication, Map<String, Object> tokenMetadata) {
8889
final String idTokenHint = (String) getFromMetadata(tokenMetadata, "id_token_hint");
89-
final Realm realm = this.realms.realm(authentication.getAuthenticatedBy().getName());
90+
final Realm realm = this.realms.realm(authentication.getEffectiveSubject().getRealm().getName());
9091
final JWT idToken;
9192
try {
9293
idToken = JWTParser.parse(idTokenHint);
@@ -108,11 +109,11 @@ private void validateAuthenticationAndMetadata(Authentication authentication, Ma
108109
throw new ElasticsearchSecurityException("No active user");
109110
}
110111

111-
final Authentication.RealmRef ref = authentication.getAuthenticatedBy();
112+
final Authentication.RealmRef ref = authentication.getEffectiveSubject().getRealm();
112113
if (ref == null || Strings.isNullOrEmpty(ref.getName())) {
113114
throw new ElasticsearchSecurityException("Authentication {} has no authenticating realm", authentication);
114115
}
115-
final Realm realm = this.realms.realm(authentication.getAuthenticatedBy().getName());
116+
final Realm realm = this.realms.realm(authentication.getEffectiveSubject().getRealm().getName());
116117
if (realm == null) {
117118
throw new ElasticsearchSecurityException("Authenticating realm {} does not exist", ref.getName());
118119
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlAuthenticateAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ protected void doExecute(Task task, SamlAuthenticateRequest request, ActionListe
6969
return;
7070
}
7171
assert authentication != null : "authentication should never be null at this point";
72+
assert false == authentication.isRunAs() : "saml realm authentication cannot have run-as";
7273
@SuppressWarnings("unchecked")
7374
final Map<String, Object> tokenMeta = (Map<String, Object>) result.getMetadata().get(SamlRealm.CONTEXT_TOKEN_DATA);
7475
tokenService.createOAuth2Tokens(

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected void doExecute(Task task, SamlLogoutRequest request, ActionListener<Sa
5858
final String token = request.getToken();
5959
tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap(tuple -> {
6060
Authentication authentication = tuple.v1();
61+
assert false == authentication.isRunAs() : "saml realm authentication cannot have run-as";
6162
final Map<String, Object> tokenMetadata = tuple.v2();
6263
SamlLogoutResponse response = buildResponse(authentication, tokenMetadata);
6364
tokenService.invalidateAccessToken(token, ActionListener.wrap(created -> {
@@ -134,9 +135,9 @@ private String getMetadataString(Map<String, Object> metadata, String key) {
134135
}
135136

136137
private SamlRealm findRealm(Authentication authentication) {
137-
final Authentication.RealmRef ref = authentication.getAuthenticatedBy();
138+
final Authentication.RealmRef ref = authentication.getEffectiveSubject().getRealm();
138139
if (ref == null || Strings.isNullOrEmpty(ref.getName())) {
139-
throw SamlUtils.samlException("Authentication {} has no authenticating realm", authentication);
140+
throw SamlUtils.samlException("Authentication {} has no effective realm", authentication);
140141
}
141142
final Realm realm = realms.realm(ref.getName());
142143
if (realm == null) {

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrail.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public void authenticationSuccess(String requestId, Authentication authenticatio
457457
)
458458
) == false) {
459459
// this is redundant information maintained for bwc purposes
460-
final String authnRealm = authentication.getAuthenticatedBy().getName();
460+
final String authnRealm = authentication.getAuthenticatingSubject().getRealm().getName();
461461
new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE)
462462
.with(EVENT_ACTION_FIELD_NAME, "authentication_success")
463463
.with(REALM_FIELD_NAME, authnRealm)
@@ -1531,10 +1531,10 @@ LogEntryBuilder withRestUriAndMethod(RestRequest request) {
15311531

15321532
LogEntryBuilder withRunAsSubject(Authentication authentication) {
15331533
logEntry.with(PRINCIPAL_FIELD_NAME, authentication.getAuthenticatingSubject().getUser().principal())
1534-
.with(PRINCIPAL_REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName())
1534+
.with(PRINCIPAL_REALM_FIELD_NAME, authentication.getAuthenticatingSubject().getRealm().getName())
15351535
.with(PRINCIPAL_RUN_AS_FIELD_NAME, authentication.getEffectiveSubject().getUser().principal());
1536-
if (authentication.getAuthenticatedBy().getDomain() != null) {
1537-
logEntry.with(PRINCIPAL_DOMAIN_FIELD_NAME, authentication.getAuthenticatedBy().getDomain().name());
1536+
if (authentication.getAuthenticatingSubject().getRealm().getDomain() != null) {
1537+
logEntry.with(PRINCIPAL_DOMAIN_FIELD_NAME, authentication.getAuthenticatingSubject().getRealm().getDomain().name());
15381538
}
15391539
if (authentication.getLookedUpBy() != null) {
15401540
logEntry.with(PRINCIPAL_RUN_AS_REALM_FIELD_NAME, authentication.getLookedUpBy().getName());
@@ -1625,7 +1625,7 @@ LogEntryBuilder withAuthentication(Authentication authentication) {
16251625
// No domain information is needed here since API key itself does not work across realms
16261626
}
16271627
} else {
1628-
final Authentication.RealmRef authenticatedBy = authentication.getAuthenticatedBy();
1628+
final Authentication.RealmRef authenticatedBy = authentication.getAuthenticatingSubject().getRealm();
16291629
if (authentication.isRunAs()) {
16301630
final Authentication.RealmRef lookedUpBy = authentication.getLookedUpBy();
16311631
logEntry.with(PRINCIPAL_REALM_FIELD_NAME, lookedUpBy.getName())

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,17 +1513,20 @@ private static Optional<ElasticsearchSecurityException> checkClientCanRefresh(
15131513
clientAuthentication.getEffectiveSubject().getUser().principal()
15141514
);
15151515
return Optional.of(invalidGrantException("tokens must be refreshed by the creating client"));
1516-
} else if (clientAuthentication.getAuthenticatedBy().getName().equals(refreshToken.getAssociatedRealm()) == false) {
1517-
logger.warn(
1518-
"[{}] created the refresh token while authenticated by [{}] but is now authenticated by [{}]",
1519-
refreshToken.getAssociatedUser(),
1520-
refreshToken.getAssociatedRealm(),
1521-
clientAuthentication.getAuthenticatedBy().getName()
1522-
);
1523-
return Optional.of(invalidGrantException("tokens must be refreshed by the creating client"));
1524-
} else {
1525-
return Optional.empty();
1526-
}
1516+
} else if (clientAuthentication.getAuthenticatingSubject()
1517+
.getRealm()
1518+
.getName()
1519+
.equals(refreshToken.getAssociatedRealm()) == false) {
1520+
logger.warn(
1521+
"[{}] created the refresh token while authenticated by [{}] but is now authenticated by [{}]",
1522+
refreshToken.getAssociatedUser(),
1523+
refreshToken.getAssociatedRealm(),
1524+
clientAuthentication.getAuthenticatingSubject().getRealm().getName()
1525+
);
1526+
return Optional.of(invalidGrantException("tokens must be refreshed by the creating client"));
1527+
} else {
1528+
return Optional.empty();
1529+
}
15271530
}
15281531
}
15291532

@@ -1795,9 +1798,9 @@ static BytesReference createTokenDocument(
17951798
builder.field("authentication", originatingClientAuth.maybeRewriteForOlderVersion(userToken.getVersion()).encode());
17961799
} else {
17971800
builder.field("user", originatingClientAuth.getEffectiveSubject().getUser().principal())
1798-
.field("realm", originatingClientAuth.getAuthenticatedBy().getName());
1799-
if (originatingClientAuth.getAuthenticatedBy().getDomain() != null) {
1800-
builder.field("realm_domain", originatingClientAuth.getAuthenticatedBy().getDomain());
1801+
.field("realm", originatingClientAuth.getAuthenticatingSubject().getRealm().getName());
1802+
if (originatingClientAuth.getAuthenticatingSubject().getRealm().getDomain() != null) {
1803+
builder.field("realm_domain", originatingClientAuth.getAuthenticatingSubject().getRealm().getDomain());
18011804
}
18021805
}
18031806
builder.endObject().endObject();
@@ -2546,7 +2549,7 @@ static final class RefreshTokenStatus {
25462549
this.invalidated = invalidated;
25472550
// not used, filled-in for consistency's sake
25482551
this.associatedUser = associatedAuthentication.getEffectiveSubject().getUser().principal();
2549-
this.associatedRealm = associatedAuthentication.getAuthenticatedBy().getName();
2552+
this.associatedRealm = associatedAuthentication.getAuthenticatingSubject().getRealm().getName();
25502553
this.associatedAuthentication = associatedAuthentication;
25512554
this.refreshed = refreshed;
25522555
this.refreshInstant = refreshInstant;

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/pki/PkiRealm.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ private void buildUser(X509AuthenticationToken token, String principal, ActionLi
214214
"pki_delegated_by_user",
215215
token.getDelegateeAuthentication().getEffectiveSubject().getUser().principal(),
216216
"pki_delegated_by_realm",
217-
token.getDelegateeAuthentication().getAuthenticatedBy().getName()
217+
// TODO: this should be the realm of effective subject
218+
token.getDelegateeAuthentication().getAuthenticatingSubject().getRealm().getName()
218219
);
219220
} else {
220221
metadata = Map.of("pki_dn", token.dn());

0 commit comments

Comments
 (0)