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