Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15172#discussion_r84776932
  
    --- Diff: 
common/network-common/src/main/java/org/apache/spark/network/sasl/SaslRpcHandler.java
 ---
    @@ -80,47 +84,79 @@ public void receive(TransportClient client, ByteBuffer 
message, RpcResponseCallb
           delegate.receive(client, message, callback);
           return;
         }
    +    if (saslServer == null || !saslServer.isComplete()) {
    +      ByteBuf nettyBuf = Unpooled.wrappedBuffer(message);
    +      SaslMessage saslMessage;
    +      try {
    +        saslMessage = SaslMessage.decode(nettyBuf);
    +      } finally {
    +        nettyBuf.release();
    +      }
     
    -    ByteBuf nettyBuf = Unpooled.wrappedBuffer(message);
    -    SaslMessage saslMessage;
    -    try {
    -      saslMessage = SaslMessage.decode(nettyBuf);
    -    } finally {
    -      nettyBuf.release();
    -    }
    -
    -    if (saslServer == null) {
    -      // First message in the handshake, setup the necessary state.
    -      client.setClientId(saslMessage.appId);
    -      saslServer = new SparkSaslServer(saslMessage.appId, secretKeyHolder,
    -        conf.saslServerAlwaysEncrypt());
    -    }
    +      if (saslServer == null) {
    +        // First message in the handshake, setup the necessary state.
    +        client.setClientId(saslMessage.appId);
    +        saslServer = new SparkSaslServer(saslMessage.appId, 
secretKeyHolder,
    +          conf.saslServerAlwaysEncrypt());
    +      }
     
    -    byte[] response;
    -    try {
    -      response = saslServer.response(JavaUtils.bufferToArray(
    -        saslMessage.body().nioByteBuffer()));
    -    } catch (IOException ioe) {
    -      throw new RuntimeException(ioe);
    +      byte[] response;
    +      try {
    +        response = saslServer.response(JavaUtils.bufferToArray(
    +          saslMessage.body().nioByteBuffer()));
    +      } catch (IOException ioe) {
    +        throw new RuntimeException(ioe);
    +      }
    +      callback.onSuccess(ByteBuffer.wrap(response));
         }
    -    callback.onSuccess(ByteBuffer.wrap(response));
     
         // Setup encryption after the SASL response is sent, otherwise the 
client can't parse the
         // response. It's ok to change the channel pipeline here since we are 
processing an incoming
         // message, so the pipeline is busy and no new incoming messages will 
be fed to it before this
         // method returns. This assumes that the code ensures, through other 
means, that no outbound
         // messages are being written to the channel while negotiation is 
still going on.
         if (saslServer.isComplete()) {
    -      logger.debug("SASL authentication successful for channel {}", 
client);
    -      isComplete = true;
    -      if 
(SparkSaslServer.QOP_AUTH_CONF.equals(saslServer.getNegotiatedProperty(Sasl.QOP)))
 {
    +      if 
(!SparkSaslServer.QOP_AUTH_CONF.equals(saslServer.getNegotiatedProperty(Sasl.QOP)))
 {
    +        logger.debug("SASL authentication successful for channel {}", 
client);
    +        saslServer.dispose();
    +        saslServer = null;
    +        isComplete = true;
    +        return ;
    +      }
    +
    +      if (!conf.saslEncryptionAesEnabled()) {
             logger.debug("Enabling encryption for channel {}", client);
             SaslEncryption.addToChannel(channel, saslServer, 
conf.maxSaslEncryptedBlockSize());
    -        saslServer = null;
           } else {
    -        saslServer.dispose();
    -        saslServer = null;
    +        // Extra negotiation should happen after authentication, so return 
directly while
    +        // processing authenticate.
    +        if (!isAuthenticated) {
    +          logger.debug("SASL authentication successful for channel {}", 
client);
    +          isAuthenticated = true;
    +          return;
    +        } else {
    +          try {
    +            AesConfigMessage configMessage = 
AesConfigMessage.decodeMessage(message);
    +            configMessage = AesCipher.responseConfigMessage(configMessage);
    +            AesCipher cipher = new AesCipher(configMessage);
    +
    +            ByteBuffer buf = configMessage.encodeMessage();
    +
    +            // Encrypt the config message.
    +            ByteBuffer encrypted = ByteBuffer.wrap(
    +              saslServer.wrap(buf.array(), 0, buf.array().length));
    +            callback.onSuccess(encrypted);
    --- End diff --
    
    At this point you're not using `saslServer` anymore, so you should dispose 
of it. I kinda prefer my previous suggestion where instead of `if..else` you 
have just `if`s and `return`s. I think it makes it a little easier to follow 
the code.
    
    You can abstract the `saslServer` cleanup by adding a `complete(boolean 
dispose)` method that updates `isComplete` and `saslServer` and is called from 
this method where needed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to