datatransfer_sasl.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef COMMON_DATATRANSFER_SASL_H_
  19. #define COMMON_DATATRANSFER_SASL_H_
  20. #include "sasl_authenticator.h"
  21. #include "common/continuation/asio.h"
  22. #include "common/continuation/protobuf.h"
  23. #include "libhdfs++/options.h"
  24. #include "datatransfer.h"
  25. #include "datatransfer.pb.h"
  26. namespace hdfs {
  27. template <class Stream>
  28. class DataTransferSaslStream {
  29. public:
  30. DataTransferSaslStream(const BlockReaderOptions &options,
  31. const std::shared_ptr<Stream> &stream,
  32. const std::string &username,
  33. const std::string &password)
  34. : stream_(stream)
  35. , options_(options)
  36. , authenticator_(username, password)
  37. {}
  38. template<class Handler>
  39. void Handshake(const Handler &next);
  40. template <typename MutableBufferSequence, typename ReadHandler>
  41. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  42. void (asio::error_code, std::size_t))
  43. async_read_some(const MutableBufferSequence& buffers,
  44. ASIO_MOVE_ARG(ReadHandler) handler) {
  45. return stream_->async_read_some(buffers, handler);
  46. }
  47. template <typename ConstBufferSequence, typename WriteHandler>
  48. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  49. void (asio::error_code, std::size_t))
  50. async_write_some(const ConstBufferSequence& buffers,
  51. ASIO_MOVE_ARG(WriteHandler) handler)
  52. {
  53. return stream_->async_write_some(buffers, handler);
  54. }
  55. private:
  56. DataTransferSaslStream(const DataTransferSaslStream&) = delete;
  57. DataTransferSaslStream &operator=(const DataTransferSaslStream &) = delete;
  58. std::shared_ptr<Stream> stream_;
  59. BlockReaderOptions options_;
  60. DigestMD5Authenticator authenticator_;
  61. struct ReadSaslMessageContinuation;
  62. struct AuthenticatorContinuation;
  63. };
  64. namespace DataTransferSaslStreamUtil {
  65. typedef hadoop::hdfs::DataTransferEncryptorMessageProto SaslMessage;
  66. Status ConvertToStatus(const SaslMessage *msg, std::string *payload);
  67. void PrepareInitialHandshake(SaslMessage *msg);
  68. }
  69. template<class Stream>
  70. struct DataTransferSaslStream<Stream>::AuthenticatorContinuation
  71. : continuation::Continuation {
  72. AuthenticatorContinuation(DigestMD5Authenticator *authenticator,
  73. BlockReaderOptions *options,
  74. const std::string *request,
  75. hadoop::hdfs::DataTransferEncryptorMessageProto *msg)
  76. : authenticator_(authenticator)
  77. , options_(options)
  78. , request_(request)
  79. , msg_(msg)
  80. {}
  81. virtual void Run(const Next& next) override {
  82. std::string response;
  83. Status status = authenticator_->EvaluateResponse(*request_, &response);
  84. msg_->Clear();
  85. if (status.ok()) {
  86. // TODO: Handle encryption scheme
  87. msg_->set_payload(response);
  88. msg_->set_status(hadoop::hdfs::DataTransferEncryptorMessageProto_DataTransferEncryptorStatus_SUCCESS);
  89. } else {
  90. msg_->set_status(hadoop::hdfs::DataTransferEncryptorMessageProto_DataTransferEncryptorStatus_ERROR);
  91. }
  92. next(Status::OK());
  93. }
  94. private:
  95. DigestMD5Authenticator *authenticator_;
  96. BlockReaderOptions *options_;
  97. const std::string *request_;
  98. hadoop::hdfs::DataTransferEncryptorMessageProto *msg_;
  99. };
  100. template<class Stream>
  101. struct DataTransferSaslStream<Stream>::ReadSaslMessageContinuation
  102. : continuation::Continuation {
  103. ReadSaslMessageContinuation(Stream *stream, std::string *data)
  104. : stream_(stream)
  105. , data_(data)
  106. {}
  107. virtual void Run(const Next& next) override {
  108. read_pb_.reset(
  109. new continuation::ReadDelimitedPBMessageContinuation<Stream, 1024>(stream_, &resp_));
  110. auto handler = [this,next](const Status &status) {
  111. if (status.ok()) {
  112. Status new_stat = DataTransferSaslStreamUtil::ConvertToStatus(&resp_, data_);
  113. next(new_stat);
  114. } else {
  115. next(status);
  116. }
  117. };
  118. read_pb_->Run(handler);
  119. }
  120. private:
  121. Stream *stream_;
  122. std::string *data_;
  123. hadoop::hdfs::DataTransferEncryptorMessageProto resp_;
  124. std::unique_ptr<continuation::Continuation> read_pb_;
  125. };
  126. template <class Stream>
  127. template <class Handler>
  128. void DataTransferSaslStream<Stream>::Handshake(const Handler &next) {
  129. using hadoop::hdfs::DataTransferEncryptorMessageProto;
  130. struct State {
  131. int magic_number;
  132. DataTransferEncryptorMessageProto request0;
  133. std::string response0;
  134. DataTransferEncryptorMessageProto request1;
  135. std::string response1;
  136. std::shared_ptr<Stream> stream;
  137. };
  138. auto s = std::make_shared<State>();
  139. s->stream = stream_;
  140. s->magic_number = htonl(kDataTransferSasl);
  141. DataTransferSaslStreamUtil::PrepareInitialHandshake(&s->request0);
  142. auto prog = continuation::Write(stream_, asio::buffer(reinterpret_cast<const char*>(&s->magic_number), sizeof(s->magic_number)))
  143. >>= WriteDelimitedPBMessage(stream_.get(), &s->request0)
  144. >>= ReadSaslMessageContinuation(stream_.get(), &s->response0)
  145. >>= AuthenticatorContinuation(&authenticator_, &options_, &s->response0, &s->request1)
  146. >>= WriteDelimitedPBMessage(stream_.get(), &s->request1)
  147. >>= ReadSaslMessageContinuation(stream_.get(), &s->response1);
  148. // TODO: Check whether the server and the client matches the QOP
  149. auto m = std::shared_ptr<decltype(prog)>(new decltype(prog)(std::move(prog)));
  150. m->Run([m,s,next](const Status &status) {
  151. next(status);
  152. });
  153. }
  154. }
  155. #endif