alts_context.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/grpc_security.h>
  19. #include <grpcpp/security/alts_context.h>
  20. #include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h"
  21. #include "src/proto/grpc/gcp/altscontext.upb.h"
  22. namespace grpc {
  23. namespace experimental {
  24. // A upb-generated grpc_gcp_AltsContext is passed in to construct an
  25. // AltsContext. Normal users should use GetAltsContextFromAuthContext to get
  26. // AltsContext, instead of constructing their own.
  27. AltsContext::AltsContext(const grpc_gcp_AltsContext* ctx) {
  28. upb_strview application_protocol =
  29. grpc_gcp_AltsContext_application_protocol(ctx);
  30. if (application_protocol.data != nullptr && application_protocol.size > 0) {
  31. application_protocol_ =
  32. std::string(application_protocol.data, application_protocol.size);
  33. }
  34. upb_strview record_protocol = grpc_gcp_AltsContext_record_protocol(ctx);
  35. if (record_protocol.data != nullptr && record_protocol.size > 0) {
  36. record_protocol_ = std::string(record_protocol.data, record_protocol.size);
  37. }
  38. upb_strview peer_service_account =
  39. grpc_gcp_AltsContext_peer_service_account(ctx);
  40. if (peer_service_account.data != nullptr && peer_service_account.size > 0) {
  41. peer_service_account_ =
  42. std::string(peer_service_account.data, peer_service_account.size);
  43. }
  44. upb_strview local_service_account =
  45. grpc_gcp_AltsContext_local_service_account(ctx);
  46. if (local_service_account.data != nullptr && local_service_account.size > 0) {
  47. local_service_account_ =
  48. std::string(local_service_account.data, local_service_account.size);
  49. }
  50. const grpc_gcp_RpcProtocolVersions* versions =
  51. grpc_gcp_AltsContext_peer_rpc_versions(ctx);
  52. if (versions != nullptr) {
  53. const grpc_gcp_RpcProtocolVersions_Version* max_version =
  54. grpc_gcp_RpcProtocolVersions_max_rpc_version(versions);
  55. if (max_version != nullptr) {
  56. int max_version_major =
  57. grpc_gcp_RpcProtocolVersions_Version_major(max_version);
  58. int max_version_minor =
  59. grpc_gcp_RpcProtocolVersions_Version_minor(max_version);
  60. peer_rpc_versions_.max_rpc_version.major_version = max_version_major;
  61. peer_rpc_versions_.max_rpc_version.minor_version = max_version_minor;
  62. }
  63. const grpc_gcp_RpcProtocolVersions_Version* min_version =
  64. grpc_gcp_RpcProtocolVersions_min_rpc_version(versions);
  65. if (min_version != nullptr) {
  66. int min_version_major =
  67. grpc_gcp_RpcProtocolVersions_Version_major(min_version);
  68. int min_version_minor =
  69. grpc_gcp_RpcProtocolVersions_Version_minor(min_version);
  70. peer_rpc_versions_.min_rpc_version.major_version = min_version_major;
  71. peer_rpc_versions_.min_rpc_version.minor_version = min_version_minor;
  72. }
  73. }
  74. if (grpc_gcp_AltsContext_security_level(ctx) >= GRPC_SECURITY_MIN ||
  75. grpc_gcp_AltsContext_security_level(ctx) <= GRPC_SECURITY_MAX) {
  76. security_level_ = static_cast<grpc_security_level>(
  77. grpc_gcp_AltsContext_security_level(ctx));
  78. }
  79. }
  80. std::string AltsContext::application_protocol() const {
  81. return application_protocol_;
  82. }
  83. std::string AltsContext::record_protocol() const { return record_protocol_; }
  84. std::string AltsContext::peer_service_account() const {
  85. return peer_service_account_;
  86. }
  87. std::string AltsContext::local_service_account() const {
  88. return local_service_account_;
  89. }
  90. grpc_security_level AltsContext::security_level() const {
  91. return security_level_;
  92. }
  93. AltsContext::RpcProtocolVersions AltsContext::peer_rpc_versions() const {
  94. return peer_rpc_versions_;
  95. }
  96. } // namespace experimental
  97. } // namespace grpc