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

Parameterize the Security plugin with the request-wise thread context supplier #95040

Merged

Conversation

albertzaharovits
Copy link
Contributor

@albertzaharovits albertzaharovits commented Apr 5, 2023

When Security authenticates a request (AuthenticationService#authenticate) it mostly looks in the thread context for all the request details that it needs.
Consequently, there's code that populates the thread context before running authentication.
This PR brings all the code that's responsible for populating the said thread context, as a parameter to the Security plugin.
This is required in the follow-up that is going to involve setting up the thread context (given the aforementioned parameter), and running the authentication under that context, before the request is dispatched (rather than "while" it's being dispatched, currently).

@@ -510,7 +501,6 @@ private static void sendContentTypeErrorMessage(@Nullable List<String> contentTy

private void tryAllHandlers(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) throws Exception {
try {
copyRestHeaders(request, threadContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The RestController is not responsible to set (part of) the thread context anymore, mainly because the Security plugin cannot hook-in at this point. Instead, the Security plugin wraps the RestController before passing it to the Netty4HttpServerTransport that it builds (Security builds a HttpServerTransport in order to implement TLS).

Comment on lines 1647 to 1648
populateClientCertificate.accept(restRequest.getHttpChannel(), threadContext);
RemoteHostHeader.process(restRequest, threadContext);
Copy link
Contributor Author

@albertzaharovits albertzaharovits Apr 5, 2023

Choose a reason for hiding this comment

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

This is the additional thread context that Security constructs, brought together in this single place. This context must be set before AuthenticationService#authenticate is attempted.

Comment on lines -104 to -110
if (extractClientCertificate) {
HttpChannel httpChannel = request.getHttpChannel();
SSLEngineUtils.extractClientCertificates(logger, threadContext, httpChannel);
}

final RestRequest wrappedRequest = maybeWrapRestRequest(request);
RemoteHostHeader.process(request, threadContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These things moved.

Comment on lines 109 to 112
HttpServerTransport.Dispatcher dispatcherWithContext = HttpServerTransport.Dispatcher.dispatchWithThreadContextWrapper(
dispatcher,
(httpPreRequest -> dispatcherContext.accept(httpPreRequest, threadPool.getThreadContext()))
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, the RestController was in charge to build the thread context from the request headers.
Now, the dispatcherContext does that, and it is exposed by the ActionModule, to be used by the plugins that build HttpServerTransport implementations (which take the RestController as the argument).

@albertzaharovits albertzaharovits changed the title Make Security plugin to request thread context Parameterize the Security plugin with the request-wise thread context supplier Apr 5, 2023
@albertzaharovits albertzaharovits added :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) >refactoring labels Apr 5, 2023
@@ -1592,6 +1596,7 @@ public Map<String, Supplier<HttpServerTransport>> getHttpTransports(
NamedXContentRegistry xContentRegistry,
NetworkService networkService,
HttpServerTransport.Dispatcher dispatcher,
BiConsumer<HttpPreRequest, ThreadContext> dispatcherContext,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is important. The Security plugin needs access to this supplier of thread context.

@albertzaharovits albertzaharovits marked this pull request as ready for review April 5, 2023 16:00
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine elasticsearchmachine added the Team:Security Meta label for security team label Apr 6, 2023
Copy link
Contributor

@jakelandis jakelandis left a comment

Choose a reason for hiding this comment

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

change is looking good, thanks for the change away from the dispatch wrapper i think this reads much better. just a couple minor questions and nitpicks

@albertzaharovits
Copy link
Contributor Author

Thank you for the speedy reviews @jakelandis !
Please take another look.

Copy link
Contributor

@jakelandis jakelandis left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the discussion and updates

@albertzaharovits albertzaharovits merged commit f662a19 into elastic:main Apr 10, 2023
@albertzaharovits albertzaharovits deleted the security-plugin-thread-context branch April 10, 2023 14:33
albertzaharovits added a commit to albertzaharovits/elasticsearch that referenced this pull request Jun 13, 2023
…xt supplier (elastic#95040)

When `Security` authenticates a request (AuthenticationService#authenticate) it mostly
looks in the thread context for all the request details that it needs.
Consequently, there's code that populates the thread context before invoking authentication.
This PR brings all the code that's responsible for populating the said thread context,
as a parameter to the `Security` plugin.
This is required in the follow-up that is going to involve setting up the thread context
(given the aforementioned parameter), and running the authentication under that
context, before the request is dispatched (rather than "while" it's being dispatched, currently).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>refactoring :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) Team:Security Meta label for security team v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants