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