| 1 |
package rpc |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
hadoop "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_common" |
| 9 |
"github.com/jcmturner/gokrb5/v8/crypto" |
| 10 |
"github.com/jcmturner/gokrb5/v8/gssapi" |
| 11 |
"github.com/jcmturner/gokrb5/v8/iana/keyusage" |
| 12 |
krbtypes "github.com/jcmturner/gokrb5/v8/types" |
| 13 |
"google.golang.org/protobuf/proto" |
| 14 |
) |
| 15 |
|
| 16 |
// saslTransport implements encrypted or signed RPC. |
| 17 |
type saslTransport struct { |
| 18 |
basicTransport |
| 19 |
|
| 20 |
// sessionKey is the encryption key used to decrypt and encrypt the payload. |
| 21 |
sessionKey krbtypes.EncryptionKey |
| 22 |
// privacy indicates full message encryption |
| 23 |
privacy bool |
| 24 |
} |
| 25 |
|
| 26 |
// readResponse reads a SASL-wrapped RPC response. |
| 27 |
func (t *saslTransport) readResponse(r io.Reader, method string, requestID int32, resp proto.Message) error { |
| 28 |
// First, read the sasl payload as a standard rpc response. |
| 29 |
sasl := hadoop.RpcSaslProto{} |
| 30 |
err := t.basicTransport.readResponse(r, method, saslRpcCallId, &sasl) |
| 31 |
if err != nil { |
| 32 |
return err |
| 33 |
} else if sasl.GetState() != hadoop.RpcSaslProto_WRAP { |
| 34 |
return fmt.Errorf("unexpected SASL state: %s", sasl.GetState().String()) |
| 35 |
} |
| 36 |
|
| 37 |
// The SaslProto contains the actual payload. |
| 38 |
var wrapToken gssapi.WrapToken |
| 39 |
err = wrapToken.Unmarshal(sasl.GetToken(), true) |
| 40 |
if err != nil { |
| 41 |
return err |
| 42 |
} |
| 43 |
|
| 44 |
rrh := &hadoop.RpcResponseHeaderProto{} |
| 45 |
|
| 46 |
if t.privacy { |
| 47 |
// Decrypt the blob, which then looks like a normal RPC response. |
| 48 |
decrypted, err := crypto.DecryptMessage(wrapToken.Payload, t.sessionKey, keyusage.GSSAPI_ACCEPTOR_SEAL) |
| 49 |
if err != nil { |
| 50 |
return err |
| 51 |
} |
| 52 |
|
| 53 |
err = readRPCPacket(bytes.NewReader(decrypted), rrh, resp) |
| 54 |
if err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
} else { |
| 58 |
// Verify the checksum; the blob is just a normal RPC response. |
| 59 |
_, err = wrapToken.Verify(t.sessionKey, keyusage.GSSAPI_ACCEPTOR_SEAL) |
| 60 |
if err != nil { |
| 61 |
return fmt.Errorf("unverifiable message from namenode: %s", err) |
| 62 |
} |
| 63 |
|
| 64 |
err = readRPCPacket(bytes.NewReader(wrapToken.Payload), rrh, resp) |
| 65 |
if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if int32(rrh.GetCallId()) != requestID { |
| 71 |
return errUnexpectedSequenceNumber |
| 72 |
} else if rrh.GetStatus() != hadoop.RpcResponseHeaderProto_SUCCESS { |
| 73 |
return &NamenodeError{ |
| 74 |
method: method, |
| 75 |
message: rrh.GetErrorMsg(), |
| 76 |
code: int(rrh.GetErrorDetail()), |
| 77 |
exception: rrh.GetExceptionClassName(), |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return nil |
| 82 |
} |