-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[broker][authentication]Support pass http auth status #14044
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
Changes from all commits
77b049f
ce3c5d3
1badfaa
cbc79e2
8924aa6
3cc0a9d
3407283
a4d8718
78c7726
713b1d1
6c34f62
8c2ef67
c9f8000
3fb6e3b
97ae44d
197411f
b20a7d5
0467c96
3fc7d57
5b08617
ae5b447
e4efa86
bb1ebfe
3764121
673818c
49c1080
b1def58
485e961
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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.pulsar.broker.PulsarServerException; | ||
| import org.apache.pulsar.broker.ServiceConfiguration; | ||
| import org.apache.pulsar.broker.web.AuthenticationFilter; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -84,10 +85,10 @@ public AuthenticationService(ServiceConfiguration conf) throws PulsarServerExcep | |
| } | ||
| } | ||
|
|
||
| public String authenticateHttpRequest(HttpServletRequest request) throws AuthenticationException { | ||
| public String authenticateHttpRequest(HttpServletRequest request, AuthenticationDataSource authData) | ||
| throws AuthenticationException { | ||
| AuthenticationException authenticationException = null; | ||
| AuthenticationDataSource authData = new AuthenticationDataHttps(request); | ||
| String authMethodName = request.getHeader("X-Pulsar-Auth-Method-Name"); | ||
| String authMethodName = request.getHeader(AuthenticationFilter.PULSAR_AUTH_METHOD_NAME); | ||
|
|
||
| if (authMethodName != null) { | ||
| AuthenticationProvider providerToUse = providers.get(authMethodName); | ||
|
|
@@ -96,6 +97,11 @@ public String authenticateHttpRequest(HttpServletRequest request) throws Authent | |
| String.format("Unsupported authentication method: [%s].", authMethodName)); | ||
| } | ||
| try { | ||
| if (authData == null) { | ||
| AuthenticationState authenticationState = providerToUse.newHttpAuthState(request); | ||
|
michaeljmarshall marked this conversation as resolved.
|
||
| authData = authenticationState.getAuthDataSource(); | ||
| } | ||
| // Backward compatible, the authData value was null in the previous implementation | ||
| return providerToUse.authenticate(authData); | ||
| } catch (AuthenticationException e) { | ||
| if (LOG.isDebugEnabled()) { | ||
|
|
@@ -109,7 +115,8 @@ public String authenticateHttpRequest(HttpServletRequest request) throws Authent | |
| } else { | ||
| for (AuthenticationProvider provider : providers.values()) { | ||
| try { | ||
| return provider.authenticate(authData); | ||
| AuthenticationState authenticationState = provider.newHttpAuthState(request); | ||
|
Member
Author
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 necessary because providers are a list |
||
| return provider.authenticate(authenticationState.getAuthDataSource()); | ||
| } catch (AuthenticationException e) { | ||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Authentication failed for provider " + provider.getAuthMethodName() + ": " | ||
|
|
@@ -137,6 +144,15 @@ public String authenticateHttpRequest(HttpServletRequest request) throws Authent | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Mark this function as deprecated, it is recommended to use a method with the AuthenticationDataSource | ||
| * signature to implement it. | ||
| */ | ||
| @Deprecated | ||
| public String authenticateHttpRequest(HttpServletRequest request) throws AuthenticationException { | ||
|
michaeljmarshall marked this conversation as resolved.
|
||
| return authenticateHttpRequest(request, null); | ||
| } | ||
|
|
||
| public AuthenticationProvider getAuthenticationProvider(String authMethodName) { | ||
| return providers.get(authMethodName); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| import javax.servlet.http.HttpServletResponse; | ||
| import org.apache.pulsar.broker.authentication.AuthenticationDataHttps; | ||
| import org.apache.pulsar.broker.authentication.AuthenticationService; | ||
| import org.apache.pulsar.broker.authentication.AuthenticationState; | ||
| import org.apache.pulsar.common.sasl.SaslConstants; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -44,6 +45,8 @@ public class AuthenticationFilter implements Filter { | |
|
|
||
| public static final String AuthenticatedRoleAttributeName = AuthenticationFilter.class.getName() + "-role"; | ||
| public static final String AuthenticatedDataAttributeName = AuthenticationFilter.class.getName() + "-data"; | ||
| public static final String PULSAR_AUTH_METHOD_NAME = "X-Pulsar-Auth-Method-Name"; | ||
|
Member
Author
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. To avoid introducing the pulsar-client-api package, define it again here |
||
|
|
||
|
|
||
| public AuthenticationFilter(AuthenticationService authenticationService) { | ||
| this.authenticationService = authenticationService; | ||
|
|
@@ -71,10 +74,21 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha | |
|
|
||
| if (!isSaslRequest(httpRequest)) { | ||
| // not sasl type, return role directly. | ||
| String role = authenticationService.authenticateHttpRequest((HttpServletRequest) request); | ||
| String authMethodName = httpRequest.getHeader(PULSAR_AUTH_METHOD_NAME); | ||
| String role; | ||
| if (authMethodName != null && authenticationService.getAuthenticationProvider(authMethodName) != null) { | ||
| AuthenticationState authenticationState = authenticationService | ||
| .getAuthenticationProvider(authMethodName).newHttpAuthState(httpRequest); | ||
| request.setAttribute(AuthenticatedDataAttributeName, authenticationState.getAuthDataSource()); | ||
|
michaeljmarshall marked this conversation as resolved.
|
||
| role = authenticationService.authenticateHttpRequest( | ||
| (HttpServletRequest) request, authenticationState.getAuthDataSource()); | ||
| } else { | ||
| request.setAttribute(AuthenticatedDataAttributeName, | ||
|
Member
Author
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. Backward compatible |
||
| new AuthenticationDataHttps((HttpServletRequest) request)); | ||
| role = authenticationService.authenticateHttpRequest((HttpServletRequest) request); | ||
| } | ||
| request.setAttribute(AuthenticatedRoleAttributeName, role); | ||
| request.setAttribute(AuthenticatedDataAttributeName, | ||
| new AuthenticationDataHttps((HttpServletRequest) request)); | ||
|
|
||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("[{}] Authenticated HTTP request with role {}", request.getRemoteAddr(), role); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,6 @@ | |
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.apache.pulsar.broker.PulsarService; | ||
| import org.apache.pulsar.broker.ServiceConfiguration; | ||
| import org.apache.pulsar.broker.authentication.AuthenticationDataHttps; | ||
| import org.apache.pulsar.broker.authentication.AuthenticationDataSource; | ||
| import org.apache.pulsar.broker.authorization.AuthorizationService; | ||
| import org.apache.pulsar.broker.namespace.LookupOptions; | ||
|
|
@@ -137,8 +136,8 @@ public String originalPrincipal() { | |
| return httpRequest.getHeader(ORIGINAL_PRINCIPAL_HEADER); | ||
| } | ||
|
|
||
| public AuthenticationDataHttps clientAuthData() { | ||
| return (AuthenticationDataHttps) httpRequest.getAttribute(AuthenticationFilter.AuthenticatedDataAttributeName); | ||
| public AuthenticationDataSource clientAuthData() { | ||
|
Member
Author
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.
|
||
| return (AuthenticationDataSource) httpRequest.getAttribute(AuthenticationFilter.AuthenticatedDataAttributeName); | ||
| } | ||
|
|
||
| public boolean isRequestHttps() { | ||
|
|
@@ -1175,7 +1174,8 @@ && pulsar().getBrokerService().isAuthorizationEnabled()) { | |
| return FutureUtil.failedFuture( | ||
| new RestException(Status.UNAUTHORIZED, "Need to authenticate to perform the request")); | ||
| } | ||
| AuthenticationDataHttps authData = clientAuthData(); | ||
|
|
||
| AuthenticationDataSource authData = clientAuthData(); | ||
|
Member
Author
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. AuthenticationDataHttps is the return value of the error, use AuthenticationDataSource instead of it |
||
| authData.setSubscription(subscription); | ||
| return pulsar().getBrokerService().getAuthorizationService() | ||
| .allowTopicOperationAsync(topicName, operation, originalPrincipal(), clientAppId(), authData) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.