sasl_protocol.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_RPC_SASLPROTOCOL_H
  19. #define LIB_RPC_SASLPROTOCOL_H
  20. #include "hdfspp/status.h"
  21. #include "common/auth_info.h"
  22. #include "common/libhdfs_events_impl.h"
  23. #include <RpcHeader.pb.h>
  24. #include <memory>
  25. #include <mutex>
  26. #include <functional>
  27. namespace hdfs {
  28. static constexpr const char * SASL_METHOD_NAME = "sasl message";
  29. class RpcConnection;
  30. class SaslEngine;
  31. class SaslProtocol : public std::enable_shared_from_this<SaslProtocol>
  32. {
  33. public:
  34. SaslProtocol(const std::string &cluster_name,
  35. const AuthInfo & auth_info,
  36. std::shared_ptr<RpcConnection> connection);
  37. virtual ~SaslProtocol();
  38. void SetEventHandlers(std::shared_ptr<LibhdfsEvents> event_handlers);
  39. // Start the async authentication process. Must be called while holding the
  40. // connection lock, but all callbacks will occur outside of the connection lock
  41. void authenticate(std::function<void(const Status & status, const AuthInfo new_auth_info)> callback);
  42. void OnServerResponse(const Status & status, const hadoop::common::RpcSaslProto * response);
  43. private:
  44. enum State {
  45. kUnstarted,
  46. kNegotiate,
  47. kAuthenticate,
  48. kComplete
  49. };
  50. // Lock for access to members of the class
  51. std::mutex sasl_state_lock_;
  52. State state_;
  53. const std::string cluster_name_;
  54. AuthInfo auth_info_;
  55. std::weak_ptr<RpcConnection> connection_;
  56. std::function<void(const Status & status, const AuthInfo new_auth_info)> callback_;
  57. std::unique_ptr<SaslEngine> sasl_engine_;
  58. std::shared_ptr<LibhdfsEvents> event_handlers_;
  59. bool SendSaslMessage(hadoop::common::RpcSaslProto & message);
  60. bool AuthComplete(const Status & status, const AuthInfo & auth_info);
  61. void Negotiate(const hadoop::common::RpcSaslProto * response);
  62. void Challenge(const hadoop::common::RpcSaslProto * response);
  63. };
  64. }
  65. #endif /* LIB_RPC_SASLPROTOCOL_H */