@@ -61,12 +61,13 @@ public class AuthenticationService {
6161 private final String nodeName ;
6262 private final AnonymousUser anonymousUser ;
6363 private final TokenService tokenService ;
64+ private final ApiKeyService apiKeyService ;
6465 private final boolean runAsEnabled ;
6566 private final boolean isAnonymousUserEnabled ;
6667
6768 public AuthenticationService (Settings settings , Realms realms , AuditTrailService auditTrail ,
6869 AuthenticationFailureHandler failureHandler , ThreadPool threadPool ,
69- AnonymousUser anonymousUser , TokenService tokenService ) {
70+ AnonymousUser anonymousUser , TokenService tokenService , ApiKeyService apiKeyService ) {
7071 this .nodeName = Node .NODE_NAME_SETTING .get (settings );
7172 this .realms = realms ;
7273 this .auditTrail = auditTrail ;
@@ -76,6 +77,7 @@ public AuthenticationService(Settings settings, Realms realms, AuditTrailService
7677 this .runAsEnabled = AuthenticationServiceField .RUN_AS_ENABLED .get (settings );
7778 this .isAnonymousUserEnabled = AnonymousUser .isAnonymousEnabled (settings );
7879 this .tokenService = tokenService ;
80+ this .apiKeyService = apiKeyService ;
7981 }
8082
8183 /**
@@ -181,7 +183,7 @@ private void authenticateAsync() {
181183 if (userToken != null ) {
182184 writeAuthToContext (userToken .getAuthentication ());
183185 } else {
184- extractToken ( this :: consumeToken );
186+ checkForApiKey ( );
185187 }
186188 }, e -> {
187189 if (e instanceof ElasticsearchSecurityException &&
@@ -196,6 +198,31 @@ private void authenticateAsync() {
196198 });
197199 }
198200
201+ private void checkForApiKey () {
202+ apiKeyService .authenticateWithApiKeyIfPresent (threadContext , ActionListener .wrap (authResult -> {
203+ if (authResult .isAuthenticated ()) {
204+ final User user = authResult .getUser ();
205+ authenticatedBy = new RealmRef ("_es_api_key" , "_es_api_key" , nodeName );
206+ writeAuthToContext (new Authentication (user , authenticatedBy , null ));
207+ } else if (authResult .getStatus () == AuthenticationResult .Status .TERMINATE ) {
208+ Exception e = (authResult .getException () != null ) ? authResult .getException ()
209+ : Exceptions .authenticationError (authResult .getMessage ());
210+ listener .onFailure (e );
211+ } else {
212+ if (authResult .getMessage () != null ) {
213+ if (authResult .getException () != null ) {
214+ logger .warn (new ParameterizedMessage ("Authentication using apikey failed - {}" , authResult .getMessage ()),
215+ authResult .getException ());
216+ } else {
217+ logger .warn ("Authentication using apikey failed - {}" , authResult .getMessage ());
218+ }
219+ }
220+ extractToken (this ::consumeToken );
221+ }
222+ },
223+ e -> listener .onFailure (request .exceptionProcessingRequest (e , null ))));
224+ }
225+
199226 /**
200227 * Looks to see if the request contains an existing {@link Authentication} and if so, that authentication will be used. The
201228 * consumer is called if no exception was thrown while trying to read the authentication and may be called with a {@code null}
0 commit comments