alts_util.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "upb/upb.hpp"
  19. #include <grpc/grpc_security.h>
  20. #include <grpc/support/log.h>
  21. #include <grpcpp/security/alts_context.h>
  22. #include <grpcpp/security/alts_util.h>
  23. #include "src/core/lib/gprpp/memory.h"
  24. #include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h"
  25. #include "src/cpp/common/secure_auth_context.h"
  26. #include "src/proto/grpc/gcp/altscontext.upb.h"
  27. namespace grpc {
  28. namespace experimental {
  29. std::unique_ptr<AltsContext> GetAltsContextFromAuthContext(
  30. const std::shared_ptr<const AuthContext>& auth_context) {
  31. if (auth_context == nullptr) {
  32. gpr_log(GPR_ERROR, "auth_context is nullptr.");
  33. return nullptr;
  34. }
  35. std::vector<string_ref> ctx_vector =
  36. auth_context->FindPropertyValues(TSI_ALTS_CONTEXT);
  37. if (ctx_vector.size() != 1) {
  38. gpr_log(GPR_ERROR, "contains zero or more than one ALTS context.");
  39. return nullptr;
  40. }
  41. upb::Arena context_arena;
  42. grpc_gcp_AltsContext* ctx = grpc_gcp_AltsContext_parse(
  43. ctx_vector[0].data(), ctx_vector[0].size(), context_arena.ptr());
  44. if (ctx == nullptr) {
  45. gpr_log(GPR_ERROR, "fails to parse ALTS context.");
  46. return nullptr;
  47. }
  48. if (grpc_gcp_AltsContext_security_level(ctx) < GRPC_SECURITY_MIN ||
  49. grpc_gcp_AltsContext_security_level(ctx) > GRPC_SECURITY_MAX) {
  50. gpr_log(GPR_ERROR, "security_level is invalid.");
  51. return nullptr;
  52. }
  53. return absl::make_unique<AltsContext>(AltsContext(ctx));
  54. }
  55. grpc::Status AltsClientAuthzCheck(
  56. const std::shared_ptr<const AuthContext>& auth_context,
  57. const std::vector<std::string>& expected_service_accounts) {
  58. std::unique_ptr<AltsContext> alts_ctx =
  59. GetAltsContextFromAuthContext(auth_context);
  60. if (alts_ctx == nullptr) {
  61. return grpc::Status(grpc::StatusCode::PERMISSION_DENIED,
  62. "fails to parse ALTS context.");
  63. }
  64. if (std::find(expected_service_accounts.begin(),
  65. expected_service_accounts.end(),
  66. alts_ctx->peer_service_account()) !=
  67. expected_service_accounts.end()) {
  68. return grpc::Status::OK;
  69. }
  70. return grpc::Status(
  71. grpc::StatusCode::PERMISSION_DENIED,
  72. "client " + alts_ctx->peer_service_account() + " is not authorized.");
  73. }
  74. } // namespace experimental
  75. } // namespace grpc