Skip to content

Commit 1ff03fd

Browse files
szetszwoHexiaoqiao
authored andcommitted
HDFS-17606. Do not require implementing CustomizedCallbackHandler. (apache#7005)
1 parent 3e1f170 commit 1ff03fd

File tree

3 files changed

+99
-16
lines changed

3 files changed

+99
-16
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/datatransfer/sasl/CustomizedCallbackHandler.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,41 @@
2020
import javax.security.auth.callback.Callback;
2121
import javax.security.auth.callback.UnsupportedCallbackException;
2222
import java.io.IOException;
23+
import java.lang.reflect.InvocationTargetException;
24+
import java.lang.reflect.Method;
2325
import java.util.List;
2426

2527
/** For handling customized {@link Callback}. */
2628
public interface CustomizedCallbackHandler {
2729
class DefaultHandler implements CustomizedCallbackHandler{
2830
@Override
29-
public void handleCallback(List<Callback> callbacks, String username, char[] password)
31+
public void handleCallbacks(List<Callback> callbacks, String username, char[] password)
3032
throws UnsupportedCallbackException {
3133
if (!callbacks.isEmpty()) {
3234
throw new UnsupportedCallbackException(callbacks.get(0));
3335
}
3436
}
3537
}
3638

37-
void handleCallback(List<Callback> callbacks, String name, char[] password)
39+
static CustomizedCallbackHandler delegate(Object delegated) {
40+
final String methodName = "handleCallbacks";
41+
final Class<?> clazz = delegated.getClass();
42+
final Method method;
43+
try {
44+
method = clazz.getMethod(methodName, List.class, String.class, char[].class);
45+
} catch (NoSuchMethodException e) {
46+
throw new IllegalStateException("Failed to get method " + methodName + " from " + clazz, e);
47+
}
48+
49+
return (callbacks, name, password) -> {
50+
try {
51+
method.invoke(delegated, callbacks, name, password);
52+
} catch (IllegalAccessException | InvocationTargetException e) {
53+
throw new IOException("Failed to invoke " + method, e);
54+
}
55+
};
56+
}
57+
58+
void handleCallbacks(List<Callback> callbacks, String name, char[] password)
3859
throws UnsupportedCallbackException, IOException;
3960
}

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/datatransfer/sasl/SaslDataTransferServer.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,20 @@ static final class SaslServerCallbackHandler
225225
SaslServerCallbackHandler(Configuration conf, PasswordFunction passwordFunction) {
226226
this.passwordFunction = passwordFunction;
227227

228-
final Class<? extends CustomizedCallbackHandler> clazz = conf.getClass(
228+
final Class<?> clazz = conf.getClass(
229229
HdfsClientConfigKeys.DFS_DATA_TRANSFER_SASL_CUSTOMIZEDCALLBACKHANDLER_CLASS_KEY,
230-
CustomizedCallbackHandler.DefaultHandler.class, CustomizedCallbackHandler.class);
230+
CustomizedCallbackHandler.DefaultHandler.class);
231+
final Object callbackHandler;
231232
try {
232-
this.customizedCallbackHandler = clazz.newInstance();
233+
callbackHandler = clazz.newInstance();
233234
} catch (Exception e) {
234235
throw new IllegalStateException("Failed to create a new instance of " + clazz, e);
235236
}
237+
if (callbackHandler instanceof CustomizedCallbackHandler) {
238+
customizedCallbackHandler = (CustomizedCallbackHandler) callbackHandler;
239+
} else {
240+
customizedCallbackHandler = CustomizedCallbackHandler.delegate(callbackHandler);
241+
}
236242
}
237243

238244
@Override
@@ -271,7 +277,7 @@ public void handle(Callback[] callbacks) throws IOException,
271277
if (unknownCallbacks != null) {
272278
final String name = nc != null ? nc.getDefaultName() : null;
273279
final char[] password = name != null ? passwordFunction.apply(name) : null;
274-
customizedCallbackHandler.handleCallback(unknownCallbacks, name, password);
280+
customizedCallbackHandler.handleCallbacks(unknownCallbacks, name, password);
275281
}
276282
}
277283
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/protocol/datatransfer/sasl/TestCustomizedCallbackHandler.java

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,45 @@
2020
import org.apache.hadoop.conf.Configuration;
2121
import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys;
2222
import org.apache.hadoop.hdfs.protocol.datatransfer.sasl.SaslDataTransferServer.SaslServerCallbackHandler;
23+
import org.apache.hadoop.test.LambdaTestUtils;
2324
import org.junit.Assert;
2425
import org.junit.Test;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728

2829
import javax.security.auth.callback.Callback;
2930
import javax.security.auth.callback.UnsupportedCallbackException;
30-
import java.util.Arrays;
31+
import java.io.IOException;
3132
import java.util.List;
33+
import java.util.concurrent.atomic.AtomicReference;
3234

