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.h> 20 #include <grpcpp/security/alts_context.h> 21 22 #include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h" 23 #include "src/proto/grpc/gcp/altscontext.upb.h" 24 25 namespace grpc { 26 namespace experimental { 27 28 // A upb-generated grpc_gcp_AltsContext is passed in to construct an 29 // AltsContext. Normal users should use GetAltsContextFromAuthContext to get 30 // AltsContext, instead of constructing their own. AltsContext(const grpc_gcp_AltsContext * ctx)31AltsContext::AltsContext(const grpc_gcp_AltsContext* ctx) { 32 upb_strview application_protocol = 33 grpc_gcp_AltsContext_application_protocol(ctx); 34 if (application_protocol.data != nullptr && application_protocol.size > 0) { 35 application_protocol_ = 36 std::string(application_protocol.data, application_protocol.size); 37 } 38 upb_strview record_protocol = grpc_gcp_AltsContext_record_protocol(ctx); 39 if (record_protocol.data != nullptr && record_protocol.size > 0) { 40 record_protocol_ = std::string(record_protocol.data, record_protocol.size); 41 } 42 upb_strview peer_service_account = 43 grpc_gcp_AltsContext_peer_service_account(ctx); 44 if (peer_service_account.data != nullptr && peer_service_account.size > 0) { 45 peer_service_account_ = 46 std::string(peer_service_account.data, peer_service_account.size); 47 } 48 upb_strview local_service_account = 49 grpc_gcp_AltsContext_local_service_account(ctx); 50 if (local_service_account.data != nullptr && local_service_account.size > 0) { 51 local_service_account_ = 52 std::string(local_service_account.data, local_service_account.size); 53 } 54 const grpc_gcp_RpcProtocolVersions* versions = 55 grpc_gcp_AltsContext_peer_rpc_versions(ctx); 56 if (versions != nullptr) { 57 const grpc_gcp_RpcProtocolVersions_Version* max_version = 58 grpc_gcp_RpcProtocolVersions_max_rpc_version(versions); 59 if (max_version != nullptr) { 60 int max_version_major = 61 grpc_gcp_RpcProtocolVersions_Version_major(max_version); 62 int max_version_minor = 63 grpc_gcp_RpcProtocolVersions_Version_minor(max_version); 64 peer_rpc_versions_.max_rpc_version.major_version = max_version_major; 65 peer_rpc_versions_.max_rpc_version.minor_version = max_version_minor; 66 } 67 const grpc_gcp_RpcProtocolVersions_Version* min_version = 68 grpc_gcp_RpcProtocolVersions_min_rpc_version(versions); 69 if (min_version != nullptr) { 70 int min_version_major = 71 grpc_gcp_RpcProtocolVersions_Version_major(min_version); 72 int min_version_minor = 73 grpc_gcp_RpcProtocolVersions_Version_minor(min_version); 74 peer_rpc_versions_.min_rpc_version.major_version = min_version_major; 75 peer_rpc_versions_.min_rpc_version.minor_version = min_version_minor; 76 } 77 } 78 if (grpc_gcp_AltsContext_security_level(ctx) >= GRPC_SECURITY_MIN || 79 grpc_gcp_AltsContext_security_level(ctx) <= GRPC_SECURITY_MAX) { 80 security_level_ = static_cast<grpc_security_level>( 81 grpc_gcp_AltsContext_security_level(ctx)); 82 } 83 } 84 application_protocol() const85std::string AltsContext::application_protocol() const { 86 return application_protocol_; 87 } 88 record_protocol() const89std::string AltsContext::record_protocol() const { return record_protocol_; } 90 peer_service_account() const91std::string AltsContext::peer_service_account() const { 92 return peer_service_account_; 93 } 94 local_service_account() const95std::string AltsContext::local_service_account() const { 96 return local_service_account_; 97 } 98 security_level() const99grpc_security_level AltsContext::security_level() const { 100 return security_level_; 101 } 102 peer_rpc_versions() const103AltsContext::RpcProtocolVersions AltsContext::peer_rpc_versions() const { 104 return peer_rpc_versions_; 105 } 106 107 } // namespace experimental 108 } // namespace grpc 109