Skip to content

Commit 0e99c89

Browse files
committed
Add internal and anonymous authentication types
This change builds upon the work done in elastic#35970 and adds appropriate types for anonymous and internal authentication to the `AuthenticationType` enum.
1 parent 602feeb commit 0e99c89

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext;
1414
import org.elasticsearch.node.Node;
1515
import org.elasticsearch.xpack.core.security.authc.Authentication;
16+
import org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType;
1617
import org.elasticsearch.xpack.core.security.user.User;
1718

1819
import java.io.IOException;
20+
import java.util.Collections;
1921
import java.util.Objects;
2022
import java.util.function.Consumer;
2123

@@ -71,7 +73,8 @@ public void setUser(User user, Version version) {
7173
} else {
7274
lookedUpBy = null;
7375
}
74-
setAuthentication(new Authentication(user, authenticatedBy, lookedUpBy, version));
76+
setAuthentication(
77+
new Authentication(user, authenticatedBy, lookedUpBy, version, AuthenticationType.INTERNAL, Collections.emptyMap()));
7578
}
7679

7780
/** Writes the authentication to the thread context */
@@ -89,7 +92,7 @@ private void setAuthentication(Authentication authentication) {
8992
*/
9093
public void executeAsUser(User user, Consumer<StoredContext> consumer, Version version) {
9194
final StoredContext original = threadContext.newStoredContext(true);
92-
try (ThreadContext.StoredContext ctx = threadContext.stashContext()) {
95+
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
9396
setUser(user, version);
9497
consumer.accept(original);
9598
}
@@ -102,9 +105,9 @@ public void executeAsUser(User user, Consumer<StoredContext> consumer, Version v
102105
public void executeAfterRewritingAuthentication(Consumer<StoredContext> consumer, Version version) {
103106
final StoredContext original = threadContext.newStoredContext(true);
104107
final Authentication authentication = Objects.requireNonNull(userSettings.getAuthentication());
105-
try (ThreadContext.StoredContext ctx = threadContext.stashContext()) {
108+
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
106109
setAuthentication(new Authentication(authentication.getUser(), authentication.getAuthenticatedBy(),
107-
authentication.getLookedUpBy(), version));
110+
authentication.getLookedUpBy(), version, authentication.getAuthenticationType(), authentication.getMetadata()));
108111
consumer.accept(original);
109112
}
110113
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ public int hashCode() {
274274
public enum AuthenticationType {
275275
REALM,
276276
API_KEY,
277-
TOKEN
277+
TOKEN,
278+
ANONYMOUS,
279+
INTERNAL
278280
}
279281
}
280282

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.transport.TransportMessage;
2525
import org.elasticsearch.xpack.core.common.IteratingActionListener;
2626
import org.elasticsearch.xpack.core.security.authc.Authentication;
27+
import org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType;
2728
import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef;
2829
import org.elasticsearch.xpack.core.security.authc.AuthenticationFailureHandler;
2930
import org.elasticsearch.xpack.core.security.authc.AuthenticationResult;
@@ -40,6 +41,7 @@
4041
import org.elasticsearch.xpack.security.audit.AuditUtil;
4142
import org.elasticsearch.xpack.security.authc.support.RealmUserLookup;
4243

44+
import java.util.Collections;
4345
import java.util.LinkedHashMap;
4446
import java.util.List;
4547
import java.util.Map;
@@ -363,7 +365,8 @@ void handleNullToken() {
363365
authentication = new Authentication(fallbackUser, authenticatedBy, null);
364366
} else if (isAnonymousUserEnabled) {
365367
RealmRef authenticatedBy = new RealmRef("__anonymous", "__anonymous", nodeName);
366-
authentication = new Authentication(anonymousUser, authenticatedBy, null);
368+
authentication = new Authentication(anonymousUser, authenticatedBy, null, Version.CURRENT, AuthenticationType.ANONYMOUS,
369+
Collections.emptyMap());
367370
} else {
368371
authentication = null;
369372
}

0 commit comments

Comments
 (0)