datatransfer_impl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 LIB_READER_DATATRANFER_IMPL_H_
  19. #define LIB_READER_DATATRANFER_IMPL_H_
  20. #include "datatransfer.pb.h"
  21. #include "common/continuation/continuation.h"
  22. #include "common/continuation/asio.h"
  23. #include "common/continuation/protobuf.h"
  24. #include <asio/read.hpp>
  25. #include <asio/buffer.hpp>
  26. namespace hdfs {
  27. namespace DataTransferSaslStreamUtil {
  28. Status
  29. ConvertToStatus(const ::hadoop::hdfs::DataTransferEncryptorMessageProto *msg,
  30. std::string *payload);
  31. void PrepareInitialHandshake(
  32. ::hadoop::hdfs::DataTransferEncryptorMessageProto *msg);
  33. }
  34. template <class Stream>
  35. struct DataTransferSaslStream<Stream>::Authenticator
  36. : continuation::Continuation {
  37. Authenticator(DigestMD5Authenticator *authenticator,
  38. const std::string *request,
  39. hadoop::hdfs::DataTransferEncryptorMessageProto *msg)
  40. : authenticator_(authenticator), request_(request), msg_(msg) {}
  41. virtual void Run(const Next &next) override {
  42. using namespace ::hadoop::hdfs;
  43. std::string response;
  44. Status status = authenticator_->EvaluateResponse(*request_, &response);
  45. msg_->Clear();
  46. if (status.ok()) {
  47. // TODO: Handle encryption scheme
  48. msg_->set_payload(response);
  49. msg_->set_status(
  50. DataTransferEncryptorMessageProto_DataTransferEncryptorStatus_SUCCESS);
  51. } else {
  52. msg_->set_status(
  53. DataTransferEncryptorMessageProto_DataTransferEncryptorStatus_ERROR);
  54. }
  55. next(Status::OK());
  56. }
  57. private:
  58. DigestMD5Authenticator *authenticator_;
  59. const std::string *request_;
  60. hadoop::hdfs::DataTransferEncryptorMessageProto *msg_;
  61. };
  62. template <class Stream>
  63. struct DataTransferSaslStream<Stream>::ReadSaslMessage
  64. : continuation::Continuation {
  65. ReadSaslMessage(std::shared_ptr<Stream> stream, std::string *data)
  66. : stream_(stream), data_(data), read_pb_(stream, &resp_) {}
  67. virtual void Run(const Next &next) override {
  68. auto handler = [this, next](const Status &status) {
  69. if (status.ok()) {
  70. Status new_stat =
  71. DataTransferSaslStreamUtil::ConvertToStatus(&resp_, data_);
  72. next(new_stat);
  73. } else {
  74. next(status);
  75. }
  76. };
  77. read_pb_.Run(handler);
  78. }
  79. private:
  80. std::shared_ptr<Stream> stream_;
  81. std::string *data_;
  82. hadoop::hdfs::DataTransferEncryptorMessageProto resp_;
  83. continuation::ReadDelimitedPBMessageContinuation<Stream, 1024> read_pb_;
  84. };
  85. template <class Stream>
  86. template <class Handler>
  87. void DataTransferSaslStream<Stream>::Handshake(const Handler &next) {
  88. using ::hadoop::hdfs::DataTransferEncryptorMessageProto;
  89. using ::hdfs::asio_continuation::Write;
  90. using ::hdfs::continuation::WriteDelimitedPBMessage;
  91. static const int kMagicNumber = htonl(kDataTransferSasl);
  92. static const asio::const_buffers_1 kMagicNumberBuffer = asio::buffer(
  93. reinterpret_cast<const char *>(kMagicNumber), sizeof(kMagicNumber));
  94. struct State {
  95. DataTransferEncryptorMessageProto req0;
  96. std::string resp0;
  97. DataTransferEncryptorMessageProto req1;
  98. std::string resp1;
  99. std::shared_ptr<Stream> stream;
  100. };
  101. auto m = continuation::Pipeline<State>::Create();
  102. State *s = &m->state();
  103. s->stream = stream_;
  104. DataTransferSaslStreamUtil::PrepareInitialHandshake(&s->req0);
  105. m->Push(Write(stream_.get(), kMagicNumberBuffer))
  106. .Push(WriteDelimitedPBMessage(stream_, &s->req0))
  107. .Push(new ReadSaslMessage(stream_, &s->resp0))
  108. .Push(new Authenticator(&authenticator_, &s->resp0, &s->req1))
  109. .Push(WriteDelimitedPBMessage(stream_, &s->req1))
  110. .Push(new ReadSaslMessage(stream_, &s->resp1));
  111. m->Run([next](const Status &status, const State &) { next(status); });
  112. }
  113. template <class Stream>
  114. void DataTransferSaslStream<Stream>::Cancel() {
  115. /* implement with secured reads */
  116. }
  117. }
  118. #endif