1
#pragma once
2

            
3
#include <memory>
4

            
5
#include "absl/status/statusor.h"
6
#include "absl/types/span.h"
7
#include "grpcpp/channel.h"
8
#include "grpcpp/client_context.h"
9
#include "grpcpp/support/sync_stream.h"
10
#include "src/proto/grpc/gcp/handshaker.grpc.pb.h"
11
#include "src/proto/grpc/gcp/handshaker.pb.h"
12
#include "src/proto/grpc/gcp/transport_security_common.pb.h"
13

            
14
namespace Envoy {
15
namespace Extensions {
16
namespace TransportSockets {
17
namespace Alts {
18

            
19
constexpr char ApplicationProtocol[] = "grpc";
20
constexpr char RecordProtocol[] = "ALTSRP_GCM_AES128_REKEY";
21
constexpr std::size_t MaxFrameSize = 1024 * 1024;
22
constexpr std::size_t MaxMajorRpcVersion = 2;
23
constexpr std::size_t MaxMinorRpcVersion = 1;
24
constexpr std::size_t MinMajorRpcVersion = 2;
25
constexpr std::size_t MinMinorRpcVersion = 1;
26

            
27
// Manages a bidirectional stream to the ALTS handshaker service. An AltsProxy
28
// instance is tied to a single ALTS handshake and must not be reused.
29
//
30
// WARNING: Several methods block the worker thread performing the ALTS
31
// handshake to make a gRPC call to the ALTS handshaker service. This can slow
32
// down or halt the proxy if the ALTS handshaker service is unavailable or
33
// experiencing high latency.
34
class AltsProxy {
35
public:
36
  static absl::StatusOr<std::unique_ptr<AltsProxy>>
37
  create(std::shared_ptr<grpc::Channel> handshaker_service_channel);
38

            
39
  ~AltsProxy();
40

            
41
  // Sends a StartClientHandshakeReq message to the ALTS handshaker service and
42
  // returns the response.
43
  //
44
  // WARNING: Blocks the worker thread performing the ALTS handshake to make a
45
  // gRPC call to the ALTS handshaker service. This can slow down or halt the
46
  // proxy if the ALTS handshaker service is unavailable or experiencing high
47
  // latency.
48
  absl::StatusOr<grpc::gcp::HandshakerResp> sendStartClientHandshakeReq();
49

            
50
  // Sends a StartServerHandshakeReq message to the ALTS handshaker service and
51
  // returns the response.
52
  //
53
  // WARNING: Blocks the worker thread performing the ALTS handshake to make a
54
  // gRPC call to the ALTS handshaker service. This can slow down or halt the
55
  // proxy if the ALTS handshaker service is unavailable or experiencing high
56
  // latency.
57
  absl::StatusOr<grpc::gcp::HandshakerResp>
58
  sendStartServerHandshakeReq(absl::Span<const uint8_t> in_bytes);
59

            
60
  // Sends a NextHandshakeMessageReq message to the ALTS handshaker service and
61
  // returns the response.
62
  //
63
  // WARNING: Blocks the worker thread performing the ALTS handshake to make a
64
  // gRPC call to the ALTS handshaker service. This can slow down or halt the
65
  // proxy if the ALTS handshaker service is unavailable or experiencing high
66
  // latency.
67
  absl::StatusOr<grpc::gcp::HandshakerResp>
68
  sendNextHandshakeReq(absl::Span<const uint8_t> in_bytes);
69

            
70
private:
71
  static void setRpcProtocolVersions(grpc::gcp::RpcProtocolVersions* rpc_protocol_versions);
72

            
73
  AltsProxy(
74
      std::unique_ptr<grpc::ClientContext> client_context,
75
      std::unique_ptr<grpc::gcp::HandshakerService::Stub> stub,
76
      std::unique_ptr<grpc::ClientReaderWriter<grpc::gcp::HandshakerReq, grpc::gcp::HandshakerResp>>
77
          stream);
78

            
79
  std::unique_ptr<grpc::ClientContext> client_context_ = nullptr;
80
  std::unique_ptr<grpc::gcp::HandshakerService::Stub> stub_;
81
  std::unique_ptr<grpc::ClientReaderWriter<grpc::gcp::HandshakerReq, grpc::gcp::HandshakerResp>>
82
      stream_ = nullptr;
83
};
84

            
85
} // namespace Alts
86
} // namespace TransportSockets
87
} // namespace Extensions
88
} // namespace Envoy