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