Skip to content

Commit a046bf9

Browse files
michaeljmarshalleolivelli
authored andcommitted
[PIP 97] Update Authentication Interfaces to Include Async Authentication Methods (apache#12104)
Master Issue: apache#12105 ### Motivation As the first part of PIP-97, we need to update the interfaces. This PR is the only PR that will update interfaces. It should not introduce any breaking changes. ### Modifications #### AuthenticationProvider * Add `AuthenticationProvider#authenticateAsync`. Include a default implementation that calls the `authenticate` method. Note that current implementations should all be non-blocking, so there is no need to push the execution to a separate thread. * Deprecate `AuthenticationProvider#authenticate`. * Add `AuthenticationProvider#authenticateHttpRequestAsync`. This method is complicated. It is only called when using the SASL authentication provider (this is hard coded into the Pulsar code base). As such, I would argue that it is worth removing support for this unreachable method and then refactor the SASL authentication provider. I annotated this method with `@InterfaceStability.Unstable` and added details to the Javadoc in order to communicate the uncertainty of this method's future. I am happy to discuss this in more detail though. * Deprecate `AuthenticationProvider#authenticateHttpRequest`. #### AuthenticationState * Add `AuthenticationState#authenticateAsync`. Include a default implementation that calls the `authenticate` method and then performs a check to determine what result to return. Note that current implementations should all be non-blocking, so there is no need to push the execution to a separate thread. * Deprecate `AuthenticationState#authenticate`. The preferred method is `AuthenticationState#authenticateAsync`. * Deprecate `AuthenticationState#isComplete`. This method can be avoided by inferring authentication completeness from the result of `AuthenticationState#authenticateAsync`. When the result is `null`, auth is complete. When it is not `null`, auth is not complete. Since the result of the `authenticateAsync` method is the body delivered to the client, this seems like a reasonable abstraction to make. As a consequence, the `AuthenticationState` is simpler and also avoids certain thread safety issues that might arise when calling `isComplete` from a different thread. #### AuthenticationDataSource * Deprecate `AuthenticationDataSource#authenticate`. This method is not called by the Pulsar authentication framework. This needs to be deprecated to prevent confusion for end users seeking to extend the authentication framework. There is no need for an async version of this method. ### Verifying this change These changes only affect the interfaces. I will need to add tests to verify the correctness of the default implementations in this PR. ### Does this pull request potentially affect one of the following parts: Yes, it affects the public API. That is why it has a PIP. ### Documentation I've updated the Javadocs. There is not any current documentation on implementing your own authentication provider, so I think updating Javadocs is sufficient documentation, for now.
1 parent 1c077fd commit a046bf9

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationDataSource.java

+3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ default String getCommandData() {
102102
/**
103103
* Evaluate and challenge the data that passed in, and return processed data back.
104104
* It is used for mutual authentication like SASL.
105+
* NOTE: this method is not called by the Pulsar authentication framework.
106+
* @deprecated use {@link AuthenticationProvider} or {@link AuthenticationState}.
105107
*/
108+
@Deprecated
106109
default AuthData authenticate(AuthData data) throws AuthenticationException {
107110
throw new AuthenticationException("Not supported");
108111
}

pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProvider.java

+56
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323

2424
import java.net.SocketAddress;
25+
import java.util.concurrent.CompletableFuture;
2526
import javax.naming.AuthenticationException;
2627

2728
import javax.net.ssl.SSLSession;
@@ -30,6 +31,8 @@
3031

3132
import org.apache.pulsar.broker.ServiceConfiguration;
3233
import org.apache.pulsar.common.api.AuthData;
34+
import org.apache.pulsar.common.classification.InterfaceStability;
35+
import org.apache.pulsar.common.util.FutureUtil;
3336

3437
/**
3538
* Provider of authentication mechanism
@@ -51,6 +54,29 @@ public interface AuthenticationProvider extends Closeable {
5154
*/
5255
String getAuthMethodName();
5356

57+
/**
58+
* Validate the authentication for the given credentials with the specified authentication data.
59+
* This method is useful in one stage authentication, if you're not doing one stage or if you're providing
60+
* your own state implementation for one stage authentication, it should return a failed future.
61+
*
62+
* <p>Warning: the calling thread is an IO thread. Any implementation that relies on blocking behavior
63+
* must ensure that the execution is completed using a separate thread pool to ensure IO threads
64+
* are never blocked.</p>
65+
*
66+
* @param authData authentication data generated while initiating a connection. There are several types,
67+
* including, but not strictly limited to, {@link AuthenticationDataHttp},
68+
* {@link AuthenticationDataHttps}, and {@link AuthenticationDataCommand}.
69+
* @return A completed future with the "role" string for the authenticated connection, if authentication is
70+
* successful, or a failed future if the authData is not valid.
71+
*/
72+
default CompletableFuture<String> authenticateAsync(AuthenticationDataSource authData) {
73+
try {
74+
return CompletableFuture.completedFuture(this.authenticate(authData));
75+
} catch (AuthenticationException e) {
76+
return FutureUtil.failedFuture(e);
77+
}
78+
}
79+
5480
/**
5581
* Validate the authentication for the given credentials with the specified authentication data.
5682
* This method is useful in one stage authn, if you're not doing one stage or if you're providing
@@ -61,7 +87,9 @@ public interface AuthenticationProvider extends Closeable {
6187
* @return the "role" string for the authenticated connection, if the authentication was successful
6288
* @throws AuthenticationException
6389
* if the credentials are not valid
90+
* @deprecated use and implement {@link AuthenticationProvider#authenticateAsync(AuthenticationDataSource)} instead.
6491
*/
92+
@Deprecated
6593
default String authenticate(AuthenticationDataSource authData) throws AuthenticationException {
6694
throw new AuthenticationException("Not supported");
6795
}
@@ -76,10 +104,38 @@ default AuthenticationState newAuthState(AuthData authData,
76104
return new OneStageAuthenticationState(authData, remoteAddress, sslSession, this);
77105
}
78106

107+
/**
108+
* Validate the authentication for the given credentials with the specified authentication data.
109+
*
110+
* <p>Warning: the calling thread is an IO thread. Any implementations that rely on blocking behavior
111+
* must ensure that the execution is completed on using a separate thread pool to ensure IO threads
112+
* are never blocked.</p>
113+
*
114+
* <p>Note: this method is marked as unstable because the Pulsar code base only calls it for the
115+
* Pulsar Broker Auth SASL plugin. All non SASL HTTP requests are authenticated using the
116+
* {@link AuthenticationProvider#authenticateAsync(AuthenticationDataSource)} method. As such,
117+
* this method might be removed in favor of the SASL provider implementing the
118+
* {@link AuthenticationProvider#authenticateAsync(AuthenticationDataSource)} method.</p>
119+
*
120+
* @return Set response, according to passed in request.
121+
* and return whether we should do following chain.doFilter or not.
122+
*/
123+
@InterfaceStability.Unstable
124+
default CompletableFuture<Boolean> authenticateHttpRequestAsync(HttpServletRequest request,
125+
HttpServletResponse response) {
126+
try {
127+
return CompletableFuture.completedFuture(this.authenticateHttpRequest(request, response));
128+
} catch (Exception e) {
129+
return FutureUtil.failedFuture(e);
130+
}
131+
}
132+
79133
/**
80134
* Set response, according to passed in request.
81135
* and return whether we should do following chain.doFilter or not.
136+
* @deprecated use and implement {@link AuthenticationProvider#authenticateHttpRequestAsync} instead.
82137
*/
138+
@Deprecated
83139
default boolean authenticateHttpRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
84140
throw new AuthenticationException("Not supported");
85141
}

pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationState.java

+36
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import javax.naming.AuthenticationException;
2323

2424
import org.apache.pulsar.common.api.AuthData;
25+
import org.apache.pulsar.common.util.FutureUtil;
26+
27+
import java.util.concurrent.CompletableFuture;
2528

2629
/**
2730
* Interface for authentication state.
@@ -39,17 +42,50 @@ public interface AuthenticationState {
3942

4043
/**
4144
* Challenge passed in auth data and get response data.
45+
* @deprecated use and implement {@link AuthenticationState#authenticateAsync(AuthData)} instead.
4246
*/
47+
@Deprecated
4348
AuthData authenticate(AuthData authData) throws AuthenticationException;
4449

50+
/**
51+
* Challenge passed in auth data. If authentication is complete after the execution of this method, return null.
52+
* Otherwise, return response data to be sent to the client.
53+
*
54+
* <p>Note: the implementation of {@link AuthenticationState#authenticate(AuthData)} converted a null result into a
55+
* zero length byte array when {@link AuthenticationState#isComplete()} returned false after authentication. In
56+
* order to simplify this interface, the determination of whether to send a challenge back to the client is only
57+
* based on the result of this method. In order to maintain backwards compatibility, the default implementation of
58+
* this method calls {@link AuthenticationState#isComplete()} and returns a result compliant with the new
59+
* paradigm.</p>
60+
*/
61+
default CompletableFuture<AuthData> authenticateAsync(AuthData authData) {
62+
try {
63+
AuthData result = this.authenticate(authData);
64+
if (isComplete()) {
65+
return CompletableFuture.completedFuture(null);
66+
} else {
67+
return result != null
68+
? CompletableFuture.completedFuture(result)
69+
: CompletableFuture.completedFuture(AuthData.of(new byte[0]));
70+
}
71+
} catch (Exception e) {
72+
return FutureUtil.failedFuture(e);
73+
}
74+
}
75+
4576
/**
4677
* Return AuthenticationDataSource.
4778
*/
4879
AuthenticationDataSource getAuthDataSource();
4980

5081
/**
5182
* Whether the authentication is completed or not.
83+
* @deprecated this method's logic is captured by the result of
84+
* {@link AuthenticationState#authenticateAsync(AuthData)}. When the result is a {@link CompletableFuture} with a
85+
* null result, authentication is complete. When the result is a {@link CompletableFuture} with a nonnull result,
86+
* authentication is incomplete and requires an auth challenge.
5287
*/
88+
@Deprecated
5389
boolean isComplete();
5490

5591
/**

0 commit comments

Comments
 (0)