Skip to content

Commit d456d07

Browse files
committed
HDFS-17575. SaslDataTransferClient should use SaslParticipant to create messages.
1 parent e48cd0e commit d456d07

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,25 +519,25 @@ private IOStreamPair doSaslHandshake(InetAddress addr,
519519
// In which case there will be no encrypted secret sent from NN.
520520
BlockTokenIdentifier blockTokenIdentifier =
521521
accessToken.decodeIdentifier();
522+
final byte[] first = sasl.createFirstMessage();
522523
if (blockTokenIdentifier != null) {
523524
byte[] handshakeSecret =
524525
accessToken.decodeIdentifier().getHandshakeMsg();
525526
if (handshakeSecret == null || handshakeSecret.length == 0) {
526527
LOG.debug("Handshake secret is null, "
527528
+ "sending without handshake secret.");
528-
sendSaslMessage(out, new byte[0]);
529+
sendSaslMessage(out, first);
529530
} else {
530531
LOG.debug("Sending handshake secret.");
531532
BlockTokenIdentifier identifier = new BlockTokenIdentifier();
532533
identifier.readFields(new DataInputStream(
533534
new ByteArrayInputStream(accessToken.getIdentifier())));
534535
String bpid = identifier.getBlockPoolId();
535-
sendSaslMessageHandshakeSecret(out, new byte[0],
536-
handshakeSecret, bpid);
536+
sendSaslMessageHandshakeSecret(out, first, handshakeSecret, bpid);
537537
}
538538
} else {
539539
LOG.debug("Block token id is null, sending without handshake secret.");
540-
sendSaslMessage(out, new byte[0]);
540+
sendSaslMessage(out, first);
541541
}
542542

543543
// step 1
@@ -565,6 +565,7 @@ private IOStreamPair doSaslHandshake(InetAddress addr,
565565
cipherOptions.add(option);
566566
}
567567
}
568+
LOG.debug("{}: cipherOptions={}", sasl, cipherOptions);
568569
sendSaslMessageAndNegotiationCipherOptions(out, localResponse,
569570
cipherOptions);
570571

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.DataInputStream;
2121
import java.io.DataOutputStream;
2222
import java.util.Map;
23+
import java.util.Objects;
2324
import javax.security.auth.callback.CallbackHandler;
2425
import javax.security.sasl.Sasl;
2526
import javax.security.sasl.SaslClient;
@@ -52,6 +53,7 @@ class SaslParticipant {
5253
private static final String SERVER_NAME = "0";
5354
private static final String PROTOCOL = "hdfs";
5455
private static final String[] MECHANISM_ARRAY = {SaslConstants.SASL_MECHANISM};
56+
private static final byte[] EMPTY_BYTE_ARRAY = {};
5557

5658
// One of these will always be null.
5759
private final SaslServer saslServer;
@@ -110,7 +112,7 @@ public static SaslParticipant createClientSaslParticipant(String userName,
110112
* @param saslServer to wrap
111113
*/
112114
private SaslParticipant(SaslServer saslServer) {
113-
this.saslServer = saslServer;
115+
this.saslServer = Objects.requireNonNull(saslServer, "saslServer == null");
114116
this.saslClient = null;
115117
}
116118

@@ -121,7 +123,12 @@ private SaslParticipant(SaslServer saslServer) {
121123
*/
122124
private SaslParticipant(SaslClient saslClient) {
123125
this.saslServer = null;
124-
this.saslClient = saslClient;
126+
this.saslClient = Objects.requireNonNull(saslClient, "saslClient == null");
127+
}
128+
129+
byte[] createFirstMessage() throws SaslException {
130+
return MECHANISM_ARRAY[0].equals(SaslConstants.SASL_MECHANISM_DEFAULT) ? EMPTY_BYTE_ARRAY
131+
: evaluateChallengeOrResponse(EMPTY_BYTE_ARRAY);
125132
}
126133

127134
/**
@@ -228,4 +235,9 @@ public IOStreamPair createStreamPair(DataOutputStream out,
228235
new SaslOutputStream(out, saslServer));
229236
}
230237
}
238+
239+
@Override
240+
public String toString() {
241+
return "Sasl" + (saslServer != null? "Server" : "Client");
242+
}
231243
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class TestSaslDataTransfer extends SaslDataTransferTestCase {
7777
public ExpectedException exception = ExpectedException.none();
7878

7979
@Rule
80-
public Timeout timeout = new Timeout(60000);
80+
public Timeout timeout = new Timeout(300_000);
8181

8282
@After
8383
public void shutdown() {

0 commit comments

Comments
 (0)