35+
/** For testing {@link CustomizedCallbackHandler}. */
3336
public class TestCustomizedCallbackHandler {
34-
public static final Logger LOG = LoggerFactory.getLogger(TestCustomizedCallbackHandler.class);
37+
static final Logger LOG = LoggerFactory.getLogger(TestCustomizedCallbackHandler.class);
38+
39+
static final AtomicReference<List<Callback>> LAST_CALLBACKS = new AtomicReference<>();
40+
41+
static void runHandleCallbacks(Object caller, List<Callback> callbacks, String name) {
42+
LOG.info("{}: handling {} for {}", caller.getClass().getSimpleName(), callbacks, name);
43+
LAST_CALLBACKS.set(callbacks);
44+
}
45+
46+
/** Assert if the callbacks in {@link #LAST_CALLBACKS} are the same as the expected callbacks. */
47+
static void assertCallbacks(Callback[] expected) {
48+
final List<Callback> computed = LAST_CALLBACKS.getAndSet(null);
49+
Assert.assertNotNull(computed);
50+
Assert.assertEquals(expected.length, computed.size());
51+
for (int i = 0; i < expected.length; i++) {
52+
Assert.assertSame(expected[i], computed.get(i));
53+
}
54+
}
3555

3656
static class MyCallback implements Callback { }
3757

3858
static class MyCallbackHandler implements CustomizedCallbackHandler {
3959
@Override
40-
public void handleCallback(List<Callback> callbacks, String name, char[] password) {
41-
LOG.info("{}: handling {} for {}", getClass().getSimpleName(), callbacks, name);
60+
public void handleCallbacks(List<Callback> callbacks, String name, char[] password) {
61+
runHandleCallbacks(this, callbacks, name);
4262
}
4363
}
4464

@@ -48,16 +68,52 @@ public void testCustomizedCallbackHandler() throws Exception {
4868
final Callback[] callbacks = {new MyCallback()};
4969

5070
// without setting conf, expect UnsupportedCallbackException
51-
try {
52-
new SaslServerCallbackHandler(conf, String::toCharArray).handle(callbacks);
53-
Assert.fail("Expected UnsupportedCallbackException for " + Arrays.asList(callbacks));
54-
} catch (UnsupportedCallbackException e) {
55-
LOG.info("The failure is expected", e);
56-
}
71+
LambdaTestUtils.intercept(UnsupportedCallbackException.class, () -> runTest(conf, callbacks));
5772

5873
// set conf and expect success
5974
conf.setClass(HdfsClientConfigKeys.DFS_DATA_TRANSFER_SASL_CUSTOMIZEDCALLBACKHANDLER_CLASS_KEY,
6075
MyCallbackHandler.class, CustomizedCallbackHandler.class);
6176
new SaslServerCallbackHandler(conf, String::toCharArray).handle(callbacks);
77+
assertCallbacks(callbacks);
78+
}
79+
80+
static class MyCallbackMethod {
81+
public void handleCallbacks(List<Callback> callbacks, String name, char[] password)
82+
throws UnsupportedCallbackException {
83+
runHandleCallbacks(this, callbacks, name);
84+
}
85+
}
86+
87+
static class MyExceptionMethod {
88+
public void handleCallbacks(List<Callback> callbacks, String name, char[] password)
89+
throws UnsupportedCallbackException {
90+
runHandleCallbacks(this, callbacks, name);
91+
throw new UnsupportedCallbackException(callbacks.get(0));
92+
}
93+
}
94+
95+
@Test
96+
public void testCustomizedCallbackMethod() throws Exception {
97+
final Configuration conf = new Configuration();
98+
final Callback[] callbacks = {new MyCallback()};
99+
100+
// without setting conf, expect UnsupportedCallbackException
101+
LambdaTestUtils.intercept(UnsupportedCallbackException.class, () -> runTest(conf, callbacks));
102+
103+
// set conf and expect success
104+
conf.setClass(HdfsClientConfigKeys.DFS_DATA_TRANSFER_SASL_CUSTOMIZEDCALLBACKHANDLER_CLASS_KEY,
105+
MyCallbackMethod.class, Object.class);
106+
new SaslServerCallbackHandler(conf, String::toCharArray).handle(callbacks);
107+
assertCallbacks(callbacks);
108+
109+
// set conf and expect exception
110+
conf.setClass(HdfsClientConfigKeys.DFS_DATA_TRANSFER_SASL_CUSTOMIZEDCALLBACKHANDLER_CLASS_KEY,
111+
MyExceptionMethod.class, Object.class);
112+
LambdaTestUtils.intercept(IOException.class, () -> runTest(conf, callbacks));
113+
}
114+
115+
static void runTest(Configuration conf, Callback... callbacks)
116+
throws IOException, UnsupportedCallbackException {
117+
new SaslServerCallbackHandler(conf, String::toCharArray).handle(callbacks);
62118
}
63119
}

0 commit comments

Comments
 (0)