• 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 <stddef.h>
22 
23 #include <map>
24 #include <string>
25 
26 #include "src/proto/grpc/gcp/altscontext.upb.h"
27 #include "src/proto/grpc/gcp/transport_security_common.upb.h"
28 #include "upb/base/string_view.h"
29 #include "upb/message/map.h"
30 
31 namespace grpc {
32 namespace experimental {
33 
34 // A upb-generated grpc_gcp_AltsContext is passed in to construct an
35 // AltsContext. Normal users should use GetAltsContextFromAuthContext to get
36 // AltsContext, instead of constructing their own.
AltsContext(const grpc_gcp_AltsContext * ctx)37 AltsContext::AltsContext(const grpc_gcp_AltsContext* ctx) {
38   upb_StringView application_protocol =
39       grpc_gcp_AltsContext_application_protocol(ctx);
40   if (application_protocol.data != nullptr && application_protocol.size > 0) {
41     application_protocol_ =
42         std::string(application_protocol.data, application_protocol.size);
43   }
44   upb_StringView record_protocol = grpc_gcp_AltsContext_record_protocol(ctx);
45   if (record_protocol.data != nullptr && record_protocol.size > 0) {
46     record_protocol_ = std::string(record_protocol.data, record_protocol.size);
47   }
48   upb_StringView peer_service_account =
49       grpc_gcp_AltsContext_peer_service_account(ctx);
50   if (peer_service_account.data != nullptr && peer_service_account.size > 0) {
51     peer_service_account_ =
52         std::string(peer_service_account.data, peer_service_account.size);
53   }
54   upb_StringView local_service_account =
55       grpc_gcp_AltsContext_local_service_account(ctx);
56   if (local_service_account.data != nullptr && local_service_account.size > 0) {
57     local_service_account_ =
58         std::string(local_service_account.data, local_service_account.size);
59   }
60   const grpc_gcp_RpcProtocolVersions* versions =
61       grpc_gcp_AltsContext_peer_rpc_versions(ctx);
62   if (versions != nullptr) {
63     const grpc_gcp_RpcProtocolVersions_Version* max_version =
64         grpc_gcp_RpcProtocolVersions_max_rpc_version(versions);
65     if (max_version != nullptr) {
66       int max_version_major =
67           grpc_gcp_RpcProtocolVersions_Version_major(max_version);
68       int max_version_minor =
69           grpc_gcp_RpcProtocolVersions_Version_minor(max_version);
70       peer_rpc_versions_.max_rpc_version.major_version = max_version_major;
71       peer_rpc_versions_.max_rpc_version.minor_version = max_version_minor;
72     }
73     const grpc_gcp_RpcProtocolVersions_Version* min_version =
74         grpc_gcp_RpcProtocolVersions_min_rpc_version(versions);
75     if (min_version != nullptr) {
76       int min_version_major =
77           grpc_gcp_RpcProtocolVersions_Version_major(min_version);
78       int min_version_minor =
79           grpc_gcp_RpcProtocolVersions_Version_minor(min_version);
80       peer_rpc_versions_.min_rpc_version.major_version = min_version_major;
81       peer_rpc_versions_.min_rpc_version.minor_version = min_version_minor;
82     }
83   }
84   if (grpc_gcp_AltsContext_security_level(ctx) >= GRPC_SECURITY_MIN ||
85       grpc_gcp_AltsContext_security_level(ctx) <= GRPC_SECURITY_MAX) {
86     security_level_ = static_cast<grpc_security_level>(
87         grpc_gcp_AltsContext_security_level(ctx));
88   }
89   if (grpc_gcp_AltsContext_peer_attributes_size(ctx) != 0) {
90     size_t iter = kUpb_Map_Begin;
91     const grpc_gcp_AltsContext_PeerAttributesEntry* peer_attributes_entry =
92         grpc_gcp_AltsContext_peer_attributes_next(ctx, &iter);
93     while (peer_attributes_entry != nullptr) {
94       upb_StringView key =
95           grpc_gcp_AltsContext_PeerAttributesEntry_key(peer_attributes_entry);
96       upb_StringView val =
97           grpc_gcp_AltsContext_PeerAttributesEntry_value(peer_attributes_entry);
98       peer_attributes_map_[std::string(key.data, key.size)] =
99           std::string(val.data, val.size);
100       peer_attributes_entry =
101           grpc_gcp_AltsContext_peer_attributes_next(ctx, &iter);
102     }
103   }
104 }
105 
application_protocol() const106 std::string AltsContext::application_protocol() const {
107   return application_protocol_;
108 }
109 
record_protocol() const110 std::string AltsContext::record_protocol() const { return record_protocol_; }
111 
peer_service_account() const112 std::string AltsContext::peer_service_account() const {
113   return peer_service_account_;
114 }
115 
local_service_account() const116 std::string AltsContext::local_service_account() const {
117   return local_service_account_;
118 }
119 
security_level() const120 grpc_security_level AltsContext::security_level() const {
121   return security_level_;
122 }
123 
peer_rpc_versions() const124 AltsContext::RpcProtocolVersions AltsContext::peer_rpc_versions() const {
125   return peer_rpc_versions_;
126 }
127 
peer_attributes() const128 const std::map<std::string, std::string>& AltsContext::peer_attributes() const {
129   return peer_attributes_map_;
130 }
131 
132 }  // namespace experimental
133 }  // namespace grpc
134